Changing default Button

I want to change the default pushbutton that gets serviced when the space bar is pressed on a Dialog box.

I've found this information so far...

******************
An application sends a DM_SETDEFID message to change the identifier of the default push button for a dialog box.

To send this message, call the SendMessage function as follows.

lResult = SendMessage(
(HWND) hWndControl, // handle to destination control
(UINT) DM_SETDEFID, // message ID
(WPARAM) wParam, // = (WPARAM) () wParam;
(LPARAM) lParam // = 0; not used, must be zero
);
*******************

... but I don't know how to use the parameters. All I have is the ID number of the control (IDC_BUTTON2). Someone please tell me how to get the hwnd and what wParam is supposed to be?

Thanks.
[900 byte] By [Dave C] at [2007-11-18 17:44:45]
# 1 Re: Changing default Button
I'm pretty sure that if your dialog box is derived from CDialog, you can just use the NextDlgCtrl function.

MSDN ::NextDlgCtrl Description (http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dv_wcemfc4/html/aflrfcdialogcolcolnextdlgctrl.asp)

Just put something in PreTranslateMessage to wait for the space bar, and once it's been hit call NextDlgCtrl.

Ozone Blue
Ozone Blue at 2007-11-11 1:25:56 >
# 2 Re: Changing default Button
It is not clear from MSDN, but I think that you pass you IDC_BUTTON2 in a wParam, and hWndControl is your dialog window.
VladimirF at 2007-11-11 1:26:58 >
# 3 Re: Changing default Button
If you are using MFC, then it might be simply:lResult = SendMessage(DM_SETDEFID, IDC_BUTTON2, 0);However that assumes you are doing it in a member function of the dialog. If you are doing that somewhere other than in a member function of the dialog, then it would be slightly different. Are you using MFC? If so, then do you understand why you don't need the window handle (HWND)?
Sam Hobbs at 2007-11-11 1:27:57 >
# 4 Re: Changing default Button
Originally posted by Dave C
To send this message, call the SendMessage function as follows.

lResult = SendMessage(
(HWND) hWndControl, // handle to destination control
(UINT) DM_SETDEFID, // message ID
(WPARAM) wParam, // = (WPARAM) () wParam;
(LPARAM) lParam // = 0; not used, must be zero
);
*******************

... but I don't know how to use the parameters. All I have is the ID number of the control (IDC_BUTTON2). Someone please tell me how to get the hwnd and what wParam is supposed to be?

SendMessage(GetDlgItem(IDC_BUTTON2)->GetSafeHwnd(), DM_SETDEFID, IDC_BUTTON2, 0);
Andreas Masur at 2007-11-11 1:29:00 >
# 5 Re: Changing default Button
Originally posted by Andreas Masur

SendMessage(GetDlgItem(IDC_BUTTON2)->GetSafeHwnd(), DM_SETDEFID, IDC_BUTTON2, 0);


Thank you. This code compiles, but the default button doesn't change. Here's what happens:

Dialog has OK and CANCEL buttons when it first appears. My third button, IDC_BUTTON, is not visible ("visible" property was set false in the Resource Editor). The OK has default action.

User presses the Enter key, OnOK() is called. In that procedure I make the OK button dimmed with EnableWindow(FALSE). I also show the third button with ShowWindow(SW_SHOW), and try to set default action to it using SendMessage.

But the Cancel button has default action even after the SendMessage. If the user presses space bar, the dialog exits.

If I click the mouse on my third button, it will have default action after that (space bar presses it).

Someone please tell me how to fix this.

The Cancel button
Dave C at 2007-11-11 1:29:59 >
# 6 Re: Changing default Button
Well...if you are using a dialog, a simple call to 'SetDefID()' should be enough...

SetDefID(IDC_BUTTON2);
Andreas Masur at 2007-11-11 1:31:05 >
# 7 Re: Changing default Button
Originally posted by Andreas Masur

SendMessage(GetDlgItem(IDC_BUTTON2)->GetSafeHwnd(), DM_SETDEFID, IDC_BUTTON2, 0);
Isn't this a guess at a solution? It seems it was not the solution for the problem. I avoided giving such a solution because the requrements were not clear enough.

People often take the first response they get and then don't even try subsequent solutions even when they are better. Fortunately that did not happen here.
Sam Hobbs at 2007-11-11 1:31:58 >
# 8 Re: Changing default Button
Since the button could be a "default button" only in a context of a dialog (and not by itself), the DM_SETDEFID message should be sent to a dialog, and not to a button. Another hint is a letter "D" in "DM_...", as in Dialog Message.

Sam, the code you suggested
lResult = SendMessage(DM_SETDEFID, IDC_BUTTON2, 0);
is essentially the same as I suggested, with the handle to a Dialog window implied in your MFC form.
VladimirF at 2007-11-11 1:33:02 >
# 9 Re: Changing default Button
Originally posted by Sam Hobbs
Isn't this a guess at a solution?

Do you see any of the words 'guess/try/assume' etc. in my reply? No...you don't, thus, it was not a guess... :rolleyes:
Andreas Masur at 2007-11-11 1:33:59 >
# 10 Re: Changing default Button
Originally posted by VladimirF
Since the button could be a "default button" only in a context of a dialog (and not by itself), the DM_SETDEFID message should be sent to a dialog, and not to a button.

Well..yes, the message is handled by the 'DefDlgProc()' function. So, the description is pretty much misleading, since it clearly says

// handle to destination control

for the first argument...and control is rather the button since they usually refer to a dialog as 'dialog box'...well...at the end it is a subjective interpretation which I did wrong in this case...sorry about that...
Andreas Masur at 2007-11-11 1:35:09 >
# 11 Re: Changing default Button
Originally posted by Dave C
Thank you. This code compiles, but the default button doesn't change.

Yes...sorry, but that was actually my fault...I misinterpreted the documentation as being pointed out in my previous replies. Instead of the handle to the button, you would need to pass the handle to the dialog instead...

However, there is more that you would need to watch for...from the MSDN:

Remarks

This message is processed by the DefDlgProc function. To set the default push button, the function can send WM_GETDLGCODE and BM_SETSTYLE messages to the specified control and the current default push button.

Using the DM_SETDEFID message can result in more than one button appearing to have the default push button state. When the system brings up a dialog, it draws the first push button in the dialog template with the default state border. Sending a DM_SETDEFID message to change the default button will not always remove the default state border from the first push button. In these cases, the application should send a BM_SETSTYLE message to change the first push button border style.
Andreas Masur at 2007-11-11 1:36:07 >