home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / dcom / atldraw / atldview.cpp < prev    next >
C/C++ Source or Header  |  1998-04-02  |  8KB  |  323 lines

  1. // ATLDrawView.cpp : implementation of the CATLDrawView class
  2. //
  3. // This is a part of the ActiveX Template Library.
  4. // Copyright (C) 1996 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // ActiveX Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // ActiveX Template Library product.
  12. //
  13.  
  14. #include "preatldr.h"
  15. #include "ATLDraw.h"
  16.  
  17. #include "ATLDDoc.h"
  18. #include "ATLDView.h"
  19.  
  20. #ifdef _DEBUG
  21. #define new DEBUG_NEW
  22. #undef THIS_FILE
  23. static char THIS_FILE[] = __FILE__;
  24. #endif
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CATLDrawView
  28.  
  29. IMPLEMENT_DYNCREATE(CATLDrawView, CView)
  30.  
  31. BEGIN_MESSAGE_MAP(CATLDrawView, CView)
  32.     //{{AFX_MSG_MAP(CATLDrawView)
  33.     ON_WM_LBUTTONDOWN()
  34.     ON_WM_MOUSEMOVE()
  35.     ON_WM_LBUTTONUP()
  36.     ON_COMMAND(ID_SERVER_CONNECT, OnServerConnect)
  37.     ON_COMMAND(ID_SERVER_DISCONNENT, OnServerDisconnent)
  38.     ON_UPDATE_COMMAND_UI(ID_SERVER_CONNECT, OnUpdateServerConnect)
  39.     ON_UPDATE_COMMAND_UI(ID_SERVER_DISCONNENT, OnUpdateServerDisconnent)
  40.     ON_COMMAND(ID_VIEW_COLOR, OnViewColor)
  41.     ON_COMMAND(ID_CLEAR, OnClear)
  42.     //}}AFX_MSG_MAP
  43.     // Standard printing commands
  44.     ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
  45.     ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
  46.     ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
  47. END_MESSAGE_MAP()
  48.  
  49. BEGIN_INTERFACE_MAP(CATLDrawView, CCmdTarget)
  50.     INTERFACE_PART(CATLDrawView, __uuidof(IDrawServ), DrawServ)
  51. END_INTERFACE_MAP()
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. // CATLDrawView construction/destruction
  55.  
  56. CATLDrawView::CATLDrawView()
  57. {
  58.     HRESULT hr;
  59.  
  60.     hr = CoInitialize(NULL);
  61.     _ASSERTE(SUCCEEDED(hr));
  62.  
  63.     hr = CoInitializeSecurity(NULL, -1, NULL, NULL,
  64.         RPC_C_AUTHN_LEVEL_NONE, RPC_C_IMP_LEVEL_IDENTIFY, NULL, EOAC_NONE, NULL);
  65.     _ASSERTE(SUCCEEDED(hr));
  66.  
  67.     m_bDragging = FALSE;
  68.     m_col = RGB(255, 0, 0);
  69. }
  70.  
  71. CATLDrawView::~CATLDrawView()
  72. {
  73.     if ((bool) m_pDrawServ)
  74.     {
  75.         DisconnectSink(__uuidof(IDrawServ), m_dwDrawServ);
  76.         m_pDrawServ = 0;
  77.     }
  78.     CoUninitialize();
  79. }
  80.  
  81. BOOL CATLDrawView::PreCreateWindow(CREATESTRUCT& cs)
  82. {
  83.     // TODO: Modify the Window class or styles here by modifying
  84.     //  the CREATESTRUCT cs
  85.  
  86.     return CView::PreCreateWindow(cs);
  87. }
  88.  
  89. /////////////////////////////////////////////////////////////////////////////
  90. // CATLDrawView drawing
  91.  
  92. void CATLDrawView::OnDraw(CDC* pDC)
  93. {
  94.     pDC;
  95.     CATLDrawDoc* pDoc = GetDocument();
  96.     ASSERT_VALID(pDoc);
  97.  
  98.     // TODO: add draw code for native data here
  99. }
  100.  
  101. /////////////////////////////////////////////////////////////////////////////
  102. // CATLDrawView printing
  103.  
  104. BOOL CATLDrawView::OnPreparePrinting(CPrintInfo* pInfo)
  105. {
  106.     // default preparation
  107.     return DoPreparePrinting(pInfo);
  108. }
  109.  
  110. void CATLDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  111. {
  112.     // TODO: add extra initialization before printing
  113. }
  114.  
  115. void CATLDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
  116. {
  117.     // TODO: add cleanup after printing
  118. }
  119.  
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CATLDrawView diagnostics
  122.  
  123. #ifdef _DEBUG
  124. void CATLDrawView::AssertValid() const
  125. {
  126.     CView::AssertValid();
  127. }
  128.  
  129. void CATLDrawView::Dump(CDumpContext& dc) const
  130. {
  131.     CView::Dump(dc);
  132. }
  133.  
  134. CATLDrawDoc* CATLDrawView::GetDocument() // non-debug version is inline
  135. {
  136.     ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CATLDrawDoc)));
  137.     return (CATLDrawDoc*)m_pDocument;
  138. }
  139. #endif //_DEBUG
  140.  
  141. /////////////////////////////////////////////////////////////////////////////
  142. // CATLDrawView message handlers
  143.  
  144. void CATLDrawView::OnLButtonDown(UINT nFlags, CPoint point) 
  145. {
  146.     m_bDragging = TRUE;
  147.     m_pos = point;
  148.     
  149.     CView::OnLButtonDown(nFlags, point);
  150. }
  151.  
  152. void CATLDrawView::OnMouseMove(UINT nFlags, CPoint point) 
  153. {
  154.     if (m_bDragging)
  155.     {
  156.         if (m_pDrawServ != NULL)
  157.         {
  158.             HRESULT hr;
  159.             hr = m_pDrawServ->raw_Draw(
  160.                 m_pos.x, m_pos.y,
  161.                 point.x, point.y,
  162.                 m_col);
  163.             if (FAILED(hr))
  164.             {
  165.                 TCHAR buf[32];
  166.                 wsprintf(buf, _T("%xd"), hr);
  167.                 AfxMessageBox(buf);
  168.             }
  169.         }
  170.         m_pos = point;
  171.     }
  172.     
  173.     CView::OnMouseMove(nFlags, point);
  174. }
  175.  
  176. void CATLDrawView::OnLButtonUp(UINT nFlags, CPoint point) 
  177. {
  178.     m_bDragging = FALSE;
  179.     
  180.     CView::OnLButtonUp(nFlags, point);
  181. }
  182.  
  183. void CATLDrawView::Draw(long x1, long y1, long x2, long y2, unsigned long col)
  184. {
  185.     CClientDC dc(this);
  186.  
  187.     CPen pen(PS_SOLID, 2, (COLORREF) col);
  188.     CPen* oldPen = dc.SelectObject(&pen);
  189.     dc.MoveTo(x1, y1);
  190.     dc.LineTo(x2, y2);
  191.     dc.SelectObject(oldPen);
  192. }
  193.  
  194. void CATLDrawView::OnServerConnect() 
  195. {   
  196.     if (SUCCEEDED(m_pDrawServ.CreateInstance(__uuidof(CDrawServ), NULL, CLSCTX_SERVER)))
  197.     {
  198.         IUnknown* pUnk;
  199.         m_xDrawServ.QueryInterface(__uuidof(IUnknown), (void**) &pUnk);
  200.         if (ConnectSink(__uuidof(IDrawServ), pUnk) == FALSE)
  201.             m_pDrawServ = 0;
  202.         pUnk->Release();
  203.     }
  204.     else
  205.         AfxMessageBox(_T("Connect failed!"));
  206. }
  207.  
  208. void CATLDrawView::OnServerDisconnent() 
  209. {
  210.     if ((bool) m_pDrawServ)
  211.     {
  212.         DisconnectSink(__uuidof(IDrawServ), m_dwDrawServ);  
  213.         m_pDrawServ = 0;
  214.     }
  215. }
  216.  
  217. BOOL CATLDrawView::ConnectSink(REFIID iid, LPUNKNOWN punkSink)
  218. {
  219.     ASSERT((bool) m_pDrawServ);
  220.  
  221.     IConnectionPointContainerPtr pCPC;
  222.     IConnectionPointPtr pCP;
  223.  
  224.     try {
  225.     pCPC = m_pDrawServ;
  226.     ASSERT((bool) pCPC);
  227.     pCPC->FindConnectionPoint(iid, &pCP);
  228.     ASSERT((bool) pCP);
  229.     pCP->Advise(punkSink, &m_dwDrawServ);
  230.     ASSERT(punkSink != NULL);
  231.     } catch(_com_error& e) {
  232.     dump_com_error(e);
  233.     AfxMessageBox(_T("Connect failed!"));
  234.     return FALSE;
  235.     }
  236.     
  237.     AfxMessageBox(_T("Connected"));
  238.     pCP = 0;
  239.     pCPC = 0;
  240.     return TRUE;
  241. }
  242.  
  243. void CATLDrawView::DisconnectSink(REFIID iid, DWORD dwCookie)
  244. {
  245.     if (dwCookie == 0)
  246.         return;
  247.  
  248.     ASSERT((bool) m_pDrawServ);
  249.  
  250.     IConnectionPointContainerPtr pCPC;
  251.     IConnectionPointPtr pCP;
  252.  
  253.     try {
  254.     pCPC = m_pDrawServ;
  255.     ASSERT((bool) pCPC);
  256.     pCPC->FindConnectionPoint(iid, &pCP);
  257.     ASSERT((bool) pCP);
  258.     pCP->Unadvise(dwCookie);
  259.     } catch(_com_error& e) {
  260.     dump_com_error(e);
  261.     AfxMessageBox(_T("Disconnect failed!"));
  262.     }
  263.     
  264.     AfxMessageBox(_T("Disconnected"));
  265.     pCP = 0;
  266.     pCPC = 0;
  267. }
  268.  
  269. STDMETHODIMP_(ULONG) CATLDrawView::XDrawServ::AddRef()
  270. {
  271.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  272.     TRACE("AddRef\n");
  273.     return (ULONG)pThis->ExternalAddRef();
  274. }
  275.  
  276. STDMETHODIMP_(ULONG) CATLDrawView::XDrawServ::Release()
  277. {
  278.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  279.     TRACE("Release\n");
  280.     return (ULONG)pThis->ExternalRelease();
  281. }
  282.  
  283. STDMETHODIMP CATLDrawView::XDrawServ::QueryInterface(
  284.     REFIID iid, LPVOID far * ppvObj)
  285. {
  286.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  287.     TRACE("QueryInterface\n");
  288.     return (HRESULT)pThis->ExternalQueryInterface(&iid, ppvObj);
  289. }
  290.  
  291. STDMETHODIMP CATLDrawView::XDrawServ::raw_Draw(
  292.     long x1, long y1, long x2, long y2, unsigned long col)
  293. {
  294.     METHOD_PROLOGUE_EX(CATLDrawView, DrawServ)
  295.     pThis->Draw(x1, y1, x2, y2, col);
  296.     return S_OK;
  297. }
  298.  
  299. void CATLDrawView::OnUpdateServerConnect(CCmdUI* pCmdUI) 
  300. {
  301.     pCmdUI->Enable(!(bool)m_pDrawServ);
  302. }
  303.  
  304. void CATLDrawView::OnUpdateServerDisconnent(CCmdUI* pCmdUI) 
  305. {
  306.     pCmdUI->Enable((bool) m_pDrawServ); 
  307. }
  308.  
  309. void CATLDrawView::OnViewColor() 
  310. {
  311.     CColorDialog dlg;
  312.  
  313.     if (dlg.DoModal() == IDOK)
  314.     {
  315.         m_col = dlg.GetColor();
  316.     }   
  317. }
  318.  
  319. void CATLDrawView::OnClear() 
  320. {
  321.     Invalidate();   
  322. }
  323.