Fixed layout of layout containers
The horizontal, vertical, and table layout containers we learned earlier will automatically adapt to changes in the container size, while the controls in the fixed layout container will not change (they will remain unchanged ).
Create a fixed layout:
GtkWidget * gtk_fixed_new (void );
Return Value: Fixed layout container pointer
Add controls to fixed layout containers:
Void gtk_fixed_put (
GtkFixed * fixed,
GtkWidget * widget,
Gint x,
Gint y );
Fixed: container used to hold controls
Widget: control to be added
X, y: The starting coordinate of the widget placement position, for example:
Set the widget size (width and height ):
Void gtk_widget_set_size_request (
GtkWidget * widget,
Gint width,
Gint height );
Widget: controls to be set
Width: width
Height: height
Move the widget position in the fixed layout:
Void gtk_fixed_move (
GtkFixed * fixed,
GtkWidget * widget,
Gint x,
Gint y );
Fixed: fixed layout container
Widget: controls to be moved
X, y: Moving position
The complete code is as follows:
# Include
Int main (int argc, char * argv []) {gtk_init (& argc, & argv); // initialize GtkWidget * window = gtk_window_new (GTK_WINDOW_TOPLEVEL ); // create a window GtkWidget * fixed = gtk_fixed_new (); // create a fixed container gtk_container_add (GTK_CONTAINER (window), fixed ); // fixed the put-in window GtkWidget * button1 = gtk_button_new_with_label (^_^); // create the button gtk_fixed_put (GTK_FIXED (fixed), button1, 0, 0 ); // Add the button to the fixed layout GtkWidget * button2 = gtk_button_new_with_label (@ _ @); // create the button gtk_fixed_put (GTK_FIXED (fixed), button2, 0, 0 ); // Add the button to the fixed layout gtk_fixed_move (GTK_FIXED (fixed), button2, 150,150); // move the position of the control gtk_widget_set_size_request (button2, 100, 50 ); // set the control size gtk_widget_show_all (window); // display all controls gtk_main (); // enter the event loop return 0 ;}
Run the program as follows: