📄 ch06.htm
字号:
<P>One difference between edit controls and the pushbutton controls you saw in Hour
5, "Button Controls," is that a button control is normally used to generate
events. An edit control can generate events also, but it most often is used to actually
store data.
<H3><FONT COLOR="#000077"><B>MFC Support for Edit Controls</B></FONT></H3>
<P>You normally add edit controls to a dialog box just as you added buttons in Hour
5. After you add the control to a dialog box, use ClassWizard to configure the control
for use in the program.</P>
<P>The MFC class <TT>CEdit</TT> is often used to interact with edit controls. As
you will see in the next section, you can use ClassWizard to associate an edit control
with a specific <TT>CEdit</TT> object. An edit control can also be associated with
a <TT>CString</TT> object, which can simplify the use of edit controls in dialog
boxes. You will learn about using edit controls associated with <TT>CString</TT>
objects in detail beginning with the section "Passing Parameters to Dialog Boxes
Using DDV and DDX Routines," later in this hour.
<BLOCKQUOTE>
<P>
<HR>
<B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>Of course, edit
controls can be used in dialog box-based programs, which were discussed in Hour 5.
However, in this hour an SDI program is used to show off some of the data exchange
and validation features often used with edit controls.
<HR>
</BLOCKQUOTE>
<H3><FONT COLOR="#000077"><B>Building an SDI Test Project</B></FONT></H3>
<P>Some of the sample programs in this book require you to build an SDI project and
add a test dialog box. You can use the following steps to build a test project that
includes a test dialog box:
<DL>
<DD>1. Create an SDI project named EditTest using MFC AppWizard, as discussed in
Hour 1, "Introducing Visual C++ 5." Feel free to add or remove any of the
optional features suggested by AppWizard, because they aren't used in this hour.<BR>
<BR>
2. As discussed in Hour 4, "Dialog Boxes and C++ Classes," add a dialog
box resource to the program. Name the dialog box <TT>IDD_TEST</TT>, and set the caption
to Test Dialog. Using ClassWizard, create a dialog box class called <TT>CTestDlg</TT>
for the new dialog box.<BR>
<BR>
3. Add a menu choice named <TT>ID_VIEW_TEST</TT>, with a caption of Test... that
brings up the Edit dialog box by adding a new menu choice on the View menu. Add a
message-handling function for the new menu item using ClassWizard. The steps required
to add a message-handling function that uses a <TT>CDialog</TT>-based object were
discussed in Hour 4. Use the source code provided in Listing 6.3 for the <TT>CMainFrame</TT>
message-handling function.<BR>
<BR>
4. Include the class declaration for <TT>CTestDlg</TT> in the <TT>MainFrm.cpp</TT>
file by adding the following line after all the <TT>#include</TT> directives in <TT>MainFrm.cpp</TT>:
</DL>
<BLOCKQUOTE>
<PRE><FONT COLOR="#0066FF"><TT>#include "testdlg.h"</TT></FONT></PRE>
</BLOCKQUOTE>
<DL>
<DD>5. Add a pushbutton control, <TT>IDC_TEST</TT>, labeled Test, to the dialog box,
as was done in Hour 5. Using ClassWizard, add a function that handles the <TT>BN_CLICKED</TT>
message, which will be used in later examples.<BR>
<BR>
6. After following these steps, make sure that the project compiles properly by pressing
the Build icon on the toolbar or by selecting Build|Build EditTest.exe from the main
menu. Try the menu item to make sure the <TT>IDC_TEST</TT> dialog box is displayed
when View|Test... is selected.
</DL>
<H4><FONT COLOR="#000077">TYPE: Listing 6.3. Handling a menu-item selection for EditTest.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CMainFrame::OnViewTest()</TT>
<TT>{</TT>
<TT> CTestDlg dlg;</TT>
<TT> dlg.DoModal();</TT>
<TT>}</TT>
</FONT></PRE>
<H3><FONT COLOR="#000077"><B>Adding an Edit Control to a Dialog Box</B></FONT></H3>
<P>You add an edit control to a dialog box just as you added a button control in
Hour 5, using either of these two basic methods:
<UL>
<LI>Using drag-and-drop, drag an edit control from the control palette and drop at
a desirable location in the dialog box.<BR>
<BR>
<LI>Select an edit control by clicking the Edit Control icon in the tool palette,
and click over the location in the dialog box where the edit control should be located.
</UL>
<P>Arrange the edit control so that the dialog box resembles the one in Figure 6.1.</P>
<P><A NAME="01"></A><A HREF="01.htm"><B>Figure 6.1.</B> </A><I><BR>
The dialog box used in the edit control examples.</I></P>
<P>In Figure 6.1, a static text control is located immediately to the left of the
edit control. Edit controls are usually labeled with static text so a user can determine
the type of input needed. Static text controls were discussed in Hour 4. The ID for
the new edit control is set by default to <TT>IDC_EDIT1</TT> or a similar name. Change
the ID to <TT>IDC_EDIT_TEST</TT>, leaving the other properties set to their default
values.
<H2><FONT COLOR="#000077"><B>Edit Control Properties</B></FONT></H2>
<P>You can examine the properties for an edit control, just as with other resources,
by right-clicking over the control and selecting Properties from the pop-up menu.
These general properties are available for an edit control:
<UL>
<LI>ID: Used for the edit control's resource ID. Like other controls, a default resource
ID is supplied by Developer Studio.<BR>
<BR>
<LI>Visible: Indicates that the edit control is initially visible. This option is
normally selected.<BR>
<BR>
<LI>Disabled: Indicates that the edit control should be initially disabled. This
option is not normally selected.<BR>
<BR>
<LI>Group: Used to mark the first control in a group. All controls following a control
with this attribute are considered part of the same group if the attribute is cleared.
A user can move between controls in the same group using the arrow keys.<BR>
<BR>
<LI>Help ID: Creates a context-sensitive help ID for this control.<BR>
<BR>
<LI>Tab Stop: Indicates that this control can be reached by pressing the Tab key.
This option is normally selected.
</UL>
<P>There also is a group of properties that apply specifically to edit controls.
The following properties are displayed by clicking the Styles tab in the Properties
dialog box:
<UL>
<LI>Align Text: A drop-down list box that is enabled if the edit control is an MLE.
The text can be aligned to the left, center, or right, with left as the default.<BR>
<BR>
<LI>Multiline: Defines the control as an MLE. This option is not selected by default.<BR>
<BR>
<LI>Number: Restricts the edit control to digits only. This feature is available
only in Windows 95 or Windows NT version 3.51 or later.<BR>
<BR>
<LI>Horizontal Scroll: Enabled only for an MLE and provides a horizontal scrollbar.
The option is not selected by default.<BR>
<BR>
<LI>Auto HScroll: Scrolls text to the right if needed. This option is normally selected.<BR>
<BR>
<LI>Vertical Scroll: Enabled only for an MLE and provides a vertical scrollbar. The
option is not selected by default.<BR>
<BR>
<LI>Auto VScroll: Enabled only for an MLE and provides automatic scrolling when the
user presses Return on the last line. The option is not selected by default.<BR>
<BR>
<LI>Password: Hides the user's input by displaying an asterisk instead of each character.
This option is available only in single-line controls and is not selected by default.<BR>
<BR>
<LI>No Hide Selection: Changes the way an edit control handles the focus. When this
option is enabled, text appears to be selected at all times. This option is not selected
by default.<BR>
<BR>
<LI>OEM Convert: Performs conversions on the user's input so that the <TT>AnsiToOem</TT>
function works correctly if called by your program. This option is not selected by
default.<BR>
<BR>
<LI>Want Return: Applies to MLE controls. This option allows an edit control to accept
an Enter keypress, so that an Enter keypress doesn't affect the dialog box's default
pushbutton.
<LI>Border: Creates a border around the control. This option is selected by default.<BR>
<BR>
<LI>Uppercase: Converts all input to uppercase characters. This option is not selected
by default.<BR>
<BR>
<LI>Lowercase: Converts all input to lowercase characters. This option is not selected
by default.<BR>
<BR>
<LI>Read-only: Prevents the user from typing or editing text in the edit control.
This option is not selected by default.
</UL>
<H2><FONT COLOR="#000077"><B>Binding a <TT>CEdit</TT> Object to an Edit Control</B></FONT></H2>
<P>As discussed earlier, one way to interact with an edit control is through a <TT>CEdit</TT>
object that is attached to the control. To attach a <TT>CEdit</TT> object to an edit
control, you use ClassWizard much as you did for button controls in the previous
hour:
<DL>
<DD>1. Open ClassWizard.<BR>
<BR>
2. Select the <TT>CDialog</TT>-derived class that manages the dialog box; in this
case, <TT>CTestDlg</TT>.<BR>
<BR>
3. Select the tab labeled Member Variables.<BR>
<BR>
4. Select the control ID representing the control associated with the new member
variable; in this case, <TT>IDC_EDIT_TEST</TT>.<BR>
<BR>
5. Click the button labeled Add Variable. An Add Member Variable dialog box appears.
Enter the control's name, category, and variable type, then click OK. For this example,
use the values from Table 6.2.
</DL>
<H4><FONT COLOR="#000077">Table 6.2. Values used to add a CEdit member variable for
CTestDlg.</FONT></H4>
<P>
<TABLE BORDER="1">
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><B>Control ID</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Variable Name</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Category</B></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><B>Type</B></TD>
</TR>
<TR ALIGN="LEFT" rowspan="1">
<TD ALIGN="LEFT" VALIGN="TOP"><TT>IDC_EDIT_TEST</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>m_editTest</TT></TD>
<TD ALIGN="LEFT" VALIGN="TOP">Control</TD>
<TD ALIGN="LEFT" VALIGN="TOP"><TT>CEdit</TT></TD>
</TR>
</TABLE>
</P>
<P>The default value displayed in the Category control is <TT>Value</TT>. The <TT>Value</TT>
category is used for some member variables later this hour, when you learn about
DDV and DDX routines.
<H2><FONT COLOR="#000077"><B>Collecting Entered Text from an Edit Control</B></FONT></H2>
<P>The primary reason for using an edit control, of course, is to collect information
from a user. To do that, you must get the information from the edit control. Using
the <TT>CEdit</TT> class simplifies this process.
<H3><FONT COLOR="#000077"><B>Using <TT>CEdit</TT> Member Functions to Retrieve Text</B></FONT></H3>
<P>Several <TT>CEdit</TT> member functions are useful when collecting information
from an edit control, such as the <TT>GetWindowText</TT> and <TT>LineLength</TT>
member functions. As an example, add the source code in Listing 6.4 to the <TT>CTestDlg::OnTest</TT>
member function, created earlier.
<H4><FONT COLOR="#000077">TYPE: Listing 6.4. Collecting input from an edit control
using CEdit.</FONT></H4>
<PRE><FONT COLOR="#0066FF"><TT>void CTestDlg::OnTest()</TT>
<TT>{</TT>
<TT> CString szEdit;</TT>
<TT> CString szResult;</TT>
<TT> int nLength = m_editTest.LineLength();</TT>
<TT> m_editTest.GetWindowText( szEdit );</TT>
<TT> szResult.Format( "%s has %d chars", szEdit, nLength );</TT>
<TT> AfxMessageBox( szResult );</TT>
<TT>}</TT>
</FONT></PRE>
<P>When the Test button is clicked, the text entered in the edit control is retrieved
by using the <TT>m_editTest</TT> object. Normally, you are interested only in data
contained in an edit control if OK is clicked. If the Cancel button is clicked, the
dialog box should be closed and, usually, any entered information is simply discarded.
<H2><FONT COLOR="#000077"><B>Passing Parameters to Dialog Boxes Using DDV and DDX
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -