Use5Dlg.cpp
上传用户:sz6275
上传日期:2022-06-17
资源大小:80k
文件大小:3k
源码类别:

ActiveX/DCOM/ATL

开发平台:

Visual C++

  1. // Use5Dlg.cpp : implementation file
  2. //
  3. #include "stdafx.h"
  4. #include "Use5.h"
  5. #include "Use5Dlg.h"
  6. #ifdef _DEBUG
  7. #define new DEBUG_NEW
  8. #undef THIS_FILE
  9. static char THIS_FILE[] = __FILE__;
  10. #endif
  11. /////////////////////////////////////////////////////////////////////////////
  12. // CUse5Dlg dialog
  13. CUse5Dlg::CUse5Dlg(CWnd* pParent /*=NULL*/)
  14. : CDialog(CUse5Dlg::IDD, pParent)
  15. {
  16. //{{AFX_DATA_INIT(CUse5Dlg)
  17. // NOTE: the ClassWizard will add member initialization here
  18. //}}AFX_DATA_INIT
  19. // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
  20. m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
  21. }
  22. void CUse5Dlg::DoDataExchange(CDataExchange* pDX)
  23. {
  24. CDialog::DoDataExchange(pDX);
  25. //{{AFX_DATA_MAP(CUse5Dlg)
  26. // NOTE: the ClassWizard will add DDX and DDV calls here
  27. //}}AFX_DATA_MAP
  28. }
  29. BEGIN_MESSAGE_MAP(CUse5Dlg, CDialog)
  30. //{{AFX_MSG_MAP(CUse5Dlg)
  31. ON_WM_PAINT()
  32. ON_WM_QUERYDRAGICON()
  33. ON_BN_CLICKED(IDC_BUTTON8, OnButton8)
  34. //}}AFX_MSG_MAP
  35. END_MESSAGE_MAP()
  36. /////////////////////////////////////////////////////////////////////////////
  37. // CUse5Dlg message handlers
  38. BOOL CUse5Dlg::OnInitDialog()
  39. {
  40. CDialog::OnInitDialog();
  41. // Set the icon for this dialog.  The framework does this automatically
  42. //  when the application's main window is not a dialog
  43. SetIcon(m_hIcon, TRUE); // Set big icon
  44. SetIcon(m_hIcon, FALSE); // Set small icon
  45. // TODO: Add extra initialization here
  46. return TRUE;  // return TRUE  unless you set the focus to a control
  47. }
  48. // If you add a minimize button to your dialog, you will need the code below
  49. //  to draw the icon.  For MFC applications using the document/view model,
  50. //  this is automatically done for you by the framework.
  51. void CUse5Dlg::OnPaint() 
  52. {
  53. if (IsIconic())
  54. {
  55. CPaintDC dc(this); // device context for painting
  56. SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
  57. // Center icon in client rectangle
  58. int cxIcon = GetSystemMetrics(SM_CXICON);
  59. int cyIcon = GetSystemMetrics(SM_CYICON);
  60. CRect rect;
  61. GetClientRect(&rect);
  62. int x = (rect.Width() - cxIcon + 1) / 2;
  63. int y = (rect.Height() - cyIcon + 1) / 2;
  64. // Draw the icon
  65. dc.DrawIcon(x, y, m_hIcon);
  66. }
  67. else
  68. {
  69. CDialog::OnPaint();
  70. }
  71. }
  72. // The system calls this to obtain the cursor to display while the user drags
  73. //  the minimized window.
  74. HCURSOR CUse5Dlg::OnQueryDragIcon()
  75. {
  76. return (HCURSOR) m_hIcon;
  77. }
  78. void CUse5Dlg::OnButton8() 
  79. {
  80. // 由于在stdafx.h中 #import 没有使用 no_namespace 因此要使用命名空间
  81. // 命名空间叫 SIMPLE1Lib,这个名称是组件的 IDL 文件中 Library 指定的
  82. try
  83. {
  84. // 这次使用智能指针的构造函数启动组件,书写简单。
  85. // 但也有缺点,因为如果失败的话,不知道失败的原因
  86. // SIMPLE1Lib::IFunPtr spFun( L"Simple1.fun.1" ); // ProgID 方式
  87. SIMPLE1Lib::IFunPtr spFun( __uuidof(SIMPLE1Lib::Fun) ); // CLSID 方式
  88. long nSum = spFun->Add( 1, 2 );
  89. CString sMsg;
  90. sMsg.Format( _T("1+2=%d"), nSum );
  91. AfxMessageBox( sMsg );
  92. _bstr_t sCat = spFun->Cat( _T("Hello"), _T(" world") );
  93. AfxMessageBox( sCat );
  94. }
  95. catch( _com_error &e )
  96. {
  97. e;
  98. AfxMessageBox( _T("Error") );
  99. }
  100. }