wxWidgets学习笔记(3)wxWidgets程序生死因果

     本文将分析wxWidgets应用程序如何开始执行、如何结束,如何创建主窗口,程序如何推动等问题。

    1 三个不同版本的 Hello World

    1-1  Hello World (Win32 SDK版)
    先看一下Win32 SDK简单程序,著名的Hello World。本程序由Code::Block在XP下通过向导生成,程序运行结果如图所示。
   
  1. #include <windows.h>
  2. /*  Declare Windows procedure  */
  3. LRESULT CALLBACK WindowProcedure (HWNDUINTWPARAMLPARAM);
  4. /*  Make the class name into a global variable  */
  5. char szClassName[ ] = "CodeBlocksWindowsApp";
  6. int WINAPI WinMain (HINSTANCE hThisInstance,                 // 1 程序入口 WinMain(...)
  7.                      HINSTANCE hPrevInstance,
  8.                      LPSTR lpszArgument,
  9.                      int nCmdShow)
  10. {
  11.     HWND hwnd;               /* This is the handle for our window */
  12.     MSG messages;            /* Here messages to the application are saved */
  13.     WNDCLASSEX wincl;        /* Data structure for the windowclass */
  14.     /* The Window structure */
  15.     wincl.hInstance = hThisInstance;
  16.     wincl.lpszClassName = szClassName;
  17.     wincl.lpfnWndProc = WindowProcedure;      /* This function is called by windows */
  18.     wincl.style = CS_DBLCLKS;                 /* Catch double-clicks */
  19.     wincl.cbSize = sizeof (WNDCLASSEX);
  20.     /* Use default icon and mouse-pointer */
  21.     wincl.hIcon = LoadIcon (NULL, IDI_APPLICATION);
  22.     wincl.hIconSm = LoadIcon (NULL, IDI_APPLICATION);
  23.     wincl.hCursor = LoadCursor (NULL, IDC_ARROW);
  24.     wincl.lpszMenuName = NULL;                 /* No menu */
  25.     wincl.cbClsExtra = 0;                      /* No extra bytes after the window class */
  26.     wincl.cbWndExtra = 0;                      /* structure or the window instance */
  27.     /* Use Windows's default colour as the background of the window */
  28.     wincl.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  29.     /* Register the window class, and if it fails quit the program */
  30.     if (!RegisterClassEx (&wincl))                                   // 2 注册窗口
  31.         return 0;
  32.     /* The class is registered, let's create the program*/
  33.     hwnd = CreateWindowEx (
  34.            0,                   /* Extended possibilites for variation */
  35.            szClassName,         /* Classname */
  36.            "Hello World!",       /* Title Text */
  37.            WS_OVERLAPPEDWINDOW, /* default window */
  38.            CW_USEDEFAULT,       /* Windows decides the position */
  39.            CW_USEDEFAULT,       /* where the window ends up on the screen */
  40.            544,                 /* The programs width */
  41.            375,                 /* and height in pixels */
  42.            HWND_DESKTOP,        /* The window is a child-window to desktop */
  43.            NULL,                /* No menu */
  44.            hThisInstance,       /* Program Instance handler */
  45.            NULL                 /* No Window Creation data */
  46.            );
  47.     /* Make the window visible on the screen */
  48.     ShowWindow (hwnd, nCmdShow);                  // 3 生成窗口并显示窗口
  49.     /* Run the message loop. It will run until GetMessage() returns 0 */
  50.     while (GetMessage (&messages, NULL, 0, 0))    // 4 消息循环,响应用户命令
  51.     {
  52.         /* Translate virtual-key messages into character messages */
  53.         TranslateMessage(&messages);
  54.         /* Send message to WindowProcedure */
  55.         DispatchMessage(&messages);
  56.     }
  57.     /* The program return-value is 0 - The value that PostQuitMessage() gave */
  58.     return messages.wParam;
  59. }
  60.                                                                     // 5 窗口处理过程
  61. /*  This function is called by the Windows function DispatchMessage()  */
  62. LRESULT CALLBACK WindowProcedure (HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
  63. {
  64.     switch (message)                  /* handle the messages */
  65.     {
  66.         case WM_DESTROY:
  67.             PostQuitMessage (0);       /* send a WM_QUIT to the message queue */
  68.             break;
  69.         default:                      /* for messages that we don't deal with */
  70.             return DefWindowProc (hwnd, message, wParam, lParam);
  71.     }
  72.     return 0;
  73. }
    可以看出,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. #include <stdlib.h>
  2. #include <gtk/gtk.h>
  3.                                                    //5 消息处理函数,通过g_signal_connect与窗口、控件绑定
  4. static void helloWorld (GtkWidget *wid, GtkWidget *win)
  5. {
  6.   GtkWidget *dialog = NULL;
  7.   dialog = gtk_message_dialog_new (GTK_WINDOW (win), GTK_DIALOG_MODAL, GTK_MESSAGE_INFO, GTK_BUTTONS_CLOSE, "Hello World!");
  8.   gtk_window_set_position (GTK_WINDOW (dialog), GTK_WIN_POS_CENTER);
  9.   gtk_dialog_run (GTK_DIALOG (dialog));
  10.   gtk_widget_destroy (dialog);
  11. }
  12. int main (int argc, char *argv[])     // 1 程序入口 main(...)
  13. {
  14.   GtkWidget *button = NULL;
  15.   GtkWidget *win = NULL;
  16.   GtkWidget *vbox = NULL;
  17.   /* Initialize GTK+ */                         // 2 初始化GTK+
  18.   g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, (GLogFunc) gtk_false, NULL);
  19.   gtk_init (&argc, &argv);
  20.   g_log_set_handler ("Gtk", G_LOG_LEVEL_WARNING, g_log_default_handler, NULL);
  21.   /* Create the main window */        // 3 生成主窗口以及窗口内的控件,并通过 g_signal_connect绑定消息处理函数。
  22.   win = gtk_window_new (GTK_WINDOW_TOPLEVEL);
  23.   gtk_container_set_border_width (GTK_CONTAINER (win), 8);
  24.   gtk_window_set_title (GTK_WINDOW (win), "Hello World");
  25.   gtk_window_set_position (GTK_WINDOW (win), GTK_WIN_POS_CENTER);
  26.   gtk_widget_realize (win);
  27.   g_signal_connect (win, "destroy", gtk_main_quit, NULL);
  28.   /* Create a vertical box with buttons */
  29.   vbox = gtk_vbox_new (TRUE, 6);
  30.   gtk_container_add (GTK_CONTAINER (win), vbox);
  31.   button = gtk_button_new_from_stock (GTK_STOCK_DIALOG_INFO);
  32.   g_signal_connect (G_OBJECT (button), "clicked", G_CALLBACK (
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值