user4344762
user4344762

Reputation:

How to set button as default on click?

This is an example of two buttons from the Font dialog in Windows:

enter image description here

The OK button is the default button, if I click on the Cancel button (mouse down), then the Cancel button will become the default.

How can I achieve this in WinAPI, should I handle the mouse down event of each button, and add to the button that was clicked the BS_DEFPUSHBUTTON style and remove this style from the other buttons?

Upvotes: 3

Views: 2152

Answers (1)

Jonathan Potter
Jonathan Potter

Reputation: 37132

Send DM_SETDEFID to the Dialog Box to change the default button ID. Take note of the following though:

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

So as well as sending DM_SETDEFID to set the new default button, you should send BM_SETSTYLE to the old default button to clear the BS_DEFPUSHBUTTON style.

Upvotes: 7

Related Questions