• 【Demo 0021】窗体创建过程


    在Windows系统中几乎我们看到的元素都是窗体,通常我们将带有WS_OVERLAPPED或WS_POPUP风格的窗体被称之为顶层窗体,而WS_CHILD风格属性的窗体称之为控件如: Button, ListBox,  CommboBox, TreeView等;   为了创建窗体以及控件Window提供了二个函数:

    (一)创建窗体

            HWND CreateWindowEx(DWORD dwExStyle,  LPCTSTR lpClassName,  LPCTSTR lpWinName, DWORD dwStyle, int x,  int y,  int nWidth, int nHeight,

                                               HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)

            HWND CreateWindow(LPCTSTR lpClassName, LPCTSTR lpWinName, DWORD dwStyle, int x, int y, int nWidth, int nHeight,

                                            HWND hWndParent, HMENU hMenu, HINSTANCE hInstance, LPVOID lpParam)

             此函数是CreateWindowEx更高些的函数, 缺少窗体的扩展风格的设置

             以下代码我们分别演示两种窗体的创建

             a. 顶层窗体

            

    HWND hWnd = CreateWindow(szWndClass, szWndCaptoin, dwStyle, 0, 0, 800, 480, NULL, NULL, hInstance, (LPVOID)7000);
    if (NULL != hWnd && IsWindow(hWnd))
    {
        ShowWindow(hWnd, SW_SHOW);
        UpdateWindow(hWnd);
        bCreated    = true;
    }
           

             b. 控件

          

    //--: Create sub control
    hButton = CreateWindow(_T("bUTTON"), _T("&Exit"), WS_VISIBLE|WS_CHILD, 0, 0, 100, 50, hWnd, (HMENU)1001, NULL, 0);
      

    (二)创建过程中触发的消息

           在窗体创建过程中我们将会依次触发几个主要消息(当然还会有别的消息): 

           1.  WM_NCCREATE

           2.  WM_CREATE

           3.  WM_SHOWWINDOW

           我们看看验证代码:

       
    switch (uMsg)
    {
    case WM_NCCREATE:
    {
        TCHAR szInfo[256];
        CREATESTRUCT*    pcs    = (CREATESTRUCT*)lParam;
        _stprintf(szInfo,
                  _T("\nCreate Step: %d (WM_NCCREATE) lParam: [Instance: 0x%0X  CreateParams: %d  Size: (%d, %d, %d, %d)]\n"),
                  ++nStep, pcs->hInstance, pcs->lpCreateParams, pcs->x, pcs->y, pcs->cx, pcs->cy);
        OutputDebugString(szInfo);

        break;
    }
    case WM_CREATE:
    {
        TCHAR szInfo[256];
        CREATESTRUCT*    pcs    = (CREATESTRUCT*)lParam;
        _stprintf(szInfo,
                  _T("\nCreate Step: %d (WM_CREATE) lParam: [Instance: 0x%0X  CreateParams: %d  Size: (%d, %d, %d, %d)]\n"),
                  ++nStep, pcs->hInstance, pcs->lpCreateParams, pcs->x, pcs->y, pcs->cx, pcs->cy);
        OutputDebugString(szInfo);

        //--: Create sub control
        hButton = CreateWindow(_T("bUTTON"), _T("&Exit"), WS_VISIBLE|WS_CHILD, 0, 0, 100, 50, hWnd, (HMENU)1001, NULL, 0);

        break;
    }
    case WM_SHOWWINDOW:
    {
        TCHAR szInfo[256];
        _stprintf(szInfo,
                  _T("\nCreate Step: %d (WM_SHOWWINDOW)\n"),
                  ++nStep);
        OutputDebugString(szInfo);

        break;
    }

           执行后结果:

          


    Create Step: 1 (WM_NCCREATE) lParam: [Instance: 0x8C0000  CreateParams: 7000  Size: (0, 0, 800, 480)]
    ¡°\uc1Demo0021.exe¡±: ÒѼÓÔØ¡°\uc1C:\Windows\System32\dwmapi.dll¡±

    Create Step: 2 (WM_CREATE) lParam: [Instance: 0x8C0000  CreateParams: 7000  Size: (0, 0, 800, 480)]

    Create Step: 3 (WM_SHOWWINDOW)

          可以看到,我们在没有看到窗体时会触发二个消息(WM_NCWREATE, WM_CREATE)

           这两个消息可以lParam参数指向一个CREATESTRUCT类型的指针,其CREATESTRUCT成员LPVOID lpCreateParams 接收创建函数的最后一个参数传递过去的参数.

         

       演示代码

  • 相关阅读:
    char、varchar、nchar、nvarchar的区别
    linux和windows下分别如何查看电脑是32位的还是64位?
    HP-Unix安装Memcache问题
    安装GCC-4.6.1详细教程
    JSTL 核心标签库 使用
    JSP && EL表达式
    UNIX环境高级编程——标准IO-实现查看所有用户
    UNIX环境高级编程——环境变量表读取/添加/修改/删除
    UNIX网络编程——进程间通信概述
    UNIX网络编程——通过UNIX域套接字传递描述符和 sendmsg/recvmsg 函数
  • 原文地址:https://www.cnblogs.com/ztercel/p/2135591.html
Copyright © 2020-2023  润新知