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

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