void CTestDlg::OnButton1()
{
// TODO: Add your control notification handler code here MSG msg;
while( GetMessage( &msg, this->m_hWnd, NULL, NULL ) )
{
if( msg.message == WM_QUIT )
MessageBox( "WM_QUIT" );
// break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
MessageBox( "Finished." );}void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
::SendMessage( GetDlgItem( IDC_BUTTON1 )->m_hWnd, WM_QUIT, 0, 0 );
}
运行时先点击Button1,然后点击Button2,为什么Button1的消息循环中没有收到WM_QUIT?谢谢!!
{
// TODO: Add your control notification handler code here MSG msg;
while( GetMessage( &msg, this->m_hWnd, NULL, NULL ) )
{
if( msg.message == WM_QUIT )
MessageBox( "WM_QUIT" );
// break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
MessageBox( "Finished." );}void CTestDlg::OnButton2()
{
// TODO: Add your control notification handler code here
::SendMessage( GetDlgItem( IDC_BUTTON1 )->m_hWnd, WM_QUIT, 0, 0 );
}
运行时先点击Button1,然后点击Button2,为什么Button1的消息循环中没有收到WM_QUIT?谢谢!!
解决方案 »
- mfc 窗体打印程序 不能打印线条 寻求解决方案 坐等
- 刚没注意发现类线程函数可以通过传递this指针访问类私有成员函数?
- listctrl(report 形式)的使用
- 求助,哪位大侠帮帮我!
- 怎么在单文档里加个关程序快捷键?
- MFC封装SDK,从那里开始执行?入口点的问题!
- 有关如何得到CPU频率的问题。
- 关于串口自发自收的问题
- 我写了COM服务器 在写接口时我要调用以前写的好的静态DLL 请问我怎么调用呀
- 怎么通过调试查看mfc的源代码? 我有一个简单的疑问 谢谢帮忙
- 如何通过socket来send unsigned char类型的数据
- 用盗版软件开发的软件产品,如何通过软件产品看出它是否用了盗版开发?
而且我觉得在GetMessage候消息还没有TranslateMessage和DispatchMessage,也就是还没有来得及处理,这时是应该会弹出MessageBox的。
回Mackz: 嗯,你说的不错.但是改成发送给窗口好像也不行啊。SendMessage( WM_QUIT, 0, 0 );
::SendMessage( AfxGetApp()->GetMainWnd()->m_hWnd, WM_QUIT, 0, 0 );
这两种应该是发给窗口的吧,但是还是不行。
先按button2,那按button1时WM_QUIT已经没了
在循环里内部,不可能会处理WM_QUIT消息
在OnButton2中,消息正确发送给了Button1,这时,Button1这个窗口的DefWindowProc一定会收到WM_QUIT这个消息的。然后把它处理掉。这个消息是定义的吧?也就是说,默认的话,这个“处理”是什么事都不做。然后WM_QUIT这个消息就从消息队列里消失了。这时再点Button1,就抓不到这个消息了。
如果从CButton这个类继承个子类,叫SuperButton(超级按钮),重写它的DefWindowProc,就可以收到这个消息了。
或者,你可以先启动一个循环,让它不停地“捞”Button1的消息(当然这样你的CPU会烧到100%),也是可以捞到它的。不过这个循环像上面那样写是不行的,
while( GetMessage( &msg, this->m_hWnd, NULL, NULL ) )GetMessage收到WM_QUIT消息时,返回0,退出while循环
“五岭散人”此话是否正确,我不确定(应该是正确的吧,我懒得查了^.^),不过反正这个循环是有问题的,要重新设计,建议去查下这个函数是怎么回事。
for(;;)
{
while(GetMessage(...)); //没有消息就一直转,注意分号 if( msg.message == WM_QUIT )
MessageBox( "WM_QUIT" );
TranslateMessage( &msg );
DispatchMessage( &msg );
break;
}
再去试试
用了leaf__leaf() 的继承Button然后在DefWindowProc中处理。void CTestDlg::OnButton1()
{
MSG msg;
while( GetMessage( &msg, this->m_hWnd, NULL, NULL ) )
{
if( CUpdate::bFlag )
break;
TranslateMessage( &msg );
DispatchMessage( &msg );
}
MessageBox( "Finished." );
}void CTestDlg::OnButton2()
{
::SendMessage( GetDlgItem( IDC_BUTTON1 )->m_hWnd, WM_QUIT, 0, 0 );
}LRESULT CUpdate::DefWindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
if( message == WM_QUIT )
bFlag = true; return CButton::DefWindowProc(message, wParam, lParam);
}
当你第一次点击BUTTON1时,产生一事件,而你在这个事件里等另一个事件发生,很显然这样是不可行的。
因为消息处理是排队的,只有前面一个消息处理完成了才有机会处理下一个消息。好好想想吧。