UIBase.cpp 18 KB

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