用CreateWindowEx创建模态对话框
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
SDK下,我们通常用DialogBox来创建模态对话框。DialogBox并不是一个Win32的API,它实际上是一个宏,调用DialogBoxParam来创建对话框。我们能在
#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上,DialogBoxParam的Remark里边又这么说“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;
}
_tWinMain中CreateWindowEx之后还有
hWndProc = (WNDPROC)S
etWindowLongPtr(hWnd, GWLP_WNDPROC, (LONG_PTR)(WNDPROC)WindowProc);
这下也可以也能把对话框窗口关掉了,但是新问题又出来了,WM_CREATE和WM_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。