Solved

BN_CLICKED and the message queue

Posted on 2004-10-16
6
856 Views
Last Modified: 2013-11-20
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->IsWindowVisible()) {

    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.
0
Comment
Question by:Skonen
6 Comments
 
LVL 16

Expert Comment

by:nonubik
ID: 12326951
The simplest way is to add an event handler to your button.
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.
0
 
LVL 4

Author Comment

by:Skonen
ID: 12328878
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.
0
 
LVL 12

Expert Comment

by:OnegaZhang
ID: 12331490
I think you need to call IsDialogMessage in the message loop.

welcome to www.fruitfruit.com
0
 
LVL 16

Accepted Solution

by:
nonubik earned 250 total points
ID: 12332992
>Do I have to subclass the dialog
Yes, if you do not intend to cerate a CDialog class for the resource.
0
 
LVL 2

Expert Comment

by:MikeAThon
ID: 12387332
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->IsWindowVisible()) {

    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();

    }
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
conditional code and condition difference 9 77
fizzArray  challenge 1 63
ShiftLeft challenge 21 76
FizzBuzz challenge 9 76
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

920 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now