home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / atl / atlduck / duck / mydlg.cpp < prev    next >
C/C++ Source or Header  |  1998-03-26  |  6KB  |  221 lines

  1. // mydlg.cpp: Implementation of CMyDlg
  2. //
  3. // This is a part of the Active Template Library.
  4. // Copyright (C) 1996-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Active Template Library Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Active Template Library product.
  12.  
  13. #include "stdafx.h"
  14. #include "mydlg.h"
  15.  
  16. BOOL CMyDlg::m_bOleInitialized = FALSE;
  17.  
  18. static const CLSID CLSID_DuckDoer = {0x120B72A0,0x65BF,0x11D0,{0x9D,0xDC,0x00,0xA0,0xC9,0x03,0x48,0x92}};
  19.  
  20. CMyDlg::CMyDlg()
  21. {
  22.     m_pIDuckConnectionPoint = NULL;
  23.     m_pDuckInt = NULL;
  24.     m_dwCookie = 0;
  25. }
  26.  
  27. CMyDlg::~CMyDlg()
  28. {
  29.     if (m_pDuckInt != NULL)
  30.     {
  31. #ifdef _DEBUG
  32.         _ASSERT(((IDuckInt*)m_pDuckInt)->Release() == 0);
  33. #else
  34.         ((IDuckInt*)m_pDuckInt)->Release();
  35. #endif
  36.         m_pDuckInt = NULL;
  37.     }
  38. }
  39.  
  40.  
  41. void CMyDlg::DoMyIToA(DWORD dwNumber, LPTSTR lpszResult)
  42. {
  43.     TCHAR* pch = lpszResult;
  44.     BOOL bSign = dwNumber < 0;
  45.     if (bSign)
  46.         dwNumber *= -1;
  47.  
  48.     do
  49.     {
  50.         *pch++ = (char)(dwNumber % 10) + '0';
  51.         dwNumber /= 10;
  52.     }
  53.     while (dwNumber > 0);
  54.  
  55.     if (bSign)
  56.         *pch++ = _T('-');
  57.  
  58.     *pch-- = 0;
  59.  
  60.     // will now reverse the string
  61.     TCHAR* pchRev = lpszResult;
  62.     TCHAR ch;
  63.     while (pch > pchRev)
  64.     {
  65.         ch = *pch;
  66.         *pch-- = *pchRev;
  67.         *pchRev++ = ch;
  68.     }
  69. }
  70.  
  71. interface DECLSPEC_UUID("120B729F-65BF-11D0-9DDC-00A0C9034892")
  72. IDuckDoer : public IUnknown
  73. {
  74. };
  75.  
  76. const IID IID_IDuckDoer = {0x120B729F,0x65BF,0x11D0,{0x9D,0xDC,0x00,0xA0,0xC9,0x03,0x48,0x92}};
  77.  
  78. LRESULT CMyDlg::OnCreateDoDuck(WORD wNotifyCode, WORD wID, HWND hwndCtl, BOOL& bHandled)
  79. {
  80.     UNUSED_ALWAYS(wID);
  81.     UNUSED_ALWAYS(hwndCtl);
  82.  
  83.     if (wNotifyCode != BN_CLICKED)
  84.     {
  85.         bHandled = FALSE;  // let someone else handle this message
  86.         return 0L;
  87.     }
  88.  
  89.     _ASSERT(m_pIDuckConnectionPoint == NULL);  // Only one connection point at a given time
  90.     if (!m_bOleInitialized)
  91.     {
  92.         OleInitialize(NULL);
  93.         m_bOleInitialized = TRUE;
  94.     }
  95.  
  96.     HRESULT hr;
  97.     IUnknown* pUnk = NULL;
  98.     IConnectionPointContainer* pConnPtContainer = NULL;
  99.     hr = CoCreateInstance(CLSID_DuckDoer, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void**)&pUnk);
  100.     if (!SUCCEEDED(hr))
  101.     {
  102.         ::MessageBox(m_hWnd, _T("Could not create DuckDoer object.  Make sure the server is registered."),
  103.             _T("Object Instantiation Error."), MB_OK | MB_ICONINFORMATION);
  104.  
  105.         return 0L;
  106.     }
  107.  
  108.     _ASSERT(pUnk != NULL);
  109.  
  110.     hr = pUnk->QueryInterface(IID_IConnectionPointContainer, (void**)&pConnPtContainer);
  111.     _ASSERT(SUCCEEDED(hr) && pConnPtContainer != NULL);
  112.     hr = pConnPtContainer->FindConnectionPoint(IID_IDuckInt, &m_pIDuckConnectionPoint);
  113.     _ASSERT(SUCCEEDED(hr) && m_pIDuckConnectionPoint != NULL);
  114.     _ASSERT(m_pDuckInt != NULL);
  115.     pUnk->Release();
  116.     pConnPtContainer->Release();
  117.  
  118.     // Will now disable controls in the dialog as needed
  119.     ::EnableWindow(GetDlgItem(IDOK), FALSE);  // cannot exit the application
  120.     ::EnableWindow(GetDlgItem(wID), FALSE);  // disable the creation of more doduck objects from this instance
  121.     ::EnableWindow(GetDlgItem(IDC_DESTROYDODUCK), TRUE);  // enable the button to release the connection point
  122.     ::EnableWindow(GetDlgItem(IDC_ADVISE), TRUE);
  123.     ::EnableWindow(GetDlgItem(IDC_UNADVISE), FALSE);
  124.  
  125.     HWND hwndStatus = GetDlgItem(IDC_STATUS);
  126.     _ASSERT(hwndStatus != NULL);
  127.     ::SetWindowText(hwndStatus, _T("Created object and got *IDuckConnectionPoint"));
  128.     return 0L;
  129. }
  130.  
  131. LRESULT CMyDlg::OnDestroyDoDuck(WORD wNotifyCode, WORD wID, HWND hwndCtl, BOOL& bHandled)
  132. {
  133.     UNUSED_ALWAYS(wID);
  134.     UNUSED_ALWAYS(hwndCtl);
  135.     UNUSED_ALWAYS(bHandled);
  136.  
  137.     _ASSERT(m_pIDuckConnectionPoint != NULL);
  138.     m_pIDuckConnectionPoint->Release();
  139.     m_pIDuckConnectionPoint = NULL;
  140.  
  141.     ::EnableWindow(GetDlgItem(IDOK), TRUE);
  142.     ::EnableWindow(GetDlgItem(wID), FALSE);
  143.     ::EnableWindow(GetDlgItem(IDC_CREATEDODUCK), TRUE);
  144.     ::EnableWindow(GetDlgItem(IDC_ADVISE), FALSE);
  145.     ::EnableWindow(GetDlgItem(IDC_UNADVISE), FALSE);
  146.     HWND hwndStatus = GetDlgItem(IDC_STATUS);
  147.     _ASSERT(hwndStatus != NULL);
  148.     ::SetWindowText(hwndStatus, _T("Released IDuckConnectionPoint interface."));
  149.  
  150.     ::SetWindowText(GetDlgItem(IDC_COOKIE), _T(""));
  151.     return 0L;
  152. }
  153.  
  154. LRESULT CMyDlg::OnInitDialog(UINT umsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled)
  155. {
  156.     UNUSED_ALWAYS(umsg);
  157.     UNUSED_ALWAYS(wParam);
  158.     UNUSED_ALWAYS(lParam);
  159.     UNUSED_ALWAYS(bHandled);
  160.  
  161.     CenterWindow();
  162.     m_pDuckInt = new CComObject<CDuckInt>;
  163.     _ASSERT(m_pDuckInt != NULL);
  164.     ((IDuckInt*)m_pDuckInt)->AddRef();
  165.     return 1;
  166. }
  167.  
  168.  
  169. LRESULT CMyDlg::OnOK(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  170. {
  171.     EndDialog(wID);
  172.     return 0;
  173. }
  174.  
  175. LRESULT CMyDlg::OnCancel(WORD wNotifyCode, WORD wID, HWND hWndCtl, BOOL& bHandled)
  176. {
  177.     EndDialog(wID);
  178.     return 0;
  179. }
  180.  
  181. LRESULT CMyDlg::OnAdvise(WORD wNotifyCode, WORD wID, HWND hwndCtl, BOOL& bHandled)
  182. {
  183.     HRESULT hr;
  184.     _ASSERT(m_pIDuckConnectionPoint != NULL);
  185.     hr = m_pIDuckConnectionPoint->Advise((IUnknown*)m_pDuckInt, &m_dwCookie);
  186.     _ASSERT(SUCCEEDED(hr) && m_dwCookie != 0);
  187.     HWND hwndCookie = GetDlgItem(IDC_COOKIE);
  188.     TCHAR  szCookie[10];
  189.     DoMyIToA(m_dwCookie, szCookie);
  190.     ::SetWindowText(hwndCookie, szCookie);
  191.     HWND hwndStatus = GetDlgItem(IDC_STATUS);
  192.     _ASSERT(hwndStatus != NULL);
  193.     ::SetWindowText(hwndStatus, _T("Advise called."));
  194.  
  195.     // enable only the valid buttons
  196.     ::EnableWindow(GetDlgItem(wID), FALSE);
  197.     ::EnableWindow(GetDlgItem(IDC_DESTROYDODUCK), FALSE);
  198.     ::EnableWindow(GetDlgItem(IDC_UNADVISE), TRUE);
  199.     return 0;
  200. }
  201.  
  202. LRESULT CMyDlg::OnUnadvise(WORD wNotifyCode, WORD wID, HWND hwndCtl, BOOL& bHandled)
  203. {
  204.     HRESULT hr;
  205.     _ASSERT(m_pIDuckConnectionPoint != NULL);
  206.     hr = m_pIDuckConnectionPoint->Unadvise(m_dwCookie);
  207.     _ASSERT(SUCCEEDED(hr));
  208.     m_dwCookie = 0;
  209.     HWND hwndCookie = GetDlgItem(IDC_COOKIE);
  210.     ::SetWindowText(hwndCookie, _T(""));
  211.     ::UpdateWindow(hwndCookie);
  212.     HWND hwndStatus = GetDlgItem(IDC_STATUS);
  213.     _ASSERT(hwndStatus != NULL);
  214.     ::SetWindowText(hwndStatus, _T("Unadvise called."));
  215.     ::UpdateWindow(hwndStatus);
  216.     ::EnableWindow(GetDlgItem(wID), FALSE);
  217.     ::EnableWindow(GetDlgItem(IDC_DESTROYDODUCK), TRUE);
  218.     ::EnableWindow(GetDlgItem(IDC_ADVISE), TRUE);
  219.     return 0;
  220. }
  221.