UIBase.cpp 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  1. #include "StdAfx.h"
  2. #include "UIManager.h"
  3. #ifdef _DEBUG
  4. #include <shlwapi.h>
  5. #pragma comment(lib, "shlwapi.lib")
  6. #endif
  7. namespace DuiLib {
  8. // WM_TOUCH 消息 ID 在 winuser.h 里受 `#if _WIN32_WINNT >= 0x0601` 保护,
  9. // 而 DuiLib 工程的 _WIN32_WINNT 兜底为 XP(0x0501),直接引用会编译失败,这里自行声明。
  10. #ifndef WM_TOUCH
  11. #define WM_TOUCH 0x0240
  12. #endif
  13. // WM_POINTER* 系列(Win8+),同样不在 XP 段里。仅用于模态期间屏蔽 owner 的输入。
  14. #define DUI_WM_POINTER_FIRST 0x0246
  15. #define DUI_WM_POINTER_LAST 0x0259
  16. namespace {
  17. typedef BOOL (WINAPI* PFN_REGISTER_TOUCH_WINDOW)(HWND, ULONG);
  18. // 与 SDK tagTOUCHINPUT 布局一致的私有副本(同样受 _WIN32_WINNT 保护,不能直接用)
  19. struct TRACE_TOUCHINPUT
  20. {
  21. LONG x;
  22. LONG y;
  23. HANDLE hSource;
  24. DWORD dwID;
  25. DWORD dwFlags;
  26. DWORD dwMask;
  27. DWORD dwTime;
  28. ULONG_PTR dwExtraInfo;
  29. DWORD cxContact;
  30. DWORD cyContact;
  31. };
  32. typedef BOOL (WINAPI* PFN_GET_TOUCH_INPUT_INFO)(HANDLE, UINT, TRACE_TOUCHINPUT*, int);
  33. void RegisterTouchWindowIfAvailable(HWND hWnd)
  34. {
  35. if (hWnd == NULL) return;
  36. HMODULE hUser32 = ::GetModuleHandle(_T("user32.dll"));
  37. if (hUser32 == NULL) return;
  38. PFN_REGISTER_TOUCH_WINDOW pfnRegister =
  39. reinterpret_cast<PFN_REGISTER_TOUCH_WINDOW>(::GetProcAddress(hUser32, "RegisterTouchWindow"));
  40. if (pfnRegister != NULL)
  41. pfnRegister(hWnd, 0);
  42. }
  43. // 诊断:WM_TOUCH 究竟落在哪个窗口上(弹框自己?还是被禁用/被挡住的 owner?)。
  44. // ⚠️ 只 peek,绝不调用 CloseTouchInputHandle:句柄必须交给 CPaintManagerUI::OnTouchInput 关闭。
  45. void TraceTouchArrival(HWND hWnd, WPARAM wParam, LPARAM lParam)
  46. {
  47. HMODULE hUser32 = ::GetModuleHandle(_T("user32.dll"));
  48. if (hUser32 == NULL) return;
  49. PFN_GET_TOUCH_INPUT_INFO pfnGet =
  50. reinterpret_cast<PFN_GET_TOUCH_INPUT_INFO>(::GetProcAddress(hUser32, "GetTouchInputInfo"));
  51. int x = 0, y = 0;
  52. DWORD flags = 0;
  53. UINT cInputs = LOWORD(wParam);
  54. TRACE_TOUCHINPUT ti;
  55. ::ZeroMemory(&ti, sizeof(ti));
  56. if (pfnGet != NULL && cInputs > 0 && pfnGet((HANDLE)lParam, 1, &ti, (int)sizeof(ti)))
  57. {
  58. x = (int)(ti.x / 100);
  59. y = (int)(ti.y / 100);
  60. flags = ti.dwFlags;
  61. }
  62. DuiLib::TouchTrace::Log(_T("WNDPROC-TOUCH hwnd=%p owner=%p parent=%p enabled=%d visible=%d pt=%d,%d flags=0x%X inputs=%u modal=%p fg=%p"),
  63. (void*)hWnd, (void*)::GetWindow(hWnd, GW_OWNER), (void*)::GetParent(hWnd),
  64. (int)::IsWindowEnabled(hWnd), (int)::IsWindowVisible(hWnd),
  65. x, y, (unsigned)flags, cInputs,
  66. (void*)DuiLib::CPaintManagerUI::GetModalWindow(), (void*)::GetForegroundWindow());
  67. }
  68. // 上一次"抢回前台"的时间戳(限流用,避免频繁改激活状态打断输入)
  69. DWORD s_dwLastModalFocusFix = 0;
  70. // 模态期间需要屏蔽的输入类消息(发给被弹框挡住的 owner 时直接丢弃,不再派发)
  71. bool IsModalBlockedInputMessage(UINT uMsg)
  72. {
  73. if (uMsg == WM_TOUCH) return true; // 0x0240
  74. if (uMsg >= DUI_WM_POINTER_FIRST && uMsg <= DUI_WM_POINTER_LAST) return true;
  75. if (uMsg >= 0x0200 && uMsg <= 0x020E) return true; // WM_MOUSEMOVE .. WM_MOUSELAST
  76. if (uMsg >= 0x00A0 && uMsg <= 0x00AD) return true; // WM_NCMOUSEMOVE .. WM_NCXBUTTONDBLCLK
  77. if (uMsg >= WM_KEYFIRST && uMsg <= WM_KEYLAST) return true; // 键盘
  78. if (uMsg == WM_MOUSEACTIVATE) return true;
  79. return false;
  80. }
  81. }
  82. /////////////////////////////////////////////////////////////////////////////////////
  83. //
  84. //
  85. void UILIB_API DUI__Trace(LPCTSTR pstrFormat, ...)
  86. {
  87. #ifdef _DEBUG
  88. TCHAR szBuffer[2048] = {0};
  89. va_list args;
  90. va_start(args, pstrFormat);
  91. _vsntprintf(szBuffer, 2048, pstrFormat, args);
  92. va_end(args);
  93. CDuiString strMsg = szBuffer;
  94. strMsg += _T("\n");
  95. OutputDebugString(strMsg.GetData());
  96. #endif
  97. }
  98. LPCTSTR DUI__TraceMsg(UINT uMsg)
  99. {
  100. #define MSGDEF(x) if(uMsg==x) return _T(#x)
  101. MSGDEF(WM_SETCURSOR);
  102. MSGDEF(WM_NCHITTEST);
  103. MSGDEF(WM_NCPAINT);
  104. MSGDEF(WM_PAINT);
  105. MSGDEF(WM_ERASEBKGND);
  106. MSGDEF(WM_NCMOUSEMOVE);
  107. MSGDEF(WM_MOUSEMOVE);
  108. MSGDEF(WM_MOUSELEAVE);
  109. MSGDEF(WM_MOUSEHOVER);
  110. MSGDEF(WM_NOTIFY);
  111. MSGDEF(WM_COMMAND);
  112. MSGDEF(WM_MEASUREITEM);
  113. MSGDEF(WM_DRAWITEM);
  114. MSGDEF(WM_LBUTTONDOWN);
  115. MSGDEF(WM_LBUTTONUP);
  116. MSGDEF(WM_LBUTTONDBLCLK);
  117. MSGDEF(WM_RBUTTONDOWN);
  118. MSGDEF(WM_RBUTTONUP);
  119. MSGDEF(WM_RBUTTONDBLCLK);
  120. MSGDEF(WM_SETFOCUS);
  121. MSGDEF(WM_KILLFOCUS);
  122. MSGDEF(WM_MOVE);
  123. MSGDEF(WM_SIZE);
  124. MSGDEF(WM_SIZING);
  125. MSGDEF(WM_MOVING);
  126. MSGDEF(WM_GETMINMAXINFO);
  127. MSGDEF(WM_CAPTURECHANGED);
  128. MSGDEF(WM_WINDOWPOSCHANGED);
  129. MSGDEF(WM_WINDOWPOSCHANGING);
  130. MSGDEF(WM_NCCALCSIZE);
  131. MSGDEF(WM_NCCREATE);
  132. MSGDEF(WM_NCDESTROY);
  133. MSGDEF(WM_TIMER);
  134. MSGDEF(WM_KEYDOWN);
  135. MSGDEF(WM_KEYUP);
  136. MSGDEF(WM_CHAR);
  137. MSGDEF(WM_SYSKEYDOWN);
  138. MSGDEF(WM_SYSKEYUP);
  139. MSGDEF(WM_SYSCOMMAND);
  140. MSGDEF(WM_SYSCHAR);
  141. MSGDEF(WM_VSCROLL);
  142. MSGDEF(WM_HSCROLL);
  143. MSGDEF(WM_CHAR);
  144. MSGDEF(WM_SHOWWINDOW);
  145. MSGDEF(WM_PARENTNOTIFY);
  146. MSGDEF(WM_CREATE);
  147. MSGDEF(WM_NCACTIVATE);
  148. MSGDEF(WM_ACTIVATE);
  149. MSGDEF(WM_ACTIVATEAPP);
  150. MSGDEF(WM_CLOSE);
  151. MSGDEF(WM_DESTROY);
  152. MSGDEF(WM_GETICON);
  153. MSGDEF(WM_GETTEXT);
  154. MSGDEF(WM_GETTEXTLENGTH);
  155. static TCHAR szMsg[10];
  156. ::wsprintf(szMsg, _T("0x%04X"), uMsg);
  157. return szMsg;
  158. }
  159. /////////////////////////////////////////////////////////////////////////////////////
  160. //
  161. //
  162. //////////////////////////////////////////////////////////////////////////
  163. //
  164. DUI_BASE_BEGIN_MESSAGE_MAP(CNotifyPump)
  165. DUI_END_MESSAGE_MAP()
  166. static const DUI_MSGMAP_ENTRY* DuiFindMessageEntry(const DUI_MSGMAP_ENTRY* lpEntry,TNotifyUI& msg )
  167. {
  168. CDuiString sMsgType = msg.sType;
  169. CDuiString sCtrlName = msg.pSender->GetName();
  170. const DUI_MSGMAP_ENTRY* pMsgTypeEntry = NULL;
  171. while (lpEntry->nSig != DuiSig_end)
  172. {
  173. if(lpEntry->sMsgType==sMsgType)
  174. {
  175. if(!lpEntry->sCtrlName.IsEmpty())
  176. {
  177. if(lpEntry->sCtrlName==sCtrlName)
  178. {
  179. return lpEntry;
  180. }
  181. }
  182. else
  183. {
  184. pMsgTypeEntry = lpEntry;
  185. }
  186. }
  187. lpEntry++;
  188. }
  189. return pMsgTypeEntry;
  190. }
  191. bool CNotifyPump::AddVirtualWnd(CDuiString strName,CNotifyPump* pObject)
  192. {
  193. if( m_VirtualWndMap.Find(strName) == NULL )
  194. {
  195. m_VirtualWndMap.Insert(strName.GetData(),(LPVOID)pObject);
  196. return true;
  197. }
  198. return false;
  199. }
  200. bool CNotifyPump::RemoveVirtualWnd(CDuiString strName)
  201. {
  202. if( m_VirtualWndMap.Find(strName) != NULL )
  203. {
  204. m_VirtualWndMap.Remove(strName);
  205. return true;
  206. }
  207. return false;
  208. }
  209. bool CNotifyPump::LoopDispatch(TNotifyUI& msg)
  210. {
  211. const DUI_MSGMAP_ENTRY* lpEntry = NULL;
  212. const DUI_MSGMAP* pMessageMap = NULL;
  213. #ifndef UILIB_STATIC
  214. for(pMessageMap = GetMessageMap(); pMessageMap!=NULL; pMessageMap = (*pMessageMap->pfnGetBaseMap)())
  215. #else
  216. for(pMessageMap = GetMessageMap(); pMessageMap!=NULL; pMessageMap = pMessageMap->pBaseMap)
  217. #endif
  218. {
  219. #ifndef UILIB_STATIC
  220. ASSERT(pMessageMap != (*pMessageMap->pfnGetBaseMap)());
  221. #else
  222. ASSERT(pMessageMap != pMessageMap->pBaseMap);
  223. #endif
  224. if ((lpEntry = DuiFindMessageEntry(pMessageMap->lpEntries,msg)) != NULL)
  225. {
  226. goto LDispatch;
  227. }
  228. }
  229. return false;
  230. LDispatch:
  231. union DuiMessageMapFunctions mmf;
  232. mmf.pfn = lpEntry->pfn;
  233. bool bRet = false;
  234. int nSig;
  235. nSig = lpEntry->nSig;
  236. switch (nSig)
  237. {
  238. default:
  239. ASSERT(FALSE);
  240. break;
  241. case DuiSig_lwl:
  242. (this->*mmf.pfn_Notify_lwl)(msg.wParam,msg.lParam);
  243. bRet = true;
  244. break;
  245. case DuiSig_vn:
  246. (this->*mmf.pfn_Notify_vn)(msg);
  247. bRet = true;
  248. break;
  249. }
  250. return bRet;
  251. }
  252. void CNotifyPump::NotifyPump(TNotifyUI& msg)
  253. {
  254. ///遍历虚拟窗口
  255. if( !msg.sVirtualWnd.IsEmpty() ){
  256. for( int i = 0; i< m_VirtualWndMap.GetSize(); i++ ) {
  257. if( LPCTSTR key = m_VirtualWndMap.GetAt(i) ) {
  258. if( _tcsicmp(key, msg.sVirtualWnd.GetData()) == 0 ){
  259. CNotifyPump* pObject = static_cast<CNotifyPump*>(m_VirtualWndMap.Find(key, false));
  260. if( pObject && pObject->LoopDispatch(msg) )
  261. return;
  262. }
  263. }
  264. }
  265. }
  266. ///
  267. //遍历主窗口
  268. LoopDispatch( msg );
  269. }
  270. //////////////////////////////////////////////////////////////////////////
  271. ///
  272. CWindowWnd::CWindowWnd() : m_hWnd(NULL), m_OldWndProc(::DefWindowProc), m_bSubclassed(false), m_bUnicode(false), m_bFakeModal(false)
  273. {
  274. }
  275. HWND CWindowWnd::GetHWND() const
  276. {
  277. return m_hWnd;
  278. }
  279. UINT CWindowWnd::GetClassStyle() const
  280. {
  281. return 0;
  282. }
  283. LPCTSTR CWindowWnd::GetSuperClassName() const
  284. {
  285. return NULL;
  286. }
  287. CWindowWnd::operator HWND() const
  288. {
  289. return m_hWnd;
  290. }
  291. void CWindowWnd::EnableUnicode()
  292. {
  293. m_bUnicode = true;
  294. }
  295. HWND CWindowWnd::CreateDuiWindow( HWND hwndParent, LPCTSTR pstrWindowName,DWORD dwStyle /*=0*/, DWORD dwExStyle /*=0*/ )
  296. {
  297. return Create(hwndParent,pstrWindowName,dwStyle,dwExStyle,0,0,0,0,NULL);
  298. }
  299. HWND CWindowWnd::Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, const RECT rc, HMENU hMenu)
  300. {
  301. return Create(hwndParent, pstrName, dwStyle, dwExStyle, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top, hMenu);
  302. }
  303. HWND CWindowWnd::Create(HWND hwndParent, LPCTSTR pstrName, DWORD dwStyle, DWORD dwExStyle, int x, int y, int cx, int cy, HMENU hMenu)
  304. {
  305. if( GetSuperClassName() != NULL && !RegisterSuperclass() ) return NULL;
  306. if( GetSuperClassName() == NULL && !RegisterWindowClass() ) return NULL;
  307. if(m_bUnicode) {
  308. #ifndef UNICODE
  309. LPWSTR lpClassName = a2w((char*)GetWindowClassName());
  310. LPWSTR lpName = a2w((char*)pstrName);
  311. #else
  312. LPWSTR lpClassName = (LPWSTR)GetWindowClassName();
  313. LPWSTR lpName = (LPWSTR)pstrName;
  314. #endif
  315. m_hWnd = ::CreateWindowExW(dwExStyle, lpClassName, lpName, dwStyle, x, y, cx, cy, hwndParent, hMenu, CPaintManagerUI::GetInstance(), this);
  316. #ifndef UNICODE
  317. delete []lpClassName;
  318. delete []lpName;
  319. #endif
  320. }
  321. else {
  322. m_hWnd = ::CreateWindowEx(dwExStyle, GetWindowClassName(), pstrName, dwStyle, x, y, cx, cy, hwndParent, hMenu, CPaintManagerUI::GetInstance(), this);
  323. }
  324. ASSERT(m_hWnd!=NULL);
  325. RegisterTouchWindowIfAvailable(m_hWnd);
  326. return m_hWnd;
  327. }
  328. HWND CWindowWnd::Subclass(HWND hWnd)
  329. {
  330. ASSERT(::IsWindow(hWnd));
  331. ASSERT(m_hWnd==NULL);
  332. m_OldWndProc = SubclassWindow(hWnd, __WndProc);
  333. if( m_OldWndProc == NULL ) return NULL;
  334. m_bSubclassed = true;
  335. m_hWnd = hWnd;
  336. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(this));
  337. RegisterTouchWindowIfAvailable(m_hWnd);
  338. return m_hWnd;
  339. }
  340. void CWindowWnd::Unsubclass()
  341. {
  342. ASSERT(::IsWindow(m_hWnd));
  343. if( !::IsWindow(m_hWnd) ) return;
  344. if( !m_bSubclassed ) return;
  345. SubclassWindow(m_hWnd, m_OldWndProc);
  346. m_OldWndProc = ::DefWindowProc;
  347. m_bSubclassed = false;
  348. }
  349. void CWindowWnd::ShowWindow(bool bShow /*= true*/, bool bTakeFocus /*= false*/)
  350. {
  351. ASSERT(::IsWindow(m_hWnd));
  352. if( !::IsWindow(m_hWnd) ) return;
  353. ::ShowWindow(m_hWnd, bShow ? (bTakeFocus ? SW_SHOWNORMAL : SW_SHOWNOACTIVATE) : SW_HIDE);
  354. }
  355. UINT CWindowWnd::ShowModal()
  356. {
  357. ASSERT(::IsWindow(m_hWnd));
  358. UINT nRet = 0;
  359. HWND hWndParent = GetWindowOwner(m_hWnd);
  360. const bool bBlockParent = (hWndParent != NULL && ::IsWindow(hWndParent));
  361. RegisterTouchWindowIfAvailable(m_hWnd);
  362. // -------------------------------------------------------------------------
  363. // 【重要】这里不再调用 ::EnableWindow(hWndParent, FALSE)。
  364. //
  365. // 原因:Windows 8/10 把触摸输入改成了 Pointer 栈,触摸消息(WM_TOUCH / WM_POINTER*)
  366. // 的投递目标与"窗口是否被禁用 / 是否是活动窗口"强相关。实测在 Win10 上,只要 owner
  367. // 被 EnableWindow(FALSE) 禁用,手势就会被系统转投给被禁用的 owner 而不再到达弹框,
  368. // 于是弹框既收不到 WM_TOUCH(滚动条无法触摸滑动)、也没有可靠的提升鼠标消息可用
  369. // (点击无反应);而在 Win7 上鼠标/触摸提升路径不受此影响,所以旧写法一直正常。
  370. //
  371. // 模态性改用两条等价手段保证:
  372. // ① 弹框置顶 + 激活,抢到前台;
  373. // ② 消息循环里丢弃所有发给 owner(及其子窗口)的输入类消息(见 IsModalBlockedInputMessage)。
  374. // —— owner 保持 enabled,它的鼠标/键盘/触摸都不会被派发,等价于"点不动";
  375. // ③ CPaintManagerUI 层再加一道:有模态弹框时,非弹框窗口收到 WM_TOUCH 直接忽略。
  376. // -------------------------------------------------------------------------
  377. ::ShowWindow(m_hWnd, SW_SHOWNORMAL);
  378. ::SetWindowPos(m_hWnd, HWND_TOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_SHOWWINDOW);
  379. ::BringWindowToTop(m_hWnd);
  380. ::SetForegroundWindow(m_hWnd);
  381. ::SetActiveWindow(m_hWnd);
  382. ::SetFocus(m_hWnd);
  383. // 显示之后再注册一次(幂等,重复调用无害):确保最终窗口样式下的触摸注册生效。
  384. RegisterTouchWindowIfAvailable(m_hWnd);
  385. // 支持弹框嵌套:保存外层模态窗口,退出时恢复
  386. HWND hPrevModal = CPaintManagerUI::GetModalWindow();
  387. CPaintManagerUI::SetModalWindow(m_hWnd);
  388. DuiLib::TouchTrace::Log(_T("MODAL-ENTER hwnd=%p owner=%p foreground=%p active=%p focus=%p enabled=%d style=0x%08X ex=0x%08X"),
  389. (void*)m_hWnd, (void*)hWndParent, (void*)::GetForegroundWindow(), (void*)::GetActiveWindow(),
  390. (void*)::GetFocus(), (int)::IsWindowEnabled(m_hWnd),
  391. (unsigned)::GetWindowLong(m_hWnd, GWL_STYLE), (unsigned)::GetWindowLong(m_hWnd, GWL_EXSTYLE));
  392. MSG msg = { 0 };
  393. while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) {
  394. if( msg.message == WM_QUIT ) break;
  395. // 模态期间:owner 及其子窗口的任何输入消息都不派发(等价于原来的禁用效果)
  396. if( bBlockParent && msg.hwnd != NULL
  397. && (msg.hwnd == hWndParent || ::IsChild(hWndParent, msg.hwnd))
  398. && IsModalBlockedInputMessage(msg.message) )
  399. {
  400. continue;
  401. }
  402. if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) {
  403. nRet = (int)msg.wParam;
  404. ::SetFocus(hWndParent);
  405. }
  406. // Win10 前台锁比 Win7 严格:弹框偶尔会丢掉前台/激活状态,之后触摸就会被路由到别处。
  407. // 这里做限流补偿(≥800ms 一次,且【绝不在手势消息期间】执行,避免打断输入路由)。
  408. if( !IsModalBlockedInputMessage(msg.message) )
  409. {
  410. const DWORD dwNow = ::GetTickCount();
  411. if( (LONG)(dwNow - s_dwLastModalFocusFix) >= 800
  412. && (::GetActiveWindow() != m_hWnd || ::GetForegroundWindow() != m_hWnd) )
  413. {
  414. s_dwLastModalFocusFix = dwNow;
  415. DuiLib::TouchTrace::Log(_T("MODAL-REACTIVATE hwnd=%p active=%p foreground=%p"),
  416. (void*)m_hWnd, (void*)::GetActiveWindow(), (void*)::GetForegroundWindow());
  417. ::BringWindowToTop(m_hWnd);
  418. ::SetForegroundWindow(m_hWnd);
  419. ::SetActiveWindow(m_hWnd);
  420. ::SetFocus(m_hWnd);
  421. }
  422. }
  423. if( !CPaintManagerUI::TranslateMessage(&msg) ) {
  424. ::TranslateMessage(&msg);
  425. ::DispatchMessage(&msg);
  426. }
  427. }
  428. CPaintManagerUI::SetModalWindow(hPrevModal);
  429. if( ::IsWindow(m_hWnd) )
  430. ::SetWindowPos(m_hWnd, HWND_NOTOPMOST, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOACTIVATE);
  431. if( hWndParent != NULL ) {
  432. if( ::IsWindow(hWndParent) ) {
  433. ::EnableWindow(hWndParent, TRUE);
  434. ::SetForegroundWindow(hWndParent);
  435. ::SetFocus(hWndParent);
  436. }
  437. }
  438. DuiLib::TouchTrace::Log(_T("MODAL-EXIT hwnd=%p owner=%p ret=%u"), (void*)m_hWnd, (void*)hWndParent, nRet);
  439. if( msg.message == WM_QUIT ) ::PostQuitMessage((int)msg.wParam);
  440. return nRet;
  441. }
  442. void CWindowWnd::ShowModalFake()
  443. {
  444. ASSERT(::IsWindow(m_hWnd));
  445. auto p_hwnd = GetWindowOwner(m_hWnd);
  446. ASSERT(::IsWindow(p_hwnd));
  447. ASSERT(p_hwnd);
  448. ::EnableWindow(p_hwnd, FALSE);
  449. ShowWindow();
  450. m_bFakeModal = true;
  451. }
  452. void CWindowWnd::Close(UINT nRet)
  453. {
  454. if (m_bFakeModal)
  455. {
  456. auto parent_hwnd = GetWindowOwner(m_hWnd);
  457. ASSERT(::IsWindow(parent_hwnd));
  458. ::EnableWindow(parent_hwnd, TRUE);
  459. ::SetFocus(parent_hwnd);
  460. m_bFakeModal = false;
  461. }
  462. ASSERT(::IsWindow(m_hWnd));
  463. if (!::IsWindow(m_hWnd)) return;
  464. PostMessage(WM_CLOSE, (WPARAM)nRet, 0L);
  465. }
  466. void CWindowWnd::CenterWindow()
  467. {
  468. ASSERT(::IsWindow(m_hWnd));
  469. ASSERT((GetWindowStyle(m_hWnd)&WS_CHILD)==0);
  470. RECT rcDlg = { 0 };
  471. ::GetWindowRect(m_hWnd, &rcDlg);
  472. RECT rcArea = { 0 };
  473. RECT rcCenter = { 0 };
  474. HWND hWnd=*this;
  475. HWND hWndParent = ::GetParent(m_hWnd);
  476. HWND hWndCenter = ::GetWindowOwner(m_hWnd);
  477. if (hWndCenter!=NULL)
  478. hWnd=hWndCenter;
  479. // 处理多显示器模式下屏幕居中
  480. MONITORINFO oMonitor = {};
  481. oMonitor.cbSize = sizeof(oMonitor);
  482. ::GetMonitorInfo(::MonitorFromWindow(hWnd, MONITOR_DEFAULTTONEAREST), &oMonitor);
  483. rcArea = oMonitor.rcWork;
  484. if( hWndCenter == NULL )
  485. rcCenter = rcArea;
  486. else
  487. ::GetWindowRect(hWndCenter, &rcCenter);
  488. int DlgWidth = rcDlg.right - rcDlg.left;
  489. int DlgHeight = rcDlg.bottom - rcDlg.top;
  490. // Find dialog's upper left based on rcCenter
  491. int xLeft = (rcCenter.left + rcCenter.right) / 2 - DlgWidth / 2;
  492. int yTop = (rcCenter.top + rcCenter.bottom) / 2 - DlgHeight / 2;
  493. // The dialog is outside the screen, move it inside
  494. if( xLeft < rcArea.left ) xLeft = rcArea.left;
  495. else if( xLeft + DlgWidth > rcArea.right ) xLeft = rcArea.right - DlgWidth;
  496. if( yTop < rcArea.top ) yTop = rcArea.top;
  497. else if( yTop + DlgHeight > rcArea.bottom ) yTop = rcArea.bottom - DlgHeight;
  498. ::SetWindowPos(m_hWnd, NULL, xLeft, yTop, -1, -1, SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE);
  499. }
  500. void CWindowWnd::SetIcon(UINT nRes)
  501. {
  502. HICON hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  503. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  504. LR_DEFAULTCOLOR);
  505. ASSERT(hIcon);
  506. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) TRUE, (LPARAM) hIcon);
  507. hIcon = (HICON)::LoadImage(CPaintManagerUI::GetInstance(), MAKEINTRESOURCE(nRes), IMAGE_ICON,
  508. (::GetSystemMetrics(SM_CXICON) + 15) & ~15, (::GetSystemMetrics(SM_CYICON) + 15) & ~15, // 防止高DPI下图标模糊
  509. LR_DEFAULTCOLOR);
  510. ASSERT(hIcon);
  511. ::SendMessage(m_hWnd, WM_SETICON, (WPARAM) FALSE, (LPARAM) hIcon);
  512. }
  513. bool CWindowWnd::RegisterWindowClass()
  514. {
  515. WNDCLASS wc = { 0 };
  516. wc.style = GetClassStyle();
  517. wc.cbClsExtra = 0;
  518. wc.cbWndExtra = 0;
  519. wc.hIcon = NULL;
  520. wc.lpfnWndProc = CWindowWnd::__WndProc;
  521. wc.hInstance = CPaintManagerUI::GetInstance();
  522. wc.hCursor = ::LoadCursor(NULL, IDC_ARROW);
  523. wc.hbrBackground = NULL;
  524. wc.lpszMenuName = NULL;
  525. wc.lpszClassName = GetWindowClassName();
  526. ATOM ret = ::RegisterClass(&wc);
  527. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  528. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  529. }
  530. bool CWindowWnd::RegisterSuperclass()
  531. {
  532. if(m_bUnicode) {
  533. #ifndef UNICODE
  534. LPWSTR lpSuperClassName = a2w((char*)GetSuperClassName());
  535. LPWSTR lpClassName = a2w((char*)GetWindowClassName());
  536. #else
  537. LPWSTR lpSuperClassName = (LPWSTR)GetSuperClassName();
  538. LPWSTR lpClassName = (LPWSTR)GetWindowClassName();
  539. #endif
  540. // Get the class information from an existing
  541. // window so we can subclass it later on...
  542. WNDCLASSEXW wc = { 0 };
  543. wc.cbSize = sizeof(WNDCLASSEXW);
  544. if( !::GetClassInfoExW(NULL, lpSuperClassName, &wc) ) {
  545. if( !::GetClassInfoExW(CPaintManagerUI::GetInstance(), lpSuperClassName, &wc) ) {
  546. ASSERT(!"Unable to locate window class");
  547. return NULL;
  548. }
  549. }
  550. m_OldWndProc = wc.lpfnWndProc;
  551. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  552. wc.hInstance = CPaintManagerUI::GetInstance();
  553. wc.lpszClassName = lpClassName;
  554. ATOM ret = ::RegisterClassExW(&wc);
  555. #ifndef UNICODE
  556. delete []lpClassName;
  557. delete []lpSuperClassName;
  558. #endif
  559. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  560. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  561. }
  562. else {
  563. // Get the class information from an existing
  564. // window so we can subclass it later on...
  565. WNDCLASSEX wc = { 0 };
  566. wc.cbSize = sizeof(WNDCLASSEX);
  567. if( !::GetClassInfoEx(NULL, GetSuperClassName(), &wc) ) {
  568. if( !::GetClassInfoEx(CPaintManagerUI::GetInstance(), GetSuperClassName(), &wc) ) {
  569. ASSERT(!"Unable to locate window class");
  570. return NULL;
  571. }
  572. }
  573. m_OldWndProc = wc.lpfnWndProc;
  574. wc.lpfnWndProc = CWindowWnd::__ControlProc;
  575. wc.hInstance = CPaintManagerUI::GetInstance();
  576. wc.lpszClassName = GetWindowClassName();
  577. ATOM ret = ::RegisterClassEx(&wc);
  578. ASSERT(ret!=NULL || ::GetLastError()==ERROR_CLASS_ALREADY_EXISTS);
  579. return ret != NULL || ::GetLastError() == ERROR_CLASS_ALREADY_EXISTS;
  580. }
  581. }
  582. LRESULT CALLBACK CWindowWnd::__WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  583. {
  584. CWindowWnd* pThis = NULL;
  585. if( uMsg == WM_NCCREATE ) {
  586. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  587. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  588. pThis->m_hWnd = hWnd;
  589. ::SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LPARAM>(pThis));
  590. }
  591. else {
  592. pThis = reinterpret_cast<CWindowWnd*>(::GetWindowLongPtr(hWnd, GWLP_USERDATA));
  593. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  594. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  595. ::SetWindowLongPtr(pThis->m_hWnd, GWLP_USERDATA, 0L);
  596. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  597. pThis->m_hWnd = NULL;
  598. pThis->OnFinalMessage(hWnd);
  599. return lRes;
  600. }
  601. }
  602. // 诊断:确认 WM_TOUCH 到底投递给了哪个窗口(弹框 / owner / 其它)。
  603. if( uMsg == WM_TOUCH ) TraceTouchArrival(hWnd, wParam, lParam);
  604. if( pThis != NULL ) {
  605. return pThis->HandleMessage(uMsg, wParam, lParam);
  606. }
  607. else {
  608. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  609. }
  610. }
  611. LRESULT CALLBACK CWindowWnd::__ControlProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  612. {
  613. CWindowWnd* pThis = NULL;
  614. if( uMsg == WM_NCCREATE ) {
  615. LPCREATESTRUCT lpcs = reinterpret_cast<LPCREATESTRUCT>(lParam);
  616. pThis = static_cast<CWindowWnd*>(lpcs->lpCreateParams);
  617. ::SetProp(hWnd, _T("WndX"), (HANDLE) pThis);
  618. pThis->m_hWnd = hWnd;
  619. }
  620. else {
  621. pThis = reinterpret_cast<CWindowWnd*>(::GetProp(hWnd, _T("WndX")));
  622. if( uMsg == WM_NCDESTROY && pThis != NULL ) {
  623. LRESULT lRes = ::CallWindowProc(pThis->m_OldWndProc, hWnd, uMsg, wParam, lParam);
  624. if( pThis->m_bSubclassed ) pThis->Unsubclass();
  625. ::SetProp(hWnd, _T("WndX"), NULL);
  626. pThis->m_hWnd = NULL;
  627. pThis->OnFinalMessage(hWnd);
  628. return lRes;
  629. }
  630. }
  631. if( pThis != NULL ) {
  632. return pThis->HandleMessage(uMsg, wParam, lParam);
  633. }
  634. else {
  635. return ::DefWindowProc(hWnd, uMsg, wParam, lParam);
  636. }
  637. }
  638. LRESULT CWindowWnd::SendMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  639. {
  640. ASSERT(::IsWindow(m_hWnd));
  641. return ::SendMessage(m_hWnd, uMsg, wParam, lParam);
  642. }
  643. LRESULT CWindowWnd::PostMessage(UINT uMsg, WPARAM wParam /*= 0*/, LPARAM lParam /*= 0*/)
  644. {
  645. ASSERT(::IsWindow(m_hWnd));
  646. return ::PostMessage(m_hWnd, uMsg, wParam, lParam);
  647. }
  648. void CWindowWnd::ResizeClient(int cx /*= -1*/, int cy /*= -1*/)
  649. {
  650. ASSERT(::IsWindow(m_hWnd));
  651. RECT rc = { 0 };
  652. if( !::GetClientRect(m_hWnd, &rc) ) return;
  653. if( cx != -1 ) rc.right = cx;
  654. if( cy != -1 ) rc.bottom = cy;
  655. if( !::AdjustWindowRectEx(&rc, GetWindowStyle(m_hWnd), (!(GetWindowStyle(m_hWnd) & WS_CHILD) && (::GetMenu(m_hWnd) != NULL)), GetWindowExStyle(m_hWnd)) ) return;
  656. ::SetWindowPos(m_hWnd, NULL, 0, 0, rc.right - rc.left, rc.bottom - rc.top, SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE);
  657. }
  658. LRESULT CWindowWnd::HandleMessage(UINT uMsg, WPARAM wParam, LPARAM lParam)
  659. {
  660. return ::CallWindowProc(m_OldWndProc, m_hWnd, uMsg, wParam, lParam);
  661. }
  662. void CWindowWnd::OnFinalMessage(HWND /*hWnd*/)
  663. {
  664. }
  665. } // namespace DuiLib