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

📄 ch18.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 页 / 共 5 页
字号:
		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_CBTHREAD1		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">Thread &amp;1		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Check Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_CBONIDLE2		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">On Idle &amp;Thread 2		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">Check Box		</TD>		<TD ALIGN="LEFT">ID		</TD>		<TD ALIGN="LEFT">IDC_CBTHREAD2		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">			<P>		</TD>		<TD ALIGN="LEFT">Caption		</TD>		<TD ALIGN="LEFT">Thread &amp;2		</TD>	</TR></TABLE></P><P><A HREF="javascript:popUp('18fig06.gif')"><B>FIGURE 18.6.</B></A><B> </B><I>Themain window design.</I></P><P>Once you add the check boxes to the window and configure their properties, usethe Class Wizard to add a variable to each of them. Make all of the variables BOOL,and give them names like in Table 18.4.</P><P><H4>TABLE 18.4. 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_CBONIDLE1		</TD>		<TD ALIGN="LEFT">m_bOnIdle1		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">BOOL		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_CBONIDLE2		</TD>		<TD ALIGN="LEFT">m_bOnIdle2		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">BOOL		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_CBTHREAD1		</TD>		<TD ALIGN="LEFT">m_bThread1		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">BOOL		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">IDC_CBTHREAD2		</TD>		<TD ALIGN="LEFT">m_bThread2		</TD>		<TD ALIGN="LEFT">Value		</TD>		<TD ALIGN="LEFT">BOOL		</TD>	</TR></TABLE><H3><A NAME="Heading7"></A>Designing Spinners</H3><P>Before you can start adding threads to your application, you'll create the spinningcolor wheel that the threads will operate. Because four of these color wheels willall spin independently of each other, it makes sense to encapsulate all of the functionalityinto a single class. This class will track what color is being drawn, where in thespinning it needs to draw the next line, the size of the color wheel, and the locationof the color wheel on the application window. It will also need a pointer to theview class so that it can get the device context in which it is supposed to drawitself. For the independent spinners, the class will need a pointer to the flag thatwill control whether the spinner is supposed to be spinning.</P><P><PRE>To start the spinner class, create a new generic class, inherited from the CObject base class. Provide the new class with a name that is descriptive of what it will be doing, such as CSpinner.</PRE><H4>Setting Spinner Variables</H4><P>Once you create a new class for your spinner object, you'll add some variablesto the class. To follow good object-oriented design principles, you need to makeall these variables private and add methods to the class to set and retrieve thevalues of each.</P><P>The variables you'll add are</P><P><UL>	<LI>The current color.	<P>	<LI>The current position in the rotation of the color wheel.	<P>	<LI>The size of the color wheel.	<P>	<LI>The position on the application window for the color wheel.	<P>	<LI>The color table from which the colors are picked for drawing in the color wheel.	<P>	<LI>A pointer to the view object so that the spinner can get the device context that	it will need for drawing on the window.	<P>	<LI>A pointer to the check box variable that specifies whether the thread should	be running.</UL><P>You can add all these variables to the spinner class using the names and typesspecified in Table 18.5.</P><P><H4>TABLE 18.5. CSpinner CLASS VARIABLES.</H4><P><TABLE BORDER="1">	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT"><I>Name</I></TD>		<TD ALIGN="LEFT"><I>Type</I></TD>		<TD ALIGN="LEFT"><I>Description</I></TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_crColor		</TD>		<TD ALIGN="LEFT">int		</TD>		<TD ALIGN="LEFT">The current color from the color table.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_nMinute		</TD>		<TD ALIGN="LEFT">int		</TD>		<TD ALIGN="LEFT">The position in the rotation around the wheel.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_iRadius		</TD>		<TD ALIGN="LEFT">int		</TD>		<TD ALIGN="LEFT">The radius (size) of the wheel.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_pCenter		</TD>		<TD ALIGN="LEFT">CPoint		</TD>		<TD ALIGN="LEFT">The center point of the wheel.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_crColors[8]		</TD>		<TD ALIGN="LEFT">static COLORREF		</TD>		<TD ALIGN="LEFT">The color table with all of the colors to be drawn in the color wheel.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_pViewWnd		</TD>		<TD ALIGN="LEFT">CWnd*		</TD>		<TD ALIGN="LEFT">A pointer to the view object.		</TD>	</TR>	<TR ALIGN="LEFT" VALIGN="TOP">		<TD ALIGN="LEFT">m_bContinue		</TD>		<TD ALIGN="LEFT">BOOL*		</TD>		<TD ALIGN="LEFT">A pointer to the check box variable that specifies whether this thread should be			running.		</TD>	</TR></TABLE></P><P>Once you add all the necessary variables, you need to make sure that your classeither initializes them or provides a suitable means of setting and retrieving thevalues of each. All the integer variables can be initialized as zero, and they'llwork their way up from that point. The pointers should be initialized with NULL.You can do all of this initialization in the class constructor, as in Listing 18.1.</P><P><H4>LISTING 18.1. THE CSpinner CONSTRUCTOR.</H4><PRE> 1: CSpinner::CSpinner() 2: { 3:     // Initialize the position, size, and color 4:     m_iRadius = 0; 5:     m_nMinute = 0; 6:     m_crColor = 0; 7:     // Nullify the pointers 8:     m_pViewWnd = NULL; 9:     m_bContinue = NULL;10: }</PRE><P>For those variables that you need to be able to set and retrieve, your spinnerclass is simple enough that you can write all the set and get functions as inlinefunctions in the class declaration. The color and position will be automaticallycalculated by the spinner object, so you don't need to add set functions for thosetwo variables, but you do need to add set functions for the rest of the variables(not counting the color table). The only variables that you need to retrieve fromthe spinner object are the pointers to the view class and the check box variable.You can add all these functions to the CSpinner class declaration by opening theSpinner header file and adding the inline functions in Listing 18.2.</P><P><H4>LISTING 18.2. THE CSpinner CLASS DECLARATION.</H4><PRE> 1: class CSpinner : public CObject 2: { 3: public: 4:     BOOL* GetContinue() {return m_bContinue;} 5:     void SetContinue(BOOL* bContinue) { m_bContinue = bContinue;} 6:     CWnd* GetViewWnd() { return m_pViewWnd;} 7:     void SetViewWnd(CWnd* pWnd) { m_pViewWnd = pWnd;} 8:     void SetLength(int iLength) { m_iRadius = iLength;} 9:     void SetPoint(CPoint pPoint) { m_pCenter = pPoint;}10:     CSpinner();11:     virtual ~CSpinner();12: 13: private:14:     BOOL* m_bContinue;</PRE><PRE>15:     CWnd* m_pViewWnd;</PRE><PRE>16:     static COLORREF m_crColors[8];17:     int m_iRadius;18:     CPoint m_pCenter;19:     int m_nMinute;20:     int m_crColor;21: };</PRE><P>Now that you have added all the support functions for setting and retrieving thenecessary variables, you need to declare and populate the color table. This willlook just like the color table definition you added to the drawing application onDay 10, &quot;Creating Single Document Interface Applications.&quot; The color tablewill consist of eight RGB values, with each value being either 0 or 255, with everycombination of these two settings. The best place to add this table declaration isin the spinner source code file, just before the class constructor, as in Listing18.3.</P><P><H4>LISTING 18.3. THE CSpinner COLOR TABLE.</H4><PRE> 1: static char THIS_FILE[]=__FILE__; 2: #define new DEBUG_NEW 3: #endif 4:  5: COLORREF CSpinner::m_crColors[8] = { 6:     RGB(   0,   0,   0),    // Black 7:     RGB(   0,   0, 255),    // Blue 8:     RGB(   0, 255,   0),    // Green 9:     RGB(   0, 255, 255),    // Cyan10:     RGB( 255,   0,   0),    // Red11:     RGB( 255,   0, 255),    // Magenta12:     RGB( 255, 255,   0),    // Yellow13:     RGB( 255, 255, 255)        // White14: };15: 16: //////////////////////////////////////////////////////////////////////17: // Construction/Destruction18: //////////////////////////////////////////////////////////////////////19: 20: CSpinner::CSpinner()21: {22:     // Initialize the position, size, and color23:     m_iRadius = 0;24: .25: .26: . </PRE><H4>Drawing the Spinner</H4><P>Now comes the fun part: getting the spinner object to actually spin. To accomplishthis, you'll calculate the new position of the starting and ending points of eachline, set the view port origination point, select the drawing color, and create apen to draw in that color. Once you have all of this, you will be able to draw theline from the starting point to the ending point. Once the line is drawn, you canrestore the pen to what it was before the line was drawn. Next, you'll calculatethe position of the next line to draw before exiting the function.</P><P>To add this functionality to your spinner object, add a member function to theCSpinner class. Specify the type as void, the name as Draw, and the access as public.Edit the function, adding the code in Listing 18.4.</P><P><H4>LISTING 18.4. THE CSpinner Draw FUNCTION.</H4><PRE> 1: void CSpinner::Draw() 2: { 3:     // Get a pointer to the device context 4:     CDC *pDC = m_pViewWnd-&gt;GetDC(); 5:     // Set the mapping mode 6:     pDC-&gt;SetMapMode (MM_LOENGLISH); 7:     // Copy the spinner center 8:     CPoint org = m_pCenter; 9:     CPoint pStartPoint;10:     // Set the starting point

⌨️ 快捷键说明

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