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

  1. // UseAtlTrigDlg.cpp : implementation file
  2.  
  3. #include "stdafx.h"
  4. #include "UseAtlTrig.h"
  5. #include "UseAtlTrigDlg.h"
  6.  
  7. #ifdef _DEBUG
  8. #define new DEBUG_NEW
  9. #undef THIS_FILE
  10. static char THIS_FILE[] = __FILE__;
  11. #endif
  12.  
  13. // CUseAtlTrigDlg dialog
  14. CUseAtlTrigDlg::CUseAtlTrigDlg(CWnd* pParent /*=NULL*/)
  15.     : CDialog(CUseAtlTrigDlg::IDD, pParent)
  16. {
  17.     //{{AFX_DATA_INIT(CUseAtlTrigDlg)
  18.         // NOTE: the ClassWizard will add member initialization here
  19.     //}}AFX_DATA_INIT
  20.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  21.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  22.     
  23.     m_pTrig = 0;
  24. }
  25.  
  26. void CUseAtlTrigDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28.     CDialog::DoDataExchange(pDX);
  29.     //{{AFX_DATA_MAP(CUseAtlTrigDlg)
  30.         // NOTE: the ClassWizard will add DDX and DDV calls here
  31.     //}}AFX_DATA_MAP
  32. }
  33.  
  34. BEGIN_MESSAGE_MAP(CUseAtlTrigDlg, CDialog)
  35.     //{{AFX_MSG_MAP(CUseAtlTrigDlg)
  36.     ON_WM_PAINT()
  37.     ON_WM_QUERYDRAGICON()
  38.     ON_BN_CLICKED(IDC_BUTTON_ATL, OnButtonAtl)
  39.     //}}AFX_MSG_MAP
  40. END_MESSAGE_MAP()
  41.  
  42. // CUseAtlTrigDlg message handlers
  43. BOOL CUseAtlTrigDlg::OnInitDialog()
  44. {
  45.     CDialog::OnInitDialog();
  46.  
  47.     // Set the icon for this dialog.  The framework does this automatically
  48.     //  when the application's main window is not a dialog
  49.     SetIcon(m_hIcon, TRUE);            // Set big icon
  50.     SetIcon(m_hIcon, FALSE);        // Set small icon
  51.     
  52.     // TODO: Add extra initialization here
  53.     
  54.     return TRUE;  // return TRUE  unless you set the focus to a control
  55. }
  56.  
  57. void CUseAtlTrigDlg::OnPaint() 
  58. {
  59.     if (IsIconic())
  60.     {
  61.         CPaintDC dc(this); // device context for painting
  62.  
  63.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  64.  
  65.         // Center icon in client rectangle
  66.         int cxIcon = GetSystemMetrics(SM_CXICON);
  67.         int cyIcon = GetSystemMetrics(SM_CYICON);
  68.         CRect rect;
  69.         GetClientRect(&rect);
  70.         int x = (rect.Width() - cxIcon + 1) / 2;
  71.         int y = (rect.Height() - cyIcon + 1) / 2;
  72.  
  73.         // Draw the icon
  74.         dc.DrawIcon(x, y, m_hIcon);
  75.     }
  76.     else
  77.     {
  78.         CDialog::OnPaint();
  79.     }
  80. }
  81.  
  82. HCURSOR CUseAtlTrigDlg::OnQueryDragIcon()
  83. {
  84.     return (HCURSOR) m_hIcon;
  85. }
  86.  
  87. void CUseAtlTrigDlg::OnButtonAtl() 
  88. {
  89.     CString s;
  90.     double x = 1.0, r;
  91.  
  92.     try
  93.     {
  94.         // Obtain an interface, but only once.
  95.         if (0 == m_pTrig)
  96.             m_pTrig = ItrigPtr(CLSID_trig);
  97.  
  98.         m_pTrig->ATLsin(x, &r);
  99.         s.Format("The sin of %3.1lf is %lf", x, r);
  100.         MessageBox(s);
  101.     }    
  102.  
  103.     catch (_com_error &ex)
  104.     {
  105.         _bstr_t bstrSource(ex.Source());
  106.         _bstr_t bstrDescription(ex.Description());
  107.         char szTemp[1024];
  108.         CString csMsg = "Oops - hit an error!\n";
  109.         wsprintf(szTemp, "Code = %08lx\n", ex.Error());
  110.         csMsg += szTemp;
  111.         wsprintf(szTemp, "Code meaning = %s\n", ex.ErrorMessage());
  112.         csMsg += szTemp;
  113.         wsprintf(szTemp, "Source = %S\n", (wchar_t*)bstrSource);
  114.         csMsg += szTemp;
  115.         wsprintf(szTemp, "Description = %S\n", (wchar_t*)bstrDescription);
  116.         csMsg += szTemp;
  117.         MessageBox (csMsg);
  118.         SysFreeString(bstrSource);
  119.         SysFreeString(bstrDescription);
  120.     }
  121. }
  122.