How do you get the ID of the pressed button

Hi,
I need to get the nID of the pressed button and add it to my button event handler function. Is there a call to get the nID, basically, the IDC value of the clicked button.
Thanks,
Rage
[215 byte] By [comicrage] at [2007-11-18 18:39:24]
# 1 Re: How do you get the ID of the pressed button
GetDlgCtrlID
RussG1 at 2007-11-11 1:20:27 >
# 2 Re: How do you get the ID of the pressed button
Hi Russ,

I placed the

int nControlID = GetDlgCtrlID();

of each of my event handlers for the button. THe IDC value for each of the 4 buttons are

ON_BN_CLICKED(IDC_BUTTON_AUTO, OnClickAuto)
ON_BN_CLICKED(IDC_BUTTON_BLOCK, OnClickBlock)
ON_BN_CLICKED(IDC_BUTTON_CLOSED, OnClickClosed)
ON_BN_CLICKED(IDC_BUTTON_MANUAL, OnClickManual)

IDC_BUTTON_CLOSED - 2073
IDC_BUTTON_BLOCK - 2074
IDC_BUTTON_MANUAL - 2075
IDC_BUTTON_AUTO - 2076

When I click on any of the button, I was hoping to get values from 2073-2076, which is the pressed button ID value. However, I get '59648' on any of the 4 pressed button. I was hoping if I click the Auto button, the function returns a 2076.

Am I doing something wrong?

Thanks,

Rage
comicrage at 2007-11-11 1:21:30 >
# 3 Re: How do you get the ID of the pressed button
GetDlgCtrlID has no idea which button was pressed.

It is normally used to get the Ctrl ID of a control from it's HWND (SDK version) or CWND (MFC version).
RussG1 at 2007-11-11 1:22:28 >
# 4 Re: How do you get the ID of the pressed button
What are you trying to do, that you need the button's control id within it's own OnClick handler?
RussG1 at 2007-11-11 1:23:31 >
# 5 Re: How do you get the ID of the pressed button
What is the event handler ? The BN_CLICKED event already has the button ID info.
kirants at 2007-11-11 1:24:30 >
# 6 Re: How do you get the ID of the pressed button
If you just want to trace all button clicks within your dialog (aside from the OnClick handlers for the buttons) you could override WindowProc for your dialog and do something like this.

LRESULT CMainDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
#ifdef _DEBUG
if (message == WM_COMMAND)
if (HIWORD(wParam) == BN_CLICKED)
{
CString strCtrlID;
strCtrlID.Format("CtrlID: %d\r\n",LOWORD(wParam));
TRACE(strCtrlID);
}
#endif
return CDialog::WindowProc(message, wParam, lParam);
}

This will print the Control ID's of the button that was clicked (whenever a button is clicked) to the VS output window. Of course this would be just for testing (debugging) purposes.
RussG1 at 2007-11-11 1:25:29 >
# 7 Re: How do you get the ID of the pressed button
Hi Russ,

I created button with handler functions. When the app first starts up, it only calls the appropriate event handler for the pressed button. However, when I clicked on the next button, I called the event handler of the last pressed button and then the correct event handler function.

I thought that if I could check the ID of the button clicked, it filter out the wrong call to the button function. The message caught with GetCurrentMessage ... ->wparam show the ID of the button called.

Strange, when I press down on the button, it called the last click button function and when I let go of mouse, it calles the correct button function.

Russ,

have you ever seen this before?

If anyone has any suggestion, I would be grateful.

Thanks,

Rage
comicrage at 2007-11-11 1:26:27 >
# 8 Re: How do you get the ID of the pressed button
That sounds strange. Are you handling OnLButtonDown too (or any other button messages?) or just On_Button_Clicked?

Are all your buttons normal CButton's, or are you using some other type of button (or button class/derived button class)?

How are you verifying this? Are you just examining data (or visual cues) after clicking a couple of buttons, or did you actually set breakpoints on each of your button's OnClick handlers and verify that way?
RussG1 at 2007-11-11 1:27:33 >
# 9 Re: How do you get the ID of the pressed button
There is something missing in your explanation. You need to provide more details and give more source code ( and complete too ). The way you are describing it, something is not handled right or you are assuming that it should be that way which may be wrong.
kirants at 2007-11-11 1:28:31 >
# 10 Re: How do you get the ID of the pressed button
It is strange. I never seen this before or heard of it. All the buttons are CBitmapbutton created dynamically with the Create call. I used ON_BN_CLICKED in the Message_Map section.

I placed all debug stop buttons and if I click on the 2nd, 3rd, so on, it will first called the last button event handler function and the correct button function. I verified this with GetCurrentMessage and saw the last button ID in the wparam when it calls the last button event handler and then the correct function call.

Strange,

-Rage
comicrage at 2007-11-11 1:29:29 >
# 11 Re: How do you get the ID of the pressed button
Try commenting out all the code within your buttons OnClick handlers and see if you still have problem.

Also, check your resource ID's for all your resources and make sure they are all unique numbers, and remove any bad entries, etc.

Did you add the message map entries yourself, or did you use class wizrd to do it?
RussG1 at 2007-11-11 1:30:38 >
# 12 Re: How do you get the ID of the pressed button
Since everything had to be dynamically created, I add everything manually.
comicrage at 2007-11-11 1:31:30 >
# 13 Re: How do you get the ID of the pressed button
Where did you define your control ID's for your buttons? Again make sure they do not conflict with other resource id's either defined in a resource file or created dynamically.

Have you double checked your message map stuff to make sure that the correct resource ID's are specified and the correct button handler's for all your buttons, etc?

Did you try commenting out the code within the button's OnClick handler to rule that out?

Maybe post your code for you buttons including message map entries (in both .h and .cpp files), and defines for the resource id's for the buttons (specifying where it is located within the file, etc)
RussG1 at 2007-11-11 1:32:37 >
# 14 Re: How do you get the ID of the pressed button
Thanks Russ,

I comment all the code out and found out that CButton->SetState(...) was causing the problem.

Strange,

-Rage
comicrage at 2007-11-11 1:33:38 >
# 15 Re: How do you get the ID of the pressed button
Glad you figured it out.

Not sure about CButton::SetState as I have never used it. The docs say it only affects the appearance of a button, but it appears from the problem you are having that it actually sends some sort of click notification...
RussG1 at 2007-11-11 1:34:42 >