UINT CWindowWnd::ShowModal() { ASSERT(::IsWindow(m_hWnd)); UINT nRet = 0; HWND hWndParent = GetWindowOwner(m_hWnd); ::ShowWindow(m_hWnd, SW_SHOWNORMAL); ::EnableWindow(hWndParent, FALSE); MSG msg = { 0 }; while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) { if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) { nRet = msg.wParam; ::EnableWindow(hWndParent, TRUE); ::SetFocus(hWndParent); } if( !CPaintManagerUI::TranslateMessage(&msg) ) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } if( msg.message == WM_QUIT ) break; } ::EnableWindow(hWndParent, TRUE); ::SetFocus(hWndParent); if( msg.message == WM_QUIT ) ::PostQuitMessage(msg.wParam); return nRet; }
//显示模态窗口 UINT CWindowWnd::ShowModal() { ASSERT(::IsWindow(m_hWnd)); UINT nRet = 0; HWND hWndParent = GetWindowOwner(m_hWnd); ::ShowWindow(m_hWnd, SW_SHOWNORMAL); //将窗口显示出来 ::EnableWindow(hWndParent, FALSE); //然后将父窗口设置不可用 MSG msg = { 0 }; //消息循环,处理显示出来的窗口所产生的消息 while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) { if( msg.message == WM_CLOSE && msg.hwnd == m_hWnd ) { nRet = msg.wParam; ::EnableWindow(hWndParent, TRUE); ::SetFocus(hWndParent); } if( !CPaintManagerUI::TranslateMessage(&msg) ) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } if( msg.message == WM_QUIT ) //一旦在队列中收到退出的消息,中断消息循环,处理退出 break; } ::EnableWindow(hWndParent, TRUE); //说明显示的窗口要退出,然后将父窗口设置为可用 ::SetFocus(hWndParent); //设置父窗口焦点 //退出 if( msg.message == WM_QUIT ) ::PostQuitMessage(msg.wParam); return nRet; }
std::vector<RunningApplication> RunningApplications() { std::unordered_map<std::string, std::vector<std::string>> processMap; using MapType = decltype(processMap); EnumWindows([](HWND hwnd, LPARAM lparam) { auto processMapPtr = (MapType *)lparam; auto owner = GetWindowOwner(hwnd); auto exstyle = GetWindowLong(hwnd, GWL_EXSTYLE); if (IsWindowVisible(hwnd) && !GetParent(hwnd) && (((exstyle & WS_EX_TOOLWINDOW) == 0 && !owner) || ((exstyle & WS_EX_APPWINDOW) && owner))) { DWORD pid; GetWindowThreadProcessId(hwnd, &pid); HANDLE hprocess = OpenProcess( PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, pid); WCHAR path[MAX_PATH], name[MAX_PATH]; if (!hprocess) { return TRUE; } if (GetModuleFileNameEx(hprocess, nullptr, path, MAX_PATH) && GetWindowText(hwnd, name, MAX_PATH)) { (*processMapPtr)[TCHARToUTF8(path)].emplace_back( TCHARToUTF8(name)); } CloseHandle(hprocess); } return TRUE; }, (LPARAM)&processMap); std::vector<RunningApplication> result; for (auto& kv : processMap) { RunningApplication app; auto productName = getProductName(kv.first); app.path = kv.first; app.name = productName.size() > 0 ? productName : kv.second[0]; result.push_back(app); } return result; }
void Main_OnTimer(HWND hwnd, UINT id) { POPTIONS pOpt = GetAppbarData(hwnd); switch (id) { // The AUTOHIDE timer has fired. Check to see if the mouse is over our // window and if not hide the appbar. case IDT_AUTOHIDE: if ((pOpt->fAutoHide) && (!pOpt->fHiding)) { // Get the mouse position, the window position, and active window POINT pt; GetCursorPos(&pt); RECT rc; GetWindowRect(hwnd, &rc); HWND hwndActive = GetForegroundWindow(); // If the mouse is outside of our window, or we are not active, // or at least one window is active, or we are not the parent // of an active window, the hide the appbar window. if ((!PtInRect(&rc, pt)) && (hwndActive != hwnd) && (hwndActive != NULL) && (GetWindowOwner(hwndActive) != hwnd)) { KillTimer(hwnd, id); AppBar_Hide(hwnd); } } break; // The period between the time the user has entered our window and the // time we should show the window has expired. case IDT_AUTOUNHIDE: { // Kill the timer, we only need it to fire once. KillTimer(hwnd, id); if ((pOpt->fAutoHide) && (pOpt->fHiding)) { // Check to see if the cursor is still in the appbar. If so, // the unhide the window. POINT pt; GetCursorPos(&pt); RECT rc; GetWindowRect(hwnd, &rc); if (PtInRect(&rc, pt)) { AppBar_UnHide(hwnd); } } break; } } }
UINT CWindowWnd::ShowModal() { ASSERT(::IsWindow(m_hWnd)); UINT nRet = 0; HWND hWndParent = GetWindowOwner(m_hWnd); ASSERT(hWndParent != m_hWnd); MSG msg = { 0 }; bool bNeedClose = false; while( ::IsWindow(m_hWnd) && ::GetMessage(&msg, NULL, 0, 0) ) { if( msg.message == WM_CLOSE ) { nRet = msg.wParam; if( msg.hwnd == hWndParent ) { ::EnableWindow(m_hWnd, TRUE); ::SetFocus(m_hWnd); } else if( msg.hwnd == m_hWnd ) { ShowWindow(false, false); bNeedClose = true; break; } } if( msg.hwnd != m_hWnd ) { if( msg.hwnd == hWndParent ) { if( (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST) ) continue; if( msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST ) continue; if( msg.message == WM_SETCURSOR ) continue; } else { if( !IsChild(m_hWnd, msg.hwnd) ) { if( (msg.message >= WM_MOUSEFIRST && msg.message <= WM_MOUSELAST) ) continue; if( msg.message >= WM_KEYFIRST && msg.message <= WM_KEYLAST ) continue; if( msg.message == WM_SETCURSOR ) continue; } } } if( !CPaintManagerUI::TranslateMessage(&msg) ) { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } if( msg.message == WM_QUIT ) break; } if( !bNeedClose ) ::EnableWindow(m_hWnd, TRUE); ::SetFocus(m_hWnd); if( msg.message == WM_QUIT ) ::PostQuitMessage(msg.wParam); else if( bNeedClose ) SendMessage(WM_CLOSE); return nRet; }