home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / com / connect / mdrive / drivedlg.cpp next >
Encoding:
C/C++ Source or Header  |  1998-04-03  |  5.9 KB  |  232 lines

  1. // DriveDlg.cpp : implementation file
  2. //
  3.  
  4. #include "premdriv.h"
  5. #include "MDrive.h"
  6. #include "DriveDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. #include "driver.h"
  15.  
  16. HDC hDrawDC;
  17. int nHeight;
  18. int nThreads=0;
  19.  
  20. /////////////////////////////////////////////////////////////////////////////
  21. // CMDriveDlg dialog
  22.  
  23. CMDriveDlg::CMDriveDlg(CWnd* pParent /*=NULL*/)
  24.     : CDialog(CMDriveDlg::IDD, pParent)
  25. {
  26.     //{{AFX_DATA_INIT(CMDriveDlg)
  27.         // NOTE: the ClassWizard will add member initialization here
  28.     //}}AFX_DATA_INIT
  29.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  30.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  31.     m_nAdviseCnt = 0;
  32.     m_arrAdvise = NULL;
  33. }
  34.  
  35. void CMDriveDlg::DoDataExchange(CDataExchange* pDX)
  36. {
  37.     CDialog::DoDataExchange(pDX);
  38.     //{{AFX_DATA_MAP(CMDriveDlg)
  39.         // NOTE: the ClassWizard will add DDX and DDV calls here
  40.     //}}AFX_DATA_MAP
  41. }
  42.  
  43. BEGIN_MESSAGE_MAP(CMDriveDlg, CDialog)
  44.     //{{AFX_MSG_MAP(CMDriveDlg)
  45.     ON_WM_PAINT()
  46.     ON_WM_QUERYDRAGICON()
  47.     ON_BN_CLICKED(IDB_START, OnStart)
  48.     ON_BN_CLICKED(IDB_STOP, OnStop)
  49.     ON_BN_CLICKED(IDB_STOP_ALL, OnStopAll)
  50.     ON_BN_CLICKED(IDB_ADVISE, OnAdvise)
  51.     ON_BN_CLICKED(IDB_UNADVISE, OnUnadvise)
  52.     //}}AFX_MSG_MAP
  53. END_MESSAGE_MAP()
  54.  
  55. /////////////////////////////////////////////////////////////////////////////
  56. // CMDriveDlg message handlers
  57.  
  58. BOOL CMDriveDlg::OnInitDialog()
  59. {
  60.     CDialog::OnInitDialog();
  61.  
  62.     // Set the icon for this dialog.  The framework does this automatically
  63.     //  when the application's main window is not a dialog
  64.     SetIcon(m_hIcon, TRUE);         // Set big icon
  65.     SetIcon(m_hIcon, FALSE);        // Set small icon
  66.     
  67.     CWnd* pWnd = GetDlgItem(IDC_PICTURE);
  68.     hDrawDC = ::GetDC(pWnd->m_hWnd);
  69.     CRect rect;
  70.     pWnd->GetClientRect(&rect);
  71.     nHeight = rect.Height();
  72.     m_nMaxAdvises = rect.Width();
  73.     m_arrAdvise = new DWORD[m_nMaxAdvises];
  74.  
  75.     if (FAILED(pRandom.CreateInstance(__uuidof(CoRandom))))
  76.         AfxMessageBox("Could not create IRandom\n");
  77.     
  78.     return TRUE;  // return TRUE  unless you set the focus to a control
  79. }
  80.  
  81. // If you add a minimize button to your dialog, you will need the code below
  82. //  to draw the icon.  For MFC applications using the document/view model,
  83. //  this is automatically done for you by the framework.
  84.  
  85. void CMDriveDlg::OnPaint() 
  86. {
  87.     if (IsIconic())
  88.     {
  89.         CPaintDC dc(this); // device context for painting
  90.  
  91.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  92.  
  93.         // Center icon in client rectangle
  94.         int cxIcon = GetSystemMetrics(SM_CXICON);
  95.         int cyIcon = GetSystemMetrics(SM_CYICON);
  96.         CRect rect;
  97.         GetClientRect(&rect);
  98.         int x = (rect.Width() - cxIcon + 1) / 2;
  99.         int y = (rect.Height() - cyIcon + 1) / 2;
  100.  
  101.         // Draw the icon
  102.         dc.DrawIcon(x, y, m_hIcon);
  103.     }
  104.     else
  105.     {
  106.         CDialog::OnPaint();
  107.     }
  108. }
  109.  
  110. // The system calls this to obtain the cursor to display while the user drags
  111. //  the minimized window.
  112. HCURSOR CMDriveDlg::OnQueryDragIcon()
  113. {
  114.     return (HCURSOR) m_hIcon;
  115. }
  116.  
  117. void CMDriveDlg::OnStart() 
  118. {
  119.     if (nThreads < nMaxThreads)
  120.     {
  121.         if (SUCCEEDED(pRandom->get_Start(&m_arrID[nThreads])))
  122.             nThreads++;
  123.     }
  124. }
  125.  
  126. void CMDriveDlg::OnStop() 
  127. {
  128.     if (nThreads > 0)
  129.         Stop();
  130. }
  131.  
  132. void CMDriveDlg::OnStopAll() 
  133. {
  134.     while (Stop())
  135.         ;
  136. }
  137.  
  138. BOOL CMDriveDlg::Stop()
  139. {
  140.     if (nThreads > 0)
  141.     {
  142.         pRandom->Stop = m_arrID[nThreads-1];
  143.         --nThreads;
  144.         return TRUE;
  145.     }
  146.     else
  147.         return FALSE;
  148. }
  149.  
  150. void CMDriveDlg::OnCancel() 
  151. {
  152.     OnStopAll();
  153.     while (Unadvise())
  154.         ;
  155.     ::ReleaseDC(GetDlgItem(IDC_PICTURE)->m_hWnd, hDrawDC);
  156.     CDialog::OnCancel();
  157. }
  158.  
  159. void CMDriveDlg::OnOK() 
  160. {
  161.     OnStopAll();
  162.     while (Unadvise())
  163.         ;
  164.     ::ReleaseDC(GetDlgItem(IDC_PICTURE)->m_hWnd, hDrawDC);
  165.     CDialog::OnOK();
  166. }
  167.  
  168. void CMDriveDlg::OnAdvise() 
  169. {
  170.     if (m_nAdviseCnt < m_nMaxAdvises)
  171.     {
  172.         IConnectionPointContainerPtr pCPC;
  173.         IConnectionPointPtr pCP;
  174.         IRandomEventPtr pRandomEvent;
  175.         
  176.         HRESULT hRes = S_OK;
  177.         // Belaboring the point -- this is essentially CoCreateInstance
  178.         CRandomEventClassFactory* pFact = new CRandomEventClassFactory;
  179.         if (pFact == 0)
  180.             hRes = E_OUTOFMEMORY;
  181.         if (SUCCEEDED(hRes))
  182.             hRes = pFact->CreateInstance(NULL, __uuidof(IRandomEvent), (void**) &pRandomEvent);
  183.         delete pFact;
  184.         
  185.         pRandomEvent->ID = m_nAdviseCnt;
  186.         try {
  187.         if (SUCCEEDED(hRes))
  188.             pCPC = pRandom;
  189.         } catch(...) {
  190.         AfxMessageBox("OnAdvise(IRandom::QI(IID_IConnectionPointContainer) Failed)\n");
  191.         }
  192.         if (SUCCEEDED(hRes))
  193.             hRes = pCPC->FindConnectionPoint(__uuidof(IRandomEvent), &pCP);
  194.         if (SUCCEEDED(hRes))
  195.             hRes = pCP->Advise(pRandomEvent, &m_arrAdvise[m_nAdviseCnt++]);
  196.         if (FAILED(hRes))
  197.         {
  198.             AfxMessageBox("Advise failed");
  199.             m_nAdviseCnt--;
  200.         }
  201.     }
  202.     else
  203.         AfxMessageBox("No more advises");
  204. }
  205.  
  206. void CMDriveDlg::OnUnadvise() 
  207. {
  208.     if (!Unadvise())
  209.         AfxMessageBox("Unadvise failed");
  210. }
  211.  
  212. BOOL CMDriveDlg::Unadvise()
  213. {
  214.     if (m_nAdviseCnt > 0)
  215.     {
  216.         IConnectionPointContainerPtr pCPC;
  217.         IConnectionPointPtr pCP;
  218.         try {
  219.         pCPC = pRandom;
  220.         } catch(...) {
  221.         AfxMessageBox("OnAdvise(IRandom::QI(IID_IConnectionPointContainer) Failed)\n");
  222.         return FALSE;
  223.         }
  224.         HRESULT hRes = pCPC->FindConnectionPoint(__uuidof(IRandomEvent), &pCP);
  225.         if (SUCCEEDED(hRes))
  226.             hRes = pCP->Unadvise(m_arrAdvise[--m_nAdviseCnt]);
  227.         return (SUCCEEDED(hRes));
  228.     }
  229.     else
  230.         return FALSE;
  231. }
  232.