PHP-GTK2 Newsletter

PHP-GTK2 Tips & Techniques
FREE Newsletter
by kksou



Sample Code 209: How to get the size of display screen?
Written by kksou   
Saturday, 24 March 2007
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   
<?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
  • Note that this is only 70% of the sample code. You have to be a registered member to see the entire sample code. Please login or register.
  • Registration is free and immediate.
  • Have some doubt about the registration? Please read this forum article.
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.


User reviews   Average user ratings:    4.5   (from 3 users)
  1. Digiku
    June 16, 2007 5:09am

    If the user is using a multi-monitor setup, this method actually returns the total dimensions of all of the monitors combined.

  2. Ris
    July 03, 2008 5:15am

  3. Marian Vlad-Marian
    March 07, 2009 2:58am
    How do I get the parent screen size?

    I have 3 monitors, in my case the window becomes 2/3 out of the whole width of all 3 in line.
    I need to get the width and height of the parent screen.

Note: You have to be a registered member to leave a comment. Free registration here.

 
< Prev   Next >

Blog - Forum - Privacy Policy - Contact Us
Copyright © 2006-2009. kksou.com. All Rights Reserved