标签:
转
看下CWindowWnd类与CPaintManagerUI类是咋进行消息分发的吧.
1. 先看下CPaintManagerUI类的MessageLoop函数:
- void CPaintManagerUI::MessageLoop()
- {
- MSG msg = { 0 };
- while( ::GetMessage(&msg, NULL, 0, 0) ) {
- if( !CPaintManagerUI::TranslateMessage(&msg) ) {
- ::TranslateMessage(&msg);
- ::DispatchMessage(&msg);
- }
- }
- }
消息第一次会由CPaintManagerUI类的TranslateMessage消息接收到.
2. 调用CWindowWnd::Create创建窗口. 完成以下操作:
1) 如果要子类下Window的控件(就是系统的控件, 而不是duilib的模拟控件), 就设置__ControlProc函数为消息回调函数.
2)不子类化, 就注册窗口类. 此时设置__WndProc为窗口消息处理回调函数.
3)用CreateWindowEx API函数创建窗口.
这里先不看子类化相关的, 我要先看明白标准的窗口创建过程. 这也操作后消息就会分发到__WndProc了,
3. 看下__WndProc函数的定义:
- LRESULT CALLBACK CWindowWnd::__WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- CWindowWnd* pThis = NULL;
- if( uMsg == WM_NCCREATE ) {
- LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
- pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
- pThis->m_hWnd = hWnd;
- ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis));
- }
- else {
- pThis = reinterpret_cast<CWindowWnd*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
- if( uMsg == WM_NCDESTROY && pThis != NULL ) {
- LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
- ::SetWindowLongPtr(pThis->m_hWnd, GWLP_USERDATA, 0L);
- if( pThis->m_bSubclassed ) pThis->Unsubclass();
- pThis->m_hWnd = NULL;
- pThis->OnFinalMessage(hWnd);
- return lRes;
- }
- }
- if( pThis != NULL ) {
- return pThis->HandleMessage(uMsg, wParam, lParam);
- }
- else {
- return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- }
消息第二次就由__WndProc接收到, 然后再传到CWindowWnd类的HandlerMessage函数中.
3. 看看CWindowWnd类的继承类对于HandlerMessage虚函数的实现.
- LRESULT CMainWnd::HandleMessage( UINT uMsg, WPARAM wParam, LPARAM lParam )
- {
- LRESULT lRes = 0;
- BOOL bHandled = TRUE;
- switch ( uMsg )
- {
- case WM_CREATE: lRes = OnInitResource( bHandled ); break;
- default:
- bHandled = FALSE;
- }
-
- if ( bHandled )
- {
- return lRes;
- }
-
- if ( m_pm.MessageHandler( uMsg, wParam, lParam, lRes ) )
- {
- return lRes;
- }
-
- return CWindowWnd::HandleMessage( uMsg, wParam, lParam );
- }
在这里就是用户要按消息进行具体的处理了. 之后要传到CPaintManagerUI类对象的MessageHandler函数. 未处理的消息就要返回给CWindowWnd类的默认消息处理函数来处理了.
4. CPaintManagerUI类的TranslateMessage, MessageHandler函数的内容.
- BOOL CPaintManagerUI::TranslateMessage(const LPMSG pMsg)
- {
- HWND hwndParent = ::GetParent(pMsg->hwnd);
- UINT uStyle = GetWindowStyle(pMsg->hwnd);
- LRESULT lRes = 0;
- for( int i = 0; i < m_aPreMessages.GetSize(); i++ ) {
- CPaintManagerUI* pT = static_cast<CPaintManagerUI*>(m_aPreMessages[i]);
- if( pMsg->hwnd == pT->GetPaintWindow()
- || (hwndParent == pT->GetPaintWindow() && ((uStyle & WS_CHILD) != 0)) )
- {
- if( pT->PreMessageHandler(pMsg->message, pMsg->wParam, pMsg->lParam, lRes) ) return TRUE;
- }
- }
- return FALSE;
- }
m_aPreMessage为静态成员变量, 在CPaintManagerUI::Init进行窗口与此类绑定时添加到此变量中.
5. CPaintManagerUI::PreMessageHandler消息过滤函数.
- BOOL CPaintManagerUI::PreMessageHandler(UINT uMsg, WPARAM wParam, LPARAM lParam, LRESULT&
- {
-
-
- for( int i = 0; i < m_aPreMessageFilters.GetSize(); i++ )
- {
- BOOL bHandled = FALSE;
- LRESULT lResult = static_cast<IMessageFilterUI*>(m_aPreMessageFilters[i])->MessageHandler(uMsg, wParam, lParam, bHandled);
- if( bHandled ) {
- return TRUE;
- }
- }
-
-
-
-
- }
5. CPaintManagerUI::MessageHandler函数.
1) 遍历m_aMessageFilters列表中的IMessageFilterUI接口, 并调用MessageHandler函数, 再次进行相关的消息过滤功能.(与上面的m_aPreMessageFilters类似)
2) 在此会处理窗口的WM_PAINT消息. 显示所有控件的外观与状态.
3) 处理鼠标事件, 实现控件激活和相关事件.
4) 处理WM_TIMER消息, 所有控件要用CPaintManagerUI的SetTimer, KillTimer等函数实现计时器功能.
5) 处理CPaintManagerUI类的自定消息, WM_APP + 1与 +2,
WM_APP + 1是用于控件延迟销毁控件对象
WM_APP + 2销毁异步消息的处理.
( 异步控件消息用CPaintManagerUI::SendNotify函数, 把消息对象添加到m_aAsyncNotify列表中, 再PostMessage函数WM_APP + 2 )
5) 其它基本的窗口相关消息的处理.
CPaintManagerUI把DUILIB内部的事件都是用TEventUI结构的形式调用CControlUI类的Event函数来投递的.
duilib库分析: 消息流程分析
标签:
原文地址:http://www.cnblogs.com/liangxiaofeng/p/4880919.html