📄 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()
//}}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;
}
// 调用自动化接口的 原理性 代码
void CUse1Dlg::OnOK()
{
::CoInitialize( NULL ); // COM 初始化
CLSID clsid; // 通过 ProgID 取得组件的 CLSID
HRESULT hr = ::CLSIDFromProgID( L"Simple6.DispSimple.1", &clsid );
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明没有注册组件
IDispatch * pDisp = NULL; // 由CLSID启动组件,并得到 IDispatch 指针
hr = ::CoCreateInstance( clsid, NULL, CLSCTX_ALL, IID_IDispatch, (LPVOID *)&pDisp );
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明没有初始化 COM
LPOLESTR pwFunName = L"Add"; // 准备取得 Add 函数的序号DispID
DISPID dispID; // 取得的序号准备保存到这里
hr = pDisp->GetIDsOfNames( // 根据函数名,取得序号
IID_NULL,
&pwFunName, // 函数名数组
1, // 函数名数组中的元素个数
LOCALE_SYSTEM_DEFAULT, // 使用系统默认的语言环境
&dispID ); // 返回值
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明组件根本就没有 ADD 函数
VARIANTARG v[2]; // 调用 Add(1,2) 函数所需要的参数
v[0].vt = VT_I4; v[0].lVal = 2; // 第二个参数,整数2
v[1].vt = VT_I4; v[1].lVal = 1; // 第一个参数,整数1
DISPPARAMS dispParams = { v, NULL, 2, 0 }; // 把参数包装到这个结构中
VARIANT vResult; // 函数返回的计算结果
hr = pDisp->Invoke( // 调用函数
dispID, // 函数由 dispID 指定
IID_NULL,
LOCALE_SYSTEM_DEFAULT, // 使用系统默认的语言环境
DISPATCH_METHOD, // 调用的是方法,不是属性
&dispParams, // 参数
&vResult, // 返回值
NULL, // 不考虑异常处理
NULL); // 不考虑错误处理
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明参数错误
CString str; // 显示一下结果
str.Format("1 + 2 = %d", vResult.lVal );
AfxMessageBox( str );
pDisp->Release(); // 释放接口指针
::CoUninitialize(); // 释放COM
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -