home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / msdn_vcb / samples / vc98 / mfc / tutorial / autodriv / autoddlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-03-27  |  5.0 KB  |  217 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.     //}}AFX_DATA_INIT
  34.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  35.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  36. }
  37.  
  38.  
  39. void CAutoDrivDlg::DoDataExchange(CDataExchange* pDX)
  40. {
  41.     CDialog::DoDataExchange(pDX);
  42.     //{{AFX_DATA_MAP(CAutoDrivDlg)
  43.     DDX_Text(pDX, IDC_EDITTEXT, m_szText);
  44.     DDX_Text(pDX, IDC_EDITX, m_x);
  45.     DDX_Text(pDX, IDC_EDITY, m_y);
  46.     //}}AFX_DATA_MAP
  47. }
  48.  
  49. BEGIN_MESSAGE_MAP(CAutoDrivDlg, CDialog)
  50.     //{{AFX_MSG_MAP(CAutoDrivDlg)
  51.     ON_WM_PAINT()
  52.     ON_WM_QUERYDRAGICON()
  53.     ON_BN_CLICKED(IDC_CLOSE, OnClose)
  54.     ON_WM_CREATE()
  55.     ON_BN_CLICKED(IDC_ANIMATE_XY, OnAnimateXY)
  56.     ON_BN_CLICKED(IDC_ANIMATEPOS, OnAnimatePos)
  57.     ON_BN_CLICKED(IDC_SETALL, OnSetAll)
  58.     ON_BN_CLICKED(IDC_SETPOS, OnSetPosition)
  59.     ON_BN_CLICKED(IDC_SETTEXT, OnSetText)
  60.     ON_BN_CLICKED(IDC_SETX, OnSetX)
  61.     ON_BN_CLICKED(IDC_SETY, OnSetY)
  62.     ON_BN_CLICKED(IDC_REFRESH, OnRefresh)
  63.     ON_BN_CLICKED(IDC_GETALL, OnGetAll)
  64.     ON_BN_CLICKED(IDC_GETPOS, OnGetPosition)
  65.     //}}AFX_MSG_MAP
  66. END_MESSAGE_MAP()
  67.  
  68. /////////////////////////////////////////////////////////////////////////////
  69. // CAutoDrivDlg message handlers
  70.  
  71. BOOL CAutoDrivDlg::OnInitDialog()
  72. {
  73.     CDialog::OnInitDialog();
  74.     CenterWindow();
  75.  
  76.     // TODO: Add extra initialization here
  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 CAutoDrivDlg::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 CAutoDrivDlg::OnQueryDragIcon()
  113. {
  114.     return (HCURSOR) m_hIcon;
  115. }
  116.  
  117. int CAutoDrivDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
  118. {
  119.     if (CDialog::OnCreate(lpCreateStruct) == -1)
  120.         return -1;  // fail
  121.  
  122.     if (!m_autoClikObject.CreateDispatch(_T("AutoClick.Document")))
  123.     {
  124.         AfxMessageBox(IDP_CANNOT_CREATE_AUTOCLIK);
  125.         return -1;  // fail
  126.     }
  127.     m_autoClikObject.ShowWindow();
  128.  
  129.     return 0;   // success
  130. }
  131.  
  132. void CAutoDrivDlg::OnClose()
  133. {
  134.     EndDialog(0);
  135. }
  136.  
  137. void CAutoDrivDlg::OnAnimateXY()
  138. {
  139.     for (short i = 10; i <= 100; i += 5)
  140.     {
  141.         m_autoClikObject.SetX(i);
  142.         m_autoClikObject.SetY(i);
  143.     }
  144. }
  145.  
  146. void CAutoDrivDlg::OnAnimatePos()
  147. {
  148.     CClikPoint point;
  149.     LPDISPATCH lpPointDiaptch = m_autoClikObject.GetPosition();
  150.  
  151.     point.AttachDispatch(lpPointDiaptch);
  152.     for (short i = 10; i <= 100; i += 5)
  153.     {
  154.         point.SetX(i);
  155.         point.SetY(i);
  156.         m_autoClikObject.SetPosition(lpPointDiaptch);
  157.     }
  158. }
  159.  
  160. void CAutoDrivDlg::OnSetAll()
  161. {
  162.     UpdateData(TRUE);
  163.     m_autoClikObject.SetAllProps((short)m_x, (short)m_y, m_szText);
  164. }
  165.  
  166. void CAutoDrivDlg::OnSetPosition()
  167. {
  168.     CClikPoint point;
  169.     point.AttachDispatch(m_autoClikObject.GetPosition());
  170.  
  171.     UpdateData(TRUE);
  172.     point.SetX((short)m_x);
  173.     point.SetY((short)m_y);
  174.     m_autoClikObject.SetPosition(point.m_lpDispatch);
  175. }
  176.  
  177. void CAutoDrivDlg::OnSetText()
  178. {
  179.     UpdateData(TRUE);
  180.     m_autoClikObject.SetText(m_szText);
  181. }
  182.  
  183. void CAutoDrivDlg::OnSetX()
  184. {
  185.     UpdateData(TRUE);
  186.     m_autoClikObject.SetX((short)m_x);
  187. }
  188.  
  189. void CAutoDrivDlg::OnSetY()
  190. {
  191.     UpdateData(TRUE);
  192.     m_autoClikObject.SetY((short)m_y);
  193. }
  194.  
  195. void CAutoDrivDlg::OnRefresh()
  196. {
  197.     m_autoClikObject.RefreshWindow();
  198. }
  199.  
  200. void CAutoDrivDlg::OnGetAll()
  201. {
  202.     m_x = m_autoClikObject.GetX();
  203.     m_y = m_autoClikObject.GetY();
  204.     m_szText = m_autoClikObject.GetText();
  205.     UpdateData(FALSE);
  206. }
  207.  
  208. void CAutoDrivDlg::OnGetPosition()
  209. {
  210.     CClikPoint point;
  211.     point.AttachDispatch(m_autoClikObject.GetPosition());
  212.  
  213.     m_x = point.GetX();
  214.     m_y = point.GetY();
  215.     UpdateData(FALSE);
  216. }
  217.