home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / ole / acdual / mfcctrl / autoddlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  7.9 KB  |  368 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. #include "autodriv.h"
  15. #include "autoclik.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. /////////////////////////////////////////////////////////////////////////////
  24. // CAutoDrivDlg dialog
  25.  
  26. CAutoDrivDlg::CAutoDrivDlg(CWnd* pParent /*=NULL*/)
  27.     : CDialog(CAutoDrivDlg::IDD, pParent)
  28. {
  29.     //{{AFX_DATA_INIT(CAutoDrivDlg)
  30.     m_szText = _T("");
  31.     m_x = 0;
  32.     m_y = 0;
  33.     m_bUseDispatch = TRUE;
  34.     //}}AFX_DATA_INIT
  35.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  36.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  37.  
  38.     m_pDualAutoClikObject = NULL;
  39. }
  40.  
  41.  
  42. void CAutoDrivDlg::DoDataExchange(CDataExchange* pDX)
  43. {
  44.     CDialog::DoDataExchange(pDX);
  45.     //{{AFX_DATA_MAP(CAutoDrivDlg)
  46.     DDX_Text(pDX, IDC_EDITTEXT, m_szText);
  47.     DDX_Text(pDX, IDC_EDITX, m_x);
  48.     DDX_Text(pDX, IDC_EDITY, m_y);
  49.     DDX_Check(pDX, IDC_USEDISPATCH, m_bUseDispatch);
  50.     //}}AFX_DATA_MAP
  51. }
  52.  
  53. BEGIN_MESSAGE_MAP(CAutoDrivDlg, CDialog)
  54.     //{{AFX_MSG_MAP(CAutoDrivDlg)
  55.     ON_WM_PAINT()
  56.     ON_WM_QUERYDRAGICON()
  57.     ON_BN_CLICKED(IDC_CLOSE, OnClose)
  58.     ON_WM_CREATE()
  59.     ON_BN_CLICKED(IDC_ANIMATE_XY, OnAnimateXY)
  60.     ON_BN_CLICKED(IDC_ANIMATEPOS, OnAnimatePos)
  61.     ON_BN_CLICKED(IDC_SETALL, OnSetAll)
  62.     ON_BN_CLICKED(IDC_SETPOS, OnSetPosition)
  63.     ON_BN_CLICKED(IDC_SETTEXT, OnSetText)
  64.     ON_BN_CLICKED(IDC_SETX, OnSetX)
  65.     ON_BN_CLICKED(IDC_SETY, OnSetY)
  66.     ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
  67.     ON_BN_CLICKED(IDC_GETALL, OnGetAll)
  68.     ON_BN_CLICKED(IDC_GETPOS, OnGetPosition)
  69.     ON_BN_CLICKED(IDC_USEDISPATCH, OnUseDispatch)
  70.     //}}AFX_MSG_MAP
  71. END_MESSAGE_MAP()
  72.  
  73. /////////////////////////////////////////////////////////////////////////////
  74. // CAutoDrivDlg message handlers
  75.  
  76. BOOL CAutoDrivDlg::OnInitDialog()
  77. {
  78.     if (!m_pDualAutoClikObject)
  79.     {
  80.         m_bUseDispatch = TRUE;
  81.         GetDlgItem(IDC_USEDISPATCH)->EnableWindow(FALSE);
  82.     }
  83.  
  84.     CDialog::OnInitDialog();
  85.     CenterWindow();
  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 CAutoDrivDlg::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 CAutoDrivDlg::OnQueryDragIcon()
  122. {
  123.     return (HCURSOR) m_hIcon;
  124. }
  125.  
  126. int CAutoDrivDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
  127. {
  128.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  129.         return -1;  // fail
  130.  
  131.     if (!m_autoClikObject.CreateDispatch(_T("ACDual.Document")))
  132.     {
  133.         AfxMessageBox(IDP_CANNOT_CREATE_AUTOCLIK);
  134.         return -1;  // fail
  135.     }
  136.  
  137.     // Get the custom interface corresponding to this dispatch object
  138.     ASSERT(m_pDualAutoClikObject == NULL);
  139.     m_autoClikObject.m_lpDispatch->QueryInterface(IID_IDualAClick,
  140.                                                   (LPVOID*)&m_pDualAutoClikObject);
  141.  
  142.     m_autoClikObject.ShowWindow();
  143.  
  144.     return 0;   // success
  145. }
  146.  
  147. void CAutoDrivDlg::OnClose()
  148. {
  149.     if (m_pDualAutoClikObject)
  150.     {
  151.         m_pDualAutoClikObject->Release();
  152.         m_pDualAutoClikObject = NULL;
  153.     }
  154.     EndDialog(0);
  155. }
  156.  
  157. void CAutoDrivDlg::OnAnimateXY()
  158. {
  159.     if (m_bUseDispatch)
  160.     {
  161.         for (short i = 10; i <= 100; i += 5)
  162.         {
  163.             m_autoClikObject.SetX(i);
  164.             m_autoClikObject.SetY(i);
  165.         }
  166.     }
  167.     else
  168.     {
  169.         ASSERT(m_pDualAutoClikObject != NULL);
  170.         for (short i = 10; i <= 100; i += 5)
  171.         {
  172.             m_pDualAutoClikObject->put_x(i);
  173.             m_pDualAutoClikObject->put_y(i);
  174.         }
  175.     }
  176. }
  177.  
  178. void CAutoDrivDlg::OnAnimatePos()
  179. {
  180.     if (m_bUseDispatch)
  181.     {
  182.         CClikPoint point;
  183.         LPDISPATCH lpPointDispatch = m_autoClikObject.GetPosition();
  184.  
  185.         point.AttachDispatch(lpPointDispatch);
  186.         for (short i = 10; i <= 100; i += 5)
  187.         {
  188.             point.SetX(i);
  189.             point.SetY(i);
  190.             m_autoClikObject.SetPosition(lpPointDispatch);
  191.         }
  192.     }
  193.     else
  194.     {
  195.         ASSERT(m_pDualAutoClikObject != NULL);
  196.         IDualAutoClickPoint* lpPoint = NULL;
  197.         m_pDualAutoClikObject->get_Position(&lpPoint);
  198.         if (lpPoint)
  199.         {
  200.             for (short i = 10; i <= 100; i += 5)
  201.             {
  202.                 lpPoint->put_x(i);
  203.                 lpPoint->put_y(i);
  204.                 m_pDualAutoClikObject->put_Position(lpPoint);
  205.             }
  206.             lpPoint->Release();
  207.         }
  208.     }
  209. }
  210.  
  211. void CAutoDrivDlg::OnSetAll()
  212. {
  213.     UpdateData(TRUE);
  214.  
  215.     if (m_bUseDispatch)
  216.     {
  217.         m_autoClikObject.SetAllProps((short)m_x, (short)m_y, m_szText);
  218.     }
  219.     else
  220.     {
  221.         ASSERT(m_pDualAutoClikObject != NULL);
  222.         m_pDualAutoClikObject->SetAllProps((short)m_x, (short)m_y,
  223.                                            m_szText.AllocSysString());
  224.     }
  225.  
  226. }
  227.  
  228. void CAutoDrivDlg::OnSetPosition()
  229. {
  230.     if (m_bUseDispatch)
  231.     {
  232.         CClikPoint point;
  233.         point.AttachDispatch(m_autoClikObject.GetPosition());
  234.  
  235.         UpdateData(TRUE);
  236.         point.SetX((short)m_x);
  237.         point.SetY((short)m_y);
  238.         m_autoClikObject.SetPosition(point.m_lpDispatch);
  239.     }
  240.     else
  241.     {
  242.         ASSERT(m_pDualAutoClikObject != NULL);
  243.         IDualAutoClickPoint* lpPoint = NULL;
  244.         m_pDualAutoClikObject->get_Position(&lpPoint);
  245.         if (lpPoint)
  246.         {
  247.             UpdateData(TRUE);
  248.             lpPoint->put_x((short)m_x);
  249.             lpPoint->put_y((short)m_y);
  250.             m_pDualAutoClikObject->put_Position(lpPoint);
  251.             lpPoint->Release();
  252.         }
  253.     }
  254. }
  255.  
  256. void CAutoDrivDlg::OnSetText()
  257. {
  258.     UpdateData(TRUE);
  259.  
  260.     if (m_bUseDispatch)
  261.     {
  262.         m_autoClikObject.SetText(m_szText);
  263.     }
  264.     else
  265.     {
  266.         ASSERT(m_pDualAutoClikObject != NULL);
  267.         m_pDualAutoClikObject->put_text(m_szText.AllocSysString());
  268.     }
  269. }
  270.  
  271. void CAutoDrivDlg::OnSetX()
  272. {
  273.     UpdateData(TRUE);
  274.  
  275.     if (m_bUseDispatch)
  276.     {
  277.         m_autoClikObject.SetX((short)m_x);
  278.     }
  279.     else
  280.     {
  281.         ASSERT(m_pDualAutoClikObject != NULL);
  282.         m_pDualAutoClikObject->put_x((short)m_x);
  283.     }
  284. }
  285.  
  286. void CAutoDrivDlg::OnSetY()
  287. {
  288.     UpdateData(TRUE);
  289.  
  290.     if (m_bUseDispatch)
  291.     {
  292.         m_autoClikObject.SetY((short)m_y);
  293.     }
  294.     else
  295.     {
  296.         ASSERT(m_pDualAutoClikObject != NULL);
  297.         m_pDualAutoClikObject->put_y((short)m_y);
  298.     }
  299. }
  300.  
  301. void CAutoDrivDlg::OnRefresh()
  302. {
  303.     if (m_bUseDispatch)
  304.     {
  305.         m_autoClikObject.RefreshWindow();
  306.     }
  307.     else
  308.     {
  309.         ASSERT(m_pDualAutoClikObject != NULL);
  310.         m_pDualAutoClikObject->RefreshWindow();
  311.     }
  312. }
  313.  
  314. void CAutoDrivDlg::OnGetAll()
  315. {
  316.     if (m_bUseDispatch)
  317.     {
  318.         m_x = m_autoClikObject.GetX();
  319.         m_y = m_autoClikObject.GetY();
  320.         m_szText = m_autoClikObject.GetText();
  321.     }
  322.     else
  323.     {
  324.         ASSERT(m_pDualAutoClikObject != NULL);
  325.         short x, y;
  326.         m_pDualAutoClikObject->get_x(&x); m_x = x;
  327.         m_pDualAutoClikObject->get_y(&y); m_y = y;
  328.  
  329.         BSTR bstrText;
  330.         m_pDualAutoClikObject->get_text(&bstrText);
  331.         m_szText = bstrText;
  332.         ::SysFreeString(bstrText);
  333.     }
  334.  
  335.     UpdateData(FALSE);
  336. }
  337.  
  338. void CAutoDrivDlg::OnGetPosition()
  339. {
  340.     if (m_bUseDispatch)
  341.     {
  342.         CClikPoint point;
  343.         point.AttachDispatch(m_autoClikObject.GetPosition());
  344.  
  345.         m_x = point.GetX();
  346.         m_y = point.GetY();
  347.     }
  348.     else
  349.     {
  350.         ASSERT(m_pDualAutoClikObject != NULL);
  351.         IDualAutoClickPoint* lpPoint = NULL;
  352.         m_pDualAutoClikObject->get_Position(&lpPoint);
  353.         if (lpPoint)
  354.         {
  355.             short x, y;
  356.             lpPoint->get_x(&x); m_x = x;
  357.             lpPoint->get_y(&y); m_y = y;
  358.             lpPoint->Release();
  359.         }
  360.     }
  361.     UpdateData(FALSE);
  362. }
  363.  
  364. void CAutoDrivDlg::OnUseDispatch()
  365. {
  366.     UpdateData(TRUE);
  367. }
  368.