可能有一些人知道windowsx.h头文件,它里边的HANDLE_MSG宏可以大大简化消息的处理,我们不再需要自己分析WPARAM和LPARAM,而是可以使用确定的语义处理窗口消息。
不过对话框就没那么好对付了,因为——
对话框回调函数与窗口回调函数的格式不一样,首先是返回值
这是窗口函数:
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
这是对话框回调函数:
// 老版本
BOOL CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
// 新版本(兼容64位)
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
其次,窗口函数要求对未处理消息调用DefWindowProc,并直接返回消息
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
}
对话框函数要求对已处理函数返回TRUE,未处理函数返回FALSE
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
case WM_CLOSE:
EndDialog(hWnd, 0);
return TRUE;
default:
return FALSE;
}
}
对于需要返回值的消息,对话框要求使用SetWindowLong/SetWindowLongPtr设置DWL_MSGRESULT/DWLP_MSGRESULT字段的方法来传递返回值
// 老版本
SetWindowLong(hWnd, DWL_MSGRESULT, result);
// 新版本(兼容64位)
SetWindowLongPtr(hWnd, DWLP_MSGRESULT, result);
对于这个问题,确实是很头疼的。微软这么做可能是为了简化对话框的编程,但是实际上这么做没有任何好处。
如果研究过WinAPI,可能会发现这样一个函数,DefDlgProc,但是——这个函数不是为DlgProc数而设计的——它是为对话框窗口类的WndProc实现的函数(可以在RC脚本中用CLASS "MyDlgClass"为对话框模板指定一个窗口类),调用它会自动调用DlgProc,如果在DlgProc中调用的话,就是无限递归了。
没有办法了吗?其实微软自己提供了一个解决方案
windowsx.h是微软编写的一组处理Win32窗口编程的宏定义集合,最著名的就是那个HANDLE_MSG。它只适用于WndProc,不适用于DlgProc,不过微软也给出了解决方案,那就是使用以下三个宏——
它们的宏定义如下(可以略过不看):
#define SetDlgMsgResult(hwnd, msg, result) (( \
(msg) == WM_CTLCOLORMSGBOX || \
(msg) == WM_CTLCOLOREDIT || \
(msg) == WM_CTLCOLORLISTBOX || \
(msg) == WM_CTLCOLORBTN || \
(msg) == WM_CTLCOLORDLG || \
(msg) == WM_CTLCOLORSCROLLBAR || \
(msg) == WM_CTLCOLORSTATIC || \
(msg) == WM_COMPAREITEM || \
(msg) == WM_VKEYTOITEM || \
(msg) == WM_CHARTOITEM || \
(msg) == WM_QUERYDRAGICON || \
(msg) == WM_INITDIALOG \
) ? (BOOL)(result) : (SetWindowLongPtr((hwnd), DWLP_MSGRESULT, (LPARAM)(LRESULT)(result)), TRUE))
#define DefDlgProcEx(hwnd, msg, wParam, lParam, pfRecursion) \
(*(pfRecursion) = TRUE, DefDlgProc(hwnd, msg, wParam, lParam))
#define CheckDefDlgRecursion(pfRecursion) \
if (*(pfRecursion)) { *(pfRecursion) = FALSE; return FALSE; }
这是一个使用着三个宏的示例——
#include <windows.h>
#include <windowsx.h>
#include "resource.h" // 使用“添加资源”添加对话框IDD_DIALOG1
HINSTANCE hInst = NULL; // 实例句柄
BOOL indlgproc = FALSE; // 递归标志
// 函数声明
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);
void OnClose(HWND hWnd);
// 入口点(使用DialogBox显示模态对话框)
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
hInst = hInstance;
return DialogBox(hInstance, MAKEINTRESOURCE(IDD_DIALOG1), NULL, DlgProc);
}
// 对话框函数(转发到WndProc窗口函数)
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// 消除递归并转发到WndProc
CheckDefDlgRecursion(&indlgproc);
return SetDlgMsgResult(hWnd, msg, WndProc(hWnd, msg, wParam, lParam));
}
// 窗口函数(可使用消息分发器HANDLE_MSG)
LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch (msg)
{
HANDLE_MSG(hWnd, WM_CLOSE, OnClose);
default:
// 设置递归标志并调用DefDlgProc
return DefDlgProcEx(hWnd, msg, wParam, lParam, &indlgproc);
}
}
// 被分发的WM_CLOSE处理函数
void OnClose(HWND hWnd)
{
// 结束对话框
EndDialog(hWnd, 0);
}
</windowsx.h></windows.h>
可以看到,使用非常简单——
首先,定义一个递归标志
BOOL indlgproc = FALSE; // 递归标志
然后,DlgProc这样写
// 对话框函数(转发到WndProc窗口函数)
INT_PTR CALLBACK DlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
// 消除递归并转发到WndProc
CheckDefDlgRecursion(&indlgproc);
return SetDlgMsgResult(hWnd, msg, WndProc(hWnd, msg, wParam, lParam));
}
然后,在WndProc最后default:下边,不要直接调用DefDlgProc,而是调用DefDlgProcEx
default:
// 设置递归标志并调用DefDlgProc
return DefDlgProcEx(hWnd, msg, wParam, lParam, &indlgproc);
这样做,就可以将DlgProc消息转发到WndProc,然后我们就可以使用HANDLE_MSG消息分发器了。