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

  1. // UseATLstrXDlg.cpp : implementation file
  2.  
  3. #include "stdafx.h"
  4. #include "UseATLstrX.h"
  5. #include "UseATLstrXDlg.h"
  6.  
  7. #include <stdlib.h>
  8. #include <ctype.h>
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. // CUseATLstrXDlg dialog
  17. CUseATLstrXDlg::CUseATLstrXDlg(CWnd* pParent /*=NULL*/)
  18.     : CDialog(CUseATLstrXDlg::IDD, pParent)
  19. {
  20.     //{{AFX_DATA_INIT(CUseATLstrXDlg)
  21.         // NOTE: the ClassWizard will add member initialization here
  22.     //}}AFX_DATA_INIT
  23.     m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  24. }
  25.  
  26. void CUseATLstrXDlg::DoDataExchange(CDataExchange* pDX)
  27. {
  28.     CDialog::DoDataExchange(pDX);
  29.     //{{AFX_DATA_MAP(CUseATLstrXDlg)
  30.         // NOTE: the ClassWizard will add DDX and DDV calls here
  31.     //}}AFX_DATA_MAP
  32. }
  33.  
  34. BEGIN_MESSAGE_MAP(CUseATLstrXDlg, CDialog)
  35.     //{{AFX_MSG_MAP(CUseATLstrXDlg)
  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. // CUseATLstrXDlg message handlers
  44. BOOL CUseATLstrXDlg::OnInitDialog()
  45. {
  46.     CDialog::OnInitDialog();
  47.  
  48.     SetIcon(m_hIcon, TRUE);            // Set big icon
  49.     SetIcon(m_hIcon, FALSE);        // Set small icon
  50.     
  51.     // TODO: Add extra initialization here
  52.     
  53.     return TRUE;  // return TRUE  unless you set the focus to a control
  54. }
  55.  
  56. void CUseATLstrXDlg::OnPaint() 
  57. {
  58.     if (IsIconic())
  59.     {
  60.         CPaintDC dc(this); // device context for painting
  61.  
  62.         SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  63.  
  64.         // Center icon in client rectangle
  65.         int cxIcon = GetSystemMetrics(SM_CXICON);
  66.         int cyIcon = GetSystemMetrics(SM_CYICON);
  67.         CRect rect;
  68.         GetClientRect(&rect);
  69.         int x = (rect.Width() - cxIcon + 1) / 2;
  70.         int y = (rect.Height() - cyIcon + 1) / 2;
  71.  
  72.         // Draw the icon
  73.         dc.DrawIcon(x, y, m_hIcon);
  74.     }
  75.     else
  76.     {
  77.         CDialog::OnPaint();
  78.     }
  79. }
  80.  
  81. HCURSOR CUseATLstrXDlg::OnQueryDragIcon()
  82. {
  83.     return (HCURSOR) m_hIcon;
  84. }
  85.  
  86. void CUseATLstrXDlg::OnButton1() 
  87. {
  88.     // Obtain a smart pointer to the interface.
  89.     IstrXPtr pString(CLSID_strX);
  90.  
  91.     CString s("HELLO WORLD");
  92.  
  93.     // The function in the remote server needs a pointer to a BSTR.
  94.     // The easiest way to construct and fill a BSTR is to use a
  95.     // CString, and then CString::AllocSysString. Finally,
  96.     // pass the address of the BSTR.
  97.     BSTR str = s.AllocSysString();
  98.     HRESULT rc = pString->ATLstrlwrX(&str);
  99.  
  100.     // Now, because BSTR aren't intended to work with MFC
  101.     // methods we convert the BSTR to a _bstr_t.
  102.     if (S_OK == rc)
  103.     {
  104.         _bstr_t b(str, false);
  105.         MessageBox((char *)b);
  106.     }
  107.     else
  108.         MessageBox("Failure in call to ATLstrlwrX()");
  109.    
  110.     ::SysFreeString(str);
  111. }
  112.  
  113. void BSTRlower(BSTR s)
  114. {
  115.     wchar_t *p = (wchar_t *) s;
  116.     while (*p)
  117.     {
  118.         *p = tolower(*p);
  119.         p++;
  120.     }
  121. }
  122.  
  123.  
  124. void CUseATLstrXDlg::OnButton2() 
  125. {
  126.     _bstr_t b("HELLO WORLD");
  127.     BSTRlower(b);
  128.  
  129.     MessageBox((char *)b);
  130. }
  131.  
  132.