WTL虽说更方便了一些,但也有些迷惑之处.
例如:
LRESULT OnCreate(LPCREATESTRUCT lpcs)
{
//从注册表读,初始化 分组目录
SetMsgHandled(FALSE);
return 0;
}
与
LRESULT OnCreate(LPCREATESTRUCT lpcs)
{
//从注册表读,初始化 分组目录
return 1;
}
如果没有前面thunk机制的分析会弄不清消息的流程,搞不懂什么时候返回0,1,什么时候应该用SetMsgHandled(FALSE);
其实只有几点:
1
需要调用默认窗口过程DefWindowProc时,执行
SetMsgHandled(FALSE)
2
不调用默认窗口过程.什么都不用做.默认SetMsgHandled(TRUE).
3
不调用默认窗口过程,直接返回,可以:
return 1/0;
这个返回值是我们的回调窗口处理过程返回给系统的值.具体是0,1,-1什么的可以看相应消息的返回值部分.
4
可以混合使用,比如:
SetMsgHandled(FALSE);
return 0;
这里的return 0;已经完全没用,因为lRes会接受DefWindowProc的返回值.只是为了防止
下面是相关代码,可加深理解:
#define MSG_WM_INITDIALOG(func) \
if (uMsg == WM_INITDIALOG) \
{ \
SetMsgHandled(TRUE); \
lResult = (LRESULT)func((HWND)wParam, lParam); \
if(IsMsgHandled()) \
return TRUE; \
}
上面的lResult的值是下面的lRes传过来的.
窗口处理函数:
template
LRESULT CALLBACK CWindowImplBaseT< TBase, TWinTraits >::WindowProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
CWindowImplBaseT< TBase, TWinTraits >* pThis = (CWindowImplBaseT< TBase, TWinTraits >*)hWnd;
// set a ptr to this message and save the old value
MSG msg = { pThis->m_hWnd, uMsg, wParam, lParam, 0, { 0, 0 } };
const MSG* pOldMsg = pThis->m_pCurrentMsg;
pThis->m_pCurrentMsg = &msg;
// pass to the message map to process
LRESULT lRes;
BOOL bRet = pThis->ProcessWindowMessage(pThis->m_hWnd, uMsg, wParam, lParam, lRes, 0);
// restore saved value for the current message
ATLASSERT(pThis->m_pCurrentMsg == &msg);
pThis->m_pCurrentMsg = pOldMsg;
// do the default processing if message was not handled
if(!bRet)
{
if(uMsg != WM_NCDESTROY)
lRes = pThis->DefWindowProc(uMsg, wParam, lParam);
else
{
// unsubclass, if needed
LONG pfnWndProc = ::GetWindowLong(pThis->m_hWnd, GWL_WNDPROC);
lRes = pThis->DefWindowProc(uMsg, wParam, lParam);
if(pThis->m_pfnSuperWindowProc != ::DefWindowProc && ::GetWindowLong(pThis->m_hWnd, GWL_WNDPROC) == pfnWndProc)
::SetWindowLong(pThis->m_hWnd, GWL_WNDPROC, (LONG)pThis->m_pfnSuperWindowProc);
// clear out window handle
HWND hWnd = pThis->m_hWnd;
pThis->m_hWnd = NULL;
// clean up after window is destroyed
pThis->OnFinalMessage(hWnd);
}
}
return lRes;
}
阅读(3030) | 评论(0) | 转发(0) |