Page 251~254 Win32 GUI项目

win32_gui  源代码:

#if defined(UNICODE) && !defined(_UNICODE)
    #define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
    #define UNICODE
#endif

#include <tchar.h>
#include <windows.h>

/*  Declare Windows procedure  */
LRESULT CALLBACK WindowProcedure (HWND, UINT, WPARAM, LPARAM);

/*  Make the class name into a global variable  */
TCHAR szClassName[ ] = _T("CodeBlocksWindowsApp");

int WINAPI WinMain (HINSTANCE hThisInstance,
                     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))
        return 0;

    /* The class is registered, let's create the program*/
    hwnd = CreateWindowEx (
           0,                   /* Extended possibilites for variation */
           szClassName,         /* Classname */
           _T("Code::Blocks Template Windows App"),       /* 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);

    /* Run the message loop. It will run until GetMessage() returns 0 */
    while (GetMessage (&messages, NULL, 0, 0))
    {
        /* 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;
}


/*  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;
}

11行,窗口过程(回调函数),实际上就是窗口用于处理消息的过程,返回值的类型是一个宏定义,即LRESULT

14行,使用全局变量定义一个类名字

21,声明窗口句柄

22,声明窗口消息

23,声明窗口类结构体,数据结构

26~40,填充窗口结构体的成员数据

43,向操作系统注册这个窗口类,其作用相当于告诉操作系统:“嗨哥们,将来我要是用“CodeBlocksWindowsApp“命名来创建一个窗口 ,你就把这个窗口的消息,都传给WindowProcedure这个函数。

47~60,创建一个窗口,使用变量hwnd保存,hwnd声明在21行,窗口创建成功之后,只是内存中的一堆数据,并不会立即显示在屏幕上,屏幕上的显示不过是表象。

63,将指定句柄的窗口显示出来

66~72,  消息循环。

66,将消息从消息队列中取出来,存放到messages中,

69,对消息做一些必要的转换

71,将消息发送到目标窗口,发送给窗口过程函数。

操作系统中有好多GUI程序(更专业的叫法是“进程process”)在运行,每个进程都需要接受消息。消息往往很多,所以进程需要一个“消息队列(message queue)”。GetMessage()从队列中取出一个消息,存放在message中,然后通过TranslateMessage(&messages)做一些必要的转换,最后通过DispatchMessage(&messages)将该消息发送到目标窗口

81~93,窗口过程函数,

85,如果收到的消息是WM_DESTROY, 调用PostQuitMessage (0),该操作将创建另一个“WM_QUIT”并将其丢进消息队列等着,直到GetMessage()将排在前面的消息取出并处理完后遇上它。

93,DefWindowProc()方法也是一个Windows API提供的“默认窗口过程”,可以处理窗口最大化,最小化,退出(前面提到的WM_QUIT)等消息。

WindowProcedure()函数的四个入参含义

表11-1 窗口过程函数的四个入参解释
参数 含义
HWND  hwnd 窗口句柄,代表接收到消息的窗口
UINT message UINT是unsigned int,用无符号整数表示接收到消息的类型
WPARAM wParam

消息的附加消息之一

比如:对一个鼠标移动的消息,wParam用于表达在鼠标移动的同时,用户是否也按下了一些特殊的键

LPARAM lParam

消息的附加消息之二

比如:对一个鼠标移动的消息,lParam存储有鼠标当前的位置坐标

课堂作业:代码中CreateWindowEx()调用,并没有用到wincl这个数据,请思考wincl如何起作用?

        43行,wincl注册到系统里,47行,调用CreateWindowEx(), 从系统中取出wincl注册到系统中的数据,然后创建窗口

相关推荐

最近更新

  1. TCP协议是安全的吗?

    2024-01-12 05:24:02       18 阅读
  2. 阿里云服务器执行yum,一直下载docker-ce-stable失败

    2024-01-12 05:24:02       19 阅读
  3. 【Python教程】压缩PDF文件大小

    2024-01-12 05:24:02       19 阅读
  4. 通过文章id递归查询所有评论(xml)

    2024-01-12 05:24:02       20 阅读

热门阅读

  1. vue3-响应式基础之ref

    2024-01-12 05:24:02       35 阅读
  2. Android 8.1 默认允许应用安装未知应用

    2024-01-12 05:24:02       37 阅读
  3. spring基于注解的IOC配置

    2024-01-12 05:24:02       37 阅读
  4. Python中‘字符串’和‘字符串流’的区别

    2024-01-12 05:24:02       35 阅读
  5. 【Vue】自定义指令

    2024-01-12 05:24:02       28 阅读
  6. UI自动化-Android

    2024-01-12 05:24:02       33 阅读
  7. Qt框架相关的可执行文件

    2024-01-12 05:24:02       30 阅读
  8. xtu 1279 Dual Prime

    2024-01-12 05:24:02       201 阅读