模拟模态对话框
参考CDialog的domodal方法
代码
#include <windows.h> #include <tchar.h> #include "resource.h" #define MsgBox(x) ::MessageBox(NULL, _T(x), _T(__FILE__), 0) #ifndef _countof #define _countof(array) (sizeof(array)/sizeof(array[0])) #endif LPCTSTR pszClassName = _T("My Window"); HINSTANCE g_hInst = NULL; LRESULT CALLBACK MyWindowProc(HWND, UINT, WPARAM, LPARAM); LRESULT ModalWnd(LPCTSTR pszWndClass, HWND hParent = NULL); BOOL RunMsgLoop(HWND hWnd = NULL); int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nShowCmd) { LPCTSTR pszMyWindowName = _T("My Test Window"); WNDCLASS wndClass; wndClass.cbClsExtra = 0; wndClass.cbWndExtra = 0; wndClass.hbrBackground = (HBRUSH)::GetStockObject(BLACK_BRUSH); wndClass.hCursor = ::LoadCursor(hInstance, IDC_HAND); wndClass.hIcon = ::LoadIcon(hInstance, MAKEINTRESOURCE(IDI_ICON1)); wndClass.hInstance = hInstance; wndClass.lpfnWndProc = MyWindowProc; wndClass.lpszClassName = pszClassName; wndClass.lpszMenuName = NULL; wndClass.style = CS_HREDRAW | CS_VREDRAW; if (!::RegisterClass(&wndClass)) { return -1; } HWND hWnd = ::CreateWindow(pszClassName, pszMyWindowName, WS_SYSMENU | WS_CAPTION | WS_THICKFRAME, 200, 200, 800, 600, NULL, NULL, hInstance, NULL); ::ShowWindow(hWnd, SW_SHOW); ::UpdateWindow(hWnd); ModalWnd(pszClassName, hWnd); RunMsgLoop(NULL); return 0; } LRESULT CALLBACK MyWindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam ) { switch (uMsg) { case WM_CLOSE: ::DestroyWindow(hwnd); break; case WM_LBUTTONDOWN: { TCHAR szTitle[128]; ::GetWindowText(hwnd, szTitle, _countof(szTitle)); wsprintf(szTitle + lstrlen(szTitle), _T(" mouse click at %d"), ::GetTickCount()); HDC hDc = ::GetDC(hwnd); ::TextOut(hDc, 0, 0, szTitle, lstrlen(szTitle)); ::ReleaseDC(hwnd, hDc); } break; case WM_RBUTTONDOWN: ModalWnd(pszClassName, hwnd); break; case WM_DESTROY: ::PostQuitMessage(0); break; default: return ::DefWindowProc(hwnd, uMsg, wParam, lParam); } return 0; } LRESULT ModalWnd(LPCTSTR pszWndClass, HWND hParent) { TCHAR szTitle[128]; wsprintf(szTitle, _T("My Test Window %X"), ::GetTickCount()); BOOL bEnbale = FALSE; if (hParent && ::IsWindowEnabled(hParent)) { ::EnableWindow(hParent, FALSE); bEnbale = TRUE; } RECT parentRect; parentRect.left = 250; parentRect.top = 250; parentRect.bottom = 500; parentRect.right = 700; if (hParent) { ::GetWindowRect(hParent, &parentRect); } parentRect.left += 20; parentRect.top += 20; parentRect.right -= 20; parentRect.bottom -= 20; HWND hWnd = ::CreateWindow(pszClassName, szTitle, WS_SYSMENU | WS_CAPTION | WS_THICKFRAME, parentRect.left, parentRect.top, parentRect.right - parentRect.left, parentRect.bottom - parentRect.top, hParent, NULL, g_hInst, NULL); ::ShowWindow(hWnd, SW_SHOW); ::UpdateWindow(hWnd); RunMsgLoop(NULL); if (bEnbale) { ::EnableWindow(hParent, TRUE); } if (hParent && ::GetActiveWindow() != hParent) { SetActiveWindow(hParent); } return 0; } BOOL RunMsgLoop(HWND hWnd) { MSG msg; BOOL bRet = FALSE; while (bRet = GetMessage(&msg, hWnd, 0, 0)) { if (bRet == -1) { break; } else { ::TranslateMessage(&msg); ::DispatchMessage(&msg); } } return TRUE; }