我已经通过msdn了解GetMessage
和DispatchMessage(&msg);
function。 我使用visual c ++(x64)创build了一个button应用程序。
问题是当我使用GetMessage
我的程序进入了无限循环。 突然我评论了GetMessage
部分和我的button工作正常(我只有两个button,只有两个不同的button单击事件显示不同的对话框)。
我有两个问题:
首先,任何想法,为什么去除GetMessage
解决了我的问题(你知道我用CreateDialogParam
和DialogProc
为我的button控件)。
其次,据我了解(请纠正我,如果我错了), GetMessage
接收消息(如果有这么多的消息一次),并在此之后DispatchMessage(&msg);
把这些消息一个接一个地发给他们,让他们完成工作。 如果第一个完成,那么第二个消息被分派等等,直到所有的消息都被处理完毕。
我的代码如下:
m_hwndPreview = CreateDialogParam( g_hInst,MAKEINTRESOURCE(IDD_MAINDIALOG), m_hwndParent,(DLGPROC)DialogProc, (LPARAM)this); if (m_hwndPreview == NULL) { hr = HRESULT_FROM_WIN32(GetLastError()); } BOOL bRet; MSG msg; while ( (bRet=GetMessage (& msg, 0, 0, 0)) != 0) { if (bRet == -1) { bRet = HRESULT_FROM_WIN32(GetLastError()); MessageBox(NULL, L"Hurr i am the error",L"Error", MB_ICONERROR | MB_OK); } else if (!IsDialogMessage (m_hwndPreview, & msg)) { TranslateMessage ( & msg ); DispatchMessage ( & msg ); MessageBox(NULL, L"there is no error in receivung the message", L"Error", MB_ICONERROR | MB_OK); } } int refMsg= (int)msg.wParam; refMsg;