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

📄 use1dlg.cpp

📁 Thinkinc++English 电子书籍,英文版
💻 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)
	m_n1 = 0;
	m_n2 = 0;
	//}}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)
	DDX_Control(pDX, IDC_EDIT3, m_edit);
	DDX_Text(pDX, IDC_EDIT1, m_n1);
	DDX_Text(pDX, IDC_EDIT2, m_n2);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUse1Dlg, CDialog)
	//{{AFX_MSG_MAP(CUse1Dlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_ADVISE, OnAdvise)
	ON_BN_CLICKED(IDC_UNADVISE, OnUnadvise)
	//}}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

	// 启动组件,COM 初始化在 CUser1App::InitInstance()中已经完成
	HRESULT hr = m_spCom.CreateInstance(__uuidof(Event1) );
	if( FAILED( hr ) )
	{
		AfxMessageBox( _T("注册了吗?COM初始化了吗?") );
		CDialog::OnCancel();	// 退出执行
	}
	m_dwCookie = 0;					// 初始化cookie,表示还没有调用 Advise() 函数
	m_sink.SetResultWnd( &m_edit );	// 告诉接收器,计算结果显示在这个窗口中

	GetDlgItem( IDC_ADVISE )->EnableWindow( TRUE );		// 允许"Advise"
	GetDlgItem( IDC_UNADVISE)->EnableWindow( FALSE );	// 禁止 "Unadvise"

	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;
}

/*HRESULT __stdcall ICallBack::raw_Fire_Result ( long nResult )
{

}
*/
void CUse1Dlg::OnOK()			// 开始计算
{
	UpdateData();				// 取得加数 和 被加数

	m_spCom->Add( m_n1, m_n2 );	// 计算去吧,我也不知道什么时候会有结果
								// 如果有结果的话,CSink::raw_Fire_Result()就会被调用
}

void CUse1Dlg::OnCancel()		// 退出程序
{
	if( 0 != m_dwCookie )		// 如果有连接
		OnUnadvise();			// 则先断掉连接
	
	CDialog::OnCancel();
}

void CUse1Dlg::OnAdvise() 
{
		// 把接收器接口指针,传递到组件中。实现“连接”
	HRESULT hr = m_spCom->Advise( &m_sink, (long *)&m_dwCookie );
	if( SUCCEEDED( hr ) )
	{
		AfxMessageBox( _T("Advise 调用成功。已经正确连接") );
		GetDlgItem( IDC_ADVISE )->EnableWindow( FALSE );
		GetDlgItem( IDC_UNADVISE)->EnableWindow( TRUE );
	}
	else
	{
		AfxMessageBox( _T("Advise 调用失败") );
	}
}

void CUse1Dlg::OnUnadvise() 
{
		// 断开“连接”
	HRESULT hr = m_spCom->Unadvise( m_dwCookie );
	if( SUCCEEDED( hr ) )
	{
		AfxMessageBox( _T("Unadvise 调用成功。已经断开连接") );
		m_dwCookie = 0;
		GetDlgItem( IDC_ADVISE )->EnableWindow( TRUE );
		GetDlgItem( IDC_UNADVISE)->EnableWindow( FALSE );
	}
	else
	{
		AfxMessageBox( _T("Unadvise 调用失败") );
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -