📄 ch21.htm
字号:
<pre><font color="#008000"> ::OleTranslateColor(m_clrForeColor, NULL, &fore);</font></pre>
<P>If Dots is FALSE, the die should be drawn with a number on it. Add the code in Listing 21.12 to <font color="#008000">OnDraw()</font>. Again the SDK
functions do the same job as the similarly named MFC functions used in the MFC version of DieRoll.</P>
<P><I>Listing 21.12—CDieRoll::OnDraw()—Draw a Number</I></P>
<pre><font color="#008000"> if (!m_bDots)</font></pre>
<pre><font
color="#008000"> {</font></pre>
<pre><font color="#008000"> _TCHAR val[20]; //character representation of the short value</font></pre>
<pre><font color="#008000"> _itot(m_sNumber, val, 10);</font></pre>
<pre><font color="#008000">
::SetTextColor(di.hdcDraw, fore);</font></pre>
<pre><font color="#008000"> ::ExtTextOut(di.hdcDraw, 0, 0, ETO_OPAQUE, </font></pre>
<pre><font color="#008000"> (RECT *)di.prcBounds, val, _tcslen(val), NULL ); </font></pre>
<pre><font
color="#008000"> }</font></pre>
<P>The code that draws dots is in Listing 21.13. Add it to <font color="#008000">OnDraw()</font> to complete the function. This code is long, but was explained in <A HREF="index17.htm" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/index17.htm" target="text">Chapter 17</A>,
"Building an ActiveX Control." As in the rest of <font color="#008000">OnDraw(),</font> MFC function calls have been replaced with SDK calls.</P>
<P><I>Listing 21.12—CDieRoll::OnDraw()—Draw Dots</I></P>
<pre><font color="#008000">
else</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> //dots are 4 units wide and high, one unit from the edge</font></pre>
<pre><font color="#008000"> int Xunit = width/16;</font></pre>
<pre><font
color="#008000"> int Yunit = height/16;</font></pre>
<pre><font color="#008000"> int Xleft = width%16;</font></pre>
<pre><font color="#008000"> int Yleft = height%16;</font></pre>
<pre><font color="#008000"> // adjust top
left by amount left over</font></pre>
<pre><font color="#008000"> int Top = di.prcBounds->top + Yleft/2;</font></pre>
<pre><font color="#008000"> int Left = di.prcBounds->left + Xleft/2; </font></pre>
<pre><font color="#008000">
HBRUSH forebrush;</font></pre>
<pre><font color="#008000"> forebrush = ::CreateSolidBrush(fore);</font></pre>
<pre><font color="#008000"> HBRUSH savebrush = (HBRUSH)::SelectObject(di.hdcDraw, forebrush);</font></pre>
<pre><font
color="#008000"> switch(m_sNumber)</font></pre>
<pre><font color="#008000"> {</font></pre>
<pre><font color="#008000"> case 1:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+6*Xunit,
Top+6*Yunit,</font></pre>
<pre><font color="#008000"> Left+10*Xunit, Top + 10*Yunit); //center</font></pre>
<pre><font color="#008000"> break;</font></pre>
<pre><font color="#008000"> case 2:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 5*Yunit); //upper left</font></pre>
<pre><font color="#008000">
::Ellipse(di.hdcDraw, Left+11*Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 15*Yunit); //lower right</font></pre>
<pre><font color="#008000"> break;</font></pre>
<pre><font
color="#008000"> case 3:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 5*Yunit); //upper
left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+6*Xunit, Top+6*Yunit,</font></pre>
<pre><font color="#008000"> Left+10*Xunit, Top + 10*Yunit); //center</font></pre>
<pre><font
color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 15*Yunit); //lower right</font></pre>
<pre><font color="#008000">
break;</font></pre>
<pre><font color="#008000"> case 4:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top +
5*Yunit); //upper left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 5*Yunit); //upper right</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 15*Yunit); //lower left</font></pre>
<pre><font color="#008000">
::Ellipse(di.hdcDraw, Left+11*Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 15*Yunit); //lower right</font></pre>
<pre><font color="#008000"> break;</font></pre>
<pre><font
color="#008000"> case 5:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 5*Yunit); //upper
left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 5*Yunit); //upper right</font></pre>
<pre><font
color="#008000"> ::Ellipse(di.hdcDraw, Left+6*Xunit, Top+6*Yunit,</font></pre>
<pre><font color="#008000"> Left+10*Xunit, Top + 10*Yunit); //center</font></pre>
<pre><font color="#008000">
::Ellipse(di.hdcDraw, Left+Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 15*Yunit); //lower left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit,
Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 15*Yunit); //lower right</font></pre>
<pre><font color="#008000"> break;</font></pre>
<pre><font color="#008000"> case
6:</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 5*Yunit); //upper left</font></pre>
<pre><font color="#008000">
::Ellipse(di.hdcDraw, Left+11*Xunit, Top+Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 5*Yunit); //upper right</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit,
Top+6*Yunit,</font></pre>
<pre><font color="#008000"> Left+5*Xunit, Top + 10*Yunit); //center left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit, Top+6*Yunit,</font></pre>
<pre><font
color="#008000"> Left+15*Xunit, Top + 10*Yunit); //center right</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000">
Left+5*Xunit, Top + 15*Yunit); //lower left</font></pre>
<pre><font color="#008000"> ::Ellipse(di.hdcDraw, Left+11*Xunit, Top+11*Yunit,</font></pre>
<pre><font color="#008000"> Left+15*Xunit, Top + 15*Yunit); //lower
right</font></pre>
<pre><font color="#008000"> break;</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> ::SelectObject(di.hdcDraw, savebrush);</font></pre>
<pre><font color="#008000">
::DeleteObject(forebrush);</font></pre>
<pre><font color="#008000"> }</font></pre>
<pre><font color="#008000"> return S_OK;</font></pre>
<pre><font color="#008000">}</font></pre>
<H3><B>Persistence and a Property Page</B></H3>
<P>The properties have
been added to the control and used in the drawing of the control. Now all that remains is to make the properties persistent and to add a property page.</P>
<P><B>Adding a Property Page </B></P>
<P>Choose <U>I</U>nsert, New <U>A</U>TL Object from the menu
bar to bring up the ATL Object Wizard. Select Controls in the left pane and Property Page in the right pane, then click <U>N</U>ext. On the Names tab, enter DieRollPPG for the <U>S</U>hort Name, and click the Strings tab (the sttings on the Attributes tab
will not be changed.) Enter General for the <U>T</U>itle and DieRoll Property Page for the <U>D</U>oc String. Blank out the <U>H</U>elpfile Name. Click OK to add the property page to the project. </P>
<P>Switch to ResourceView in the Workspace pane, and
open the dialog IDD_DIEROLLPPG. Add a check box with the resource ID <font color="#008000">IDC_DOTS</font> and the caption Display Dot Pattern and an edit box with the resource ID <font color="#008000">IDC_IMAGE</font> labelled Image URL, as shown in
Figure 21.12.</P>
<A HREF="KKfig12.gif" tppabs="http://www.mcp.com/814147200/0-7897/0-7897-1145-1/figs/ch21/KKfig12.gif"><b>Fig. 21.12</b></A>
<P><I>Add two controls to the property page.</I></P>
<P>At the top of DieRollPPG.h, add this line:</P>
<pre><font color="#008000">#include "DieRollControl.h"</font></pre>
<P>You need to connect the controls on this property page to properties of the dieroll control. The first step is to add three lines to the message map in DieRollPPG.h so that it resembles Listing 21.13.</P>
<P><I>Listing
21.13—DieRollPPG.h—Message Map</I></P>
<pre><font color="#008000">BEGIN_MSG_MAP(CDieRollPPG)</font></pre>
<pre><font color="#008000"> MESSAGE_HANDLER(WM_INITDIALOG, OnInitDialog)</font></pre>
<pre><font color="#008000">
COMMAND_HANDLER(IDC_DOTS, BN_CLICKED, OnDotsChanged)</font></pre>
<pre><font color="#008000"> COMMAND_HANDLER(IDC_IMAGE, EN_CHANGE, OnImageChanged)</font></pre>
<pre><font color="#008000">
CHAIN_MSG_MAP(IPropertyPageImpl<CDieRollPPG>)</font></pre>
<pre><font color="#008000">END_MSG_MAP()</font></pre>
<P>These new lines ensure that <font color="#008000">OnInitDialog()</font> will be called when the dialog box is initialized, and <font
color="#008000">OnDotsChanged()</font> or <font color="#008000">OnImageChanged()</font> will be called whenever <font color="#008000">Dots</font> or <font color="#008000">Image</font> are changed (the other properties don't have put methods and so can't be
changed.) </P>
<P>Add the code in Listing 21.14 to the header file to declare and implement <font color="#008000">OnInitDialog()</font>.</P>
<P><I>Listing 21.14—DieRollPPG.h—CDieRollPPG::OnInitDialog()</I></P>
<pre><font color="#008000">
LRESULT OnInitDialog(UINT uMsg, WPARAM wParam, LPARAM lParam, </font></pre>
<p
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -