学习《Windows via C/C++》的时候,看到了一个宏~
chHANDLE_DLGMSG(hwnd, WM_COMMAND, Dlg_OnCommand);
于是查了一下,原来这个宏是这样子的:
/////////////////////////// chHANDLE_DLGMSG Macro ///////////////////////////// // The normal HANDLE_MSG macro in WindowsX.h does not work properly for dialog // boxes because DlgProc returns a BOOL instead of an LRESULT (like // WndProcs). This chHANDLE_DLGMSG macro corrects the problem: #define chHANDLE_DLGMSG(hWnd, message, fn) \ case (message): return (SetDlgMsgResult(hWnd, uMsg, \ HANDLE_##message((hWnd), (wParam), (lParam), (fn))))
http://blog.csdn.net/jasonM2008/article/details/3869296
但是 SetDlgMsgResult 函数依旧令人费解,VS2010 的 MSDN 上居然找不到,后来终于在 windowsX.h 上发现它的踪影了:
#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))
怎么样?够繁杂吧!看来看去看不懂,只好继续 google ,终于找到了这篇文章:
http://www.powerbasic.com/support/pbforums/showthread.php?t=18613
也就是说,当对话框接收到的是上面列出的消息时,因为它们的返回值是固定类型的,所以就直接返回 result 了。
但如果接收到其他消息的时候,程序员可能希望返回一些其他东西,比如句柄啊,指针啊,等等。
此时如果直接以返回值的形式返回 result,则会将结果返回到默认的对话框处理函数 DefDlgProc 中,而程序员是无法进入 DefDlgProc 去取得这个返回值的(一般来说)。这时微软为每个窗口所定义的额外字节便可以粉墨登场了!程序员可以将需要返回的特殊值写入到那4个额外字节中:
SetWindowLongPtr (hwnd, DWLP_MSGRESULT, result) ;
然后,程序员就可以在其他的地方通过 GetWindowLongPtr 取得原先存储在额外字节中的内容了。
result = GetWindowLongPtr (hwnd, DWLP_MSGRESULT) ;
当然,前提是一旦设置了 SetWindowLongPtr 以后就不能再对窗口进行其他的操作,比如 SetWindowText ,这样一来会清空窗口的额外字节中的内容~~
哦,对了。DWLP_MSGRESULT 和 DWL_MSGRESULT 是差不多的~~具体看定义:
#include <winuser.h> /* * Get/SetWindowWord/Long offsets for use with WC_DIALOG windows */ #define DWL_MSGRESULT 0 #define DWL_DLGPROC 4 #define DWL_USER 8 #ifdef _WIN64 #undef DWL_MSGRESULT #undef DWL_DLGPROC #undef DWL_USER #endif /* _WIN64 */ #define DWLP_MSGRESULT 0 #define DWLP_DLGPROC DWLP_MSGRESULT + sizeof(LRESULT) #define DWLP_USER DWLP_DLGPROC + sizeof(DLGPROC)