GTK + 2.0 Tutorial - Write Hello World with GTK

zhaozj2021-02-16  184

Write Hello World with GTK

Ok, now to write a program with only one button component, this is a standard GTK Hello World.

#include

/ * This is a callback function. The DATA parameter is ignored in this presentation. * There are more callback functions after later. * / void hello (gtkwidget * widget, gpointer data) {g_print ("Hello World / N");}

Gint delete_event (gtkwidget * widget, gdkevent * event, gpoint data) {/ * If your "delete_event" signal processing function returns FALSE, GTK will issue a "Destroy" signal. * Return true, you don't want to close the window. * It is useful when you want to pop up "Do you sure you want to quit?" Dialog. * /

G_Print ("delete Event Occurred / N);

/ * Change TRUE will close to the False program. * /

Return True;}

/ * Another callback function * / void destroy (gtkwidget * widget, gtk_main_quit ();}

INT main (int Argc, char * argv []) {/ * gtkwidget is the storage type of the component * / gtkwidget * window; gtkwidget * button; / * This function is called in all GTK programs. The parameter is resolved in the command line and sent to the program * / gtk_init (& argc, & argv); / * Create a new window * / window = gtk_window_new (gtk_window_toplevel); / * When the window receives "delete_event" signal (this signal) The window manager is sent, usually "off" option or the closing button on the title bar), let it call the delete_event () function in the form of the above definition. * The DATA parameter value transmitted to the callback function is NULL, which will be ignored by the callback function. * / G_signal_connect (g_object (window), "delete_event", g_callback (delete_event), null); / * Here we connect "Destroy" events to handle functions. * Call the gtk_widget_destroy () function or return false value in the "delete_event" callback function * can trigger the "Destroy" event. * / G_signal_connect (g_object (window), "destroy", g_callback (design), null; / * Set the width of the window border. * / Gtk_container_set_border_width (gtk_container (window), 10); / * Create a new button that is labeled "Hello World". * / Button = gtk_button_new_with_label ("Hello World"); / * When receiving the "Clicked" signal, the Hello () function is called above. * / G_signal_connect (g_object (button), "clicked", g_callback (hello), null; / * When the button is clicked, the window will be turned off by calling the gtk_widget_destroy (Window) function. * The "Destroy" signal will be sent from here or from the window manager. * / G_signal_connect_swapped (g_object (button), "clicked", g_callback, window); / * put the button in the window (GTK container). * / Gtk_container_add (gtk_container (window), button); / * The last step is to display the newly created button and window * / gtk_widget_show (button); gtk_widget_show (window); / * All GTK programs must have a gtk_main () function. The program is running here * Wait for events (such as keyboard events or mouse events).

转载请注明原文地址:https://www.9cbs.com/read-8488.html

New Post(0)