标签: messagebox vc it
分类: VC/MFC
void CTestDlg::OnOK()
{
// TODO: 在此添加控件通知处理程序代码
SetTimer(NULL,2000,NULL); //设置一个定时器,2秒后触发
MessageBox(_T("aaaaa"),_T("B"),MB_YESNO); //跳出一个标题为B的窗口
}
void CTestDlg::OnTimer(UINT_PTR nIDEvent)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
HWND hWnd=::FindWindow(NULL,_T("B")); //查找标题为B的窗口,返回窗口句柄
if (hWnd) //如果窗口句柄存在
{
//::SendMessage(hWnd,WM_SYSCOMMAND,SC_CLOSE,NULL); //关闭这个窗口,如果Messagebox的对话框没有MB_YESNO或者MB_OKCANCEL这样类似的属性,这个也可以实现关闭
keybd_event(13,0,0,0); //模拟回车键按下
}
KillTimer(nIDEvent); //关闭定时器
CDialog::OnTimer(nIDEvent);
}
第二套方案
下面的程序从5种方法,关闭弹出的MessageBox对话框:
首先,创建定时器和弹出MessageBox:
- void CTestDlg::OnBnClickedButton1()
- {
-
- SetTimer(1, 1000, NULL);
-
-
- int ret = MessageBox("Hello", "MyMessageBox", MB_YESNO);
- if (ret == IDYES)
- {
- TRACE("按钮【YES】被按下\n");
- }
- else if(ret == IDNO)
- {
- TRACE("按钮【NO】被按下\n");
- }
- }
然后,在定时器回调中:
- void CTestDlg::OnTimer(UINT_PTR nIDEvent)
- {
-
- if (nIDEvent == 1)
- {
- HANDLE hWnd = ::FindWindowEx(NULL, NULL, NULL, "MyMessageBox");
-
- if (hWnd)
- {
- TRACE("发现了MyMessageBox窗口\n");
-
-
-
-
-
- ::SendMessage((HWND)hWnd, WM_CLOSE, NULL, NULL);
-
-
-
-
-
- ::EndDialog((HWND)hWnd, IDNO);
-
-
-
-
-
- HWND h = ::GetDlgItem((HWND)hWnd, IDNO);
- if(h)
- {
- TRACE("找到了IDNO控件窗口\n");
- ::PostMessage((HWND)hWnd, WM_COMMAND, MAKEWPARAM(IDNO, BN_CLICKED), (LPARAM)h);
- }
-
-
-
-
-
- HWND h = ::GetDlgItem((HWND)hWnd, IDNO);
- if(h)
- {
- TRACE("找到了IDNO控件窗口\n");
- POINT pt;
- CRect rc;
- ::GetWindowRect(h, &rc);
- pt.x = rc.left+10;
- pt.y = rc.top+10;
- SetCursorPos(pt.x, pt.y);
-
-
- mouse_event(MOUSEEVENTF_LEFTDOWN|MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0);
- mouse_event(MOUSEEVENTF_LEFTUP|MOUSEEVENTF_ABSOLUTE, 0, 0, 0, 0);
- }
-
-
-
-
- keybd_event(VK_RETURN, 0, 0,0);
- keybd_event(VK_RETURN, 0, KEYEVENTF_KEYUP,0);
-
- }
-
- }
-
- CDialog::OnTimer(nIDEvent);
- }