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

📄 ch17.htm

📁 VC 21天 学习VC 的好东西
💻 HTM
📖 第 1 页 / 共 4 页
字号:
an understanding of the different approaches that are necessary with the two DLL
styles.</P>
<P>
<H3><A NAME="Heading5"></A>Creating the MFC Extension DLL</H3>
<P>To convert the library module you created yesterday into an MFC extension DLL,
you need to create a new MFC DLL Wizard project, specifying that the project is an
MFC extension DLL. Copy the source code and header files for the line and drawing
classes into the project directory. Load the files for the line and drawing classes
into the current project. Add the AFX_EXT_CLASS macro to the drawing class. Finally,
move the color table from a global static table to a local variable inside the function
that creates the squiggles.</P>
<P>To create this DLL, start a new project. Give the project a suitable name, such
as ModArtDll, and specify that the project is an MFC AppWizard (DLL) project, as
in Figure 17.1. Once in the DLL Wizard, specify that the DLL is an MFC Extension
DLL, as in Figure 17.2.</P>
<P><A HREF="javascript:popUp('17fig01.gif')"><B>FIGURE 17.1.</B></A><B> </B><I>Selecting
the MFC DLL Wizard.</I></P>

<P>Once you create the DLL shell, open the file explorer and copy the source code
and header files for the line and drawing classes (line.cpp, line.h, ModArt.cpp,
and ModArt.h) from the library module project you created yesterday into the project
directory that you just created. Add all four of these files to the project. Both
classes should appear in the Class View of the workspace pane.</P>
<P><A HREF="javascript:popUp('17fig02.gif')"><B>FIGURE 17.2.</B></A><B> </B><I>Specifying
the DLL type.</I></P>

<P>Open the header file containing the definition of the drawing class. Add the AFX_EXT_CLASS
macro to the class declaration as shown in Listing 17.1. Remove the color table variable
from the class declaration also.</P>
<P>
<H4>LISTING 17.1. THE MODIFIED CModArt CLASS DECLARATION.</H4>
<PRE>1:  class AFX_EXT_CLASS CModArt : public CObject
2:  {
3:  public:
4:      void NewDrawing();
5:      virtual void Serialize(CArchive &amp;ar);
6:      void Draw(CDC *pDC);
7:      void ClearDrawing();
8:      void SetRect(CRect rDrawArea);
9:      CModArt();
10:     virtual ~CModArt();
11:
12: private:
13:     void NewLine();
14:     CRect m_rDrawArea;
15:     CObArray m_oaLines;
16: };
</PRE>
<P>You cannot have public static tables in DLLs, so you cannot declare the color
table as a public, static member of the drawing class, as it was yesterday. As a
result, you'll move it to a local variable in the NewLine member function. Edit the
NewLine function to add this local variable and to reset the function to behave as
it did in its initial incarnation, as in Listing 17.2.</P>
<P>
<H4>LISTING 17.2. THE CModArt NewLine FUNCTION.</H4>
<PRE>1:  void CModArt::NewLine()
2:  {
3:      int lNumLines;
4:      int lCurLine;
5:      int nCurColor;
6:      UINT nCurWidth;
7:      CPoint pTo;
8:      CPoint pFrom;
9:
10:     // Normalize the rectangle before determining the width and height
11:     m_rDrawArea.NormalizeRect();
12:     // get the area width and height
13:     int lWidth = m_rDrawArea.Width();
14:     int lHeight = m_rDrawArea.Height();
15:
16:     COLORREF crColors[8] = {
17:     RGB(   0,   0,   0),    // Black
18:     RGB(   0,   0, 255),    // Blue
19:     RGB(   0, 255,   0),    // Green
20:     RGB(   0, 255, 255),    // Cyan
21:     RGB( 255,   0,   0),    // Red
22:     RGB( 255,   0, 255),    // Magenta
23:     RGB( 255, 255,   0),    // Yellow
24:     RGB( 255, 255, 255)     // White
25:     };
26:
27:     // Determine the number of parts to this squiggle
28:     lNumLines = rand() % 100;
29:     // Are there any parts to this squiggle?
30:     if (lNumLines &gt; 0)
31:     {
32:         // Determine the color
33:         nCurColor = rand() % 8;
34:         // Determine the pen width
35:         nCurWidth = (rand() % 8) + 1;
36:         // Determine the starting point for the squiggle
37:         pFrom.x = (rand() % lWidth) + m_rDrawArea.left;
38:         pFrom.y = (rand() % lHeight) + m_rDrawArea.top;
39:         // Loop through the number of segments
40:         for (lCurLine = 0; lCurLine &lt; lNumLines; lCurLine++)
41:         {
42:             // Determine the end point of the segment
43:             pTo.x = ((rand() % 20) - 10) + pFrom.x;
44:             pTo.y = ((rand() % 20) - 10) + pFrom.y;
45:             // Create a new CLine object
46:             CLine *pLine = new CLine(pFrom, pTo, nCurWidth, 
                        &Acirc;crColors[nCurColor]);
47:             try
48:             {
49: // Add the new line to the object array
50:                 m_oaLines.Add(pLine);
51:             }
52:             // Did we run into a memory exception?
53:             catch (CMemoryException* perr)
54:             {
55:                 // Display a message for the user, giving him the
56:                 // bad news
57:                 AfxMessageBox(&quot;Out of memory&quot;, MB_ICONSTOP | MB_OK);
58:                 // Did we create a line object?
59:                 if (pLine)
60:                 {
61:                     // Delete it
62:                     delete pLine;
63:                     pLine = NULL;
64:                 }
65:                 // Delete the exception object
66:                 perr-&gt;Delete();
67:             }
68:             // Set the starting point to the end point
69:             pFrom = pTo;
70:         }
71:     }
72: }
</PRE>
<P>After making these changes to the drawing class, you are ready to compile your
DLL. Once you compile the DLL, switch over to the file explorer, find the DLL in
the debug subdirectory under the project directory, and copy the DLL to the debug
directory in the test application project directory.</P>
<P>
<H3><A NAME="Heading6"></A>Adapting the Test Application</H3>
<P>To adapt the test application to use the DLL, open the test application project
that you created yesterday. You are going to delete the library module that you created
yesterday and add the LIB file that was created with the DLL. You are also going
to change the header file that is included for the drawing class. After making these
two changes, your test application will be ready to use with the DLL.</P>
<P>To delete the library module from the project, open the File View in the workspace
pane. Select the LIB file from the list of project files and press the Delete key.
Once you delete the library file from the project, select Project | Add To Project
| Files from the main menu. Specify the Library Files (.lib) file type, and then
navigate to the debug directory of the DLL project. Select the LIB file that was
created with your DLL, in this case, ModArtDll.lib. Click OK to add the file to the
project.</P>
<P>Once you add the DLL's LIB file, edit the source-code files for the document,
view, and application classes, changing the include of the drawing class to point
to the project directory of the DLL, as in line 7 in Listing 17.3.</P>
<P>
<H4>LISTING 17.3. THE CTestAppDoc INCLUDES.</H4>
<PRE>1: // TestAppDoc.cpp : implementation of the CTestAppDoc class
2: //
3:
4: #include &quot;stdafx.h&quot;
5: #include &quot;TestApp.h&quot;
6:
7: #include &quot;..\ModArtDll\ModArt.h&quot;
8: #include &quot;TestAppDoc.h&quot;
</PRE>
<P>After making this change to all three source-code files, you are ready to compile
and run your test application. You should find your test application running just
like it did yesterday, only generating shorter squiggles and using only the eight
colors in the color table.</P>
<P>
<H3><A NAME="Heading7"></A>Changing the DLL</H3>
<P>Now that you have the test application running with the DLL, you'll make the same
changes to the DLL that you made to the library module yesterday. You'll increase
the number of squiggles that can be included in a drawing, increase the possible
length of each squiggle, and generate any number of colors for use in the squiggles.</P>
<P>To make these changes, switch back to the DLL project. Increase the number of
lines that may be generated in the NewDrawing member function of the drawing class.
Increase the possible length of the squiggles in the NewLine member function, and
add the random colors back in, as in Listing 17.4.</P>
<P>
<H4>LISTING 17.4. THE MODIFIED CModArt NewLine FUNCTION.</H4>
<PRE>1:  void CModArt::NewLine()
2:  {
3:      int lNumLines;
4:      int lCurLine;
5:  //  int nCurColor;
6:      UINT nCurWidth;
7:      CPoint pTo;
8:      CPoint pFrom;
9:      int cRed;
10:     int cBlue;
11:     int cGreen;
12:
13:     // Normalize the rectangle before determining the width and height
14:     m_rDrawArea.NormalizeRect();
15:     // get the area width and height
16:     int lWidth = m_rDrawArea.Width();
17:     int lHeight = m_rDrawArea.Height();
18:
19: //    COLORREF crColors[8] = {
20: //    RGB(   0,   0,   0),    // Black
21: //    RGB(   0,   0, 255),    // Blue
22: //    RGB(   0, 255,   0),    // Green
23: //    RGB(   0, 255, 255),    // Cyan
24: //    RGB( 255,   0,   0),    // Red
25: //    RGB( 255,   0, 255),    // Magenta
26: //    RGB( 255, 255,   0),    // Yellow
27: //    RGB( 255, 255, 255)     // White
28: //    };
29:
30:     // Determine the number of parts to this squiggle
31:     lNumLines = rand() % 200;
32:     // Are there any parts to this squiggle?
33:     if (lNumLines &gt; 0)
34:     {
35:         // Determine the color
36: //      nCurColor = rand() % 8;
37:         cRed = rand() % 256;
38:         cBlue = rand() % 256;
39:         cGreen = rand() % 256;
40:         // Determine the pen width
41:         nCurWidth = (rand() % 8) + 1;
42:         // Determine the starting point for the squiggle
43:         pFrom.x = (rand() % lWidth) + m_rDrawArea.left;
44:         pFrom.y = (rand() % lHeight) + m_rDrawArea.top;
45:         // Loop through the number of segments
46:         for (lCurLine = 0; lCurLine &lt; lNumLines; lCurLine++)
47:         {
48:             // Determine the end point of the segment
49:             pTo.x = ((rand() % 20) - 10) + pFrom.x;
50:             pTo.y = ((rand() % 20) - 10) + pFrom.y;
51:             // Create a new CLine object
52:             CLine *pLine = new CLine(pFrom, pTo, nCurWidth, 
                        &Acirc;RGB(cRed, cGreen, cBlue));
53:             try
54:             {
55:                 // Add the new line to the object array
56:                 m_oaLines.Add(pLine);
57:             }
58:             // Did we run into a memory exception?
59:             catch (CMemoryException* perr)
60:             {
61:                 // Display a message for the user, giving him the
62:                 // bad news
63:                 AfxMessageBox(&quot;Out of memory&quot;, MB_ICONSTOP | MB_OK);
64:                 // Did we create a line object?
65:                 if (pLine)
66:                 {
67:                     // Delete it
68:                     delete pLine;
69:                     pLine = NULL;
70:                 }
71:                 // Delete the exception object
72:                 perr-&gt;Delete();
73:             }
74:             // Set the starting point to the end point
75:             pFrom = pTo;
76:         }
77:     }
78: }
</PRE>
<P>After making these changes, compile the DLL again. Once you compile the DLL, switch
to the file explorer and copy the DLL into the debug directory of the test application
again. Once you copy the DLL, run the test application from the Start | Run Taskbar,
as in Figure 17.3. You should find that the application has been updated, and it
is now including more squiggles and using many different colors.</P>
<P><A HREF="javascript:popUp('17fig03.gif')"><B>FIGURE 17.3.</B></A><B> </B><I>Starting
the sample application.</I></P>

<P><I></I>
<H2><A NAME="Heading8"></A>Creating and Using a Regular DLL</H2>
<P>You might think that you broke the rules about using variables that are not owned
by the application in a DLL when you created and used the MFC extension DLL. Well,
you didn't. The instance of the drawing class was a member of the document class
in the test application. It was created and maintained by the application, not the
DLL. Now that you are turning your attention to implementing the same functionality
as a regular DLL, this will become clearer.</P>
<P>To convert the MFC extension DLL into a regular DLL, you'll have to convert the
drawing class into a series of regular function calls. In the course of making this
conversion, the object array must become a member variable of the application document
class and must be passed as an argument to every exported function in the DLL.</P>
<P>
<H3><A NAME="Heading9"></A>Creating the Regular DLL</H3>
<P>To convert the MFC extension DLL into a regular DLL, you have to start a new project.
Visual C++ has to build a project that tells the compiler what type of file it's

⌨️ 快捷键说明

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