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

  1. // autoddlg.cpp : implementation file
  2. //
  3. // This is a part of the Microsoft Foundation Classes C++ library.
  4. // Copyright (C) 1992-1998 Microsoft Corporation
  5. // All rights reserved.
  6. //
  7. // This source code is only intended as a supplement to the
  8. // Microsoft Foundation Classes Reference and related
  9. // electronic documentation provided with the library.
  10. // See these sources for detailed information regarding the
  11. // Microsoft Foundation Classes product.
  12.  
  13. #include "stdafx.h"
  14. using namespace ACDual;
  15. #include "autodriv.h"
  16. #include "autoddlg.h"
  17.  
  18. #ifdef _DEBUG
  19. #undef THIS_FILE
  20. static char BASED_CODE THIS_FILE[] = __FILE__;
  21. #endif
  22.  
  23. void dump_com_error(_com_error &e)
  24. {
  25.     _bstr_t bstrSource(e.Source());
  26.     _bstr_t bstrDescription(e.Description());
  27.     TCHAR szTemp[1024];
  28.     CString csMsg = "Oops - hit an error!\n";
  29.     wsprintf(szTemp, _T("Code = %08lx\n"), e.Error());
  30.     csMsg += szTemp;
  31.     wsprintf(szTemp, _T("Code meaning = %s\n"), e.ErrorMessage());
  32.     csMsg += szTemp;
  33.     wsprintf(szTemp, _T("Source = %s\n"), bstrSource.length() ? (LPCTSTR)bstrSource : _T("null"));
  34.     csMsg += szTemp;
  35.     wsprintf(szTemp, _T("Description = %s\n"), bstrDescription.length() ? (LPCTSTR)bstrDescription : _T("null"));
  36.     csMsg += szTemp;
  37.     AfxMessageBox(csMsg);
  38. }
  39.  
  40. /////////////////////////////////////////////////////////////////////////////
  41. // CAutoDrivDlg dialog
  42.  
  43. CAutoDrivDlg::CAutoDrivDlg(CWnd* pParent /*=NULL*/)
  44.     : CDialog(CAutoDrivDlg::IDD, pParent)
  45. {
  46.     //{{AFX_DATA_INIT(CAutoDrivDlg)
  47.     m_szText = _T("");
  48.     m_x = 0;
  49.     m_y = 0;
  50.     m_bUseDispatch = TRUE;
  51.     //}}AFX_DATA_INIT
  52.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  53.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  54.  
  55.     m_pDualAutoClikObject = NULL;
  56. }
  57.  
  58.  
  59. void CAutoDrivDlg::DoDataExchange(CDataExchange* pDX)
  60. {
  61.     CDialog::DoDataExchange(pDX);
  62.     //{{AFX_DATA_MAP(CAutoDrivDlg)
  63.     DDX_Text(pDX, IDC_EDITTEXT, m_szText);
  64.     DDX_Text(pDX, IDC_EDITX, m_x);
  65.     DDX_Text(pDX, IDC_EDITY, m_y);
  66.     DDX_Check(pDX, IDC_USEDISPATCH, m_bUseDispatch);
  67.     //}}AFX_DATA_MAP
  68. }
  69.  
  70. BEGIN_MESSAGE_MAP(CAutoDrivDlg, CDialog)
  71.     //{{AFX_MSG_MAP(CAutoDrivDlg)
  72.     ON_WM_PAINT()
  73.     ON_WM_QUERYDRAGICON()
  74.     ON_BN_CLICKED(IDC_CLOSE, OnClose)
  75.     ON_WM_CREATE()
  76.     ON_BN_CLICKED(IDC_ANIMATE_XY, OnAnimateXY)
  77.     ON_BN_CLICKED(IDC_ANIMATEPOS, OnAnimatePos)
  78.     ON_BN_CLICKED(IDC_SETALL, OnSetAll)
  79.     ON_BN_CLICKED(IDC_SETPOS, OnSetPosition)
  80.     ON_BN_CLICKED(IDC_SETTEXT, OnSetText)
  81.     ON_BN_CLICKED(IDC_SETX, OnSetX)
  82.     ON_BN_CLICKED(IDC_SETY, OnSetY)
  83.     ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
  84.     ON_BN_CLICKED(IDC_GETALL, OnGetAll)
  85.     ON_BN_CLICKED(IDC_GETPOS, OnGetPosition)
  86.     ON_BN_CLICKED(IDC_USEDISPATCH, OnUseDispatch)
  87.     //}}AFX_MSG_MAP
  88. END_MESSAGE_MAP()
  89.  
  90. /////////////////////////////////////////////////////////////////////////////
  91. // CAutoDrivDlg message handlers
  92.  
  93. BOOL CAutoDrivDlg::OnInitDialog()
  94. {
  95.     if (!bool(m_pDualAutoClikObject))
  96.     {
  97.         m_bUseDispatch = TRUE;
  98.         GetDlgItem(IDC_USEDISPATCH)->EnableWindow(FALSE);
  99.     }
  100.  
  101.     m_szText = _T("Foobar");
  102.     m_x = 0;
  103.     m_y = 0;
  104.     CDialog::OnInitDialog();
  105.     CenterWindow();
  106.  
  107.     return TRUE;  // return TRUE  unless you set the focus to a control
  108. }
  109.  
  110. // If you add a minimize button to your dialog, you will need the code below
  111. //  to draw the icon.  For MFC applications using the document/view model,
  112. //  this is automatically done for you by the framework.
  113.  
  114. void CAutoDrivDlg::OnPaint()
  115. {
  116.     if (IsIconic())
  117.     {
  118.         CPaintDC dc(this); // device context for painting
  119.  
  120.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  121.  
  122.         // Center icon in client rectangle
  123.         int cxIcon = GetSystemMetrics(SM_CXICON);
  124.         int cyIcon = GetSystemMetrics(SM_CYICON);
  125.         CRect rect;
  126.         GetClientRect(&rect);
  127.         int x = (rect.Width() - cxIcon + 1) / 2;
  128.         int y = (rect.Height() - cyIcon + 1) / 2;
  129.  
  130.         // Draw the icon
  131.         dc.DrawIcon(x, y, m_hIcon);
  132.     }
  133.     else
  134.     {
  135.         CDialog::OnPaint();
  136.     }
  137. }
  138.  
  139. // The system calls this to obtain the cursor to display while the user drags
  140. //  the minimized window.
  141. HCURSOR CAutoDrivDlg::OnQueryDragIcon()
  142. {
  143.     return (HCURSOR) m_hIcon;
  144. }
  145.  
  146. int CAutoDrivDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
  147. {
  148.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  149.         return -1;  // fail
  150.  
  151.     try
  152.     {
  153.         HRESULT hr = m_pAutoClikObject.CreateInstance(__uuidof(Document));
  154.         if (FAILED(hr))
  155.             _com_raise_error(hr);
  156.     }
  157.     catch (_com_error& e)
  158.     {
  159.         AfxMessageBox(IDP_CANNOT_CREATE_AUTOCLIK);
  160.         dump_com_error(e);
  161.         return -1;  // fail
  162.     }
  163.  
  164.     // Get the dual interface corresponding to this dispatch object
  165.     ASSERT(m_pDualAutoClikObject == NULL);
  166.     try
  167.     {
  168.         m_pDualAutoClikObject = m_pAutoClikObject;
  169.     }
  170.     catch (_com_error& e)
  171.     {
  172.         AfxMessageBox(IDP_CANNOT_CREATE_AUTODUALCLIK);
  173.         dump_com_error(e);
  174.         return -1;  // fail
  175.     }
  176.  
  177.     m_pAutoClikObject->ShowWindow();
  178.  
  179.     return 0;   // success
  180. }
  181.  
  182. void CAutoDrivDlg::OnClose()
  183. {
  184.     if (m_pDualAutoClikObject)
  185.     {
  186.         try
  187.         {
  188.             m_pDualAutoClikObject.Release();
  189.         }
  190.         catch (_com_error ce)
  191.         {
  192.             AfxMessageBox(_T("We caught an exception releasing m_pDualAutoClikObject"));
  193.             dump_com_error(ce);
  194.         }
  195.     }
  196.  
  197.     if (m_pAutoClikObject)
  198.     {
  199.         try
  200.         {
  201.             m_pAutoClikObject.Release();
  202.         }
  203.         catch(_com_error ce)
  204.         {
  205.             AfxMessageBox(_T("We caught an exception releasing m_pAutoClikObject"));
  206.             dump_com_error(ce);
  207.         }
  208.     }
  209.     EndDialog(0);
  210. }
  211.  
  212. void CAutoDrivDlg::OnAnimateXY()
  213. {
  214.     if (m_bUseDispatch)
  215.     {
  216.         for (short i = 10; i <= 100; i += 5)
  217.         {
  218.             m_pAutoClikObject->x = i;
  219.             m_pAutoClikObject->y = i;
  220.         }
  221.     }
  222.     else
  223.     {
  224.         for (short i = 10; i <= 100; i += 5)
  225.         {
  226.             m_pDualAutoClikObject->x = i;
  227.             m_pDualAutoClikObject->y = i;
  228.         }
  229.     }
  230. }
  231.  
  232. void CAutoDrivDlg::OnAnimatePos()
  233. {
  234.     if (m_bUseDispatch)
  235.     {
  236.         IAutoClickPointPtr point = m_pAutoClikObject->Position;
  237.  
  238.         for (short i = 10; i <= 100; i += 5)
  239.         {
  240.             point->x = i;
  241.             point->y = i;
  242.             m_pAutoClikObject->Position = point;
  243.         }
  244.     }
  245.     else
  246.     {
  247.         IDualAutoClickPointPtr point = m_pDualAutoClikObject->Position;
  248.  
  249.         for (short i = 10; i <= 100; i += 5)
  250.         {
  251.             point->x = i;
  252.             point->y = i;
  253.             m_pDualAutoClikObject->Position = point;
  254.         }
  255.     }
  256. }
  257.  
  258. void CAutoDrivDlg::OnSetAll()
  259. {
  260.     UpdateData(TRUE);
  261.     _bstr_t bstr = (LPCTSTR) m_szText;
  262.  
  263.     if (m_bUseDispatch)
  264.     {
  265.  
  266.         m_pAutoClikObject->SetAllProps(m_x, m_y, bstr);
  267.     }
  268.     else
  269.     {
  270.         m_pDualAutoClikObject->SetAllProps(m_x, m_y, bstr);
  271.     }
  272. }
  273.  
  274. void CAutoDrivDlg::OnSetPosition()
  275. {
  276.     UpdateData(TRUE);
  277.     if (m_bUseDispatch)
  278.     {
  279.         IAutoClickPointPtr point = m_pAutoClikObject->Position;
  280.         point->x = m_x;
  281.         point->y = m_y;
  282.         m_pAutoClikObject->Position = point;
  283.     }
  284.     else
  285.     {
  286.         IDualAutoClickPointPtr point = m_pDualAutoClikObject->Position;
  287.         point->x = m_x;
  288.         point->y = m_y;
  289.         m_pDualAutoClikObject->Position = point;
  290.     }
  291. }
  292.  
  293. void CAutoDrivDlg::OnSetText()
  294. {
  295.     UpdateData(TRUE);
  296.     _bstr_t bstr = (LPCTSTR) m_szText;
  297.     try
  298.     {
  299.         if (m_bUseDispatch)
  300.         {
  301.             m_pAutoClikObject->text = bstr;
  302.         }
  303.         else
  304.         {
  305.             m_pDualAutoClikObject->text = bstr;
  306.         }
  307.     }
  308.     catch(_com_error& e)
  309.     {
  310.         dump_com_error(e);
  311.     }
  312. }
  313.  
  314. void CAutoDrivDlg::OnSetX()
  315. {
  316.     UpdateData(TRUE);
  317.     if (m_bUseDispatch)
  318.     {
  319.         m_pAutoClikObject->x = m_x;
  320.     }
  321.     else
  322.     {
  323.         m_pDualAutoClikObject->x = m_x;
  324.     }
  325. }
  326.  
  327. void CAutoDrivDlg::OnSetY()
  328. {
  329.     UpdateData(TRUE);
  330.     if (m_bUseDispatch)
  331.     {
  332.         m_pAutoClikObject->y = m_y;
  333.     }
  334.     else
  335.     {
  336.         m_pDualAutoClikObject->y = m_y;
  337.     }
  338. }
  339.  
  340. void CAutoDrivDlg::OnRefresh()
  341. {
  342.     UpdateData(TRUE);
  343.     if (m_bUseDispatch)
  344.     {
  345.         m_pAutoClikObject->RefreshWindow();
  346.     }
  347.     else
  348.     {
  349.         m_pDualAutoClikObject->RefreshWindow();
  350.     }
  351. }
  352.  
  353. void CAutoDrivDlg::OnGetAll()
  354. {
  355.     if (m_bUseDispatch)
  356.     {
  357.         m_x = m_pAutoClikObject->x;
  358.         m_y = m_pAutoClikObject->y;
  359.         m_szText = (LPCTSTR) m_pAutoClikObject->text;
  360.     }
  361.     else
  362.     {
  363.         m_x = m_pDualAutoClikObject->x;
  364.         m_y = m_pDualAutoClikObject->y;
  365.         m_szText = (LPCTSTR) m_pDualAutoClikObject->text;
  366.     }
  367.     UpdateData(FALSE);
  368. }
  369.  
  370. void CAutoDrivDlg::OnGetPosition()
  371. {
  372.     if (m_bUseDispatch)
  373.     {
  374.         IAutoClickPointPtr point = m_pAutoClikObject->Position;
  375.         m_x = point->x;
  376.         m_y = point->y;
  377.  
  378.     }
  379.     else
  380.     {
  381.         IDualAutoClickPointPtr point = m_pDualAutoClikObject->Position;
  382.         m_x = point->x;
  383.         m_y = point->y;
  384.  
  385.     }
  386.     UpdateData(FALSE);
  387. }
  388.  
  389. void CAutoDrivDlg::OnUseDispatch()
  390. {
  391.     UpdateData(TRUE);
  392. }
  393.