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 / testdll2.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  7KB  |  232 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 "testres2.h"       // symbols unique to this DLL
  15.  
  16. #include "testdll2.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 - do not allocate memory here,
  36.         //   use the TRACE or ASSERT macros or call MessageBox
  37.         if (!AfxInitExtensionModule(extensionDLL, hInstance))
  38.             return 0;
  39.  
  40.         // Other initialization could be done here, as long as
  41.         // it doesn't result in direct or indirect calls to AfxGetApp.
  42.         // This extension DLL doesn't need to access the app object
  43.         // but to be consistent with testdll1.dll, this DLL requires
  44.         // explicit initialization as well (see below).
  45.  
  46.         // This allows for greater flexibility later in development.
  47.     }
  48.     return 1;   // ok
  49. }
  50.  
  51. // Exported DLL initialization is run in context of running application
  52. extern "C" void WINAPI InitTestDLL2()
  53. {
  54.     // create a new CDynLinkLibrary for this app
  55.     new CDynLinkLibrary(extensionDLL);
  56.     // nothing more to do
  57. }
  58.  
  59. /////////////////////////////////////////////////////////////////////////////
  60. // class CListOutputFrame
  61.  
  62. IMPLEMENT_DYNAMIC(CListOutputFrame, CMDIChildWnd)
  63.  
  64. BEGIN_MESSAGE_MAP(CListOutputFrame, CMDIChildWnd)
  65.     //{{AFX_MSG_MAP(CListOutputFrame)
  66.     ON_WM_CREATE()
  67.     ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  68.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  69.     ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  70.     //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72.  
  73. CListOutputFrame::CListOutputFrame()
  74. {
  75.     m_ppThis = NULL;        // backpointer
  76.     // do nothing now (two-phase create)
  77. }
  78.  
  79. CListOutputFrame::~CListOutputFrame()
  80. {
  81.     // clear backpointer
  82.     if (m_ppThis != NULL)
  83.     {
  84.         ASSERT(*m_ppThis == this);
  85.         *m_ppThis = NULL;
  86.     }
  87. }
  88.  
  89. BOOL CListOutputFrame::Create(LPCTSTR lpszWindowName,
  90.     DWORD dwStyle, const RECT& rect, CMDIFrameWnd* pParentWnd)
  91. {
  92.     // we want to load a specific menu from this DLL
  93.     HINSTANCE hInstOld = AfxGetResourceHandle();
  94.     AfxSetResourceHandle(extensionDLL.hModule);
  95.     if (!m_menu.LoadMenu(IDR_LISTOUTPUT))
  96.     {
  97.         // restore the old resource chain and return error
  98.         AfxSetResourceHandle(hInstOld);
  99.         return FALSE;
  100.     }
  101.     AfxSetResourceHandle(hInstOld); // restore the old resource chain
  102.  
  103.     // when the list output frame window is active, use this menu
  104.     m_hMenuShared = m_menu.m_hMenu;
  105.  
  106.     // create the CMDIChildWnd, listbox created in OnCreate
  107.     return CMDIChildWnd::Create(NULL, lpszWindowName,
  108.         dwStyle, rect, pParentWnd);
  109. }
  110.  
  111.  
  112. int CListOutputFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  113. {
  114.     if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
  115.         return -1;
  116.  
  117.     // create dialog bar at top of the frame window (it is a toolbar)
  118.     if (!m_dlgBar.Create(this, IDD_DIALOGBAR1, WS_CHILD|CBRS_TOP,
  119.             AFX_IDW_TOOLBAR))
  120.     {
  121.         TRACE(_T("Failed to create toolbar for CListOutputFrame\n"));
  122.         return -1;
  123.     }
  124.  
  125.     // create listbox as main pane (will fill rest of frame window)
  126.     if (!m_listBox.Create(WS_CHILD | WS_VISIBLE|
  127.         LBS_NOTIFY | LBS_NOINTEGRALHEIGHT |
  128.         WS_VSCROLL | LBS_DISABLENOSCROLL | WS_BORDER,
  129.         CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST))
  130.     {
  131.         TRACE(_T("Failed to create listbox for CListOutputFrame\n"));
  132.         return -1;
  133.     }
  134.     m_listBox.SetFont(m_dlgBar.GetFont());
  135.     ModifyStyleEx(WS_EX_CLIENTEDGE, 0, SWP_DRAWFRAME);
  136.     m_listBox.ModifyStyle(0, WS_EX_CLIENTEDGE, SWP_DRAWFRAME);
  137.  
  138.     return 0; // creation ok
  139. }
  140.  
  141. void CListOutputFrame::Clear()
  142. {
  143.     ASSERT(m_listBox.m_hWnd != NULL);       // must be created
  144.     m_listBox.ResetContent();
  145. }
  146.  
  147. void CListOutputFrame::AddString(LPCTSTR lpszItem)
  148. {
  149.     ASSERT(m_listBox.m_hWnd != NULL);       // must be created
  150.     int nIndex;
  151.     if ((nIndex = m_listBox.AddString(lpszItem)) < 0)
  152.         AfxThrowMemoryException();
  153.     m_listBox.SetCurSel(nIndex);    // make last line visible
  154. }
  155.  
  156. /////////////////////////////////////////////////////////////////////////////
  157. // Edit commands
  158.  
  159. void CListOutputFrame::OnEditClear()
  160. {
  161.     Clear();
  162. }
  163.  
  164. void CListOutputFrame::OnEditCut()
  165. {
  166.     OnEditCopy();
  167.     Clear();
  168. }
  169.  
  170. void CListOutputFrame::OnEditCopy()
  171. {
  172.     if (!OpenClipboard())
  173.     {
  174.         AfxMessageBox(_T("Failed to open clipboard"));
  175.         return;
  176.     }
  177.     if (!::EmptyClipboard())
  178.     {
  179.         AfxMessageBox(_T("Failed to empty clipboard"));
  180.         ::CloseClipboard();
  181.         return;
  182.     }
  183.  
  184.     // copy the current listbox contents to the clipboard as text
  185.     HGLOBAL hTextData = GetTextData();
  186.     if (hTextData == NULL ||
  187. #ifndef _UNICODE
  188.         ::SetClipboardData(CF_TEXT, hTextData) == NULL)
  189. #else
  190.         ::SetClipboardData(CF_UNICODETEXT, hTextData) == NULL)
  191. #endif
  192.     {
  193.         AfxMessageBox(_T("Failed to set clipboard data"));
  194.         ::CloseClipboard();
  195.         return;
  196.     }
  197.     if (!::CloseClipboard())
  198.         AfxMessageBox(_T("Failed to close clipboard"));
  199. }
  200.  
  201. HGLOBAL CListOutputFrame::GetTextData()
  202. {
  203.     // first find out how much space we need
  204.     int nLines = m_listBox.GetCount();
  205.     long cbTotal = 0;
  206.     for (int i = 0; i < nLines; i++)
  207.         cbTotal += m_listBox.GetTextLen(i) + 2;     // CR/LF for each line
  208.     cbTotal += 1;   // for end '\0'
  209.  
  210.     // allocate a (shared) global memory block for the data
  211.     HGLOBAL hData = ::GlobalAlloc(GMEM_DDESHARE, cbTotal*sizeof(TCHAR));
  212.     if (hData == NULL)
  213.         return NULL;        // NULL for memory failure
  214.  
  215.     // now copy the strings, terminate each with CR/LF
  216.     LPTSTR lpOut = (LPTSTR)::GlobalLock(hData);
  217.     ASSERT(lpOut != NULL);
  218.     for (i = 0; i < nLines; i++)
  219.     {
  220.         m_listBox.GetText(i, lpOut);
  221.         lpOut += lstrlen(lpOut)*sizeof(TCHAR);   // skip new string
  222.         *lpOut++ = 0xD; // CR
  223.         *lpOut++ = 0xA; // LF
  224.     }
  225.     *lpOut = '\0';
  226.     ASSERT(lpOut + 1 == (LPCTSTR)GlobalLock(hData) + cbTotal);
  227.  
  228.     return hData;
  229. }
  230.  
  231. /////////////////////////////////////////////////////////////////////////////
  232.