要调用的地方
SetTimer(g_hMainWnd,ID_GIVEUP,1000,NULL);//会给主窗口发送WM_TIMER消息;1000=1秒;ID_GIVEUP为标识 if (MessageBox(g_hMainWnd,L"确定要退出吗?",L"提示",MB_YESNO)==IDYES) { KillTimer(g_hMainWnd,ID_GIVEUP); //自己的代码 }
主窗口消息处理函数:
LRESULT CALLBACK GameMainWnd::GameMainWndProc(HWND hWnd, UINT nMsg, WPARAM wParam, LPARAM lParam) { HDC hdc; UINT nCtrlId; PAINTSTRUCT ps; LPCREATESTRUCT lpcs; GameMainWnd* lpWnd = (GameMainWnd*)GetWindowLongPtr(hWnd, GWLP_USERDATA); switch (nMsg) { case WM_CREATE: lpcs = (LPCREATESTRUCT)lParam; SetWindowLongPtr(hWnd, GWLP_USERDATA, (LONG_PTR)lpcs->lpCreateParams); break; case WM_DESTROY: SetWindowLongPtr(hWnd, GWLP_USERDATA, NULL); break; case WM_PAINT: hdc =BeginPaint(hWnd, &ps); lpWnd->OnPaint(hdc); EndPaint(hWnd, &ps); break; case WM_SIZE: lpWnd->OnSize(wParam, lParam); break; case WM_COMMAND: nCtrlId = LOWORD(wParam); lpWnd->OnCommand(nCtrlId); break; case WM_TIMER://倒计时,SetTimer()发送的消息 lpWnd->OnTimer(wParam, lParam); break; default: return DefWindowProc(hWnd, nMsg, wParam, lParam); } return 0; }OnTime()消息处理:
void OnTimer(WPARAM wParam, LPARAM lParam) { UNREFERENCED_PARAMETER(lParam); UINT nID = (UINT)wParam; switch (nID) { case ID_POKER_AGAIN: g_nPokerAgin++; if(g_nPokerAgin>=3) { HWND hwnd = FindWindow(NULL,L"错误提示"); EndDialog(hwnd, IDYES); g_nPokerAgin=0; } break; case ID_GIVEUP: g_nGiveUpTime++; if(g_nGiveUpTime>=3) { HWND hwnd = FindWindow(NULL,L"提示"); EndDialog(hwnd, IDNO); g_nGiveUpTime=0; } break; } }