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

  1. // This is a part of the Microsoft Foundation Classes C++ library.
  2. // Copyright (C) 1992 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 Microsoft
  7. // WinHelp 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. #include "afxext.h"     // for CMetaFileDC
  13. #include "minsvr.h"
  14.  
  15. /////////////////////////////////////////////////////////////////////////////
  16.  
  17. CMinItem::CMinItem(CMinDoc* pContainerDoc)
  18.     : COleServerItem(pContainerDoc)
  19. {
  20.     m_data = "example string";
  21. }
  22.  
  23. void CMinItem::Serialize(CArchive& ar)
  24. {
  25.     // Customize this to store real data
  26.     if (ar.IsStoring())
  27.     {
  28.         ar << m_data;
  29.     }
  30.     else
  31.     {
  32.         ar >> m_data;
  33.     }
  34. }
  35.  
  36.  
  37. OLESTATUS CMinItem::OnShow(BOOL /* bTakeFocus */)
  38. {
  39.     // make sure server is the topmost window
  40.     AfxGetApp()->m_pMainWnd->BringWindowToTop();
  41.     return OLE_OK;
  42. }
  43.  
  44.  
  45. BOOL CMinItem::OnGetTextData(CString& rStringReturn)
  46. {
  47.     rStringReturn = m_data;
  48.     return TRUE;
  49. }
  50.  
  51. /////////////////////////////////////////////////////////////////////////////
  52. // Simple dialog for changing the string
  53.  
  54. class CChangeDlg : public CDialog
  55. {
  56. // Construction
  57. public:
  58.     CChangeDlg::CChangeDlg();
  59.  
  60. // Dialog Data
  61.     //{{AFX_DATA(CChangeDlg)
  62.     enum { IDD = IDD_CHANGEDLG };
  63.     CString m_str;
  64.     //}}AFX_DATA
  65.  
  66. // Implementation
  67. protected:
  68.     virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
  69.     //{{AFX_MSG(CChangeDlg)
  70.     //}}AFX_MSG
  71.     DECLARE_MESSAGE_MAP()
  72. };
  73.  
  74. CChangeDlg::CChangeDlg()
  75.     : CDialog(CChangeDlg::IDD)
  76. {
  77. }
  78.  
  79. void CChangeDlg::DoDataExchange(CDataExchange* pDX)
  80. {
  81.     CDialog::DoDataExchange(pDX);
  82.     //{{AFX_DATA_MAP(CChangeDlg)
  83.     DDX_Text(pDX, IDC_EDIT1, m_str);
  84.     //}}AFX_DATA_MAP
  85. }
  86.  
  87. BEGIN_MESSAGE_MAP(CChangeDlg, CDialog)
  88.     //{{AFX_MSG_MAP(CChangeDlg)
  89.     //}}AFX_MSG_MAP
  90. END_MESSAGE_MAP()
  91.  
  92. BOOL CMinItem::PromptChangeString()
  93. {
  94.     CChangeDlg dlg;
  95.     dlg.m_str = m_data;
  96.     if (dlg.DoModal() != IDOK)
  97.         return FALSE;
  98.     m_data = dlg.m_str;
  99.     return TRUE;        // changed
  100. }
  101.  
  102. /////////////////////////////////////////////////////////////////////////////
  103. // Drawing items into bitmap or metafile
  104.  
  105. BOOL CMinItem::OnDraw(CDC* pDC)
  106. {
  107.     ASSERT_VALID(pDC);
  108.     CSize textSize;
  109.  
  110.     // first calculate the text size in MM_TEXT units
  111.     {
  112.         CWindowDC screenDC(NULL);
  113.         textSize = screenDC.GetTextExtent(m_data, m_data.GetLength());
  114.     }
  115.  
  116.     // if you want the item to always be drawn in a specific mapping
  117.     // mode set it here.
  118.  
  119.     // Otherwise the OLE DLLs will scale the metafile to fit the
  120.     // client specified size.  Setting the viewport size/extent
  121.     // determines the relative scale of everything.
  122.  
  123.     int cx = textSize.cx + 100;
  124.     int cy = (cx * 4) / 3;      // nice aspect ratio
  125.     TRACE("Item drawing size is %d x %d\n", cx, cy);
  126.     pDC->SetWindowExt(cx, cy);
  127.  
  128.     // to keep some OLE client apps like MS-Write happy, you should
  129.     // set the bounding rect to the suggested size in MM_HIMETRIC units
  130.     if (m_rectBounds.IsRectNull())
  131.     {
  132.         CClientDC dcTmp(NULL);
  133.         dcTmp.SetMapMode(MM_HIMETRIC);
  134.         m_rectBounds.SetRect(0, 0, cx, cy);
  135.         dcTmp.DPtoLP(&m_rectBounds);        // rectBounds in HIMETRIC
  136.     }
  137.  
  138.     // Draw a shaded circle
  139.     pDC->SelectStockObject(LTGRAY_BRUSH);
  140.     pDC->Ellipse(0, 0, cx, cy);
  141.  
  142.     // draw the text in the middle (as best we can)
  143.     pDC->SetBkMode(TRANSPARENT);
  144.     pDC->TextOut((cx - textSize.cx) / 2, (cy - textSize.cy) / 2, m_data);
  145.  
  146.     return TRUE;
  147. }
  148.  
  149. /////////////////////////////////////////////////////////////////////////////
  150.