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

📄 ch19.htm

📁 visual c plus plus its a book for how to learn c plus plus in just 21 days its an easy way to lea
💻 HTM
📖 第 1 页 / 共 4 页
字号:
than with code.</P><P>Adding a property page to your control is pretty easy. If you select the Resourcesview tab in the workspace and expand the dialog folder, you'll see a dialog for yourcontrol's property page already in the folder. Open this dialog, and you'll findthat it's a standard dialog window that you can design using the standard controlsavailable in the dialog designer. To design the property page for your sample control,lay out the property page dialog as shown in Figure 19.4, using the property settingsin Table 19.2.</P><P><A HREF="javascript:popUp('19fig04.gif')"><B>FIGURE 19.4.</B></A><B> </B><I>Thecontrol property page layout.</I></P><P><H4>TABLE 19.2. CONTROL PROPERTY SETTINGS.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Object</TD>		<TD ALIGN="LEFT">Property</TD>		<TD ALIGN="LEFT">Setting</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"></TD>		<TD ALIGN="LEFT">Caption</TD>		<TD ALIGN="LEFT">Maximum Number of Squiggles:</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box</TD>		<TD ALIGN="LEFT">ID</TD>		<TD ALIGN="LEFT">IDC_ENBRSQUIG</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"></TD>		<TD ALIGN="LEFT">Caption</TD>		<TD ALIGN="LEFT">Maximum Length of Squiggles:</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Edit Box</TD>		<TD ALIGN="LEFT">ID</TD>		<TD ALIGN="LEFT">IDC_ELENSQUIG</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Check Box</TD>		<TD ALIGN="LEFT">ID</TD>		<TD ALIGN="LEFT">IDC_CMAINTDRAW</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"></TD>		<TD ALIGN="LEFT">Caption</TD>		<TD ALIGN="LEFT">Maintain Current Drawing</TD>	</TR></TABLE></P><P>Once you add all the controls and specify their properties, open the Class Wizardto add variables for these controls. When you add a variable to one of the controlson the property page dialog, you'll notice an additional combo box on the Add MemberVariable dialog. This new combo box is for the external name of the property thatthe variable should be tied to in the control. The drop-down list on this combo boxis a list of all of the standard properties that you might want to tie the propertypage control to, but if you are tying it to a custom property, you have to enterthe property name yourself, as shown in Figure 19.5.</P><P><A HREF="javascript:popUp('19fig05.gif')"><B>FIGURE 19.5.</B></A><B> </B><I>TheAdd Member Variable dialog.</I></P><P>Add variables to the controls on the property page for your control, tying themto the control's properties, as specified in Table 19.3.</P><P><H4>TABLE 19.3. 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>		<TD ALIGN="LEFT"><I>Property</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_CMAINTDRAW</TD>		<TD ALIGN="LEFT">m_bKeepDrawing</TD>		<TD ALIGN="LEFT">Value</TD>		<TD ALIGN="LEFT">BOOL</TD>		<TD ALIGN="LEFT">KeepCurrentDrawing</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_ELENSQUIG</TD>		<TD ALIGN="LEFT">m_iLenSquig</TD>		<TD ALIGN="LEFT">Value</TD>		<TD ALIGN="LEFT">int</TD>		<TD ALIGN="LEFT">SquiggleLength</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_ENBRSQUIG</TD>		<TD ALIGN="LEFT">m_iNbrSquiggles</TD>		<TD ALIGN="LEFT">Value</TD>		<TD ALIGN="LEFT">int</TD>		<TD ALIGN="LEFT">NumberSquiggles</TD>	</TR></TABLE></P><P>Click the OK button to add all these variables to the control property page class.</P><P><H3><A NAME="Heading10"></A>Adding Basic Control Functionality</H3><P>The basic functionality that your control needs is the ability to respond to mouseclicks by generating a new drawing. To control this behavior, you'll add a secondboolean variable to the control class so that the OnDraw function knows that a mouseclick has been triggered. The easiest place to get the drawing area of the controlis the OnDraw function, so this is where the new drawing needs to be generated. Doyou want the control to generate a new drawing every time the user moves the applicationusing your control in front of another application? Probably not. You will most likelywant a greater amount of control over the behavior of the control, so it makes senseto add this second boolean variable. Add a member variable to the control class (CSquiggleCtrl),specifying the variable type as BOOL, the variable name as m_bGenNewDrawing, andthe variables access as private.</P><P>Before you start adding the code to perform all the various tasks, it's importantthat you initialize all the member variables in the control class. This consistsof the member variable property, m_keepCurrentDrawing, and the member variable thatyou just added, m_bGenNewDrawing. You'll want your control to generate a new drawingright off the bat, and you probably don't want it to maintain any drawings, unlessthe container application explicitly specifies that a drawing is to be maintained.You'll set these two variables accordingly in the control class constructor, as shownin Listing 19.8.</P><P><H4>LISTING 19.8. THE CSquiggleCtrl CONSTRUCTOR.</H4><PRE>1: CSquiggleCtrl::CSquiggleCtrl()2: {3:     InitializeIIDs(&amp;IID_DSquiggle, &amp;IID_DSquiggleEvents);4:5:     // TODO: Initialize your control's instance data here.6:     // Initialize the variables7:     m_bGenNewDrawing = TRUE;8:     m_keepCurrentDrawing = FALSE;9: }</PRE><P>Next, you'll add the code to generate and display the squiggle drawings. The placeto add this functionality is the OnDraw function in the control class. This functionis called every time that the control needs to draw itself, whether it was hiddenor something triggered the redrawing by calling the Invalidate function on the control.Once in the OnDraw function, you'll determine whether you need to generate a newdrawing or just draw the existing drawing. Another thing to keep in mind is thatyou are responsible for drawing the entire area that the control occupies. This meansthat you need to draw the background of the squiggle drawing, or else the squiggleswill be drawn on top of whatever was displayed in that same spot on the screen. (Whoknows? That might be the effect you are looking for.) To add this functionality toyour control, edit the OnDraw function in the control class, adding the code in Listing19.9.</P><P><H4>LISTING 19.9. THE CSquiggleCtrl OnDraw FUNCTION.</H4><PRE> 1: void CSquiggleCtrl::OnDraw( 2:             CDC* pdc, const CRect&amp; rcBounds, const CRect&amp; rcInvalid) 3: { 4:     // TODO: Replace the following code with your own drawing code. 5:     //pdc-&gt;FillRect(rcBounds, CBrush::FromHandle((HBRUSH)                &Acirc;GetStockObject(WHITE_BRUSH))); 6:     //pdc-&gt;Ellipse(rcBounds); 7:     // Do we need to generate a new drawing? 8:     if (m_bGenNewDrawing) 9:     {10:         // Set the drawing area for the new drawing11:         m_maDrawing.SetRect(rcBounds);12:         // Clear out the old drawing13:         m_maDrawing.ClearDrawing();14:         // Generate the new drawing15:         m_maDrawing.NewDrawing();16:         // Reset the control flag17:         m_bGenNewDrawing = FALSE;18:     }19:     // Fill in the background20:     pdc-&gt;FillRect(rcBounds, 21:         CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));22:     // Draw the squiggle drawing23:     m_maDrawing.Draw(pdc);24: }</PRE><P>Finally, you'll trigger the control to generate a new drawing whenever the controlis clicked. This requires adding an event handler for the control's OnClick event.First, however, you'll add a stock method to the control to make sure that it receivesthe OnClick event message. To add this stock method, open the Class Wizard and selectthe Automation tab. Add a new method to the control class, selecting the DoClickmethod from the drop-down list of stock methods that can be added to your control,as shown in Figure 19.6. Click the OK button to add the method to your control, andthen select the Message Maps tab in the Class Wizard. Select the OnClick event messagefrom the list of available event messages, and add a function to handle this eventmessage. Edit the code for the OnClick event handler, adding the code in Listing19.10.</P><P><A HREF="javascript:popUp('19fig06.gif')"><B>FIGURE 19.6.</B></A><B> </B><I>TheAdd Method dialog.</I></P><P><H4>LISTING 19.10. THE CSquiggleCtrl OnClick FUNCTION.</H4><PRE> 1: void CSquiggleCtrl::OnClick(USHORT iButton) 2: { 3:     // TODO: Add your specialized code here and/or call the base class 4:     // Can we generate a new drawing? 5:     if (!m_keepCurrentDrawing) 6:     { 7:         // Set the flag so a new drawing will be generated 8:         m_bGenNewDrawing = TRUE; 9:         // Invalidate the control to trigger the OnDraw function10:         Invalidate();11:     }12:     COleControl::OnClick(iButton);13: }</PRE><P>In the OnClick function, you check to see whether you could generate a new drawingor maintain the current drawing. If you could generate a new drawing, you set them_bGenNewDrawing flag to TRUE and invalidated the control, which triggers the OnDrawfunction.</P><P><H3><A NAME="Heading11"></A>Adding Methods</H3><P>Remember the functionality that you are going to give your control: One of thefunctions is loading a squiggle drawing created with the version of the Squigglemodule that you created on Day 16. To add this functionality, you'll add a methodto the control that the container application can call to pass a filename to be loaded.You've already added one method to your application, a stock method. Adding a custommethod is similar, but you have to provide a little more information to the Add Methoddialog.</P><P>In the method to load an existing drawing, you'll create a CFile object for thefilename that was passed as a parameter. The CFile constructor will take the filenameand the flag CFile::modeRead to let it know that you are opening the file for readingonly. Once you create the CFile object, you'll create a CArchive object to read thefile. The CArchive constructor will take the CFile object that you just created andthe CArchive::load flag to tell it that it needs to load the file. At this point,you can pass the CArchive object to the drawing object's Serialize function and letit read and load the drawing. Once the drawing is loaded, you need to display thedrawing by invalidating the control. Before you invalidate the control, you probablywant to make sure that the m_bGenNewDrawing flag is set to FALSE so that the drawingyou just loaded won't be overwritten.</P><P>To add this functionality to your control, open the Class Wizard and select theAutomation tab. Click the Add Method button to add a custom method. Enter the externalmethod name in the first combo box; in this case, call it LoadDrawing. The internalname will automatically be generated based on the external name you entered. Next,specify the return type as BOOL so that you can let the container application know

⌨️ 快捷键说明

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