⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usedlg.cpp

📁 Thinkinc++English 电子书籍,英文版
💻 CPP
字号:
// UseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Use.h"
#include "UseDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CUseDlg dialog

CUseDlg::CUseDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CUseDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CUseDlg)
		// 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 CUseDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CUseDlg)
		// NOTE: the ClassWizard will add DDX and DDV calls here
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUseDlg, CDialog)
	//{{AFX_MSG_MAP(CUseDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BUTTON1, OnButton1)
	ON_BN_CLICKED(IDC_BUTTON2, OnButton2)
	ON_BN_CLICKED(IDC_BUTTON3, OnButton3)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CUseDlg message handlers

BOOL CUseDlg::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 CUseDlg::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 CUseDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

// 简单的错误异常 使用举例
void CUseDlg::OnButton1() 
{
	ISimpleErrPtr sp;	// 使用 CreateInstance() 启动组件
	HRESULT hr = sp.CreateInstance( _T("Simple9.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::OnButton2() 
{
	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::OnButton3() 
{
	ISimpleErrPtr sp( _T("Simple9.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 + -