Skonen
asked on
BN_CLICKED and the message queue
I'm trying to perfom an operation if a certain button is clicked within a Dialog created with the CDialog constructor and Create. After creating the dialog, I'm going into a loop which processes messages using PeekMessage. The problem is, the WM_COMMAND and BN_CLICKED messages never show themselves with PeekMessage. Can someone please tell me what I am doing incorrectly:
CDialog *dlgProductKey = new CDialog();
MSG msg;
dlgProductKey->Create(IDD_ ENTERKEY, this);
dlgProductKey->ShowWindow( SW_SHOW);
while (dlgProductKey->IsWindowVi sible()) {
if (PeekMessage(&msg, dlgProductKey->m_hWnd, 0, 0, PM_REMOVE)) {
if (msg.message == WM_COMMAND && HIWORD(msg.wParam) == BN_CLICKED){
MessageBox("Button was clicked");
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
When I use the Microsoft Message Spy, I can clearly see that the dlgProductKey dialog is receiving the WM_COMMAND|BN_CLICKED message. Do I have to subclass the dialog, or is there an easier way (like the method I am attempting). I was hoping not to create a new class for the IDD_ENTERKEY dialog resource.
CDialog *dlgProductKey = new CDialog();
MSG msg;
dlgProductKey->Create(IDD_
dlgProductKey->ShowWindow(
while (dlgProductKey->IsWindowVi
if (PeekMessage(&msg, dlgProductKey->m_hWnd, 0, 0, PM_REMOVE)) {
if (msg.message == WM_COMMAND && HIWORD(msg.wParam) == BN_CLICKED){
MessageBox("Button was clicked");
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
When I use the Microsoft Message Spy, I can clearly see that the dlgProductKey dialog is receiving the WM_COMMAND|BN_CLICKED message. Do I have to subclass the dialog, or is there an easier way (like the method I am attempting). I was hoping not to create a new class for the IDD_ENTERKEY dialog resource.
ASKER
That only works if the dialog resource has an associated class, I can do that quicker than you can blink. But now it isn't so much over how to do it the fastest, I am just wondering what I'd have to do to make the code above work properly.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
I think OnegaZhang is correct: you need to call IsDialogMessage.
But since this is an MFC app, you might get better results by calling CWinThread::PumpMessage:
while (dlgProductKey->IsWindowVi sible()) {
if (PeekMessage(&msg, dlgProductKey->m_hWnd, 0, 0, PM_NOREMOVE)) { // <<-- note change to PM_NOREMOVE
if (msg.message == WM_COMMAND && HIWORD(msg.wParam) == BN_CLICKED){
MessageBox("Button was clicked");
}
AfxGetApp()->PumpMessage() ;
}
}
But since this is an MFC app, you might get better results by calling CWinThread::PumpMessage:
while (dlgProductKey->IsWindowVi
if (PeekMessage(&msg, dlgProductKey->m_hWnd, 0, 0, PM_NOREMOVE)) { // <<-- note change to PM_NOREMOVE
if (msg.message == WM_COMMAND && HIWORD(msg.wParam) == BN_CLICKED){
MessageBox("Button was clicked");
}
AfxGetApp()->PumpMessage()
}
}
Go to your dialog resource, right click on the desired button and choose 'Events..' Then select BN_CLICKED and choose 'Add and edit'. You will be asked for the handler method name.