📄 ch10.htm
字号:
4: m_ptFrom = ptFrom;
5: m_ptTo = ptTo;
6: m_crColor = crColor;
7: }
</PRE>
<DL>
<DT></DT>
<DD><B>7. </B>Scroll down to the Draw function and modify it as in Listing 10.15.
<P>
</DL>
<H4>LISTING 10.15. THE MODIFIED Draw FUNCTION.</H4>
<PRE> 1: void CLine::Draw(CDC * pDC)
2: {
3: // Create a pen
4: CPen lpen (PS_SOLID, 1, m_crColor);
5:
6: // Set the new pen as the drawing object
7: CPen* pOldPen = pDC->SelectObject(&lpen);
8: // Draw the line
9: pDC->MoveTo(m_ptFrom);
10: pDC->LineTo(m_ptTo);
11: // Reset the previous pen
12: pDC->SelectObject(pOldPen);
13: }
</PRE>
<DL>
<DT></DT>
<DD><B>8. </B>Scroll down to the Serialize function and modify it as in Listing 10.16.
<P>
</DL>
<H4>LISTING 10.16. THE MODIFIED Serialize FUNCTION.</H4>
<PRE> 1: void CLine::Serialize(CArchive &ar)
2: {
3: CObject::Serialize(ar);
4:
5: if (ar.IsStoring())
6: ar << m_ptFrom << m_ptTo << (DWORD) m_crColor;
7: else
8: ar >> m_ptFrom >> m_ptTo >> (DWORD) m_crColor;
9: }
</PRE>
<P>The only part of any of these steps that should be a surprise is that you are
capturing the return value from the SelectObject function when you are specifying
the pen to use in drawing the lines. You didn't do this last week. The return value
from the SelectObject function is the pen that was in use before you changed it.
This way, you can use the previous pen to restore it to the device context when you
are done drawing.</P>
<P>
<H3><A NAME="Heading12"></A>Adding Color to the Document</H3>
<P>The changes that you need to make to the CDay10Doc class are just slightly more
extensive than those made to the CLine class. You need to add a member variable to
hold the current color and a color table to convert color IDs into RGB values. You
need to initialize the current color variable in the OnNewDocument function. Then,
you need to modify the AddLine function to add the current color to the CLine constructor.
Finally, you add a function to return the current color. That's all that you need
to do for now until you start adding menu message handlers for setting the current
color. To do these things, follow these steps:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Select the CDay10Doc class in the Class View tab on the workspace pane.
Right-click the mouse and choose Add Member Variable from the pop-up menu.
<P>
<DT></DT>
<DD><B>2. </B>Specify the variable type as UINT, the name as m_nColor, and the access
as private. Click OK to add the variable.
<P>
<DT></DT>
<DD><B>3. </B>Repeat step 1.
<P>
<DT></DT>
<DD><B>4. </B>Specify the variable type as "static const COLORREF," the
name as m_crColors[8], and the access as public.
<P>
<DT></DT>
<DD><B>5. </B>Open the CDay10Doc source code (Day10Doc.cpp) and add the population
of the m_crColors color table as in Listing 10.17.
<P>
</DL>
<H4>LISTING 10.17. THE COLOR TABLE SPECIFICATION.</H4>
<PRE> 1: //}}AFX_MSG_MAP
2: END_MESSAGE_MAP()
3:
4: const COLORREF CDay10Doc::m_crColors[8] = {
5: RGB( 0, 0, 0), // Black
6: RGB( 0, 0, 255), // Blue
7: RGB( 0, 255, 0), // Green
8: RGB( 0, 255, 255), // Cyan
9: RGB( 255, 0, 0), // Red
10: RGB( 255, 0, 255), // Magenta
11: RGB( 255, 255, 0), // Yellow
12: RGB( 255, 255, 255) // White
13: };
14:
15: ////////////////////////////////////////////////////////////////////// 16: // CDay10Doc construction/destruction
17:
</PRE>
<PRE>18: CDay10Doc::CDay10Doc()
</PRE>
<PRE>19: .
20: .
21: .
22: }
</PRE>
<DL>
<DT></DT>
<DD><B>6. </B>Scroll down to the OnNewDocument function and edit it as in Listing
10.18.
<P>
</DL>
<H4>LISTING 10.18. THE MODIFIED OnNewDocument FUNCTION.</H4>
<PRE> 1: BOOL CDay10Doc::OnNewDocument()
2: {
3: if (!CDocument::OnNewDocument())
4: return FALSE;
5:
6: // TODO: add reinitialization code here
7: // (SDI documents will reuse this document)
8:
9: ///////////////////////
10: // MY CODE STARTS HERE
11: ///////////////////////
12:
13: // Initialize the color to black
14: m_nColor = ID_COLOR_BLACK - ID_COLOR_BLACK;
15:
16: ///////////////////////
17: // MY CODE ENDS HERE
18: ///////////////////////
19:
20: return TRUE;
21: }
</PRE>
<DL>
<DT></DT>
<DD><B>7. </B>Scroll down to the AddLine function, and modify it as in Listing 10.19.
<P>
</DL>
<H4>LISTING 10.19. THE MODIFIED AddLine FUNCTION.</H4>
<PRE> 1: CLine * CDay10Doc::AddLine(CPoint ptFrom, CPoint ptTo)
2: {
3: // Create a new CLine object
4: CLine *pLine = new CLine(ptFrom, ptTo, m_crColors[m_nColor]);
5: try
6: {
7: // Add the new line to the object array
8: m_oaLines.Add(pLine);
9: // Mark the document as dirty
10: SetModifiedFlag();
11: }
12: // Did we run into a memory exception?
13: catch (CMemoryException* perr)
14: {
15: // Display a message for the user, giving him or her the
16: // bad news
17: AfxMessageBox("Out of memory", MB_ICONSTOP | MB_OK);
18: // Did we create a line object?
19: if (pLine)
20: {
21: // Delete it
22: delete pLine;
23: pLine = NULL;
24: }
25: // Delete the exception object
26: perr->Delete();
27: }
28: return pLine;
29: }
</PRE>
<DL>
<DT></DT>
<DD><B>8. </B>Add a new member function to the CDay10Doc class. Specify the function
type as UINT, the declaration as GetColor, and the access as public.
<P>
<DT></DT>
<DD><B>9. </B>Edit the GetColor function, adding the code in Listing 10.20.
<P>
</DL>
<H4>LISTING 10.20. THE GetColor FUNCTION.</H4>
<PRE>1: UINT CDay10Doc::GetColor()
</PRE>
<PRE>2: {
</PRE>
<PRE>3: // Return the current color
4: return ID_COLOR_BLACK + m_nColor;
5: }
</PRE>
<P>In the OnNewDocument and the GetColor functions, the color is added and subtracted
from ID_COLOR_BLACK. This is the lowest numbered color menu ID when you add the menu
entries. These calculations maintain the variable as a number between 0 and 7, but
when working with the menus, they allow comparison with the actual menu IDs.</P>
<P>
<H3><A NAME="Heading13"></A>Modifying the Menu</H3>
<P>Now comes the fun part. You need to add a new pull-down menu to the main menu.
You need to add menu entries for all the colors in the color table. You need to add
message handlers for all the color menu entries. Finally, you need to add event handlers
to check the menu entry that is the current color. To do all of this, follow these
steps:</P>
<DL>
<DT></DT>
<DD><B>1. </B>Select the Resource View tab in the workspace pane. Expand the tree
so that you can see the contents of the Menu folder. Double-click the menu resource.
<P>
<DT></DT>
<DD><B>2. </B>Grab the blank top-level menu (at the right end of the menu bar) and
drag it to the left, dropping it in front of the View menu entry.
<P>
<DT></DT>
<DD><B>3. </B>Open the properties for the blank menu entry. Specify the caption as
&Color. Close the properties dialog.
<P>
<DT></DT>
<DD><B>4. </B>Add submenu entries below the Color top-level menu. Specify the submenus
in order, setting their properties as specified in Table 10.2. You should wind up
with a menu looking like Figure 10.7.
<P>
</DL>
<P><A HREF="javascript:popUp('10fig11.gif')"><B>FIGURE 10.7.</B></A><B> </B><I>The
Color menu as designed.</I></P>
<P>
<H4>TABLE 10.2. MENU 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">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_BLACK</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Black</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_BLUE</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">B&lue</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_GREEN</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Green</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_CYAN</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Cyan</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_RED</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Red</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_MAGENTA</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Magenta</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_YELLOW</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT"></TD>
<TD ALIGN="LEFT">Caption</TD>
<TD ALIGN="LEFT">&Yellow</TD>
</TR>
<TR ALIGN="LEFT" VALIGN="TOP">
<TD ALIGN="LEFT">Menu Entry</TD>
<TD ALIGN="LEFT">ID</TD>
<TD ALIGN="LEFT">ID_COLOR_WHITE</TD>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -