home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c480 / 19.ddi / MFC / SAMPLES / DLLHUSK / TESTDLL2.CP_ / TESTDLL2.CP
Encoding:
Text File  |  1993-02-08  |  5.7 KB  |  214 lines

  1.  
  2. // This is a part of the Microsoft Foundation Classes C++ library.
  3. // Copyright (C) 1992 Microsoft Corporation
  4. // All rights reserved.
  5. //
  6. // This source code is only intended as a supplement to the
  7. // Microsoft Foundation Classes Reference and Microsoft
  8. // QuickHelp and/or WinHelp documentation provided with the library.
  9. // See these sources for detailed information regarding the
  10. // Microsoft Foundation Classes product.
  11.  
  12.  
  13. #include "stdafx.h"
  14.  
  15. #include "resource.h"       // main symbols
  16. #include "testres2.h"       // symbols unique to this DLL
  17.  
  18. #include "testdll2.h"
  19.  
  20. #ifdef _DEBUG
  21. #undef THIS_FILE
  22. static char BASED_CODE THIS_FILE[] = __FILE__;
  23. #endif
  24.  
  25. /////////////////////////////////////////////////////////////////////////////
  26. // Initialization of MFC Extension DLL
  27.  
  28. #include "afxdllx.h"    // standard MFC Extension DLL routines
  29.  
  30. static AFX_EXTENSION_MODULE NEAR extensionDLL = { NULL, NULL };
  31.  
  32. extern "C" int CALLBACK LibMain(HINSTANCE hInstance, WORD, WORD, LPSTR)
  33. {
  34.     // Extension DLL one-time initialization - do not allocate memory here,
  35.     //   use the TRACE or ASSERT macros or call MessageBox
  36.     AfxInitExtensionModule(extensionDLL, hInstance);
  37.     return 1;   // ok
  38. }
  39.  
  40. // Exported DLL initialization is run in context of running application
  41. extern "C" extern void WINAPI InitTestDLL2()
  42. {
  43.     // create a new CDynLinkLibrary for this app
  44.     new CDynLinkLibrary(extensionDLL);
  45.     // nothing more to do
  46. }
  47.  
  48. /////////////////////////////////////////////////////////////////////////////
  49. // class CListOutputFrame
  50.  
  51. IMPLEMENT_DYNAMIC(CListOutputFrame, CMDIChildWnd)
  52.  
  53. BEGIN_MESSAGE_MAP(CListOutputFrame, CMDIChildWnd)
  54.     //{{AFX_MSG_MAP(CListOutputFrame)
  55.     ON_WM_CREATE()
  56.     ON_COMMAND(ID_EDIT_CLEAR, OnEditClear)
  57.     ON_COMMAND(ID_EDIT_COPY, OnEditCopy)
  58.     ON_COMMAND(ID_EDIT_CUT, OnEditCut)
  59.     //}}AFX_MSG_MAP
  60. END_MESSAGE_MAP()
  61.  
  62. CListOutputFrame::CListOutputFrame()
  63. {
  64.     m_ppThis = NULL;        // backpointer
  65.     // do nothing now (two-phase create)
  66. }
  67.  
  68. CListOutputFrame::~CListOutputFrame()
  69. {
  70.     // clear backpointer
  71.     if (m_ppThis != NULL)
  72.     {
  73.         ASSERT(*m_ppThis == this);
  74.         *m_ppThis = NULL;
  75.     }
  76. }
  77.  
  78. BOOL CListOutputFrame::Create(LPCSTR lpszWindowName,
  79.     DWORD dwStyle, const RECT& rect, CMDIFrameWnd* pParentWnd)
  80. {
  81.     // we want to load a specific menu from this DLL
  82.     HINSTANCE hInstOld = AfxGetResourceHandle();
  83.     AfxSetResourceHandle(extensionDLL.hModule);
  84.     if (!m_menu.LoadMenu(IDR_LISTOUTPUT))
  85.     {
  86.         // restore the old resource chain and return error
  87.         AfxSetResourceHandle(hInstOld);
  88.         return FALSE;
  89.     }
  90.     AfxSetResourceHandle(hInstOld); // restore the old resource chain
  91.  
  92.     // when the list output frame window is active, use this menu
  93.     m_hMenuShared = m_menu.m_hMenu;
  94.  
  95.     // create the CMDIChildWnd, listbox created in OnCreate
  96.     return CMDIChildWnd::Create(NULL, lpszWindowName,
  97.         dwStyle, rect, pParentWnd);
  98. }
  99.  
  100.  
  101. int CListOutputFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
  102. {
  103.     if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
  104.         return -1;
  105.  
  106.     // create dialog bar at top of the frame window (it is a toolbar)
  107.     if (!m_dlgBar.Create(this, IDD_DIALOGBAR1, WS_CHILD|CBRS_TOP,
  108.             AFX_IDW_TOOLBAR))
  109.     {
  110.         TRACE("Failed to create toolbar for CListOutputFrame\n");
  111.         return -1;
  112.     }
  113.  
  114.     // create listbox as main pane (will fill rest of frame window)
  115.     if (!m_listBox.Create(WS_CHILD | WS_VISIBLE|
  116.         LBS_NOTIFY | LBS_NOINTEGRALHEIGHT |
  117.         WS_VSCROLL | LBS_DISABLENOSCROLL | WS_BORDER,
  118.         CRect(0,0,0,0), this, AFX_IDW_PANE_FIRST))
  119.     {
  120.         TRACE("Failed to create listbox for CListOutputFrame\n");
  121.         return -1;
  122.     }
  123.     m_listBox.SetFont(m_dlgBar.GetFont());
  124.     return 0; // creation ok
  125. }
  126.  
  127. void CListOutputFrame::Clear()
  128. {
  129.     ASSERT(m_listBox.m_hWnd != NULL);       // must be created
  130.     m_listBox.ResetContent();
  131. }
  132.  
  133. void CListOutputFrame::AddString(LPCSTR lpszItem)
  134. {
  135.     ASSERT(m_listBox.m_hWnd != NULL);       // must be created
  136.     int nIndex;
  137.     if ((nIndex = m_listBox.AddString(lpszItem)) < 0)
  138.         AfxThrowMemoryException();
  139.     m_listBox.SetCurSel(nIndex);    // make last line visible
  140. }
  141.  
  142. /////////////////////////////////////////////////////////////////////////////
  143. // Edit commands
  144.  
  145. void CListOutputFrame::OnEditClear()
  146. {
  147.     Clear();
  148. }
  149.  
  150. void CListOutputFrame::OnEditCut()
  151. {
  152.     OnEditCopy();
  153.     Clear();
  154. }
  155.  
  156. void CListOutputFrame::OnEditCopy()
  157. {
  158.     if (!OpenClipboard())
  159.     {
  160.         AfxMessageBox("Failed to open clipboard");
  161.         return;
  162.     }
  163.     if (!::EmptyClipboard())
  164.     {
  165.         AfxMessageBox("Failed to empty clipboard");
  166.         ::CloseClipboard();
  167.         return;
  168.     }
  169.  
  170.     // copy the current listbox contents to the clipboard as text
  171.     HGLOBAL hTextData = GetTextData();
  172.     if (hTextData == NULL || ::SetClipboardData(CF_TEXT, hTextData) == NULL)
  173.     {
  174.         AfxMessageBox("Failed to set clipboard data");
  175.         ::CloseClipboard();
  176.         return;
  177.     }
  178.     if (!::CloseClipboard())
  179.         AfxMessageBox("Failed to close clipboard");
  180. }
  181.  
  182. HGLOBAL CListOutputFrame::GetTextData()
  183. {
  184.     // first find out how much space we need
  185.     int nLines = m_listBox.GetCount();
  186.     long cbTotal = 0;
  187.     for (int i = 0; i < nLines; i++)
  188.         cbTotal += m_listBox.GetTextLen(i) + 2;     // CR/LF for each line
  189.     cbTotal += 1;   // for end '\0'
  190.  
  191.     // allocate a (shared) global memory block for the data
  192.     HGLOBAL hData = ::GlobalAlloc(GMEM_DDESHARE, cbTotal);
  193.     if (hData == NULL)
  194.         return NULL;        // NULL for memory failure
  195.  
  196.     // now copy the strings, terminate each with CR/LF
  197.     LPSTR lpOut = (LPSTR)::GlobalLock(hData);
  198.     ASSERT(lpOut != NULL);
  199.     for (i = 0; i < nLines; i++)
  200.     {
  201.         m_listBox.GetText(i, lpOut);
  202.         lpOut += lstrlen(lpOut);        // skip new string
  203.         *lpOut++ = 0xD; // CR
  204.         *lpOut++ = 0xA; // LF
  205.     }
  206.     *lpOut = '\0';
  207.     ASSERT(lpOut == (LPCSTR)GlobalLock(hData) + cbTotal);
  208.  
  209.     return hData;
  210. }
  211.  
  212. /////////////////////////////////////////////////////////////////////////////
  213.  
  214.