📄 usedlg.cpp
字号:
// UseDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Use.h"
#include "UseDlg.h"
#include ".\usedlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CUseDlg 对话框
CUseDlg::CUseDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUseDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CUseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CUseDlg, CDialog)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
//}}AFX_MSG_MAP
ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedButton1)
ON_BN_CLICKED(IDC_BUTTON2, OnBnClickedButton2)
ON_BN_CLICKED(IDC_BUTTON3, OnBnClickedButton3)
END_MESSAGE_MAP()
// CUseDlg 消息处理程序
BOOL CUseDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
return TRUE; // 除非设置了控件的焦点,否则返回 TRUE
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CUseDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标显示。
HCURSOR CUseDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
// 简单的错误异常 使用举例
void CUseDlg::OnBnClickedButton1()
{
ISimpleErrPtr sp; // 使用 CreateInstance() 启动组件
HRESULT hr = sp.CreateInstance( _T("Simple10.SimpleErr.1") );
ASSERT( SUCCEEDED( hr ) ); // 不成功?!你注册了吗?COM 初始化了吗?
try
{
long nResult;
CString str;
nResult = sp->Div1( 10, 5 );
str.Format( _T("10 / 5 = %d"), nResult );
MessageBox( str, _T("计算正确") );
sp->Div1( 10, 0 ); // 10/0 会发出异常,转到catch处执行
}
catch( _com_error &e )
{
MessageBox( e.ErrorMessage(), _T("计算错误原因") );
}
}
// 显式使用 ISupportErrorInfo 接口,取得错误信息
// 也就是使用 未经过异常包装 的使用方法举例
#include <atlbase.h>
void CUseDlg::OnBnClickedButton2()
{
ISimpleErrPtr sp( __uuidof( SimpleErr ) ); // 使用构造函数启动组件
ASSERT( NULL != sp ); // 不成功?!你注册了吗?COM 初始化了吗?
long nResult;
// raw_xxx() 表示调用未经 异常处理包装 的函数
HRESULT hr = sp->raw_Div2( 10, 5, &nResult ); // 举例一个正常的情况
if( SUCCEEDED( hr ) )
{
CString str;
str.Format( _T("10 / 5 = %d"), nResult );
MessageBox( str, _T("计算正确") );
}
hr = sp->raw_Div2( 10, 0, &nResult ); // 举例一个非正常的情况
if( FAILED( hr ) )
{ // 组件是否提供了 ISupportErrorInfo 接口?
CComQIPtr < ISupportErrorInfo > spSEI = sp;
if( spSEI )
{ // 是否支持 ISimpleErr 接口的错误处理?
hr = spSEI->InterfaceSupportsErrorInfo( __uuidof(ISimpleErr) );
if( SUCCEEDED( hr ) )
{ // 支持,太好了。取出异常信息
CComQIPtr < IErrorInfo > spErrInfo; // 声明 IErrorInfo 接口
hr = ::GetErrorInfo( 0, &spErrInfo ); // 取得接口
if( SUCCEEDED( hr ) )
{
CComBSTR bstrDes;
spErrInfo->GetDescription( &bstrDes ); // 取得错误描述
::MessageBoxW( GetSafeHwnd(), bstrDes, L"计算错误原因", MB_OK );
CComBSTR bstrSour;
spErrInfo->GetSource( &bstrSour ); // 取得错误源
::MessageBoxW( GetSafeHwnd(), bstrSour, L"错误发生源", MB_OK );
}
}
}
}
}
// 最标准,最简单的异常接收处理方法
void CUseDlg::OnBnClickedButton3()
{
ISimpleErrPtr sp( _T("Simple10.SimpleErr.1") ); // 使用构造函数启动组件
ASSERT( NULL != sp ); // 不成功?!你注册了吗?COM 初始化了吗?
try
{
long nResult;
CString str;
nResult = sp->Div2( 10, 5 ); // 举例一个正常情况
str.Format( _T("10 / 5 = %d"), nResult );
MessageBox( str, _T("计算正确") );
sp->Div2( 10, 0 ); // 举例一个非正常情况,转 catch 处理
}
catch( _com_error &e )
{ // 取得错误描述
MessageBox( e.Description(), _T("计算错误原因") );
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -