209. How to get the size of display screen?

Problem

You want to get the size of the display screen as shown below:

How to get the size of display screen?


Solution


Sample Code

1   
2   
3   
4   
5   
6   
7   
8   
9   
10   
11   
12   
13   
14   
15   
16   
17   
18   
19   
20   
21   
23   
24   
25   
26   
27   
<?php
$window = new GtkWindow();
$window->connect_simple('destroy', array('Gtk','main_quit'));
$window->add($vbox = new GtkVBox());

// display title
$title = new GtkLabel("Set the window size to\n".
"2/3 of screen width and 2/3 of screen height\n\n");
$title->modify_font(new PangoFontDescription("Times New Roman Italic 10"));
$title->modify_fg(Gtk::STATE_NORMAL, GdkColor::parse("#0000ff"));
$title->set_size_request(-1, 40);
$title->set_justify(Gtk::JUSTIFY_CENTER);
$alignment = new GtkAlignment(0.5, 0.5, 0, 0);
$alignment->add($title);
$vbox->pack_start($alignment);

$screen = $window->get_screen(); // note 1
$screen_width = $screen->get_width(); // note 2
$screen_height = $screen->get_height(); // note 3

$window->set_size_request($screen_width*2/3, $screen_height*2/3);
echo "screen size = $screen_width * $screen_height\n";

$window->show_all();
Gtk::main();
?>

Output

As shown above.

 

Explanation

  1. Get the GdkScreen.
  2. Get the screen width.
  3. Get the screen height.

Notes

set_size_request works on most other widgets, including hbox, vbox, buttons, labels, etc.

For example, you can use $label->set_size_request(48,36) to set the size of a label.

Add comment


Security code
Refresh