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

📄 ch18.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 5 页
字号:
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Check Box		</TD>
		<TD ALIGN="LEFT">ID		</TD>
		<TD ALIGN="LEFT">IDC_CBONIDLE1		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">			<P>
		</TD>
		<TD ALIGN="LEFT">Caption		</TD>
		<TD ALIGN="LEFT">On &amp;Idle Thread 1		</TD>
	</TR>
	<TR ALIGN="LEFT" VALIGN="TOP">
		<TD ALIGN="LEFT">Check Box		</TD>
		<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>The
main window design.</I></P>

<P>Once you add the check boxes to the window and configure their properties, use
the 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 spinning
color wheel that the threads will operate. Because four of these color wheels will
all spin independently of each other, it makes sense to encapsulate all of the functionality
into a single class. This class will track what color is being drawn, where in the
spinning it needs to draw the next line, the size of the color wheel, and the location
of the color wheel on the application window. It will also need a pointer to the
view class so that it can get the device context in which it is supposed to draw
itself. For the independent spinners, the class will need a pointer to the flag that
will 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 variables
to the class. To follow good object-oriented design principles, you need to make
all these variables private and add methods to the class to set and retrieve the
values 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 types
specified 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 class
either initializes them or provides a suitable means of setting and retrieving the
values of each. All the integer variables can be initialized as zero, and they'll
work 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 spinner
class is simple enough that you can write all the set and get functions as inline
functions in the class declaration. The color and position will be automatically
calculated by the spinner object, so you don't need to add set functions for those
two 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 from
the 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 the
Spinner 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 the
necessary variables, you need to declare and populate the color table. This will
look just like the color table definition you added to the drawing application on
Day 10, &quot;Creating Single Document Interface Applications.&quot; The color table
will consist of eight RGB values, with each value being either 0 or 255, with every
combination of these two settings. The best place to add this table declaration is
in the spinner source code file, just before the class constructor, as in Listing
18.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),    // Cyan
10:     RGB( 255,   0,   0),    // Red
11:     RGB( 255,   0, 255),    // Magenta
12:     RGB( 255, 255,   0),    // Yellow
13:     RGB( 255, 255, 255)        // White
14: };
15: 
16: //////////////////////////////////////////////////////////////////////
17: // Construction/Destruction
18: //////////////////////////////////////////////////////////////////////
19: 
20: CSpinner::CSpinner()
21: {
22:     // Initialize the position, size, and color
23:     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 accomplish

⌨️ 快捷键说明

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