?? ch08.htm
字號:
<DT></DT>
<DD><B>5. </B>Arrange the check boxes under the radio buttons.
<P>
<DT></DT>
<DD><B>6. </B>Click the Group box on the Controls floating toolbar, and then click
and drag a group box around the radio buttons. Change the caption to <B>Text Color</B>.
<P>
<DT></DT>
<DD><B>7. </B>Move the OK and Cancel buttons down to the bottom of the dialog box.
<P>
<DT></DT>
<DD><B>8. </B>Select each horizontal group of controls and use Layout, Center in
Dialog, Horizontal to make things neater.
<P>
<DT></DT>
<DD><B>9. </B>Choose Edit, Select All, and then drag all the controls up toward the
top of the dialog box. Shrink the dialog box to fit around the new controls. It should
now resemble Figure 8.15.
<P>
</DL>
<P><A HREF="javascript:popUp('08uvc15.gif')"><B>FIG. 8.15</B></A><B> </B><I>The Options
dialog box for ShowString has been expanded.</I></P>
<BLOCKQUOTE>
<P>
<HR>
<strong>TIP:</strong> If you don't recognize the icons on the Controls toolbar, use the
ToolTips<I>.</I> If you hold the cursor over any of the toolbar buttons, a tip pops
up after a few seconds, telling you what control the button represents.
<HR>
</BLOCKQUOTE>
<P>Finally, set the tab order by choosing Layout, Tab Order and then clicking the
controls, in this order:</P>
<DL>
<DT></DT>
<DD><B>1. </B>IDC_OPTIONS_STRING
<P>
<DT></DT>
<DD><B>2. </B>IDC_OPTIONS_BLACK
<P>
<DT></DT>
<DD><B>3. </B>IDC_OPTIONS_RED
<P>
<DT></DT>
<DD><B>4. </B>IDC_OPTIONS_GREEN
<P>
<DT></DT>
<DD><B>5. </B>IDC_OPTIONS_HORIZCENTER
<P>
<DT></DT>
<DD><B>6. </B>IDC_OPTIONS_VERTCENTER
<P>
<DT></DT>
<DD><B>7. </B>IDOK
<P>
<DT></DT>
<DD><B>8. </B>IDCANCEL
<P>
</DL>
<P>Then click away from the dialog box to leave the two static text controls as positions
9 and 10.</P>
<P>
<H3><A NAME="Heading14"></A>Adding Member Variables to the Dialog Box Class</H3>
<P>Having added controls to the dialog box, you need to add corresponding member
variables to the COptionsDialog class. Bring up ClassWizard, select the Member Variable
tab, and add member variables for each control. Figure 8.16 shows the summary of
the member variables created. The check boxes are connected to BOOL variables; these
member variables are TRUE if the box is selected and FALSE if it isn't. The radio
buttons are handled differently. Only the first--the one with the Group box selected
in its Properties dialog box--is connected to a member variable. That integer is
a zero-based index that indicates which button is selected. In other words, when
the Black button is selected, m_color is 0; when Red is selected, m color is 1; and
when Green is selected, m_color is 2.</P>
<P><A HREF="javascript:popUp('08uvc16.gif')"><B>FIG. 8.16</B></A><B> </B><I>Member
variables in the dialog box class are connected to individual controls or the group
of radio buttons.</I></P>
<P>
<H3><A NAME="Heading15"></A>Adding Member Variables to the Document</H3>
<P>The variables to be added to the document are the same ones that were added to
the dialog box. You add them to the CShowStringDoc class definition in the header
file, to OnNewDocument(), and to Serialize(). Add the lines in Listing 8.10 at the
top of the CShowStringDoc definition in ShowStringDoc.h, replacing the previous definition
of string and GetString(). Make sure that the variables are private and the functions
are public.</P>
<P>
<H4>Listing 8.10  SHOWSTRINGDOC.H--CShowStringDoc Member Variables</H4>
<PRE>private:
CString string;
int color;
BOOL horizcenter;
BOOL vertcenter;
public:
CString GetString() {return string;}
int GetColor() {return color;}
BOOL GetHorizcenter() {return horizcenter;}
</PRE>
<PRE> BOOL GetVertcenter() {return vertcenter;}
</PRE>
<P>As with string, these are private variables with public get functions but no set
functions. All these options should be serialized; the new Serialize() is shown in
Listing 8.11. Change your copy by double-clicking the function name in ClassView
and adding the new code.</P>
<P>
<H4>Listing 8.11  SHOWSTRINGDOC.CPP--Serialize()</H4>
<PRE>void CShowStringDoc::Serialize(CArchive& ar)
{
if (ar.IsStoring())
{
ar << string;
ar << color;
ar << horizcenter;
ar << vertcenter;
}
else
{
ar >> string;
ar >> color;
ar >> horizcenter;
ar >> vertcenter;
}
</PRE>
<PRE>}
</PRE>
<P>Finally, you need to initialize these variables in OnNewDocument(). What are good
defaults for these new member variables? Black text, centered in both directions,
was the old behavior, and it makes sense to use it as the default. The new OnNewDocument()
is shown in Listing 8.12.</P>
<P>
<H4>Listing 8.12  SHOWSTRINGDOC.CPP--OnNewDocument()</H4>
<PRE>BOOL CShowStringDoc::OnNewDocument()
{
if (!CDocument::OnNewDocument())
return FALSE;
string = "Hello, world!";
color = 0; //black
horizcenter = TRUE;
vertcenter = TRUE;
return TRUE;
</PRE>
<PRE>}
</PRE>
<P>Of course, at the moment, users cannot change these member variables from the
defaults. To allow the user to change the variables, you have to change the function
that handles the dialog box.</P>
<P>
<H3><A NAME="Heading16"></A>Changing OnToolsOptions()</H3>
<P>The OnToolsOptions() function sets the values of the dialog box member variables
from the document member variables and then displays the dialog box. If the user
clicks OK, the document member variables are set from the dialog box member variables
and the view is redrawn. Having just added three member variables to the dialog box
and the document, you have three lines to add before the dialog box displays and
then three more to add in the block that's called after OK is clicked. The new OnToolsOptions()
is shown in Listing 8.13.</P>
<P>
<H4>Listing 8.13  SHOWSTRINGDOC.CPP--OnToolsOptions()</H4>
<PRE>void CShowStringDoc::OnToolsOptions()
{
COptionsDialog dlg;
dlg.m_string = string;
dlg.m_color = color;
dlg.m_horizcenter = horizcenter;
dlg.m_vertcenter = vertcenter;
if (dlg.DoModal() == IDOK)
{
string = dlg.m_string;
color = dlg.m_color;
horizcenter = dlg.m_horizcenter;
vertcenter = dlg.m_vertcenter;
SetModifiedFlag();
UpdateAllViews(NULL);
}
</PRE>
<PRE>}
</PRE>
<P>What happens when the user opens the dialog box and changes the value of a control,
say, by deselecting Center Horizontally? The framework--through Dialog Data Exchange
(DDX), as set up by ClassWizard--changes the value of COptionsDialog::m_horizcenter
to FALSE. This code in OnToolsOptions() changes the value of CShowStringDoc::horizcenter
to FALSE. When the user saves the document, Serialize() saves horizcenter. This is
all good, but none of this code actually changes the way the view is drawn. That
involves OnDraw().</P>
<P>
<H3><A NAME="Heading17"></A>Changing OnDraw()</H3>
<P>The single call to DrawText() in OnDraw() becomes a little more complex now. The
document member variables are used to set the view's appearance. Edit OnDraw() by
expanding CShowStringView in the ClassView and double-clicking OnDraw().</P>
<P>The color is set with CDC::SetTextColor() before the call to DrawText(). You should
always save the old text color and restore it when you are finished. The parameter
to SetTextColor() is a COLORREF, and you can directly specify combinations of red,
green, and blue as hex numbers in the form 0x00bbggrr, so that, for example, 0x000000FF
is bright red. Most people prefer to use the RGB macro, which takes hex numbers from
0x0 to 0xFF, specifying the amount of each color; bright red is RGB(FF,0,0), for
instance. Add the lines shown in Listing 8.14 before the call to DrawText() to set
up everything.</P>
<P>
<H4>Listing 8.14  SHOWSTRINGDOC.CPP--OnDraw() Additions Before DrawText()
Call</H4>
<PRE> COLORREF oldcolor;
switch (pDoc->GetColor())
{
case 0:
oldcolor = pDC->SetTextColor(RGB(0,0,0)); //black
break;
case 1:
oldcolor = pDC->SetTextColor(RGB(0xFF,0,0)); //red
break;
case 2:
oldcolor = pDC->SetTextColor(RGB(0,0xFF,0)); //green
break;
</PRE>
<PRE> }
</PRE>
<P>Add this line after the call to DrawText():</P>
<P>
<PRE>pDC->SetTextColor(oldcolor);
</PRE>
<P>There are two approaches to setting the centering flags. The brute-force way is
to list the four possibilities (neither, horizontal, vertical, and both) and have
a different DrawText() statement for each. If you were to add other settings, this
would quickly become unworkable. It's better to set up an integer to hold the DrawText()
flags and OR in each flag, if appropriate. Add the lines shown in Listing 8.15 before
the call to DrawText().</P>
<P>
<H4>Listing 8.15  SHOWSTRINGDOC.CPP--OnDraw() Additions After DrawText()
Call</H4>
<PRE> int DTflags = 0;
if (pDoc->GetHorizcenter())
{
DTflags |= DT_CENTER;
}
if (pDoc->GetVertcenter())
{
DTflags |= (DT_VCENTER|DT_SINGLELINE);
</PRE>
<PRE> }
</PRE>
<P>The call to DrawText() now uses the DTflags variable:</P>
<P>
<PRE>pDC->DrawText(pDoc->GetString(), &rect, DTflags);
</PRE>
<P>Now the settings from the dialog box have made their way to the dialog box class,
to the document, and finally to the view, to actually affect the appearance of the
text string. Build and execute ShowString and then try it. Any surprises? Be sure
to change the text, experiment with various combinations of the centering options,
and try all three colors. l</P>
<H1></H1>
<CENTER>
<P>
<HR>
<A HREF="../ch07/ch07.htm"><IMG SRC="../button/previous.gif" WIDTH="128" HEIGHT="28"
ALIGN="BOTTOM" ALT="Previous chapter" BORDER="0"></A><A HREF="../ch09/ch09.htm"><IMG
SRC="../button/next.gif" WIDTH="128" HEIGHT="28" ALIGN="BOTTOM" ALT="Next chapter"
BORDER="0"></A><A HREF="../index.htm"><IMG SRC="../button/contents.gif" WIDTH="128"
HEIGHT="28" ALIGN="BOTTOM" ALT="Contents" BORDER="0"></A> <BR>
</P>
<P>© <A HREF="../copy.htm">Copyright</A>, Macmillan Computer Publishing. All
rights reserved.
</CENTER>
</BODY>
</HTML>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -