SDK:用CreateWindowEx创建模态对话框

PomeloWu原创©,转载请注明出处

 

SDK下,我们通常用DialogBox来创建模态对话框。DialogBox并不是一个Win32API,它实际上是一个宏,调用DialogBoxParam来创建对话框。我们能在<winuser.h>中看到这样的宏定义:

#define DialogBoxA(hInstance, lpTemplate, hWndParent, lpDialogFunc) /

DialogBoxParamA(hInstance, lpTemplate, hWndParent, lpDialogFunc, 0L )

#define DialogBoxW(hInstance, lpTemplate, hWndParent, lpDialogFunc) /

DialogBoxParamW(hInstance, lpTemplate, hWndParent, lpDialogFunc, 0L )

#ifdef UNICODE

#define DialogBox  DialogBoxW

#else

#define DialogBox  DialogBoxA

#endif // !UNICODE

而在MSDN上,DialogBoxParamRemark里边又这么说“The DialogBoxParam function uses the CreateWindowEx function to create the dialog box”,看来似乎用CreateWindowEx也就能搞定了。建一个SDK的工程,用下面的代码,果然,模态对话框弹出来了。

// SDKDialog.cpp : Defines the entry point for the application.

//

 

#include "stdafx.h"

#include "SDKDialog.h"

 

#define MAX_LOADSTRING 100

 

// Global Variables:

TCHAR szTitle[MAX_LOADSTRING];                     // The title bar text

 

int APIENTRY _tWinMain(HINSTANCE hInstance,

                     HINSTANCE hPrevInstance,

                     LPTSTR    lpCmdLine,

                     int       nCmdShow)

{

     UNREFERENCED_PARAMETER(hPrevInstance);

     UNREFERENCED_PARAMETER(lpCmdLine);

 

     // TODO: Place code here.

     MSG msg;

     HACCEL hAccelTable;

 

     // Initialize global strings

     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);

 

     // Perform application initialization:

     HWND hWnd = CreateWindowEx(WS_EX_APPWINDOW, _T("#32770"), szTitle, WS_OVERLAPPEDWINDOW,

         CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);

     ShowWindow(hWnd, nCmdShow);

     UpdateWindow(hWnd);

 

     hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_SDKDIALOG));

 

     // Main message loop:

     while (GetMessage(&msg, NULL, 0, 0))

     {

         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))

         {

              TranslateMessage(&msg);

              DispatchMessage(&msg);

         }

     }

 

     return (int) msg.wParam;

}

 

可是,弹是弹出来了,关不掉啊。试试看加个DialogProc,还是关不掉。子类化吧,在_tWinMain前加上

WNDPROC hWndProc;

LRESULT CALLBACK WindowProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)

{

     switch (uMsg)

     {

     case  WM_CLOSE:

         DestroyWindow(hWnd);

         return 0;

 

     case  WM_DESTROY:

         PostQuitMessage (0);

         break;

 

     default:

         return CallWindowProc(hWndProc, hWnd, uMsg, wParam, lParam);

     }

     return 0;

}

_tWinMainCreateWindowEx之后还有

     hWndProc = (WNDPROC)SetWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)(WNDPROC)WindowProc);

 

这下也可以也能把对话框窗口关掉了,但是新问题又出来了,WM_CREATEWM_INITDIALOG好像都没有响应,怎么初始化对话框呢?DialogBoxParam Remark部分还有一段话,DialogBoxParam then sends a WM_INITDIALOG message (and a WM_SETFONT message if the template specifies the DS_SETFONT or DS_SHELLFONT style) to the dialog box procedure. The function displays the dialog box (regardless of whether the template specifies the WS_VISIBLE style), disables the owner window, and starts its own message loop to retrieve and dispatch messages for the dialog box.看到了吧,自己发一个WM_INITDIALOG过去就是了。(需要的话,再发送一个WM_SETFONT)。大功告成!

ps,如果不自己发送一条WM_INITDIALOG消息的话,WM_INITDIALOG、WM_CREATE、WM_NCCREATE消息都不会收到;这个对话框收到的第一条消息将是WM_GETMINMAXINFO。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值