The above example is simply too shabby to have anything in it. Next we add controls to the window. Before we add, let's look at the controls.
Controls are the encapsulation of data and methods. The control has its own properties and methods. A property is a characteristic of an accusation piece. Methods are some simple and visible features of the accusation piece. If the button is a control, this button is square, there is a picture, this is we can see the appearance of properties, at the same time, this button has been pressed by the function.
There are two main types of controls in GTK: container controls , non-container controls .
Container control: It can hold other controls, which we can understand as boxes, boxes for loading things. Container controls are divided into two categories, one that can only hold a single control , such as a window, a button, and another that can hold multiple controls , such as layout controls.
Non-container control: it can not accommodate other controls, such as labels, row edits.
The blank window we wrote earlier is a control that can hold a control, and now all we have to do is add a button to the window.
First, we need to create a button, then we need to add the button to the window, and then the button control is displayed.
The creation of the button:
Gtkwidget *gtk_button_new_with_label (const Gchar *label);
Container Add Control:
void Gtk_container_add (Gtkcontainer *container, Gtkwidget *widget);
Display controls:
void Gtk_widget_show (Gtkwidget *widget);
The complete code is as follows:
#include <gtk/gtk.h>//header file int main (int argc,char *argv[]) {gtk_init (&ARGC, &ARGV);//Initialize Gtkwidget *window = Gtk_window_new (gtk_window_toplevel);//Create Top-level window//Create button with text message "Hello GTK +" Gtkwidget *button = Gtk_button_new_with_label ("Hello GTK +");//Put the button into the window (the window is also a container) Gtk_container_add (Gtk_container (Windows), button); Gtk_widget_show (button);// Display button gtk_widget_show (window);//Display Windows Gtk_main ();//main Event loop return 0;}
The program runs as follows:
Code Analysis:
void Gtk_container_add (Gtkcontainer *container, Gtkwidget *widget);
Here we are to add the button to the window container, the widget is the button pointer (buttons of the above code), container as the window pointer (above the code of Windows), it is important to note that the window we created (the Windows) return value is Gtkwidget * type , and the first parameter of Gtk_container_add () is the gtkcontainer * type, because the variable of the Gtkwidget * Type is a universal pointer to any control , so when the function is passed, the corresponding conversion is made according to the parameter type. , such as:
Gtk_container_add ((Gtkcontainer *) window, button);
In C language, we use this method to convert. In GTK, many of the internal variable pointer type conversions define macro definitions, such as:
#define gtk_container (x) (Gtkcontainer *) (x)
So, we can also write:gtk_container_add (Gtk_container (window), button);
So, how do we know which type corresponds to which macro definition? To convert the type name to all uppercase, and the word and the word "_" connection between words, and then, this name is the name of which macro definition,for example, (Gtkbutton *) x is defined with a macro method is Gtk_button (x).
The above code, we are both buttons and windows are to be shown through Gtk_widget_show ():
Gtk_widget_show (button); //Display button
Gtk_widget_show (window); //Display window
However, if there are 100 controls in the window, it will be more troublesome to show them one by one, so we can show all the controls by Gtk_widget_show_all () , which is the container that holds the control (this is the window). The controls on the container are then displayed as well.
Gtk_widget_show_all (window); //Display all controls on the window
Source code download please click here.
Controls are added