Changing/Setting the Default Push Button in a Dialog Box

Last reviewed: November 2, 1995
Article ID: Q67655
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.0 and 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

The default push button in a dialog box is defined to be the button that is pressed when the user chooses the ENTER key, provided that the input focus is not on another button in the dialog box. The default push button is visually distinguished from other buttons by a thick dark border. This article describes how to change the default push button.

MORE INFORMATION

To change the default push button, perform the following three steps:

  1. Send the BM_SETSTYLE message to the current default push button to change its border to that of a regular push button.

  2. Send a DM_SETDEFID message to the dialog box to change the ID of the default push button.

  3. Send the BM_SETSTYLE message to the new default push button to change its border to that of a default push button.

The following is sample code that performs the three steps:

Sample Code

   // Reset the current default push button to a regular button.
   SendDlgItemMessage(hDlg, <ID of current default push button>,
         BM_SETSTYLE, BS_PUSHBUTTON, (LONG)TRUE);

   // Update the default push button's control ID.
   SendMessage(hDlg, DM_SETDEFID, <ID of new default push button>,
         0L);

   // Set the new style.
   SendDlgItemMessage(hDlg, <ID of new default push button>,
         BM_SETSTYLE, BS_DEFPUSHBUTTON, (LONG)TRUE);

NOTE: For Win32, the (LONG) casts should be changed to (LPARAM).

Note, however, that ANY push button that has the input focus will have a dark border. A default push button will retain this dark border even when the input focus is transferred to another control in the dialog box, provided the new control is not another push button.

For example, if the input focus is on an edit control, check box, radio button, or any control other than a push button, and the ENTER key is pressed, Windows sends a WM_COMMAND message to the dialog box procedure with the wParam set to the control ID of the default push button.


Additional reference words: 3.00 3.10 3.50 3.51 4.00 95
KBCategory: kbui
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 2, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.