⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 ch06.htm

📁 24小时学会vc++
💻 HTM
📖 第 1 页 / 共 3 页
字号:
Routines</B></FONT></H2><P>The DDV and DDX routines are helper functions that help manage data for dialogboxes. DDV, or Dialog Data Validation, routines are used for data validation. DDX,or Dialog Data Exchange, routines are used to exchange data to and from the controlsin a dialog box.<BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Time Saver:</B></FONT><B> </B>Although you can use	the DDV and DDX routines in your dialog boxes directly, ClassWizard adds the code	for you at the click of a button. Normally, you add the DDV and DDX routines with	ClassWizard instead of trying to hand-code the necessary function calls. <HR></BLOCKQUOTE><H3><FONT COLOR="#000077"><B>Why Are DDV and DDX Routines Used?</B></FONT></H3><P>The DDV routines are useful when collecting data from an edit control. In general,you have little control over how a user enters data in an edit control. A DDV enablesyou to perform some simple validation based on range or string length.<BLOCKQUOTE>	<P><HR><B> </B><FONT COLOR="#000077"><B>Just a Minute:</B></FONT><B> </B>For example, if	an edit control is used to collect an abbreviated state name, you want to limit the	entered text to two characters. Using a DDV routine, it's easy to make sure that	two characters have been entered. <HR></BLOCKQUOTE><P>DDX functions link member variables from the dialog box class to controls thatare contained in the dialog box. DDX routines enable data to be transferred to andfrom the controls much easier than is otherwise possible. As discussed in Hour 4,a dialog box is normally used something like this:</P><PRE><FONT COLOR="#0066FF"><TT>CMyDialog    dlgMine;</TT><TT>dlgMine.DoModal();</TT></FONT></PRE><P>In this example, the dialog box is created when <TT>DoModal</TT> is called, andthe function does not return until the user closes the dialog box. This presentsa problem if data must be passed to or from the dialog box. Because none of the controlsexist until the dialog box is created, using <TT>SetWindowText</TT>, <TT>GetWindowText</TT>,or other functions to interact directly with controls contained in the dialog boxis not possible. After the dialog box has been dismissed, it is too late to use thosefunctions to collect user input.</P><P>When DDX routines are used to exchange information with a dialog box, the dialogbox can be used like this:</P><PRE><FONT COLOR="#0066FF"><TT>CMyDialog    dlgMine;</TT><TT>dlgMine.m_szTest = &quot;Hello World&quot;;</TT><TT>dlgMine.DoModal();</TT></FONT></PRE><P>The DDX routines enable you to have access to the dialog box's controls beforeand after the dialog box has been created. This simplifies dialog box programmingbecause it is a much more flexible method than adding code in the <TT>InitDialog</TT>member function.<H3><FONT COLOR="#000077"><B>How Are DDV and DDX Routines Used?</B></FONT></H3><P>The easiest and most useful way to add DDV and DDX routines to your dialog boxclass is by using ClassWizard. Member variables associated with dialog box controlsby value automatically use the DDV and DDX routines provided by MFC. For example,<TT>CString</TT> member variables are often associated with edit controls. ClassWizardadds source code to handle the exchange and validation of data in two places:<UL>	<LI>In the dialog box's constructor, source code is added to initialize the member	variable.<BR>	<BR>		<LI>In the dialog box's <TT>DoDataExchange</TT> member function, ClassWizard adds	DDV and DDX routines for each member variable associated with a control's value.</UL><P><TT>DoDataExchange</TT> is a virtual function that is called to move data betweenthe control and the dialog box's member data. <TT>DoDataExchange</TT> takes a singleparameter, either <TT>TRUE</TT> or <TT>FALSE</TT>, with <TT>TRUE</TT> as the defaultparameter. When <TT>DoDataExchange( FALSE )</TT> is called, data is moved from themember variable to the control. When <TT>DoDataExchange( TRUE )</TT> is called, datais copied from the control to the member variable.</P><P>When the dialog box is initially displayed during <TT>CDialog::OnInitDialog</TT>,<TT>UpdateData(FALSE)</TT> is called to transfer data from the member variables tothe dialog box's controls. Later, during <TT>CDialog::OnOk</TT>, <TT>UpdateData()</TT>is called to transfer data from the dialog box's controls to member variables.</P><P>As shown in Figure 6.2, <TT>DoDataExchange</TT> has a single parameter that controlsthe direction that data, in this case <TT>m_szTest</TT>, is copied.</P><P><A HREF="02.htm"><B>Figure 6.2.</B></A> <I><BR>DDV and DDX routines used to handle dialog box data.</I><H3><FONT COLOR="#000077"><B>Associating a Control's Value with a Member Variable</B></FONT></H3><P>You add member variables that are associated with a control's value almost exactlythe way you added control-type variables earlier in this hour. For example, to createa member variable associated with the <TT>IDD_EDIT_TEST</TT> edit control, followthese steps:<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 Member Variables tab.</DL><P><DL>	<DD>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 Add Variable button. 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.3.</DL><H4><FONT COLOR="#000077">Table 6.3. Values used to associate a CString member variablewith an edit control.</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_EDIT_TEST</TT></TD>		<TD ALIGN="LEFT"><TT>m_szTest</TT></TD>		<TD ALIGN="LEFT">Value</TD>		<TD ALIGN="LEFT"><TT>CString</TT></TD>	</TR></TABLE></P><P>The preceding steps are exactly like the steps used to add a control-type variableearlier in this hour, except that the control type is set to <TT>Value</TT>. A membervariable associated by value with an edit control can also be an <TT>int</TT>, <TT>UINT</TT>,<TT>long</TT>, <TT>DWORD</TT>, <TT>float</TT>, <TT>double</TT>, or <TT>BYTE</TT>,although it is most commonly a <TT>CString</TT>.</P><P>After closing the Add Member Variable dialog box, ClassWizard displays an editcontrol that you can use to specify the type of validation to be performed on themember variable. If a <TT>CString</TT> object is associated with an edit control,the maximum string length can be specified. If a numeric variable is used, the allowedrange can be defined.<H3><FONT COLOR="#000077"><B>Exchanging Edit-Control Information Using DDX Functions</B></FONT></H3><P>The member variables associated with dialog box controls by ClassWizard are addedto the dialog box class as public variables. This allows the member variables tobe easily accessed and used. For example, to use the <TT>m_szTest</TT> variable thatwas added in the previous section, edit the <TT>CMainFrame::OnViewTest</TT> memberfunction so it looks like the function in Listing 6.5. Before compiling the project,remove the following line, which was added to <TT>CTestDlg::OnInitDialog</TT> earlier:</P><PRE><FONT COLOR="#0066FF"><TT>m_editTest.SetWindowText( &quot;Default&quot; );</TT></FONT></PRE><H4><FONT COLOR="#000077">TYPE: Listing 6.5. Using member variables to exchange informationwith an edit control.</FONT></H4><PRE><FONT COLOR="#0066FF"><TT>void CMainFrame::OnViewTest()</TT><TT>{</TT><TT>    CTestDlg    dlg;</TT><TT>    dlg.m_szTest = &quot;DDX Test&quot;;</TT><TT>    if( dlg.DoModal() == IDOK )</TT><TT>    {</TT><TT>        AfxMessageBox( dlg.m_szTest );</TT><TT>    }</TT><TT>    else</TT><TT>    {</TT><TT>        AfxMessageBox( &quot;Dialog cancelled&quot; );</TT><TT>    }</TT><TT>}</TT></FONT></PRE><P>Listing 6.5 sets the value of <TT>m_szTest</TT> before the dialog box is displayedto the user. <TT>CDialog::OnInitDialog</TT> calls the <TT>CWnd::UpdateData</TT> function,which calls <TT>UpdateData</TT>. Because <TT>UpdateData</TT> is a virtual function,the proper version of the function is called--the version that is part of the <TT>CDialog</TT>-derivedclass that handles the dialog box.</P><P>After the dialog box closes, the <TT>CMainFrame::OnViewTest</TT> function checksthe return value of <TT>DoModal</TT>. If <TT>IDOK</TT> was returned, the dialog boxwas closed using the OK button, and the value of <TT>m_szTest</TT> is displayed.<H2><FONT COLOR="#000077"><B>Summary</B></FONT></H2><P>In this hour, you learned about identifier scope and lifetime. You also learnedabout the Windows edit control and how it is usually used in a dialog box. You sawhow to associate an edit control with a <TT>CEdit</TT> object using ClassWizard andused data exchange and validation to pass parameters to and from dialog boxes.<H2><FONT COLOR="#000077"><B>Q&amp;A</B></FONT></H2><DL>	<DD><B>Q I would like to use the DDV routines for all my dialog box data, but what	if I have a complex data type, such as a credit card number? How can I use the DDV	DDX mechanism?</B><BR>	<BR>	<B>A</B> You have two options. You can perform the validation yourself when accepting	user input by testing for valid data before the user is allowed to close the dialog	box. You can also write your own custom DDV routine. Technical Note 26 describes	how to write such a routine. You can find this technical note by searching for TN026	in the Developer Studio online help.<BR>	<BR>	<B>Q Can I call <TT>UpdateData</TT> at any time? I would like to implement an Undo	feature in my dialog box.</B><BR>	<BR>	<B>A</B> Normally, <TT>UpdateData</TT> is called when the dialog box is initialized	and when the user closes the dialog box by clicking OK. However, if you must call	<TT>UpdateData</TT> at other times, it's perfectly okay.</DL><H2><FONT COLOR="#000077"><B>Workshop</B></FONT></H2><P>The Workshop is designed to help you anticipate possible questions, review whatyou've learned, and begin thinking ahead to putting your knowledge into practice.The answers to the quiz are in Appendix B, &quot;Quiz Answers.&quot;<H3><FONT COLOR="#000077"><B>Quiz</B></FONT></H3><DL>	<DD>1. What MFC class is used to manage edit controls?<BR>	<BR>	2. What is the difference between an MLE and an SLE?<BR>	<BR>	3. How are DDV and DDX routines used?<BR>	<BR>	4. What member function do you call to transfer data to and from your dialog box	controls?<BR>	<BR>	5. What function is used to retrieve text from an edit control?<BR>	<BR>	6. What function is used to set text in an edit control?<BR>	<BR>	7. What property is used to hide user input in an edit control by replacing it with	asterisks?<BR>	<BR>	8. What keystroke is used to paste text into an edit control?<BR>	<BR>	9. What keystroke is used to copy text from an edit control?<BR>	<BR>	10. What keystroke is used to cut text from an edit control?</DL><H3><FONT COLOR="#000077"><B>Exercises</B></FONT></H3><DL>	<DD>1. Set the maximum length for the text entered in the edit control in the Test	project to five characters.<BR>	<BR>	2. Change the Test project so that the edit control is used to store an integer value	instead of a string.<FONT COLOR="#000077"></FONT></DL><CENTER><P><HR><A HREF="../ch05/ch05.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch07/ch07.htm"><IMGSRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR><BR><BR><IMG SRC="../button/corp.gif" WIDTH="284" HEIGHT="45" ALIGN="BOTTOM" ALT="Macmillan Computer Publishing USA"BORDER="0"></P><P>&#169; <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. Allrights reserved.</CENTER></BODY></HTML>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -