DRIVEDLG.CPP
上传用户:bangxh
上传日期:2007-01-31
资源大小:42235k
文件大小:6k
源码类别:

Windows编程

开发平台:

Visual C++

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