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

📄 ch13.htm

📁 这是一本关于VC++自学教程的书籍 对于初学者以及编程有一定基础的人均有帮助
💻 HTM
📖 第 1 页 / 共 5 页
字号:
buttons, as shown in Figure 13.2.</P><P><A HREF="javascript:popUp('13fig02tif.gif')"><B>FIGURE 13.2.</B></A><B> </B><I>Thewindow designer for an SDI appli-cation.</I></P><P><I></I><H4>Designing Your Application Window</H4><P>After you create an SDI or MDI application where the view class is based on theCFormView class, you need to design your application view. Designing the view ismuch like designing the window layout for a dialog window, but you don't need toworry about including any buttons to close the window while either saving or cancelingthe work done by the user. With an SDI or MDI application, the functionality to saveand exit the window is traditionally located on the application menus or on the toolbar.As a result, you need to include only the controls for the function that your applicationwindow will perform.</P><BLOCKQUOTE>	<P><HR><STRONG>NOTE:</STRONG> If you are building dialog-style applications, the AppWizard doesn't	provide any serialization code in your application shell. If you need to serialize	a dialog-style application, you'll need to add all this code yourself.<HR></BLOCKQUOTE><P>For the sample application that you are building today, lay out controls on thewindow canvas as shown in Figure 13.3 using the control properties listed in Table13.1.</P><P><A HREF="javascript:popUp('13fig03tif.gif')"><B>FIGURE 13.3.</B></A><B> </B><I>Thesample application window layout.</I></P><P><I></I><H4>TABLE 13.1. CONTROL PROPERTY SETTINGS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Object</I></TD>		<TD ALIGN="LEFT"><I>Property</I></TD>		<TD ALIGN="LEFT"><I>Setting</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<H4>Static Text		</TD>		<TD ALIGN="LEFT">			<H4>ID		</TD>		<TD ALIGN="LEFT">			<H4>IDC_STATIC		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Name:		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_ENAME		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Static Text		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_STATIC		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Age		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_EAGE		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Static Text		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_STATIC		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">Marital Status:		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Radio Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_RSINGLE		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Single		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Group		</TD>		<TD ALIGN="LEFT">Checked		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Radio Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_RMARRIED		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Married		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Radio Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_RDIVORCED		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Divorced		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Radio Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_RWIDOW		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Widowed		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Check Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_CBEMPLOYED		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Employed		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_BFIRST		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;First		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_BPREV		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Previous		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_BNEXT		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">Nex&amp;t		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Button		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_BLAST		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">&amp;Last		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Static Text		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_SPOSITION		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">Record 0 of 0		</TD>	</TR></TABLE></P><P>When you were developing dialog-style applications or windows, you attached variablesto the controls on the window in the dialog class. However, with an SDI or MDI application,which class do you create the variables in? Because the UpdateData function is amember of the CWnd class, and the view class is descended from the CWnd class, althoughthe document is not, then the view class is the most logical place to add the variablesthat you will attach to the controls you placed on the window.</P><P>To attach variables to the controls in your sample application, open the ClassWizard and add variables to the controls, specifying that the place to add them isthe view class (in this case, CSerializeView). For the sample application, add thevariables in Table 13.2 to the controls specified.</P><P><H4>TABLE 13.2. CONTROL VARIABLES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Object</I></TD>		<TD ALIGN="LEFT"><I>Name</I></TD>		<TD ALIGN="LEFT"><I>Category</I></TD>		<TD ALIGN="LEFT"><I>Type</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_CBEMPLOYED		</TD>		<TD ALIGN="LEFT">m_bEmployed		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">BOOL		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_EAGE		</TD>		<TD ALIGN="LEFT">m_iAge		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">int		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_ENAME		</TD>		<TD ALIGN="LEFT">m_sName		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">CString		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_RSINGLE		</TD>		<TD ALIGN="LEFT">m_iMaritalStatus		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">int		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_SPOSITION		</TD>		<TD ALIGN="LEFT">m_sPosition		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">CString		</TD>	</TR></TABLE></P><P>If you examine the source code for the view class, you will notice that thereis no OnDraw function. If you are using the CFormView ancestor class for your SDIor MDI application, you don't need to worry about the OnDraw function. Instead, youtreat the view class very much as you would the dialog class in a dialog window ordialog-style application. The primary difference is that the data that you need touse to populate the controls on the window are not in the view class, but in thedocument class. As a result, you need to build the interaction between these twoclasses to pass the data for the controls back and forth.</P><P><H3><A NAME="Heading8"></A>Creating a Serializable Class</H3><P>When you create a form-based application, it is assumed that your applicationwill hold multiple records in the form and that the user will be able to scroll throughthe records to make changes. The user will be able to add additional records or evenremove records from the record set. The challenge at this point in building thisapplication is how you represent this set of records, supporting all the necessaryfunctionality.</P><P>One approach is to create a class that would encapsulate each record, and thenhold these records in an array, much as you did with the drawing application thatyou created and enhanced over the past few days. This class would need to descendfrom the CObject class and would need to contain variables for all the control variablesthat you added to the view class, along with methods to read and write all of thesevariables. Along with adding the methods to set and read all of the variables, youneed to make the class serializable by adding the Serialize function to the class,as well as the two macros that complete the serialization of the class.</P><P><H4>Creating the Basic Class</H4><P>As you may remember from Day 10, when you want to create a new class, you canselect the project in the Class View tab of the workspace pane, right-click the mousebutton, and select New Class from the context menu. This opens the New Class dialog.</P><P>In the New Class dialog, you specify the type of class, whether it's an MFC class,and generic class, or a form class. To create a class that can contain one record'sdata, you most likely want to create a generic class. You'll learn more about howto determine which of these types of classes to create on Day 16, &quot;Creating

⌨️ 快捷键说明

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