UIBase.cpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576
  1. #include "StdAfx.h"
  2. #ifdef _DEBUG
  3. #include <shlwapi.h>
  4. #pragma comment(lib, "shlwapi.lib")
  5. #endif
  6. namespace DuiLib {
  7. /////////////////////////////////////////////////////////////////////////////////////
  8. //
  9. //
  10. void UILIB_API DUI__Trace(LPCTSTR pstrFormat, ...)
  11. {
  12. #ifdef _DEBUG
  13. TCHAR szBuffer[2048] = {0};
  14. va_list args;
  15. va_start(args, pstrFormat);
  16. _vsntprintf(szBuffer, 2048, pstrFormat, args);
  17. va_end(args);
  18. CDuiString strMsg = szBuffer;
  19. strMsg += _T("\n");
  20. OutputDebugString(strMsg.GetData());
  21. #endif
  22. }
  23. LPCTSTR DUI__TraceMsg(UINT uMsg)
  24. {
  25. #define MSGDEF(x) if(uMsg==x) return _T(#x)
  26. MSGDEF(WM_SETCURSOR);
  27. MSGDEF(WM_NCHITTEST);
  28. MSGDEF(WM_NCPAINT);
  29. MSGDEF(WM_PAINT);
  30. MSGDEF(WM_ERASEBKGND);
  31. MSGDEF(WM_NCMOUSEMOVE);
  32. MSGDEF(WM_MOUSEMOVE);
  33. MSGDEF(WM_MOUSELEAVE);
  34. MSGDEF(WM_MOUSEHOVER);
  35. MSGDEF(WM_NOTIFY);
  36. MSGDEF(WM_COMMAND);
  37. MSGDEF(WM_MEASUREITEM);
  38. MSGDEF(WM_DRAWITEM);
  39. MSGDEF(WM_LBUTTONDOWN);
  40. MSGDEF(WM_LBUTTONUP);
  41. MSGDEF(WM_LBUTTONDBLCLK);
  42. MSGDEF(WM_RBUTTONDOWN);
  43. MSGDEF(WM_RBUTTONUP);
  44. MSGDEF(WM_RBUTTONDBLCLK);
  45. MSGDEF(WM_SETFOCUS);
  46. MSGDEF(WM_KILLFOCUS);
  47. MSGDEF(WM_MOVE);
  48. MSGDEF(WM_SIZE);
  49. MSGDEF(WM_SIZING);
  50. MSGDEF(WM_MOVING);
  51. MSGDEF(WM_GETMINMAXINFO);
  52. MSGDEF(WM_CAPTURECHANGED);
  53. MSGDEF(WM_WINDOWPOSCHANGED);
  54. MSGDEF(WM_WINDOWPOSCHANGING);
  55. MSGDEF(WM_NCCALCSIZE);
  56. MSGDEF(WM_NCCREATE);
  57. MSGDEF(WM_NCDESTROY);
  58. MSGDEF(WM_TIMER);
  59. MSGDEF(WM_KEYDOWN);
  60. MSGDEF(WM_KEYUP);
  61. MSGDEF(WM_CHAR);
  62. MSGDEF(WM_SYSKEYDOWN);
  63. MSGDEF(WM_SYSKEYUP);
  64. MSGDEF(WM_SYSCOMMAND);
  65. MSGDEF(WM_SYSCHAR);
  66. MSGDEF(WM_VSCROLL);
  67. MSGDEF(WM_HSCROLL);
  68. MSGDEF(WM_CHAR);
  69. MSGDEF(WM_SHOWWINDOW);
  70. MSGDEF(WM_PARENTNOTIFY);
  71. MSGDEF(WM_CREATE);
  72. MSGDEF(WM_NCACTIVATE);
  73. MSGDEF(WM_ACTIVATE);
  74. MSGDEF(WM_ACTIVATEAPP);
  75. MSGDEF(WM_CLOSE);
  76. MSGDEF(WM_DESTROY);
  77. MSGDEF(WM_GETICON);
  78. MSGDEF(WM_GETTEXT);
  79. MSGDEF(WM_GETTEXTLENGTH);
  80. static TCHAR szMsg[10];
  81. ::wsprintf(szMsg, _T("0x%04X"), uMsg);
  82. return szMsg;
  83. }
  84. /////////////////////////////////////////////////////////////////////////////////////
  85. //
  86. //
  87. //////////////////////////////////////////////////////////////////////////
  88. //
  89. DUI_BASE_BEGIN_MESSAGE_MAP(CNotifyPump)
  90. DUI_END_MESSAGE_MAP()
  91. static const DUI_MSGMAP_ENTRY* DuiFindMessageEntry(const DUI_MSGMAP_ENTRY* lpEntry,TNotifyUI& msg )
  92. {
  93. CDuiString sMsgType = msg.sType;
  94. CDuiString sCtrlName = msg.pSender->GetName();
  95. const DUI_MSGMAP_ENTRY* pMsgTypeEntry = NULL;
  96. while (lpEntry->nSig != DuiSig_end)
  97. {
  98. if(lpEntry->sMsgType==sMsgType)
  99. {
  100. if(!lpEntry->sCtrlName.IsEmpty())
  101. {
  102. if(lpEntry->sCtrlName==sCtrlName)
  103. {
  104. return lpEntry;
  105. }
  106. }
  107. else
  108. {
  109. pMsgTypeEntry = lpEntry;
  110. }
  111. }
  112. lpEntry++;
  113. }
  114. return pMsgTypeEntry;
  115. }
  116. bool CNotifyPump::AddVirtualWnd(CDuiString strName,CNotifyPump* pObject)
  117. {
  118. if( m_VirtualWndMap.Find(strName) == NULL )
  119. {
  120. m_VirtualWndMap.Insert(strName.GetData(),(LPVOID)pObject);
  121. return true;
  122. }
  123. return false;
  124. }
  125. bool CNotifyPump::RemoveVirtualWnd(CDuiString strName)
  126. {
  127. if( m_VirtualWndMap.Find(strName) != NULL )
  128. {
  129. m_VirtualWndMap.Remove(strName);
  130. return true;
  131. }
  132. return false;
  133. }
  134. bool CNotifyPump::LoopDispatch(TNotifyUI& msg)
  135. {
  136. const DUI_MSGMAP_ENTRY* lpEntry = NULL;
  137. const DUI_MSGMAP* pMessageMap = NULL;
  138. #ifndef UILIB_STATIC
  139. for(pMessageMap = GetMessageMap(); pMessageMap!=NULL; pMessageMap = (*pMessageMap->pfnGetBaseMap)())
  140. #else
  141. for(pMessageMap = GetMessageMap(); pMessageMap!=NULL; pMessageMap = pMessageMap->pBaseMap)
  142. #endif
  143. {
  144. #ifndef UILIB_STATIC
  145. ASSERT(pMessageMap != (*pMessageMap->pfnGetBaseMap)());
  146. #else
  147. ASSERT(pMessageMap != pMessageMap->pBaseMap);
  148. #endif
  149. if ((lpEntry = DuiFindMessageEntry(pMessageMap->lpEntries,msg)) != NULL)
  150. {
  151. goto LDispatch;
  152. }
  153. }
  154. return false;
  155. LDispatch:
  156. union DuiMessageMapFunctions mmf;
  157. mmf.pfn = lpEntry->pfn;
  158. bool bRet = false;
  159. int nSig;
  160. nSig = lpEntry->nSig;
  161. switch (nSig)
  162. {
  163. default:
  164. ASSERT(FALSE);
  165. break;
  166. case DuiSig_lwl:
  167. (this->*mmf.pfn_Notify_lwl)(msg.wParam,msg.lParam);
  168. bRet = true;
  169. break;
  170. case DuiSig_vn:
  171. (this->*mmf.pfn_Notify_vn)(msg);
  172. bRet = true;
  173. break;
  174. }
  175. return bRet;
  176. }
  177. void CNotifyPump::NotifyPump(TNotifyUI& msg)
  178. {
  179. ///遍历虚拟窗口
  180. if( !msg.sVirtualWnd.IsEmpty() ){
  181. for( int i = 0; i< m_VirtualWndMap.GetSize(); i++ ) {
  182. if( LPCTSTR key = m_VirtualWndMap.GetAt(i) ) {
  183. if( _tcsicmp(key, msg.sVirtualWnd.GetData()) == 0 ){
  184. CNotifyPump* pObject = static_cast<CNotifyPump*>(m_VirtualWndMap.Find(key, false));
  185. if( pObject && pObject->LoopDispatch(msg) )
  186. return;
  187. }
  188. }
  189. }
  190. }
  191. ///
  192. //遍历主窗口
  193. LoopDispatch( msg );
  194. }
  195. //////////////////////////////////////////////////////////////////////////
  196. ///
  197. CWindowWnd::CWindowWnd() : m_hWnd(NULL), m_OldWndProc(::DefWindowProc), m_bSubclassed(false), m_bUnicode(false), m_bFakeModal(false)
  198. {
  199. }
  200. HWND CWindowWnd::GetHWND() const
  201. {
  202. return m_hWnd;
  203. }
  204. UINT CWindowWnd::GetClassStyle() const
  205. {
  206. return 0;
  207. }
  208. LPCTSTR CWindowWnd::GetSuperClassName() const
  209. {
  210. return NULL;
  211. }
  212. CWindowWnd::operator HWND() const
  213. {
  214. return m_hWnd;
  215. }
  216. void CWindowWnd::EnableUnicode()
  217. {
  218. m_bUnicode = true;
  219. }
  220. HWND CWindowWnd::CreateDuiWindow( HWND hwndParent, LPCTSTR pstrWindowName,DWORD dwStyle /*=0*/, DWORD dwExStyle /*=0*/ )
  221. {
  222. return Create(hwndParent,pstrWindowName,dwStyle,dwExStyle,0,0,0,0,NULL);
  223. }
  224. HWND CWindowWnd::Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, const RECT rc, HMENU hMenu)
  225. {
  226. return Create(hwndParent, pstrName, dwStyle, dwExStyle, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hMenu);
  227. }
  228. HWND CWindowWnd::Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int cx, int cy, HMENU hMenu)
  229. {
  230. if( GetSuperClassName() != NULL && !RegisterSuperclass() ) return NULL;
  231. if( GetSuperClassName() == NULL && !RegisterWindowClass() ) return NULL;
  232. if(m_bUnicode) {
  233. #ifndef UNICODE
  234. LPWSTR lpClassName = a2w((char*)GetWindowClassName());
  235. LPWSTR lpName = a2w((char*)pstrName);
  236. #else
  237. LPWSTR lpClassName = (LPWSTR)GetWindowClassName();
  238. LPWSTR lpName = (LPWSTR)pstrName;
  239. #endif
  240. m_hWnd = ::CreateWindowExW(dwExStyle, lpClassName, lpName, dwStyle, x, y, cx, cy, hwndParent, hMenu, CPaintManagerUI::GetInstance(), this);
  241. #ifndef UNICODE
  242. delete []lpClassName;
  243. delete []lpName;
  244. #endif
  245. }
  246. else {
  247. m_hWnd = ::CreateWindowEx(dwExStyle, GetWindowClassName(), pstrName, dwStyle, x, y, cx, cy, hwndParent, hMenu, CPaintManagerUI::GetInstance(), this);
  248. }
  249. ASSERT(m_hWnd!=NULL);
  250. return m_hWnd;
  251. }
  252. HWND CWindowWnd::Subclass(HWND hWnd)
  253. {
  254. ASSERT(::IsWindow(hWnd));
  255. ASSERT(m_hWnd==NULL);
  256. m_OldWndProc = SubclassWindow(hWnd, __WndProc);
  257. if( m_OldWndProc == NULL ) return NULL;
  258. m_bSubclassed = true;
  259. m_hWnd = hWnd;
  260. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(this));
  261. return m_hWnd;
  262. }
  263. void CWindowWnd::Unsubclass()
  264. {
  265. ASSERT(::IsWindow(m_hWnd));
  266. if( !::IsWindow(m_hWnd) ) return;
  267. if( !m_bSubclassed ) return;
  268. SubclassWindow(m_hWnd, m_OldWndProc);
  269. m_OldWndProc = ::DefWindowProc;
  270. m_bSubclassed = false;
  271. }
  272. void CWindowWnd::ShowWindow(bool bShow /*= true*/, bool bTakeFocus /*= false*/)
  273. {
  274. ASSERT(::IsWindow(m_hWnd));
  275. if( !::IsWindow(m_hWnd) ) return;
  276. ::ShowWindow(m_hWnd, bShow ? (bTakeFocus ? SW_SHOWNORMAL : SW_SHOWNOACTIVATE) : SW_HIDE);
  277. }
  278. UINT CWindowWnd::ShowModal()
  279. {
  280. ASSERT(::IsWindow(m_hWnd));
  281. UINT nRet = 0;
  282. HWND hWndParent = GetWindowOwner(m_hWnd);
  283. ::ShowWindow(m_hWnd, SW_SHOWNORMAL);
  284. ::EnableWindow(hWndParent, FALSE);
  285. MSG msg = { 0 };
  286. while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) {
  287. if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) {
  288. nRet = (int)msg.wParam;
  289. ::EnableWindow(hWndParent, TRUE);
  290. ::SetFocus(hWndParent);
  291. }
  292. if( !CPaintManagerUI::TranslateMessage(&msg) ) {
  293. ::TranslateMessage(&msg);
  294. ::DispatchMessage(&msg);
  295. }
  296. if( msg.message == WM_QUIT ) break;
  297. }
  298. ::EnableWindow(hWndParent, TRUE);
  299. ::SetFocus(hWndParent);
  300. if( msg.message == WM_QUIT ) ::PostQuitMessage((int)msg.wParam);
  301. return nRet;
  302. }
  303. void CWindowWnd::ShowModalFake()
  304. {
  305. ASSERT(::IsWindow(m_hWnd));
  306. auto p_hwnd = GetWindowOwner(m_hWnd);
  307. ASSERT(::IsWindow(p_hwnd));
  308. ASSERT(p_hwnd);
  309. ::EnableWindow(p_hwnd, FALSE);
  310. ShowWindow();
  311. m_bFakeModal = true;
  312. }
  313. void CWindowWnd::Close(UINT nRet)
  314. {
  315. if (m_bFakeModal)
  316. {
  317. auto parent_hwnd = GetWindowOwner(m_hWnd);
  318. ASSERT(::IsWindow(parent_hwnd));
  319. ::EnableWindow(parent_hwnd, TRUE);
  320. ::SetFocus(parent_hwnd);
  321. m_bFakeModal = false;
  322. }
  323. ASSERT(::IsWindow(m_hWnd));
  324. if (!::IsWindow(m_hWnd)) return;
  325. PostMessage(WM_CLOSE, (WPARAM)nRet, 0L);
  326. }
  327. void CWindowWnd::CenterWindow()
  328. {
  329. ASSERT(::IsWindow(m_hWnd));
  330. ASSERT((GetWindowStyle(m_hWnd)&WS_CHILD)==0);
  331. RECT rcDlg = { 0 };
  332. ::GetWindowRect(m_hWnd, &rcDlg);
  333. RECT rcArea = { 0 };
  334. RECT rcCenter = { 0 };
  335. HWND hWnd=*this;
  336. HWND hWndParent = ::GetParent(m_hWnd);
  337. HWND hWndCenter = ::GetWindowOwner(m_hWnd);
  338. if (hWndCenter!=NULL)
  339. hWnd=hWndCenter;
  340. // 处理多显示器模式下屏幕居中
  341. MONITORINFO oMonitor = {};
  342. oMonitor.cbSize = sizeof(oMonitor);
  343. ::GetMonitorInfo(::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &oMonitor);
  344. rcArea = oMonitor.rcWork;
  345. if( hWndCenter == NULL )
  346. rcCenter = rcArea;
  347. else
  348. ::GetWindowRect(hWndCenter, &rcCenter);
  349. int DlgWidth = rcDlg.right - rcDlg.left;
  350. int DlgHeight = rcDlg.bottom - rcDlg.top;
  351. // Find dialog's upper left based on rcCenter
  352. int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
  353. int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;
  354. // The dialog is outside the screen, move it inside
  355. if( xLeft < rcArea.left ) xLeft = rcArea.left;
  356. else if( xLeft + DlgWidth > rcArea.right ) xLeft = rcArea.right - DlgWidth;
  357. if( yTop < rcArea.top ) yTop = rcArea.top;
  358. else if( yTop + DlgHeight > rcArea.bottom ) yTop = rcArea.bottom - DlgHeight;
  359. ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  360. }
  361. void CWindowWnd::SetIcon(UINT nRes)
  362. {
  363. HICON hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  364. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  365. LR_DEFAULTCOLOR);
  366. ASSERT(hIcon);
  367. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) TRUE, (LPARAM) hIcon);
  368. hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  369. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  370. LR_DEFAULTCOLOR);
  371. ASSERT(hIcon);
  372. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) FALSE, (LPARAM) hIcon);
  373. }
  374. bool CWindowWnd::RegisterWindowClass()
  375. {
  376. WNDCLASS wc = { 0 };
  377. wc.style = GetClassStyle();
  378. wc.cbClsExtra = 0;
  379. wc.cbWndExtra = 0;
  380. wc.hIcon = NULL;
  381. wc.lpfnWndProc = CWindowWnd::__WndProc;
  382. wc.hInstance = CPaintManagerUI::GetInstance();
  383. wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  384. wc.hbrBackground = NULL;
  385. wc.lpszMenuName = NULL;
  386. wc.lpszClassName = GetWindowClassName();
  387. ATOM ret = ::RegisterClass(&wc);
  388. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  389. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  390. }
  391. bool CWindowWnd::RegisterSuperclass()
  392. {
  393. if(m_bUnicode) {
  394. #ifndef UNICODE
  395. LPWSTR lpSuperClassName = a2w((char*)GetSuperClassName());
  396. LPWSTR lpClassName = a2w((char*)GetWindowClassName());
  397. #else
  398. LPWSTR lpSuperClassName = (LPWSTR)GetSuperClassName();
  399. LPWSTR lpClassName = (LPWSTR)GetWindowClassName();
  400. #endif
  401. // Get the class information from an existing
  402. // window so we can subclass it later on...
  403. WNDCLASSEXW wc = { 0 };
  404. wc.cbSize = sizeof(WNDCLASSEXW);
  405. if( !::GetClassInfoExW(NULL, lpSuperClassName, &wc) ) {
  406. if( !::GetClassInfoExW(CPaintManagerUI::GetInstance(), lpSuperClassName, &wc) ) {
  407. ASSERT(!"Unable to locate window class");
  408. return NULL;
  409. }
  410. }
  411. m_OldWndProc = wc.lpfnWndProc;
  412. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  413. wc.hInstance = CPaintManagerUI::GetInstance();
  414. wc.lpszClassName = lpClassName;
  415. ATOM ret = ::RegisterClassExW(&wc);
  416. #ifndef UNICODE
  417. delete []lpClassName;
  418. delete []lpSuperClassName;
  419. #endif
  420. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  421. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  422. }
  423. else {
  424. // Get the class information from an existing
  425. // window so we can subclass it later on...
  426. WNDCLASSEX wc = { 0 };
  427. wc.cbSize = sizeof(WNDCLASSEX);
  428. if( !::GetClassInfoEx(NULL, GetSuperClassName(), &wc) ) {
  429. if( !::GetClassInfoEx(CPaintManagerUI::GetInstance(), GetSuperClassName(), &wc) ) {
  430. ASSERT(!"Unable to locate window class");
  431. return NULL;
  432. }
  433. }
  434. m_OldWndProc = wc.lpfnWndProc;
  435. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  436. wc.hInstance = CPaintManagerUI::GetInstance();
  437. wc.lpszClassName = GetWindowClassName();
  438. ATOM ret = ::RegisterClassEx(&wc);
  439. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  440. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  441. }
  442. }
  443. LRESULT CALLBACK CWindowWnd::__WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  444. {
  445. CWindowWnd* pThis = NULL;
  446. if( uMsg == WM_NCCREATE ) {
  447. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  448. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  449. pThis->m_hWnd = hWnd;
  450. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis));
  451. }
  452. else {
  453. pThis = reinterpret_cast<CWindowWnd*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
  454. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  455. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  456. ::SetWindowLongPtr(pThis->m_hWnd, GWLP_USERDATA, 0L);
  457. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  458. pThis->m_hWnd = NULL;
  459. pThis->OnFinalMessage(hWnd);
  460. return lRes;
  461. }
  462. }
  463. if( pThis != NULL ) {
  464. return pThis->HandleMessage(uMsg, wParam, lParam);
  465. }
  466. else {
  467. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  468. }
  469. }
  470. LRESULT CALLBACK CWindowWnd::__ControlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  471. {
  472. CWindowWnd* pThis = NULL;
  473. if( uMsg == WM_NCCREATE ) {
  474. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  475. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  476. ::SetProp(hWnd, _T("WndX"), (HANDLE) pThis);
  477. pThis->m_hWnd = hWnd;
  478. }
  479. else {
  480. pThis = reinterpret_cast<CWindowWnd*>(::GetProp(hWnd, _T("WndX")));
  481. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  482. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  483. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  484. ::SetProp(hWnd, _T("WndX"), NULL);
  485. pThis->m_hWnd = NULL;
  486. pThis->OnFinalMessage(hWnd);
  487. return lRes;
  488. }
  489. }
  490. if( pThis != NULL ) {
  491. return pThis->HandleMessage(uMsg, wParam, lParam);
  492. }
  493. else {
  494. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  495. }
  496. }
  497. LRESULT CWindowWnd::SendMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  498. {
  499. ASSERT(::IsWindow(m_hWnd));
  500. return ::SendMessage(m_hWnd, uMsg, wParam, lParam);
  501. }
  502. LRESULT CWindowWnd::PostMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  503. {
  504. ASSERT(::IsWindow(m_hWnd));
  505. return ::PostMessage(m_hWnd, uMsg, wParam, lParam);
  506. }
  507. void CWindowWnd::ResizeClient(int cx /*= -1*/, int cy /*= -1*/)
  508. {
  509. ASSERT(::IsWindow(m_hWnd));
  510. RECT rc = { 0 };
  511. if( !::GetClientRect(m_hWnd, &rc) ) return;
  512. if( cx != -1 ) rc.right = cx;
  513. if( cy != -1 ) rc.bottom = cy;
  514. if( !::AdjustWindowRectEx(&rc, GetWindowStyle(m_hWnd), (!(GetWindowStyle(m_hWnd) & WS_CHILD) && (::GetMenu(m_hWnd) != NULL)), GetWindowExStyle(m_hWnd)) ) return;
  515. ::SetWindowPos(m_hWnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  516. }
  517. LRESULT CWindowWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  518. {
  519. return ::CallWindowProc(m_OldWndProc, m_hWnd, uMsg, wParam, lParam);
  520. }
  521. void CWindowWnd::OnFinalMessage(HWND /*hWnd*/)
  522. {
  523. }
  524. } // namespace DuiLib