Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1006963
  • 博文数量: 177
  • 博客积分: 3629
  • 博客等级: 中校
  • 技术积分: 1839
  • 用 户 组: 普通用户
  • 注册时间: 2005-02-23 21:21
文章分类

全部博文(177)

文章存档

2021年(1)

2020年(5)

2019年(4)

2018年(7)

2017年(1)

2016年(4)

2014年(1)

2013年(8)

2012年(10)

2011年(50)

2009年(12)

2008年(10)

2006年(56)

2005年(8)

分类: WINDOWS

2006-05-03 01:42:35

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;
}
 
阅读(2956) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~