SendMessage的两个小问题
我用MFC的向导建立了一个基于对话框的程序,在对话框的OnInitDialog中AfxBeginThread(CommuProc, 0)开启一个线程,线程的代码如下:
- C/C++ code
UINT CommuProc(LPVOID pParam){ MSG msg; while (1) { if (::PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == CM_TEST1) { ::SendMessage(hDlgWnd, CM_T2D_1, 0, 0);//hDlgWnd是对话框的窗口句柄 } } } return 0;}问题一:CM_TEST1是点击对话框上的一个按钮向线程发送的消息,线程收到CM_TEST1消息后向对话框发送CM_T2D_1消息,对应的消息处理函数是OnTest1(), 在OnTest1()里打开一个子对话框,我想问的是,打开的子对话框是属于哪个线程的?父线程还是子线程?
问题二:PeekMessage的第二个参数我看了下MSDN,上面说可以为-1,我想问下是不是这样写(HWND)-1?
[解决办法]
>>我想问的就是,内部调用OnTest1()了,打开的这个对话框是属于哪个线程的?这个和模态、非模态还有关系吗?
对话框是属于主线程也就是OnTest1所在的线程,和模态和非模态没关系
消息循环式属于线程的,CommuProc里的循环不影响主线程 OnTest1()所在线程。
[解决办法]
[解决办法]
关于非模态对话框的消息循环?模态对话框的消息循环是否建立子线程?
模态对话框的消息循环和主线程的消息循环的关系
看看msdn的说明:
- C/C++ code
To process messages for the modal dialog box, the system starts its own message loop, taking temporary control of the message queue for the entire application. When the system retrieves a message that is not explicitly for the dialog box, it dispatches the message to the appropriate window. If it retrieves a WM_QUIT message, it posts the message back to the application message queue so that the application's main message loop can eventually retrieve the message.