?? ch05.htm
字號:
<H3><FONT COLOR="#000077"><B>What Are Radio Buttons?</B></FONT></H3>
<P>Radio buttons are used when a selection must be made from several mutually exclusive
options, such as a user's gender. Only one of the radio buttons, which usually are
grouped together, is checked at any particular time.
<H3><FONT COLOR="#000077"><B>What Are Check Boxes?</B></FONT></H3>
<P>Check boxes are used as Boolean flags that indicate whether a particular condition
is <TT>true</TT> or <TT>false</TT>. Unlike radio buttons, several check boxes in
a group can be checked. Optionally, a check box can support a third state--disabled--meaning
that the control is neither <TT>true</TT> nor <TT>false</TT>.
<H3><FONT COLOR="#000077"><B>What Are Group Boxes?</B></FONT></H3>
<P>A group box logically groups controls that are used for similar purposes. This
helps the user understand the relationships between controls and makes a dialog box
easier to use. Radio buttons are almost always enclosed in a group box so that it's
obvious which controls are associated with each other.
<H3><FONT COLOR="#000077"><B>MFC Support for Buttons</B></FONT></H3>
<P>Button controls normally are created as part of a dialog box. After you add a
button to a dialog box, you can use ClassWizard to add functions that can be used
to handle events created when the button is pressed, checked, or selected. You also
use ClassWizard to create <TT>CButton</TT> objects that are associated with individual
button controls.</P>
<P>You can use the MFC class <TT>CButton</TT> to interact with button controls--both
buttons that have been added to a dialog box resource and buttons that have been
created dynamically. Use ClassWizard to associate a button control with a specific
<TT>CButton</TT> object.
<H3><FONT COLOR="#000077"><B>A Sample Project Using Buttons</B></FONT></H3>
<P>In order to see how button controls can be used with dialog boxes, create a dialog
box-based project named Button using AppWizard, following the steps provided in Hour
4, "Dialog Boxes and C++ Classes." You will use this project for the rest
of this hour as an example of how to use buttons in a dialog box.</P>
<P>Click the ResourceView tab in the project workspace. Open the dialog box editor
by double-clicking the <TT>IDD_BUTTON_DIALOG</TT> icon in the Dialog resource folder.</P>
<P>The <TT>IDD_BUTTON_DIALOG</TT> dialog box is displayed in the dialog box editor,
along with a dockable control toolbar or palette. The floating control palette contains
all the controls available for a dialog box, as shown in Figure 5.1.</P>
<P><A NAME="01"></A><A HREF="01.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch05/01.htm"><B>Figure 5.1.</B></A> <I><BR>
The floating control palette, showing the buttons and boxes needed to create basic
dialog boxes.</I></P>
<P>There are four different icons on the control palette for buttons, each used for
a particular button type. Use one of the following steps to add a button control
to a dialog box:
<UL>
<LI>Drag a button control from the palette to the dialog box by pressing the left
mouse button while over the control button, then dragging the mouse cursor to the
dialog box with the left mouse button still pressed. Release the mouse button when
the cursor is over the desired spot in the dialog box.<BR>
<BR>
<LI>Select a button control by clicking a control in the control palette. Click the
desired location for the control in the dialog box, and the dialog box editor creates
a control for you in that location.
</UL>
<P>These steps apply for all controls in the control palette. After you've added
a control to the dialog box, you can use the mouse to reposition and resize it.</P>
<P>As a demonstration, add several buttons to the main dialog box used in the Button
project. You will use these controls later this hour to demonstrate button events.
Refer to Figure 5.2 for the location of the added buttons.</P>
<P><A NAME="02"></A><A HREF="02.htm" tppabs="http://www.mcp.com/824169600/0-672/0-672-31242-5/ch05/02.htm"><B>Figure 5.2.</B></A> <I><BR>
The main dialog box used by the Button project.</I></P>
<P>A total of five buttons are added to <TT>IDD_BUTTON_DIALOG</TT>. Use the values
from Table 5.1 to set the properties for each control. Except for the ID and caption,
all controls use the default set of properties.
<H4><FONT COLOR="#000077">Table 5.1. Values used for controls in IDD_BUTTON_DIALOG.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Control ID</B></TD>
<TD ALIGN="LEFT"><B>Button Type</B></TD>
<TD ALIGN="LEFT"><B>Caption</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_BTN_TEST</TT></TD>
<TD ALIGN="LEFT">Pushbutton</TD>
<TD ALIGN="LEFT">&Test</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_RADIO_HIGH</TT></TD>
<TD ALIGN="LEFT">Radio button</TD>
<TD ALIGN="LEFT">&High</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_RADIO_LOW</TT></TD>
<TD ALIGN="LEFT">Radio button</TD>
<TD ALIGN="LEFT">&Low</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_GROUP_VOLUME</TT></TD>
<TD ALIGN="LEFT">Group control</TD>
<TD ALIGN="LEFT">&Volume</TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_CHECK_AMP</TT></TD>
<TD ALIGN="LEFT">Check box</TD>
<TD ALIGN="LEFT">&Amplified</TD>
</TR>
</TABLE>
<H4><FONT COLOR="#000077">Button Control Properties</FONT></H4>
<P>Like all controls, buttons have a set of properties that define the behavior of
each control. Although there are four different types of button controls, they share
a common set of properties. You can display the properties for a particular control
by selecting Properties from the menu displayed when you right-click the control.
These properties are shared by all button controls:
<UL>
<LI>ID: Used for the button's resource ID. A default resource ID, such as <TT>IDC_BUTTON1</TT>,
is supplied by Developer Studio. Using <TT>IDC_</TT> as a prefix for control resource
IDs is a Microsoft naming convention.<BR>
<BR>
<LI>Caption: Indicates the text that appears as the button's label. Developer Studio
supplies a default name, such as Button. To make one of the letters in the caption
of a control the mnemonic key, precede it with an ampersand (&).<BR>
<BR>
<LI>Visible: Indicates that the button is initially visible. This check box is normally
checked.<BR>
<BR>
<LI>Disabled: Indicates that the button should be initially disabled. This check
box is normally cleared.<BR>
<BR>
<LI>Group: Marks the first control in a group. All controls following a control with
this attribute are considered part of the same group if this check box is cleared.
A user can move between controls in the same group using the arrow keys.<BR>
<BR>
<LI>Tab Stop: Indicates that this control can be reached by pressing Tab on the keyboard.
This check box is normally checked.<BR>
<BR>
<LI>Default Button: Marks this control as the dialog box's default button. There
can be only one default button in a dialog box, and it is executed if the user presses
Enter without using any other controls in the dialog box. This check box is normally
cleared.<BR>
<BR>
<LI>Owner Draw: Indicates that the button will be drawn by the button's owner; in
this case, the dialog box. In most cases, this check box is cleared.
</UL>
<P>Group boxes support the fewest properties of any button control. All button properties
are supported except <TT>Default Button</TT> and <TT>Owner Draw</TT>.</P>
<P>Radio buttons do not use the default button property because they aren't used
as default buttons. However, radio buttons do support two properties not used by
pushbutton controls:
<UL>
<LI>Auto: Automatically changes the state of the control when it is selected. This
check box is normally selected.<BR>
<BR>
<LI>Left Text: Places the control's label on the left side of the check box instead
of the right. This check box is normally cleared.
</UL>
<P>Check boxes support the same properties as radio controls, except that they are
used with one additional attribute:
<UL>
<LI>Tri-state: The check box can have three states instead of two. In addition to
<TT>true</TT> and <TT>false</TT>, the control can be disabled, meaning that the value
is neither <TT>true</TT> nor <TT>false</TT>.
</UL>
<P>In addition, all controls have a property page that is labeled Extended Styles.
These styles are rarely used, and aren't discussed in this book.
<H4><FONT COLOR="#000077">Using Standard Pushbutton Layouts in Your Dialog Boxes</FONT></H4>
<P>Several pushbuttons are commonly used in dialog boxes that contain controls. Because
each of these pushbuttons carries a specific meaning, you should try to use the standard
terminology whenever possible because it minimizes the amount of work required for
users of your programs. Here are the standard meanings for these buttons:
<UL>
<LI>OK: Used to close and accept any information that is present in the dialog box.
Any user-supplied information in the dialog box is used by the program. Note that
the OK pushbutton is the only button spelled with all capital letters.<BR>
<BR>
<LI>Cancel: Used to close the dialog box and remove any changes that might have been
performed while the dialog box was open. If there are changes that cannot be reversed,
the label for this button should be changed to read Close. Changing the label for
a button is discussed later in this hour.<BR>
<BR>
<LI>Close: Used to close the dialog box. It does not necessarily imply that any action
is taken by the program. Close is most often used when a Cancel button cannot be
used to remove changes made while the dialog box is open. Many programs change a
Cancel button into a Close button.<BR>
<BR>
<LI>Help: Used to request context-sensitive help for the open dialog box.<BR>
<BR>
<LI>Apply: Used to perform changes based on data that has been entered in the dialog
box. Unlike the OK button, the dialog box should remain open after the Apply button
is pressed.
</UL>
<H2><FONT COLOR="#000077"><B>Binding a Button Control to a <TT>CButton</TT> Object</B></FONT></H2>
<P>The easiest way to set or retrieve the value of a control is to associate it with
a class-member variable using ClassWizard. When associating a member variable with
a control, you can associate the member variable either with the control or with
the control's value. Member variables representing buttons are rarely associated
by value; instead, the <TT>CButton</TT> class is used to represent most button controls.
You will learn about associating member variables by value with dialog box controls
in Hour 6, "Using Edit Controls."</P>
<P>To add a member variable to a <TT>CDialog</TT>-derived class, follow these steps:
<DL>
<DD>1. Open ClassWizard.<BR>
<BR>
2. Select the tab labeled Member Variables.<BR>
<BR>
3. Select the <TT>CDialog</TT>-derived class that manages the dialog box; in this
case, <TT>CButtonDlg</TT>.<BR>
<BR>
4. Select the control ID representing the control associated with the new member
variable.<BR>
<BR>
5. Press the button labeled Add Variable. An Add Member Variable dialog box appears.
<BR>
Enter the control's name, category, and variable type, then press OK.<BR>
<BR>
6. Close ClassWizard.
</DL>
<P>Follow these steps for all controls added to the <TT>IDD_BUTTON_DIALOG</TT> earlier.
Use the values from Table 5.2 for each new member variable added to <TT>CButtonDlg</TT>.
<H4><FONT COLOR="#000077">Table 5.2. Values used to add member variables for CButtonDlg.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><B>Control ID</B></TD>
<TD ALIGN="LEFT"><B>Variable Name</B></TD>
<TD ALIGN="LEFT"><B>Category</B></TD>
<TD ALIGN="LEFT"><B>Type</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_BTN_TEST</TT></TD>
<TD ALIGN="LEFT"><TT>m_btnTest</TT></TD>
<TD ALIGN="LEFT">Control</TD>
<TD ALIGN="LEFT"><TT>CButton</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_GROUP_VOLUME</TT></TD>
<TD ALIGN="LEFT"><TT>m_btnVolume</TT></TD>
<TD ALIGN="LEFT">Control</TD>
<TD ALIGN="LEFT"><TT>CButton</TT></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT"><TT>IDC_CHECK_AMP</TT></TD>
<TD ALIGN="LEFT"><TT>m_btnAmp</TT></TD>
<TD ALIGN="LEFT">Control</TD>
<TD ALIGN="LEFT"><TT>CButton</TT></TD>
</TR>
</TABLE>
</P>
<P>ClassWizard automatically adds the member variables to the <TT>CButtonDlg</TT>
class declaration for you.
<H3><FONT COLOR="#000077"><B>Adding Button Events to a Dialog Box Class</B></FONT></H3>
<P>Although the buttons are part of the dialog box resource and appear whenever the
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -