home *** CD-ROM | disk | FTP | other *** search
/ Mastering MFC Development / MMD.ISO / samples / c13 / useatltrigarc / useatltrigarcdlg.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1997-02-20  |  4.5 KB  |  173 lines

  1. // UseAtlTrigArcDlg.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "UseAtlTrigArc.h"
  6. #include "UseAtlTrigArcDlg.h"
  7.  
  8. #ifdef _DEBUG
  9. #define new DEBUG_NEW
  10. #undef THIS_FILE
  11. static char THIS_FILE[] = __FILE__;
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. // CUseAtlTrigArcDlg dialog
  16.  
  17. CUseAtlTrigArcDlg::CUseAtlTrigArcDlg(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CUseAtlTrigArcDlg::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CUseAtlTrigArcDlg)
  21.         // NOTE: the ClassWizard will add member initialization here
  22.     //}}AFX_DATA_INIT
  23.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  24. }
  25.  
  26. void CUseAtlTrigArcDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28.     CDialog::DoDataExchange(pDX);
  29.     //{{AFX_DATA_MAP(CUseAtlTrigArcDlg)
  30.         // NOTE: the ClassWizard will add DDX and DDV calls here
  31.     //}}AFX_DATA_MAP
  32. }
  33.  
  34. BEGIN_MESSAGE_MAP(CUseAtlTrigArcDlg, CDialog)
  35.     //{{AFX_MSG_MAP(CUseAtlTrigArcDlg)
  36.     ON_WM_PAINT()
  37.     ON_WM_QUERYDRAGICON()
  38.     ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
  39.     ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
  40.     //}}AFX_MSG_MAP
  41. END_MESSAGE_MAP()
  42.  
  43. /////////////////////////////////////////////////////////////////////////////
  44. // CUseAtlTrigArcDlg message handlers
  45.  
  46. BOOL CUseAtlTrigArcDlg::OnInitDialog()
  47. {
  48.     CDialog::OnInitDialog();
  49.  
  50.     SetIcon(m_hIcon, TRUE);            // Set big icon
  51.     SetIcon(m_hIcon, FALSE);        // Set small icon
  52.     
  53.     // TODO: Add extra initialization here
  54.     
  55.     return TRUE;  // return TRUE  unless you set the focus to a control
  56. }
  57.  
  58. // If you add a minimize button to your dialog, you will need the code below
  59. //  to draw the icon.  For MFC applications using the document/view model,
  60. //  this is automatically done for you by the framework.
  61.  
  62. void CUseAtlTrigArcDlg::OnPaint() 
  63. {
  64.     if (IsIconic())
  65.     {
  66.         CPaintDC dc(this); // device context for painting
  67.  
  68.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  69.  
  70.         // Center icon in client rectangle
  71.         int cxIcon = GetSystemMetrics(SM_CXICON);
  72.         int cyIcon = GetSystemMetrics(SM_CYICON);
  73.         CRect rect;
  74.         GetClientRect(&rect);
  75.         int x = (rect.Width() - cxIcon + 1) / 2;
  76.         int y = (rect.Height() - cyIcon + 1) / 2;
  77.  
  78.         // Draw the icon
  79.         dc.DrawIcon(x, y, m_hIcon);
  80.     }
  81.     else
  82.     {
  83.         CDialog::OnPaint();
  84.     }
  85. }
  86.  
  87. HCURSOR CUseAtlTrigArcDlg::OnQueryDragIcon()
  88. {
  89.     return (HCURSOR) m_hIcon;
  90. }
  91.  
  92. void CUseAtlTrigArcDlg::OnButton1() 
  93. {
  94.     double x = 1.0, r;
  95.     CString s;
  96.  
  97.     // Begin with one interface of CLSID_trig
  98.     ItrigPtr pTrig(CLSID_trig);
  99.     pTrig->ATLsin(x, &r);
  100.     s.Format("The sin of %3.1lf is %lf", x, r);
  101.     MessageBox(s);
  102.  
  103.     // Attempt to switch to another interface.
  104.     // If successful, invoke one of its methods.
  105.     // That pTrig and pArc are smart pointers, courtesy
  106.     // of _com_ptr_t, negates the need to do reference
  107.     // counting by the client programmer.
  108.     IarcPtr pArc;
  109.     HRESULT rc = pTrig->QueryInterface(IID_Iarc, (void **)&pArc);
  110.     if (S_OK == rc)
  111.     {
  112.         pArc->ATLatan(x, &r);
  113.         s.Format("The arctangent of %3.1f is %lf", x, r);
  114.         MessageBox(s);
  115.     }
  116.  
  117.     // Query for a non-existent interface just to show
  118.     // how it might be done.
  119.     IPersistStreamPtr pPersistStream;
  120.     rc = pArc->QueryInterface(IID_IPersistStream, 
  121.                             (void **)&pPersistStream);
  122.     if (E_NOINTERFACE == rc)
  123.         MessageBox("The object doesn't support IPersistStream.");
  124. }
  125.  
  126. void CUseAtlTrigArcDlg::OnButton2() 
  127. {
  128.     double x = 1.0, r;
  129.     CString s;
  130.  
  131.     // See comments in OnButton1. The purpose here is to show
  132.     // that the process works both ways: We can begin with 
  133.     // one interface and switch to the other. Note, too, that
  134.     // this version uses exception handling,
  135.     // which is a very good idea.
  136.  
  137.     try
  138.     {
  139.         IarcPtr pArc(CLSID_trig);
  140.         pArc->ATLacos(x, &r);
  141.         s.Format("The arccosine of %3.1lf is %lf", x, r);
  142.         MessageBox(s);
  143.  
  144.         ItrigPtr pTrig;
  145.         HRESULT rc = pArc->QueryInterface(IID_Iarc, (void **)&pTrig);
  146.         if (S_OK == rc)
  147.         {
  148.             pTrig->ATLtan(x, &r);
  149.             s.Format("The tangent of %3.1f is %lf", x, r);
  150.             MessageBox(s);
  151.         }
  152.     }
  153.     catch (_com_error &ex)
  154.     {
  155.         _bstr_t bstrSource(ex.Source());
  156.         _bstr_t bstrDescription(ex.Description());
  157.         char szTemp[1024];
  158.         CString csMsg = "Oops - hit an error!\n";
  159.         wsprintf(szTemp, "Code = %08lx\n", ex.Error());
  160.         csMsg += szTemp;
  161.         wsprintf(szTemp, "Code meaning = %s\n", ex.ErrorMessage());
  162.         csMsg += szTemp;
  163.         wsprintf(szTemp, "Source = %S\n", (wchar_t*)bstrSource);
  164.         csMsg += szTemp;
  165.         wsprintf(szTemp, "Description = %S\n", (wchar_t*)bstrDescription);
  166.         csMsg += szTemp;
  167.         MessageBox (csMsg);
  168.         SysFreeString(bstrSource);
  169.         SysFreeString(bstrDescription);
  170.     }
  171.  
  172. }
  173.