概述
源代码:
编译命令和结果:
一些解释说明:
The file includes all of the widgets, variables, functions, and structures available in GTK+ as well as header files from other libraries that GTK+ depends on. When you implement applications using GTK+, it is enough to use only , except for some advanced applications.
The following code declares a pointer variable for a GtkWidget object.
GtkWidget *window;
A GTK+ program consists of widgets. Components such as windows, buttons, and scrollbars are called widgets. We will see widgets in detail later.
The following function initializes GTK+
/* Initialize GTK+ and all of its supporting libraries. */
gtk_init (&argc, &argv);
By calling gtk_init(), all initialization work is automatically performed for you. It begins by setting up the GTK+ environment, including obtaining the GDK display and preparing the Glib main event loop and basic signal handling. It is important to call gtk_init() before any other function calls to the GTK+ libraries. Otherwise, your application will not work properly and will likely crash.
The following code creates a new GtkWindow object and sets some properties.
/* Create a new window, give it a title and display it to the user. */
window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
gtk_window_set_title (GTK_WINDOW (window), "Hello World");
gtk_widget_show (window);
gtk_window_new() creates a GtkWindow object that is set to the default width and height of 200 pixels. We passed GTK_WINDOW_TOPLEVEL to gtk_window_new(). This makes GTK+ create a new top-level window. Top-level windows use window manager decorations, have a border frame, and allow themselves to be placed by the window manager. Conversely, you can use GTK_WINDOW_POPUP to create a pop-up window. Pop-up windows are used for things that are not normally thought of as windows, such as tooltips and menus. GTK_WINDOW_TOPLEVEL and GTK_WINDOW_POPUP are the only two elements available in the GtkWindowType enumeration.
The following function, gtk_window_set_title(), requests the title bar and taskbar to display ”Hello World” as the title of the window. The first parameter is for GtkWindow object and the second one is the string that you want to display.
Lastly, the gtk_widget_show() function tells GTK+ to set the specified widget as visible. The widget may not be immediately shown when you call gtk_widget_show(), because GTK+ queues the widget until all preprocessing is completed before it is drawn onto the screen.
This function is the main loop that takes control and starts processing events.
/* Hand control over to the main loop. */
gtk_main ();
The gtk_main() function will continue to run until you call gtk_main_quit() or the application terminates. This should be the last GTK+ function called in main(). In GTK+, signals and callback functions are triggered by user actions such as button clicks, asynchronous input-output events, programmable timeouts, and others.
So far, we have written a simple GTK+ program, learned about the structure of GTK+, and understood what each part means and how it works. Now, we should compile the code we wrote in order to make it executable.
The following command compiles the code.
gcc hello.c -o hello ‘pgk-config -cflags -libs gtk+-2.0‘
注意这里的``是Tab上面的斜点,不是单引号。
This command can be used for most of the examples except for some which use specific library like pthread.
Take care to type backticks, not apostrophes - backticks are instructions to the shell to execute and append the output of the enclosed command.
In addition to the GCC compiler, you need to use the pkg-config application, which returns a list of specified libraries or paths. The first instance, pkg-config --cflags gtk-2.0+, returns directory names to the compiler’s ’include path’. This will make sure that the GTK+ header files are available to the compiler. The second call, pkg-config --libs gtk-2.0+, appends options to the command line used by the linker including library directory path extensions and a list of libraries needed for linking to the executable.
The libraries that are returned in a standard Linux environment are:
• GTK+ (-lgtk): Graphical widgets
• GDK (-lgdk): The standard graphics rendering library
• GdkPixbuf (-lgdk pixbuf): Client-side image manipulation
• Pango (-lpango): Font rendering and output
• GObject (-lgobject): Object-oriented type system
• GModule (-lgmodule): Dynamically loading libraries
• GLib (-lglib): Data types and utility functions
• Xlib (-lX11): X Window System protocol library
• Xext (-lXext): X extensions library routines
• GNU math library (-lm): The GNU library from which GTK+ uses many routines
最后
以上就是糟糕煎蛋为你收集整理的一个简单的GTK的例子程序 的全部内容,希望文章能够帮你解决一个简单的GTK的例子程序 所遇到的程序开发问题。
如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。
发表评论 取消回复