- Closing the Window
- Button Types
- Button Types and Styles
- Check Boxes
- Group Boxes
- Push Buttons
- Radio Buttons
- Quickly Close Open Windows Using Shortcut Keys
- How to type your way out of a mess with Windows keyboard shortcuts
- How to Close Windows With Alt + Spacebar + C
- How to Close Windows With Fn + Alt + F4
- How to Close Tabs With CTRL + W
- How to Select Open Windows With Alt + Tab
- How to See Your Desktop With Windows Key + D
- How to Close a Group of Windows With the Mouse
- The fastest way to close all running programs in Windows
- Related stories
Closing the Window
When the user closes a window, that action triggers a sequence of window messages.
The user can close an application window by clicking the Close button, or by using a keyboard shortcut such as ALT+F4. Any of these actions causes the window to receive a WM_CLOSE message. The WM_CLOSE message gives you an opportunity to prompt the user before closing the window. If you really do want to close the window, call the DestroyWindow function. Otherwise, simply return zero from the WM_CLOSE message, and the operating system will ignore the message and not destroy the window.
Here is an example of how a program might handle WM_CLOSE.
In this example, the MessageBox function shows a modal dialog that contains OK and Cancel buttons. If the user clicks OK, the program calls DestroyWindow. Otherwise, if the user clicks Cancel, the call to DestroyWindow is skipped, and the window remains open. In either case, return zero to indicate that you handled the message.
If you want to close the window without prompting the user, you could simply call DestroyWindow without the call to MessageBox. However, there is a shortcut in this case. Recall that DefWindowProc executes the default action for any window message. In the case of WM_CLOSE, DefWindowProc automatically calls DestroyWindow. That means if you ignore the WM_CLOSE message in your switch statement, the window is destroyed by default.
When a window is about to be destroyed, it receives a WM_DESTROY message. This message is sent after the window is removed from the screen, but before the destruction occurs (in particular, before any child windows are destroyed).
In your main application window, you will typically respond to WM_DESTROY by calling PostQuitMessage.
We saw in the Window Messages section that PostQuitMessage puts a WM_QUIT message on the message queue, causing the message loop to end.
Here is a flow chart showing the typical way to process WM_CLOSE and WM_DESTROY messages:
Button Types
There are several types of buttons and one or more button styles to distinguish between buttons of the same type.
This document discusses the following topics.
Button Types and Styles
A button belongs to a type and may have additional styles that affect its appearance and behavior. For a table of button styles, see Button Styles.
The following screen shot shows the different types of buttons.
The screen shot shows how buttons might appear in WindowsВ Vista. The appearance will vary on different versions of the operating system, and according to the theme set by the user.
Note the following points about the illustration:
- The three-state check box is shown in the indeterminate state. When checked or unchecked, it looks like a normal check box.
- The large push button has been set to the pushed state programmatically (by sending the BM_SETSTATE message), so that it retains its appearance even when it is not being clicked.
- In the visual style shown, the background of the default push button (or another push button that has the input focus) cycles between blue and gray.
Check Boxes
A check box consists of a square box and an application-defined label, icon, or bitmap that indicates a choice the user can make by selecting the button. Applications typically display check boxes to enable the user to choose one or more options that are not mutually exclusive.
A check box can be one of four styles: standard, automatic, three-state, and automatic three-state, as defined by the constants BS_CHECKBOX, BS_AUTOCHECKBOX, BS_3STATE, and BS_AUTO3STATE, respectively. Each style can assume two check states: checked (a check mark inside the box) or cleared (no check mark). In addition, a three-state check box can assume an indeterminate state (a shaded box inside the check box), which might signify that the user has not made a choice. Repeatedly clicking a standard or automatic check box toggles it from checked to cleared and back again. Repeatedly clicking a three-state check box toggles it from checked to cleared to indeterminate and then repeats the cycle.
When the user clicks a check box (of any style), the check box receives the keyboard focus. The system sends the check box’s parent window a WM_COMMAND message containing the BN_CLICKED notification code. The parent window does not have to handle this message if it comes from an automatic check box or automatic three-state check box, because the system automatically sets the check state for those styles. But the parent window must handle the message if it comes from a non-automatic check box or three-state check box, because the parent window is responsible for setting the check state for those styles. Regardless of the check box style, the system automatically repaints the check box once its state is changed.
The application can ascertain the state of a check box by using the IsDlgButtonChecked function.
Group Boxes
A group box is a rectangle that surrounds a set of controls, such as check boxes or radio buttons, with an application-defined text label in its upper left corner. The sole purpose of a group box is to organize controls related by a common purpose (usually indicated by the label). The group box has only one style, defined by the constant BS_GROUPBOX. Because a group box cannot be selected, it has no check state, focus state, or push state.
Push Buttons
A push button is a rectangle containing an application-defined text label, an icon, or a bitmap that indicates what the button does when the user selects it.
A push button can be one of two styles, standard or default, as defined by the constants BS_PUSHBUTTON and BS_DEFPUSHBUTTON. A standard push button is typically used to start an operation. It receives the keyboard focus when the user clicks it. A default push button is typically used to indicate the most common or default choice, such as closing the dialog box. It is a button that the user can select by simply pressing ENTER when no other push button in the dialog box has the input focus.
When the user clicks a push button, it receives the keyboard focus. The system sends the button’s parent window a WM_COMMAND message that contains the BN_CLICKED notification code.
The split button is a special kind of push button introduced in WindowsВ Vista and Version 6.00. A split button is divided into two parts. The main part functions like a regular or default push button. The second part has an arrow pointing downward. Typically a menu is displayed when the arrow is clicked.
A split button has the BS_SPLITBUTTON style, or the BS_DEFSPLITBUTTON style if it is the default button in a dialog box. You can modify the appearance of the button by using the BCM_SETSPLITINFO message or the corresponding Button_SetSplitInfo macro.
When the user clicks on the main part of the split button, it sends a BN_CLICKED notification just like a normal push button. But when the user clicks on the down arrow, it sends a BCN_DROPDOWN notification. It is the application’s responsibility to display a menu in response to BCN_DROPDOWN.
WindowsВ Vista and Version 6.00 also introduced another kind of push button, the command link. Visually, a command link is very different from a normal push button, but it has the same functionality. A command link typically displays an arrow icon, a line of text, and additional text in a smaller font.
Radio Buttons
A radio button (also called option button) consists of a round button and an application-defined label, icon, or bitmap that indicates a choice the user can make by selecting the button. An application typically uses radio buttons in a group box to enable the user to choose one of a set of related but mutually exclusive options.
A radio button can be one of two styles: standard or automatic, as defined by the style constants BS_RADIOBUTTON and BS_AUTORADIOBUTTON. Each style can assume two check states: checked (a dot in the button) or cleared (no dot in the button).
When the user selects either state, the radio button receives the keyboard focus. The system sends the button’s parent window a WM_COMMAND message containing the BN_CLICKED notification code. The parent window need not handle this message if it comes from an automatic radio button, because the system automatically sets the check state for that style. But the parent window should handle the message if it comes from a non-automatic radio button, because the parent window is responsible for setting the check state for that style. Regardless of the radio button style, the system automatically repaints the button as its state changes.
Radio buttons are arranged in groups, and only one button in the group can be checked at any time. If the WS_GROUP flag is set for any radio button, that button is the first button in a group, and all buttons that follow it immediately in the tab order (but do not themselves have the WS_GROUP flag) are part of its group. If no radio buttons have the WS_GROUP flag, all the radio buttons in the dialog box are treated as a single group.
The application can ascertain whether a radio button is checked by using the IsDlgButtonChecked function.
Quickly Close Open Windows Using Shortcut Keys
How to type your way out of a mess with Windows keyboard shortcuts
One of the advantages of Microsoft Windows PCs is that you can have many different programs and windows open at the same time. This advantage becomes a disadvantage, however, when you have to close dozens of open windows. Fortunately, you can carry out repetitive actions like closing windows with keyboard shortcuts.
Instructions in this article apply to Windows 10, Windows 8, Windows 7, Windows Vista, and Windows XP.
How to Close Windows With Alt + Spacebar + C
One option for closing windows with keyboard shortcuts is as follows:
Open the window that you would like to close using your mouse.
Press and hold down the Alt key, then press the Spacebar to reveal the right-click context menu at the top of the program window you’re trying to close.
Release both keys and press the letter C. This will cause the window to close.
If you can execute this sequence using one hand while the other hand controls the mouse, you’ll be able to close roughly a dozen windows in about as many seconds.
How to Close Windows With Fn + Alt + F4
Another option is to select the window you want to close and then press Fn+Alt+F4. You’ll probably need two hands for this one.
Although the shortcut is officially listed as Alt+F4, you must hold down the Function (Fn) key for it to work.
How to Close Tabs With CTRL + W
The Ctrl+W shortcut only closes the current file you’re working on, but it leaves the program open. This feature can be handy if you want to leave the desktop program open but get rid of all the files you’re working on in quick succession.
Ctrl+W works in most browsers too, so you can close the current tab you’re looking at without taking your hands off the keyboard. If you use Ctrl+W when only one browser tab is open, then the program window will close.
How to Select Open Windows With Alt + Tab
It’s possible to select an open window without using the mouse. Press Alt+Tab to cycle through your open windows. Use this shortcut in conjunction with the other shortcuts to close all open windows without taking your hands off the keyboard.
How to See Your Desktop With Windows Key + D
Sometimes you don’t actually want to close all those windows; what you really want to do is just look at your desktop. To quickly access your desktop, press the Windows Key+D. Use the same shortcut to bring back all your windows.
If you are running Windows 7 or later, there are multiple ways to access your Windows desktop.
How to Close a Group of Windows With the Mouse
When you have numerous files open in the same program, like a bunch of emails in Outlook, Word files, or several spreadsheets in Excel, you can close all of them at once using the mouse. Right-click the program in the Windows taskbar and select Close all windows (or Close Group in older versions of Windows).
The fastest way to close all running programs in Windows
With just a handful of keystrokes you can shut down all active apps on your system via Task Manager’s Programs tab. Plus: power off your PC by pressing three keys.
Need to shut down your PC in a hurry? Simple. Here are two handy keyboard shortcuts to save you time and mousing.
Close all open programs
A little-known set of keystrokes will shut down all active programs at once in no time.
Press Ctrl-Alt-Delete and then Alt-T to open Task Manager’s Applications tab. Press the down arrow, and then Shift-down arrow to select all the programs listed in the window. When they’re all selected, press Alt-E, then Alt-F, and finally x to close Task Manager.
For those of you keeping score at home, that’s seven steps that can be accomplished in less than 10 seconds.
If you’re concerned about having to remember these or other shortcut keys, press the Alt key to show the underline beneath the letter to press to activate each option.
There’s more than one way to turn off your PC
At the end of a long workday your last official act may be to press the power button on your PC to turn it off. Alternatively, you can click Start > Shut down.
But there’s a way to power off your system without taking your hands off the keyboard: in Windows 7, press the Windows key, then the right-arrow key, and then Enter; in Vista, press the Windows key, then the right arrow three times, and then u; in XP, press the Windows key and then u twice.
Related stories
Windows will prompt you to save any files that require it before closing the host app, just as it does when you press the power button or click «Shut down» on the Start menu (which is an odd place for a «Shut down» button, if you ask me).
Equal time for Mac users: open the shutdown dialog by pressing Control-Eject; activate sleep mode by pressing Option-Command-Eject; close all applications and restart the machine by pressing Control-Command-Eject (you’ll be prompted to save changes to open documents); close all applications and shut down by pressing Control Option-Command-Eject (once again, you’ll be prompted to save changes to open documents).
Keep in mind that some organizations want you to leave your PC running because updates and backups may be scheduled for the middle of the night. You can set the power button to put your system in hibernate or standby mode via Windows’ Power Options.
To open Power Options, press the Windows key, type «power options,» and press Enter. Click «Choose what the power buttons do» in the left pane, click the menu next to «When I press the power button» and make your selection: Shut down, Hibernate, Sleep, or Do nothing.
Windows 7’s Power Options dialog lets you change the power button’s action from the default «Shut down» to either «Hibernate,» «Sleep,» or «Do nothing.» screenshot by Dennis O’Reilly/CNET
(If Windows seems to take forever to turn off your PC, check out my post from March 2008 titled » Shut down Windows in an instant .»)