📄 use1dlg.cpp
字号:
// Use1Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "Use1.h"
#include "Use1Dlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUse1Dlg dialog
CUse1Dlg::CUse1Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CUse1Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUse1Dlg)
// 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 CUse1Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUse1Dlg)
// NOTE: the ClassWizard will add DDX and DDV calls here
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUse1Dlg, CDialog)
//{{AFX_MSG_MAP(CUse1Dlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUse1Dlg message handlers
BOOL CUse1Dlg::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 CUse1Dlg::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 CUse1Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
// 包含组件编译后给我们生成的 C 语言的接口描述文件
#include "..\Simple1\simple1.h"
#include "..\Simple1\Simple1_i.c"
void CUse1Dlg::OnButton1()
{
::CoInitialize( NULL );
IUnknown * pUnk = NULL;
IFun * pFun = NULL;
HRESULT hr;
try
{
hr = ::CoCreateInstance(
CLSID_Fun,
NULL,
CLSCTX_INPROC_SERVER, // 以进程内组件 DLL 方式加载
IID_IUnknown, // 想取得 IUnknown 接口指针
(LPVOID *) &pUnk);
if( FAILED( hr ) ) throw( _T("没注册吧?") );
hr = pUnk->QueryInterface( // 从 IUnknown 得到其它接口指针
IID_IFun, // 想取得 IFun 接口指针
(LPVOID *)&pFun );
if( FAILED( hr ) ) throw( _T("居然没有接口?") );
long nSum;
hr = pFun->Add( 1, 2, &nSum ); // IFun::Add()
if( SUCCEEDED( hr ) )
{
CString sMsg;
sMsg.Format( _T("1 + 2 = %d"), nSum );
AfxMessageBox( sMsg );
}
BSTR s1 = ::SysAllocString( L"Hello" );
BSTR s2 = ::SysAllocString( L" world" );
BSTR s3 = NULL;
hr = pFun->Cat( s1, s2, &s3 ); // IFun::Cat()
if( SUCCEEDED( hr ) )
{
CString sMsg( s3 );
AfxMessageBox( sMsg );
}
// IFun::Cat()最后一个参数是 [out] 方向属性,因此调用者要释放内存
if( s3 ) ::SysFreeString( s3 );
}
catch( LPCTSTR lpErr )
{
AfxMessageBox( lpErr );
}
if( pUnk ) pUnk->Release();
if( pFun ) pFun->Release();
::CoUninitialize();
}
// 包含 CComBSTR 需要使用的头文件
#include <atlbase.h>
void CUse1Dlg::OnButton2()
{
::CoInitialize( NULL );
IFun * pFun = NULL;
HRESULT hr;
try
{
hr = ::CoCreateInstance(
CLSID_Fun,
NULL,
CLSCTX_INPROC_SERVER,
IID_IFun, // 不再经过 IUnknown, 直接想得到 IFun 接口指针
(LPVOID *) &pFun);
if( FAILED( hr ) ) throw( _T("没注册吧或没有接口?") );
long nSum;
hr = pFun->Add( 1, 2, &nSum );
if( SUCCEEDED( hr ) )
{
CString sMsg;
sMsg.Format( _T("1 + 2 = %d"), nSum );
AfxMessageBox( sMsg );
}
CComBSTR s1( "Hello" ); // 不再使用 API 方式操作 BSTR
CComBSTR s2( " world" ); // 使用 CComBSTR 的包装类比较简单,并且
CComBSTR s3; // 最大的好处是不用咱们自己来释放
hr = pFun->Cat( s1, s2, &s3 );
if( SUCCEEDED( hr ) )
{
CString sMsg( s3 );
AfxMessageBox( sMsg );
}
}
catch( LPCTSTR lpErr )
{
AfxMessageBox( lpErr );
}
if( pFun ) pFun->Release();
::CoUninitialize();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -