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

  1. // UseAtlTrigDlg.cpp : implementation file
  2.  
  3. #include "stdafx.h"
  4. #include "UseAtlTrig.h"
  5.  
  6. #include "atltrig.h"
  7.  
  8. #include "UseAtlTrigDlg.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. /////////////////////////////////////////////////////////////////////////////
  17. // CUseAtlTrigDlg dialog
  18.  
  19. CUseAtlTrigDlg::CUseAtlTrigDlg(CWnd* pParent /*=NULL*/)
  20.     : CDialog(CUseAtlTrigDlg::IDD, pParent)
  21. {
  22.     //{{AFX_DATA_INIT(CUseAtlTrigDlg)
  23.         // NOTE: the ClassWizard will add member initialization here
  24.     //}}AFX_DATA_INIT
  25.     // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  26.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  27.  
  28.     m_pTrig = 0;
  29. }
  30.  
  31. void CUseAtlTrigDlg::DoDataExchange(CDataExchange* pDX)
  32. {
  33.     CDialog::DoDataExchange(pDX);
  34.     //{{AFX_DATA_MAP(CUseAtlTrigDlg)
  35.         // NOTE: the ClassWizard will add DDX and DDV calls here
  36.     //}}AFX_DATA_MAP
  37. }
  38.  
  39. BEGIN_MESSAGE_MAP(CUseAtlTrigDlg, CDialog)
  40.     //{{AFX_MSG_MAP(CUseAtlTrigDlg)
  41.     ON_WM_PAINT()
  42.     ON_WM_QUERYDRAGICON()
  43.     ON_BN_CLICKED(IDC_BUTTON, OnButton)
  44.     //}}AFX_MSG_MAP
  45. END_MESSAGE_MAP()
  46.  
  47. /////////////////////////////////////////////////////////////////////////////
  48. // CUseAtlTrigDlg message handlers
  49.  
  50. BOOL CUseAtlTrigDlg::OnInitDialog()
  51. {
  52.     CDialog::OnInitDialog();
  53.  
  54.     // Set the icon for this dialog.  The framework does this automatically
  55.     //  when the application's main window is not a dialog
  56.     SetIcon(m_hIcon, TRUE);            // Set big icon
  57.     SetIcon(m_hIcon, FALSE);        // Set small icon
  58.     
  59.     return TRUE;
  60. }
  61.  
  62. // If you add a minimize button to your dialog, you will need the code below
  63. // to draw the icon.  For MFC applications using the document/view model,
  64. // this is automatically done for you by the framework.
  65. void CUseAtlTrigDlg::OnPaint() 
  66. {
  67.     if (IsIconic())
  68.     {
  69.         CPaintDC dc(this); // device context for painting
  70.  
  71.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  72.  
  73.         // Center icon in client rectangle
  74.         int cxIcon = GetSystemMetrics(SM_CXICON);
  75.         int cyIcon = GetSystemMetrics(SM_CYICON);
  76.         CRect rect;
  77.         GetClientRect(&rect);
  78.         int x = (rect.Width() - cxIcon + 1) / 2;
  79.         int y = (rect.Height() - cyIcon + 1) / 2;
  80.  
  81.         // Draw the icon
  82.         dc.DrawIcon(x, y, m_hIcon);
  83.     }
  84.     else
  85.     {
  86.         CDialog::OnPaint();
  87.     }
  88. }
  89.  
  90. // The system calls this to obtain the cursor to display while
  91. // the user drags the minimized window.
  92. HCURSOR CUseAtlTrigDlg::OnQueryDragIcon()
  93. {
  94.     return (HCURSOR) m_hIcon;
  95. }
  96.  
  97. void CUseAtlTrigDlg::OnButton()
  98. {
  99.     // One can create the dispatch interfce using either the CLSID
  100.     // or its name. Regardless of the type of object, one will get
  101.     // whatever interface the object exposes when it receives
  102.     // a request for its IDispatch interface, Itrig in this case.
  103.     // See the COM_INTERFACE_ENTRY2 entry of the server's header file.
  104.     // The first COM_INTERFACE_ENTRY2 entry in the BEGIN_COM_MAP is
  105.     // the one that will be used.
  106.  
  107.     if (0 == m_pTrig)
  108.     {
  109.         m_pTrig = new Itrig;
  110.         COleException e;
  111.         BOOL rc = m_pTrig->CreateDispatch("trig.trig.1", &e);
  112.         //BOOL rc = m_pTrig->CreateDispatch(CLSID_trig, &e);
  113.         if (FALSE == rc)
  114.         {
  115.             MessageBox("Unable to create dispatch interface");
  116.             delete m_pTrig;
  117.             m_pTrig = 0;
  118.             return;
  119.         }
  120.     }
  121.  
  122.     double x = 1.0, r;
  123.     m_pTrig->ATLsin(x, &r);
  124.  
  125.     CString s;
  126.     s.Format("The sin of %lf is %lf", x, r);
  127.     MessageBox(s);
  128. }
  129.  
  130. BOOL CUseAtlTrigDlg::DestroyWindow() 
  131. {
  132.     if (0 != m_pTrig)
  133.         delete m_pTrig;
  134.  
  135.     return CDialog::DestroyWindow();
  136. }
  137.