home *** CD-ROM | disk | FTP | other *** search
- // UseAtlTrigArcDlg.cpp : implementation file
- //
-
- #include "stdafx.h"
- #include "UseAtlTrigArc.h"
- #include "UseAtlTrigArcDlg.h"
-
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
-
- /////////////////////////////////////////////////////////////////////////////
- // CUseAtlTrigArcDlg dialog
-
- CUseAtlTrigArcDlg::CUseAtlTrigArcDlg(CWnd* pParent /*=NULL*/)
- : CDialog(CUseAtlTrigArcDlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CUseAtlTrigArcDlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
-
- void CUseAtlTrigArcDlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CUseAtlTrigArcDlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
-
- BEGIN_MESSAGE_MAP(CUseAtlTrigArcDlg, CDialog)
- //{{AFX_MSG_MAP(CUseAtlTrigArcDlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
- ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
-
- /////////////////////////////////////////////////////////////////////////////
- // CUseAtlTrigArcDlg message handlers
-
- BOOL CUseAtlTrigArcDlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
-
- SetIcon(m_hIcon, TRUE); // Set big icon
- SetIcon(m_hIcon, FALSE); // Set small icon
-
- // TODO: Add extra initialization here
-
- return TRUE; // return TRUE unless you set the focus to a control
- }
-
- // If you add a minimize button to your dialog, you will need the code below
- // to draw the icon. For MFC applications using the document/view model,
- // this is automatically done for you by the framework.
-
- void CUseAtlTrigArcDlg::OnPaint()
- {
- if (IsIconic())
- {
- CPaintDC dc(this); // device context for painting
-
- SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
-
- // Center icon in client rectangle
- int cxIcon = GetSystemMetrics(SM_CXICON);
- int cyIcon = GetSystemMetrics(SM_CYICON);
- CRect rect;
- GetClientRect(&rect);
- int x = (rect.Width() - cxIcon + 1) / 2;
- int y = (rect.Height() - cyIcon + 1) / 2;
-
- // Draw the icon
- dc.DrawIcon(x, y, m_hIcon);
- }
- else
- {
- CDialog::OnPaint();
- }
- }
-
- HCURSOR CUseAtlTrigArcDlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
-
- void CUseAtlTrigArcDlg::OnButton1()
- {
- double x = 1.0, r;
- CString s;
-
- // Begin with one interface of CLSID_trig
- ItrigPtr pTrig(CLSID_trig);
- pTrig->ATLsin(x, &r);
- s.Format("The sin of %3.1lf is %lf", x, r);
- MessageBox(s);
-
- // Attempt to switch to another interface.
- // If successful, invoke one of its methods.
- // That pTrig and pArc are smart pointers, courtesy
- // of _com_ptr_t, negates the need to do reference
- // counting by the client programmer.
- IarcPtr pArc;
- HRESULT rc = pTrig->QueryInterface(IID_Iarc, (void **)&pArc);
- if (S_OK == rc)
- {
- pArc->ATLatan(x, &r);
- s.Format("The arctangent of %3.1f is %lf", x, r);
- MessageBox(s);
- }
-
- // Query for a non-existent interface just to show
- // how it might be done.
- IPersistStreamPtr pPersistStream;
- rc = pArc->QueryInterface(IID_IPersistStream,
- (void **)&pPersistStream);
- if (E_NOINTERFACE == rc)
- MessageBox("The object doesn't support IPersistStream.");
- }
-
- void CUseAtlTrigArcDlg::OnButton2()
- {
- double x = 1.0, r;
- CString s;
-
- // See comments in OnButton1. The purpose here is to show
- // that the process works both ways: We can begin with
- // one interface and switch to the other. Note, too, that
- // this version uses exception handling,
- // which is a very good idea.
-
- try
- {
- IarcPtr pArc(CLSID_trig);
- pArc->ATLacos(x, &r);
- s.Format("The arccosine of %3.1lf is %lf", x, r);
- MessageBox(s);
-
- ItrigPtr pTrig;
- HRESULT rc = pArc->QueryInterface(IID_Iarc, (void **)&pTrig);
- if (S_OK == rc)
- {
- pTrig->ATLtan(x, &r);
- s.Format("The tangent of %3.1f is %lf", x, r);
- MessageBox(s);
- }
- }
- catch (_com_error &ex)
- {
- _bstr_t bstrSource(ex.Source());
- _bstr_t bstrDescription(ex.Description());
- char szTemp[1024];
- CString csMsg = "Oops - hit an error!\n";
- wsprintf(szTemp, "Code = %08lx\n", ex.Error());
- csMsg += szTemp;
- wsprintf(szTemp, "Code meaning = %s\n", ex.ErrorMessage());
- csMsg += szTemp;
- wsprintf(szTemp, "Source = %S\n", (wchar_t*)bstrSource);
- csMsg += szTemp;
- wsprintf(szTemp, "Description = %S\n", (wchar_t*)bstrDescription);
- csMsg += szTemp;
- MessageBox (csMsg);
- SysFreeString(bstrSource);
- SysFreeString(bstrDescription);
- }
-
- }
-