UIBase.cpp 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623
  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. RECT rcModal = { 0 };
  296. ::GetWindowRect(m_hWnd, &rcModal);
  297. DuiLib::TouchTrace::Log(_T("MODAL-ENTER hwnd=%p owner=%p parent=%p rect=%d,%d,%d,%d foreground=%p active=%p focus=%p enabled=%d visible=%d style=0x%08X ex=0x%08X"),
  298. (void*)m_hWnd, (void*)hWndParent, (void*)::GetParent(m_hWnd),
  299. (int)rcModal.left, (int)rcModal.top, (int)rcModal.right, (int)rcModal.bottom,
  300. (void*)::GetForegroundWindow(), (void*)::GetActiveWindow(), (void*)::GetFocus(),
  301. (int)::IsWindowEnabled(m_hWnd), (int)::IsWindowVisible(m_hWnd),
  302. (unsigned)::GetWindowLong(m_hWnd, GWL_STYLE), (unsigned)::GetWindowLong(m_hWnd, GWL_EXSTYLE));
  303. ::EnableWindow(hWndParent, FALSE);
  304. MSG msg = { 0 };
  305. while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) {
  306. if( msg.message == WM_TOUCH || msg.message == WM_LBUTTONDOWN || msg.message == WM_LBUTTONUP || msg.message == WM_MOUSEMOVE ) {
  307. 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"),
  308. m_hWnd, msg.message, msg.hwnd, (void*)msg.wParam, (void*)msg.lParam,
  309. (void*)::GetCapture(), (void*)::GetFocus(), (void*)::GetActiveWindow(), (void*)::GetForegroundWindow());
  310. }
  311. if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) {
  312. nRet = (int)msg.wParam;
  313. ::EnableWindow(hWndParent, TRUE);
  314. ::SetFocus(hWndParent);
  315. }
  316. if( !CPaintManagerUI::TranslateMessage(&msg) ) {
  317. if( msg.message == WM_TOUCH || msg.message == WM_LBUTTONDOWN || msg.message == WM_LBUTTONUP || msg.message == WM_MOUSEMOVE ) {
  318. DuiLib::TouchTrace::Log(_T("MODAL-LOOP dispatch hwnd=%p msg=0x%04X target=%p"), m_hWnd, msg.message, msg.hwnd);
  319. }
  320. ::TranslateMessage(&msg);
  321. ::DispatchMessage(&msg);
  322. }
  323. else if( msg.message == WM_TOUCH || msg.message == WM_LBUTTONDOWN || msg.message == WM_LBUTTONUP || msg.message == WM_MOUSEMOVE ) {
  324. DuiLib::TouchTrace::Log(_T("MODAL-LOOP consumed hwnd=%p msg=0x%04X target=%p"), m_hWnd, msg.message, msg.hwnd);
  325. }
  326. if( msg.message == WM_QUIT ) break;
  327. }
  328. ::EnableWindow(hWndParent, TRUE);
  329. ::SetFocus(hWndParent);
  330. if( msg.message == WM_QUIT ) ::PostQuitMessage((int)msg.wParam);
  331. return nRet;
  332. }
  333. void CWindowWnd::ShowModalFake()
  334. {
  335. ASSERT(::IsWindow(m_hWnd));
  336. auto p_hwnd = GetWindowOwner(m_hWnd);
  337. ASSERT(::IsWindow(p_hwnd));
  338. ASSERT(p_hwnd);
  339. ::EnableWindow(p_hwnd, FALSE);
  340. ShowWindow();
  341. m_bFakeModal = true;
  342. }
  343. void CWindowWnd::Close(UINT nRet)
  344. {
  345. if (m_bFakeModal)
  346. {
  347. auto parent_hwnd = GetWindowOwner(m_hWnd);
  348. ASSERT(::IsWindow(parent_hwnd));
  349. ::EnableWindow(parent_hwnd, TRUE);
  350. ::SetFocus(parent_hwnd);
  351. m_bFakeModal = false;
  352. }
  353. ASSERT(::IsWindow(m_hWnd));
  354. if (!::IsWindow(m_hWnd)) return;
  355. PostMessage(WM_CLOSE, (WPARAM)nRet, 0L);
  356. }
  357. void CWindowWnd::CenterWindow()
  358. {
  359. ASSERT(::IsWindow(m_hWnd));
  360. ASSERT((GetWindowStyle(m_hWnd)&WS_CHILD)==0);
  361. RECT rcDlg = { 0 };
  362. ::GetWindowRect(m_hWnd, &rcDlg);
  363. RECT rcArea = { 0 };
  364. RECT rcCenter = { 0 };
  365. HWND hWnd=*this;
  366. HWND hWndParent = ::GetParent(m_hWnd);
  367. HWND hWndCenter = ::GetWindowOwner(m_hWnd);
  368. if (hWndCenter!=NULL)
  369. hWnd=hWndCenter;
  370. // 处理多显示器模式下屏幕居中
  371. MONITORINFO oMonitor = {};
  372. oMonitor.cbSize = sizeof(oMonitor);
  373. ::GetMonitorInfo(::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &oMonitor);
  374. rcArea = oMonitor.rcWork;
  375. if( hWndCenter == NULL )
  376. rcCenter = rcArea;
  377. else
  378. ::GetWindowRect(hWndCenter, &rcCenter);
  379. int DlgWidth = rcDlg.right - rcDlg.left;
  380. int DlgHeight = rcDlg.bottom - rcDlg.top;
  381. // Find dialog's upper left based on rcCenter
  382. int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
  383. int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;
  384. // The dialog is outside the screen, move it inside
  385. if( xLeft < rcArea.left ) xLeft = rcArea.left;
  386. else if( xLeft + DlgWidth > rcArea.right ) xLeft = rcArea.right - DlgWidth;
  387. if( yTop < rcArea.top ) yTop = rcArea.top;
  388. else if( yTop + DlgHeight > rcArea.bottom ) yTop = rcArea.bottom - DlgHeight;
  389. ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  390. }
  391. void CWindowWnd::SetIcon(UINT nRes)
  392. {
  393. HICON hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  394. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  395. LR_DEFAULTCOLOR);
  396. ASSERT(hIcon);
  397. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) TRUE, (LPARAM) hIcon);
  398. hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  399. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  400. LR_DEFAULTCOLOR);
  401. ASSERT(hIcon);
  402. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) FALSE, (LPARAM) hIcon);
  403. }
  404. bool CWindowWnd::RegisterWindowClass()
  405. {
  406. WNDCLASS wc = { 0 };
  407. wc.style = GetClassStyle();
  408. wc.cbClsExtra = 0;
  409. wc.cbWndExtra = 0;
  410. wc.hIcon = NULL;
  411. wc.lpfnWndProc = CWindowWnd::__WndProc;
  412. wc.hInstance = CPaintManagerUI::GetInstance();
  413. wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  414. wc.hbrBackground = NULL;
  415. wc.lpszMenuName = NULL;
  416. wc.lpszClassName = GetWindowClassName();
  417. ATOM ret = ::RegisterClass(&wc);
  418. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  419. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  420. }
  421. bool CWindowWnd::RegisterSuperclass()
  422. {
  423. if(m_bUnicode) {
  424. #ifndef UNICODE
  425. LPWSTR lpSuperClassName = a2w((char*)GetSuperClassName());
  426. LPWSTR lpClassName = a2w((char*)GetWindowClassName());
  427. #else
  428. LPWSTR lpSuperClassName = (LPWSTR)GetSuperClassName();
  429. LPWSTR lpClassName = (LPWSTR)GetWindowClassName();
  430. #endif
  431. // Get the class information from an existing
  432. // window so we can subclass it later on...
  433. WNDCLASSEXW wc = { 0 };
  434. wc.cbSize = sizeof(WNDCLASSEXW);
  435. if( !::GetClassInfoExW(NULL, lpSuperClassName, &wc) ) {
  436. if( !::GetClassInfoExW(CPaintManagerUI::GetInstance(), lpSuperClassName, &wc) ) {
  437. ASSERT(!"Unable to locate window class");
  438. return NULL;
  439. }
  440. }
  441. m_OldWndProc = wc.lpfnWndProc;
  442. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  443. wc.hInstance = CPaintManagerUI::GetInstance();
  444. wc.lpszClassName = lpClassName;
  445. ATOM ret = ::RegisterClassExW(&wc);
  446. #ifndef UNICODE
  447. delete []lpClassName;
  448. delete []lpSuperClassName;
  449. #endif
  450. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  451. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  452. }
  453. else {
  454. // Get the class information from an existing
  455. // window so we can subclass it later on...
  456. WNDCLASSEX wc = { 0 };
  457. wc.cbSize = sizeof(WNDCLASSEX);
  458. if( !::GetClassInfoEx(NULL, GetSuperClassName(), &wc) ) {
  459. if( !::GetClassInfoEx(CPaintManagerUI::GetInstance(), GetSuperClassName(), &wc) ) {
  460. ASSERT(!"Unable to locate window class");
  461. return NULL;
  462. }
  463. }
  464. m_OldWndProc = wc.lpfnWndProc;
  465. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  466. wc.hInstance = CPaintManagerUI::GetInstance();
  467. wc.lpszClassName = GetWindowClassName();
  468. ATOM ret = ::RegisterClassEx(&wc);
  469. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  470. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  471. }
  472. }
  473. LRESULT CALLBACK CWindowWnd::__WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  474. {
  475. CWindowWnd* pThis = NULL;
  476. if( uMsg == WM_NCCREATE ) {
  477. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  478. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  479. pThis->m_hWnd = hWnd;
  480. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis));
  481. }
  482. else {
  483. pThis = reinterpret_cast<CWindowWnd*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
  484. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  485. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  486. ::SetWindowLongPtr(pThis->m_hWnd, GWLP_USERDATA, 0L);
  487. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  488. pThis->m_hWnd = NULL;
  489. pThis->OnFinalMessage(hWnd);
  490. return lRes;
  491. }
  492. }
  493. // 临时诊断(Route A):确认 WM_TOUCH 是否真被投递,并读出触点屏幕坐标,
  494. // 用于判断「手指点在弹框上」时 WM_TOUCH 是否落到了 owner(主窗口)。
  495. // ⚠️ 只 peek,绝不调用 CloseTouchInputHandle —— 句柄必须交给 DuiLib 的
  496. // OnTouchInput 去关闭,否则主窗口触摸会断。
  497. if( uMsg == WM_TOUCH ) {
  498. UINT cInputs = (UINT)(ULONG_PTR)wParam;
  499. HTOUCHINPUT hTouch = (HTOUCHINPUT)lParam;
  500. int tx = 0, ty = 0;
  501. TOUCHINPUT ti = { 0 };
  502. if( cInputs > 0 && ::GetTouchInputInfo(hTouch, 1, &ti, sizeof(TOUCHINPUT)) ) {
  503. tx = (int)(ti.x / 100); // 原始坐标为 1/100 像素,转近似屏幕像素
  504. ty = (int)(ti.y / 100);
  505. }
  506. DuiLib::TouchTrace::Log(_T("WNDPROC-TOUCH hwnd=%p pThis=%p owner=%p parent=%p tx=%d ty=%d inputs=%u extra=0x%08X style=0x%08X ex=0x%08X"),
  507. (void*)hWnd, (void*)pThis, (void*)::GetWindow(hWnd, GW_OWNER), (void*)::GetParent(hWnd),
  508. tx, ty, cInputs,
  509. (unsigned)(DWORD_PTR)::GetMessageExtraInfo(),
  510. (unsigned)::GetWindowLong(hWnd, GWL_STYLE), (unsigned)::GetWindowLong(hWnd, GWL_EXSTYLE));
  511. }
  512. if( pThis != NULL ) {
  513. return pThis->HandleMessage(uMsg, wParam, lParam);
  514. }
  515. else {
  516. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  517. }
  518. }
  519. LRESULT CALLBACK CWindowWnd::__ControlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  520. {
  521. CWindowWnd* pThis = NULL;
  522. if( uMsg == WM_NCCREATE ) {
  523. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  524. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  525. ::SetProp(hWnd, _T("WndX"), (HANDLE) pThis);
  526. pThis->m_hWnd = hWnd;
  527. }
  528. else {
  529. pThis = reinterpret_cast<CWindowWnd*>(::GetProp(hWnd, _T("WndX")));
  530. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  531. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  532. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  533. ::SetProp(hWnd, _T("WndX"), NULL);
  534. pThis->m_hWnd = NULL;
  535. pThis->OnFinalMessage(hWnd);
  536. return lRes;
  537. }
  538. }
  539. if( pThis != NULL ) {
  540. return pThis->HandleMessage(uMsg, wParam, lParam);
  541. }
  542. else {
  543. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  544. }
  545. }
  546. LRESULT CWindowWnd::SendMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  547. {
  548. ASSERT(::IsWindow(m_hWnd));
  549. return ::SendMessage(m_hWnd, uMsg, wParam, lParam);
  550. }
  551. LRESULT CWindowWnd::PostMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  552. {
  553. ASSERT(::IsWindow(m_hWnd));
  554. return ::PostMessage(m_hWnd, uMsg, wParam, lParam);
  555. }
  556. void CWindowWnd::ResizeClient(int cx /*= -1*/, int cy /*= -1*/)
  557. {
  558. ASSERT(::IsWindow(m_hWnd));
  559. RECT rc = { 0 };
  560. if( !::GetClientRect(m_hWnd, &rc) ) return;
  561. if( cx != -1 ) rc.right = cx;
  562. if( cy != -1 ) rc.bottom = cy;
  563. if( !::AdjustWindowRectEx(&rc, GetWindowStyle(m_hWnd), (!(GetWindowStyle(m_hWnd) & WS_CHILD) && (::GetMenu(m_hWnd) != NULL)), GetWindowExStyle(m_hWnd)) ) return;
  564. ::SetWindowPos(m_hWnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  565. }
  566. LRESULT CWindowWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  567. {
  568. return ::CallWindowProc(m_OldWndProc, m_hWnd, uMsg, wParam, lParam);
  569. }
  570. void CWindowWnd::OnFinalMessage(HWND /*hWnd*/)
  571. {
  572. }
  573. } // namespace DuiLib