GTK + learning: not just Hello world!

Source: Internet
Author: User

When learning a new technology, what I want most is not the thick theoretical book or programming skills. Even if there is no skill, what I want is to see my achievements, even a small window that can't do anything, it can satisfy my curiosity. So now let's write a small program to see how the GTK + program is written. Since the compiling environment has been configured last time, open your favorite IDE or notepad and click the following code. Here I use VS2005 for compiling on Windows. Some terms may differ from each other on other platforms, but they are similar in general. 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> # include <gtk/gtk. h>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> int main (int argc, char ** argv)
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> {
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> GtkWidget * window;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_init (& argc, & argv );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_widget_show (window );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_main ();
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> return 0;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) '. click =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif "Align =" top "/>} And then compile the connection. After running the command, if there is no error, a small window will appear. Well, this is the interface we wrote with GTK +. Next let's take a look at this program. The first line is the referenced header file. Generally, you only need to reference this gtk. h. If you introduce the include file according to the method described above, it is the gtk. h In the gtk directory. Then the main function is defined, which is exactly the same as the C language. In the main function, the first sentence declares the pointer of A GtkWidget. As mentioned above, GTK + is designed according to the object-oriented idea. You may consider this GtkWidget as a class, although the C compiler does not think so. Then call gtk_init, which is to initialize the GTK + environment. Friends who have written OpenGL should be clear. OpenGL also has a similar init function. Then assign values to the previously declared pointer. Look at the function name: gtk_window_new. It is clear that a new window pointer is created. The object-oriented method is: GtkWidget * window = new GtkWindow (GTK_WINDOW_TOPLEVEL); how? A lot clearer, right? Actually, it is implied that GtkWindow inherits the GtkWidget class. As you can see later, GtkWidget is actually the parent class of all controls. The passed parameter is GTK_WINDOW_TOPLEVEL, indicating that it is a top-level window. Then use gtk_widget_show to set the display, and think of it as the object-oriented Syntax: window-> show (); the last sentence gtk_main, bring our program into the event listening loop of GTK +. In this way, our program is completely introduced. But you may find a problem: why is there a black Console window behind it? This is because the default running mode is Debug. Can you change it to Release? The ugly window is gone, right? The generated binary file is much smaller than that one. Well, when we press the close button, the window disappears, but the program does not exit? Yes, because we didn't add event listening, so when the window is closed, GTK + does not know how to do it. Therefore, we continue to modify the code as follows: 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src) '. click =" window. open (" http://blog.51cto.com/viewpic.php?refimg= "+ This. src)" src =" http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif "Align =" top "/>#include <gtk/gtk. h>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> int main (int argc, char ** argv)
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> {
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> GtkWidget * window;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_init (& argc, & argv );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> g_signal_connect (GTK_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_widget_show (window );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_main ();
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> return 0;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) "src =" http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif "align =" top "/>} note that we added an event listening function: g_signal_connect (GTK_OBJECT (window)," destroy ", g_CALLBACK (gtk_main_quit), NULL); it means to call the gtk_main_quit function when the "destroy" time occurs for the Object window, and the parameter passed to this function is NULL. If we use the event listening method in Java, it is like this: window. addDestroyListener (new CallbackFunc () {gtk_main_quit () ;}); however, this code should be easier to understand. In fact, this is how to add event listening in GTK +. Every event listening of each control is written in this way, which is very unified. After the modification is complete, run it. Click the close button: Ha, the program exits! So, let's modify the program again: 650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> # include <gtk/gtk. h>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/>
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> int main (int argc, char ** argv)
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> {
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> GtkWidget * window;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_init (& argc, & argv );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> window = gtk_window_new (GTK_WINDOW_TOPLEVEL );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_window_set_title (GTK_WINDOW (window), "Hello world! ");
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> g_signal_connect (GTK_OBJECT (window), "destroy", G_CALLBACK (gtk_main_quit), NULL );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_widget_show (window );
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> gtk_main ();
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/> return 0;
650) this. width = 650; ". click = 'window. open (" http://blog.51cto.com/viewpic.php? Refimg = "+ this. src) '. click =" window. open ("http://blog.51cto.com/viewpic.php? Refimg = "+ this. src)" src = "http://www.bkjia.com/uploads/allimg/131228/1ZQ96424-0.gif" align = "top"/>}run it to see? The title of the window has been changed to the classic Hello world! . After reading the code, we added the following sentence: gtk_window_set_title (GTK_WINDOW (window), "Hello world! "); If you can think of its object-oriented syntax, you can achieve the goal: (GtkWindow *) window)-> setTitle (" Hello world! "); Note that, because the C compiler does not understand the object-oriented polymorphism mechanism, GTK + uses many macros for type conversion, such as the GTK_WINDOW here. Remember that we declare the GtkWidget pointer. You need to convert it to the GtkWindow pointer to use the set_title function. At the same time, this macro also has the type detection function. If it cannot be converted, an exception will be thrown. Now, after step-by-step code addition, we have learned how to write the GTK + program and can create a form, add event listening and modify control properties-isn't the subject of GUI programming? The rest is some details and learning about the huge control library.

This article is from the "bean space" blog, please be sure to keep this source http://devbean.blog.51cto.com/448512/108758

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.