UIBase.cpp 21 KB

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