Use2Dlg.cpp
资源名称:comp5.rar [点击查看]
上传用户:sz6275
上传日期:2022-06-17
资源大小:80k
文件大小:5k
源码类别:
ActiveX/DCOM/ATL
开发平台:
Visual C++
- // Use2Dlg.cpp : implementation file
- //
- #include "stdafx.h"
- #include "Use2.h"
- #include "Use2Dlg.h"
- #ifdef _DEBUG
- #define new DEBUG_NEW
- #undef THIS_FILE
- static char THIS_FILE[] = __FILE__;
- #endif
- CUse2Dlg::CUse2Dlg(CWnd* pParent /*=NULL*/)
- : CDialog(CUse2Dlg::IDD, pParent)
- {
- //{{AFX_DATA_INIT(CUse2Dlg)
- // NOTE: the ClassWizard will add member initialization here
- //}}AFX_DATA_INIT
- // Note that LoadIcon does not require a subsequent DestroyIcon in Win32
- m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
- }
- void CUse2Dlg::DoDataExchange(CDataExchange* pDX)
- {
- CDialog::DoDataExchange(pDX);
- //{{AFX_DATA_MAP(CUse2Dlg)
- // NOTE: the ClassWizard will add DDX and DDV calls here
- //}}AFX_DATA_MAP
- }
- BEGIN_MESSAGE_MAP(CUse2Dlg, CDialog)
- //{{AFX_MSG_MAP(CUse2Dlg)
- ON_WM_PAINT()
- ON_WM_QUERYDRAGICON()
- ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
- ON_BN_CLICKED(IDC_BUTTON4, OnButton4)
- ON_BN_CLICKED(IDC_BUTTON5, OnButton5)
- //}}AFX_MSG_MAP
- END_MESSAGE_MAP()
- /////////////////////////////////////////////////////////////////////////////
- // CUse2Dlg message handlers
- BOOL CUse2Dlg::OnInitDialog()
- {
- CDialog::OnInitDialog();
- // Set the icon for this dialog. The framework does this automatically
- // when the application's main window is not a dialog
- 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 CUse2Dlg::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();
- }
- }
- // The system calls this to obtain the cursor to display while the user drags
- // the minimized window.
- HCURSOR CUse2Dlg::OnQueryDragIcon()
- {
- return (HCURSOR) m_hIcon;
- }
- // 包含 CComPtr,CComQIPtr,CComBSTR 需要使用的头文件
- #include <atlbase.h>
- #include "..Simple1simple1.h"
- #include "..Simple1Simple1_i.c"
- void CUse2Dlg::OnButton3() // CComPtr 使用举例
- {
- // COM 初始化 以 AfxOleInit() 函数调用,放在了CUse2App::InitInstance() 中了。
- CComPtr < IUnknown > spUnk; // 定义 IUnknown 的智能指针
- CComPtr < IFun > spFun; // 定义 IFun 的智能指针
- HRESULT hr;
- try
- {
- // 可以使用 CLSID 启动组件,也可以使用 ProgID
- hr = spUnk.CoCreateInstance( CLSID_Fun );
- if( FAILED( hr ) ) throw( _T("没注册组件吧?") );
- hr = spUnk.QueryInterface( &spFun );
- if( FAILED( hr ) ) throw( _T("居然没有接口") );
- long nSum;
- hr = spFun->Add( 1, 2, &nSum );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg;
- sMsg.Format( _T("1 + 2 = %d"), nSum );
- AfxMessageBox( sMsg );
- }
- CComBSTR s1( "Hello" ), s2( " world" ), s3;
- hr = spFun->Cat( s1, s2, &s3 );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg( s3 );
- AfxMessageBox( sMsg );
- }
- }
- catch( LPCTSTR lpErr )
- {
- AfxMessageBox( lpErr );
- }
- // 智能指针的最大好处是 我们不用负责释放接口指针
- }
- void CUse2Dlg::OnButton4() // CComQIPtr 使用举例
- {
- CComPtr < IUnknown > spUnk; // 定义 IUnknown 的智能指针
- CComQIPtr < IFun > spFun; // 定义 IFun 的智能指针
- HRESULT hr;
- try
- {
- // 使用 ProgID 启动组件
- hr = spUnk.CoCreateInstance( L"Simple1.fun.1" );
- if( FAILED( hr ) ) throw( _T("没注册组件吧?") );
- spFun = spUnk; // CComQIPtr 会帮咱们自动调用 QueryInterface
- if( !spFun ) throw( _T("居然没有接口") ); // 成功与否,可以用非 NULL 判断
- long nSum;
- hr = spFun->Add( 1, 2, &nSum );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg;
- sMsg.Format( _T("1 + 2 = %d"), nSum );
- AfxMessageBox( sMsg );
- }
- CComBSTR s1( "Hello" ), s2( " world" ), s3;
- hr = spFun->Cat( s1, s2, &s3 );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg( s3 );
- AfxMessageBox( sMsg );
- }
- }
- catch( LPCTSTR lpErr )
- {
- AfxMessageBox( lpErr );
- }
- }
- void CUse2Dlg::OnButton5() // 不再经过 CComPtr< IUnknown >
- {
- CComQIPtr < IFun, &IID_IFun > spFun; // 定义 IFun 的智能指针
- HRESULT hr;
- try
- {
- hr = spFun.CoCreateInstance( L"Simple1.fun.1" );
- if( FAILED( hr ) ) throw( _T("没注册组件或没有接口?") );
- long nSum;
- hr = spFun->Add( 1, 2, &nSum );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg;
- sMsg.Format( _T("1 + 2 = %d"), nSum );
- AfxMessageBox( sMsg );
- }
- CComBSTR s1( "Hello" ), s2( " world" ), s3;
- hr = spFun->Cat( s1, s2, &s3 );
- if( SUCCEEDED( hr ) )
- {
- CString sMsg( s3 );
- AfxMessageBox( sMsg );
- }
- }
- catch( LPCTSTR lpErr )
- {
- AfxMessageBox( lpErr );
- }
- }