本文将分析wxWidgets应用程序如何开始执行、如何结束,如何创建主窗口,程序如何推动等问题。
1 三个不同版本的 Hello World
1-1 Hello World (Win32 SDK版)
先看一下Win32 SDK简单程序,著名的Hello World。本程序由Code::Block在XP下通过向导生成,程序运行结果如图所示。
可以看出,Win32 SDK程序
1) 从WinMain(...)开始执行;
2) 通过RegisterClassEx(...)注册窗口类;
3)通过CreateWindowEx(...)创建主窗口,并通过ShowWindow(...)显示窗口;
4)通过 while (GetMessage (&messages, NULL, 0, 0)) 消息循环,等待响应用户命令;
5) 窗口处理过程为WindowProcedure(...),在注册窗口的时候绑定。
1-2 Hello World (GTK+ 版)
再看一下GTK+ 简单程序,也是著名的Hello World。本程序由Code::Block在Ubuntu下通过向导生成,程序运行结果如图所示。
1 三个不同版本的 Hello World
1-1 Hello World (Win32 SDK版)
先看一下Win32 SDK简单程序,著名的Hello World。本程序由Code::Block在XP下通过向导生成,程序运行结果如图所示。
- #include <windows.h>
- /* Declare Windows procedure */
- LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);
- /* Make the class name into a global variable */
- char szClassName[ ] = "CodeBlocksWindowsApp";
- int WINAPI WinMain (HINSTANCE hThisInstance, // 1 程序入口 WinMain(...)
- HINSTANCE hPrevInstance,
- LPSTR lpszArgument,
- int nCmdShow)
- {
- HWND hwnd; /* This is the handle for our window */
- MSG messages; /* Here messages to the application are saved */
- WNDCLASSEX wincl; /* Data structure for the windowclass */
- /* The Window structure */
- wincl.hInstance = hThisInstance;
- wincl.lpszClassName = szClassName;
- wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
- wincl.style = CS_DBLCLKS; /* Catch double-clicks */
- wincl.cbSize = sizeof (WNDCLASSEX);
- /* Use default icon and mouse-pointer */
- wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
- wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
- wincl.lpszMenuName = NULL; /* No menu */
- wincl.cbClsExtra = 0; /* No extra bytes after the window class */
- wincl.cbWndExtra = 0; /* structure or the window instance */
- /* Use Windows's default colour as the background of the window */
- wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
- /* Register the window class, and if it fails quit the program */
- if (!RegisterClassEx (&wincl)) // 2 注册窗口
- return 0;
- /* The class is registered, let's create the program*/
- hwnd = CreateWindowEx (
- 0, /* Extended possibilites for variation */
- szClassName, /* Classname */
- "Hello World!", /* Title Text */
- WS_OVERLAPPEDWINDOW, /* default window */
- CW_USEDEFAULT, /* Windows decides the position */
- CW_USEDEFAULT, /* where the window ends up on the screen */
- 544, /* The programs width */
- 375, /* and height in pixels */
- HWND_DESKTOP, /* The window is a child-window to desktop */
- NULL, /* No menu */
- hThisInstance, /* Program Instance handler */
- NULL /* No Window Creation data */
- );
- /* Make the window visible on the screen */
- ShowWindow (hwnd, nCmdShow); // 3 生成窗口并显示窗口
- /* Run the message loop. It will run until GetMessage() returns 0 */
- while (GetMessage (&messages, NULL, 0, 0)) // 4 消息循环,响应用户命令
- {
- /* Translate virtual-key messages into character messages */
- TranslateMessage(&messages);
- /* Send message to WindowProcedure */
- DispatchMessage(&messages);
- }
- /* The program return-value is 0 - The value that PostQuitMessage() gave */
- return messages.wParam;
- }
- // 5 窗口处理过程
- /* This function is called by the Windows function DispatchMessage() */
- LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
- {
- switch (message) /* handle the messages */
- {
- case WM_DESTROY:
- PostQuitMessage (0); /* send a WM_QUIT to the message queue */
- break;
- default: /* for messages that we don't deal with */
- return DefWindowProc (hwnd, message, wParam, lParam);
- }
- return 0;
- }
1) 从WinMain(...)开始执行;
2) 通过RegisterClassEx(...)注册窗口类;
3)通过CreateWindowEx(...)创建主窗口,并通过ShowWindow(...)显示窗口;
4)通过 while (GetMessage (&messages, NULL, 0, 0)) 消息循环,等待响应用户命令;
5) 窗口处理过程为WindowProcedure(...),在注册窗口的时候绑定。
1-2 Hello World (GTK+ 版)
再看一下GTK+ 简单程序,也是著名的Hello World。本程序由Code::Block在Ubuntu下通过向导生成,程序运行结果如图所示。
- #include <stdlib.h>
- #include <gtk/gtk.h>
- //5 消息处理函数,通过g_signal_connect与窗口、控件绑定
- static void helloWorld (GtkWidget *wid, GtkWidget *win)
- {
- GtkWidget *dialog = NULL;
- dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
- gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
- gtk_dialog_run (GTK_DIALOG (dialog));
- gtk_widget_destroy (dialog);
- }
- int main (int argc, char *argv[]) // 1 程序入口 main(...)
- {
- GtkWidget *button = NULL;
- GtkWidget *win = NULL;
- GtkWidget *vbox = NULL;
- /* Initialize GTK+ */ // 2 初始化GTK+
- g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
- gtk_init (&argc, &argv);
- g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
- /* Create the main window */ // 3 生成主窗口以及窗口内的控件,并通过 g_signal_connect绑定消息处理函数。
- win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
- gtk_container_set_border_width (GTK_CONTAINER (win), 8);
- gtk_window_set_title (GTK_WINDOW (win), "Hello World");
- gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
- gtk_widget_realize (win);
- g_signal_connect (win, "destroy", gtk_main_quit, NULL);
- /* Create a vertical box with buttons */
- vbox = gtk_vbox_new (TRUE, 6);
- gtk_container_add (GTK_CONTAINER (win), vbox);
- button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
- g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (