📄 use2dlg.cpp
字号:
// 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 dialog
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()
//}}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;
}
#include <atlbase.h>
// 使用 ATL 中的 CComDispatchDriver 智能指针,前提条件是:
// 必须在#include <atlbase.h> 后,
// 紧接着声明全局 CComModule _Module 对象,
// 这个类似于 MFC 程序中的 theApp
CComModule _Module;
// 如果你使用 vc.net ATL 7.0 则更简单了,不用声明 _Module 对象
#include <atlcom.h> // CComDispatchDriver 在这里定义的
void CUse2Dlg::OnOK()
{
// 在 App 类 InitInstance() 函数中,
// 已经调用 AfxOleInit() 进行了 COM 初始化
CLSID clsid; // 通过 ProgID 取得组件的 CLSID
HRESULT hr = ::CLSIDFromProgID( L"Simple6.DispSimple.1", &clsid );
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明没有注册组件
CComPtr < IUnknown > spUnk; // 由CLSID启动组件,并得到 IUnknown 指针
hr = ::CoCreateInstance( clsid, NULL, CLSCTX_ALL, IID_IUnknown, (LPVOID *)&spUnk );
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明没有初始化 COM
CComDispatchDriver spDisp( spUnk ); // 构造智能指针
CComVariant v1(1), v2(2), vResult; // 参数
hr = spDisp.Invoke2( // 调用2个参数的函数
L"Add", // 函数名是 Add
&v1, // 第一个参数,值为整数1
&v2, // 第二个参数,值为整数2
&vResult); // 返回值
ASSERT( SUCCEEDED( hr ) ); // 如果失败,说明可能没有 ADD 函数或参数错误
CString str; // 显示一下结果
str.Format("1 + 2 = %d", vResult.lVal );
AfxMessageBox( str );
// spUnk 和 spDisp 都是智能指针,会自动释放
// 如果使用 CoInitialize(NULL) 初始化,则必须在 CoUninitialize() 之前
// 调用 spUnk.Release() 和 spDisp.Release() 释放
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -