Sample Code 6: How to place a background image in GtkWindow? |
|
Written by kksou
|
|
Tuesday, 12 September 2006 |
|
Problem You want to place a background image in a GtkWindow as shown below:

Solution
- Load the image with
GdkPixbuf::new_from_file().
- Get the corresponding pixmap using
GdkPixbuf::render_pixmap_and_mask().
- Get the GtkStyle corresponding to the window with GtkWidget::get_style().
- Set the new style with GtkWidget::set_style().
Sample Code The following image file (sample6.png) is required by the sample code below. Please save a copy of the image file and put it in the same directory where you store the sample code.

1 2 3 4 5 6 7 8 9 10 11 12 13 14
| <?php $window = new GtkWindow(); $window->connect_simple('destroy', array('Gtk','main_quit')); $window->set_size_request(480, 240);
// set background image
$pixbuf=GdkPixbuf::new_from_file("sample6.png"); // note 1
list($pixmap,$mask)=$pixbuf-> render_pixmap_and_mask(255); // note 2
$style = $window->get_style(); // note 3
$style=$style->copy(); // note 4
$style->bg_pixmap[Gtk::STATE_NORMAL]=$pixmap; // note 5
$window->set_style($style); // note 6
// prints hello world
|
- 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
- Load the background image. This works with all standard image file format such as .gif, .jpg or .png.
- Convert to pixmap.
- Get the GtkStyle corresponding to the window.
- Make a copy of the style.
- Set the background image.
- Set the window with the new style.
Related Links
User reviews Average user ratings: 4.0 (from 6 users) Note: You have to be a registered member to leave a comment. Free registration here. |
April 04, 2008 8:09pm
January 28, 2009 4:05am
February 17, 2009 2:45am
May 27, 2009 8:08pm
May 27, 2009 8:22pm
December 10, 2009 6:27pm
rare