home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / advanced / dllhusk / testdll1.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  5KB  |  176 lines

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992-1998 Microsoft Corporation
  3. // All rights reserved.
  4. //
  5. // This source code is only intended as a supplement to the
  6. // Microsoft Foundation Classes Reference and related
  7. // electronic documentation provided with the library.
  8. // See these sources for detailed information regarding the
  9. // Microsoft Foundation Classes product.
  10.  
  11. #include "stdafx.h"
  12.  
  13. #include "resource.h"       // main symbols
  14. #include "testres1.h"       // symbols unique to this DLL
  15.  
  16. #include "testdll1.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. /////////////////////////////////////////////////////////////////////////////
  24. // Initialization of MFC Extension DLL
  25.  
  26. #include "afxdllx.h"    // standard MFC Extension DLL routines
  27.  
  28. static AFX_EXTENSION_MODULE NEAR extensionDLL = { NULL, NULL };
  29.  
  30. extern "C" int APIENTRY
  31. DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID lpReserved)
  32. {
  33.     if (dwReason == DLL_PROCESS_ATTACH)
  34.     {
  35.         // Extension DLL one-time initialization.
  36.         if (!AfxInitExtensionModule(extensionDLL, hInstance))
  37.            return 0;
  38.  
  39.         // Other initialization could be done here, as long as
  40.         // it doesn't result in direct or indirect calls to AfxGetApp.
  41.         // This extension DLL needs to add doc templates to the
  42.         // app object, so it waits for the application to boot
  43.         // and call an initialization function (see below).
  44.     }
  45.     return 1;   // ok
  46. }
  47.  
  48. // Exported DLL initialization is run in context of running application
  49. extern "C" void WINAPI InitTestDLL1()
  50. {
  51.     // create a new CDynLinkLibrary for this app
  52.     new CDynLinkLibrary(extensionDLL);
  53.  
  54.     // Register the doc templates we provide to the app
  55.     CWinApp* pApp = AfxGetApp();
  56.     ASSERT(pApp != NULL);
  57.     pApp->AddDocTemplate(new CMultiDocTemplate(IDR_TEXTTYPE,
  58.             RUNTIME_CLASS(CTextDoc),
  59.             RUNTIME_CLASS(CMDIChildWnd),
  60.             RUNTIME_CLASS(CEditView)));
  61.     pApp->AddDocTemplate(new CMultiDocTemplate(IDR_HELLOTYPE,
  62.             RUNTIME_CLASS(CDummyDoc),
  63.             RUNTIME_CLASS(CMDIChildWnd),
  64.             RUNTIME_CLASS(CHelloView)));
  65.     // add other initialization here
  66. }
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // Example of a Simple CEditView (taken from MultiPad)
  70.  
  71. IMPLEMENT_DYNCREATE(CTextDoc, CDocument)
  72.  
  73. BEGIN_MESSAGE_MAP(CTextDoc, CDocument)
  74.     //{{AFX_MSG_MAP(CTextDoc)
  75.     //}}AFX_MSG_MAP
  76. END_MESSAGE_MAP()
  77.  
  78. void CTextDoc::Serialize(CArchive& ar)
  79. {
  80.     CEditView* pView = (CEditView*)m_viewList.GetHead();
  81.     ASSERT_KINDOF(CEditView, pView);
  82.     pView->SerializeRaw(ar);
  83. }
  84.  
  85. /////////////////////////////////////////////////////////////////////////////
  86. // CHelloView : example of a simple output only document and view
  87. //      (taken from Hello/MDI)
  88.  
  89. IMPLEMENT_DYNCREATE(CHelloView, CView)
  90.  
  91. BEGIN_MESSAGE_MAP(CHelloView, CView)
  92.     //{{AFX_MSG_MAP(CHelloView)
  93.     ON_WM_PAINT()
  94.     ON_COMMAND(IDM_BLACK, OnColor)
  95.     ON_COMMAND(IDM_CUSTOM, OnCustomColor)
  96.     ON_COMMAND(IDM_RED, OnColor)
  97.     ON_COMMAND(IDM_GREEN, OnColor)
  98.     ON_COMMAND(IDM_BLUE, OnColor)
  99.     ON_COMMAND(IDM_WHITE, OnColor)
  100.     ON_UPDATE_COMMAND_UI(IDM_BLACK, OnUpdateColor)
  101.     ON_UPDATE_COMMAND_UI(IDM_BLUE, OnUpdateColor)
  102.     ON_UPDATE_COMMAND_UI(IDM_GREEN, OnUpdateColor)
  103.     ON_UPDATE_COMMAND_UI(IDM_RED, OnUpdateColor)
  104.     ON_UPDATE_COMMAND_UI(IDM_WHITE, OnUpdateColor)
  105.     ON_UPDATE_COMMAND_UI(IDM_CUSTOM, OnUpdateColor)
  106.     //}}AFX_MSG_MAP
  107. END_MESSAGE_MAP()
  108.  
  109. CHelloView::CHelloView()
  110. {
  111.     m_nIDColor = IDM_BLACK;
  112.     m_clrText = RGB(0, 0, 0);
  113. }
  114.  
  115. // OnDraw: Draw a string in the center of the client area.
  116. void CHelloView::OnDraw(CDC* pDC)
  117. {
  118.     CRect rect;
  119.  
  120.     pDC->SetTextColor(m_clrText);
  121.     pDC->SetBkColor(::GetSysColor(COLOR_WINDOW));
  122.     GetClientRect(rect);
  123.     pDC->DrawText(_T("Hello, World!"), -1, rect,
  124.         DT_SINGLELINE | DT_CENTER | DT_VCENTER);
  125. }
  126.  
  127. /////////////////////////////////////////////////////////////////////////////
  128. // CHelloView command
  129.  
  130. void CHelloView::OnUpdateColor(CCmdUI* pCmdUI)
  131. {
  132.     pCmdUI->SetCheck(pCmdUI->m_nID == m_nIDColor);
  133. }
  134.  
  135. // Color array maps to order of the Color menu
  136. static COLORREF NEAR colorArray[] =
  137. {
  138.     RGB (0, 0, 0),
  139.     RGB (255, 0, 0),
  140.     RGB (0, 255, 0),
  141.     RGB (0, 0, 255),
  142.     RGB (255, 255, 255)
  143. };
  144.  
  145. void CHelloView::OnColor()
  146. {
  147.     m_nIDColor = LOWORD(GetCurrentMessage()->wParam);
  148.     m_clrText = colorArray[m_nIDColor - IDM_BLACK];
  149.  
  150.     // Force the client area text to be repainted in the new color
  151.     Invalidate();
  152. }
  153.  
  154. void CHelloView::OnCustomColor()
  155. {
  156.     CColorDialog dlgColor(m_clrText);
  157.     if (dlgColor.DoModal() == IDOK)
  158.     {
  159.         m_clrText = dlgColor.GetColor();
  160.         m_nIDColor = IDM_CUSTOM;
  161.         Invalidate();
  162.     }
  163. }
  164.  
  165. /////////////////////////////////////////////////////////////////////////////
  166. // CDummyDoc : a do-nothing document that can be used with non-data views
  167.  
  168. IMPLEMENT_DYNCREATE(CDummyDoc, CDocument)
  169.  
  170. BEGIN_MESSAGE_MAP(CDummyDoc, CDocument)
  171.     //{{AFX_MSG_MAP(CDummyDoc)
  172.     //}}AFX_MSG_MAP
  173. END_MESSAGE_MAP()
  174.  
  175. /////////////////////////////////////////////////////////////////////////////
  176.