gusucode.com > COM组件设计与应用(十八)——属性包C++源码程序 > COM组件设计与应用(十八)——属性包/comtut18src/ComP18/Use/UseDlg.cpp

    // UseDlg.cpp : implementation file
//

#include "stdafx.h"
#include "Use.h"
#include "UseDlg.h"
#include <atlbase.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)
	m_str = _T("");
	m_integer = 0;
	//}}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)
	DDX_Text(pDX, IDC_STR, m_str);
	DDX_Text(pDX, IDC_INTEGER, m_integer);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CUseDlg, CDialog)
	//{{AFX_MSG_MAP(CUseDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_BEGIN, OnBegin)
	ON_BN_CLICKED(IDC_END, OnEnd)
	ON_BN_CLICKED(IDC_SET, OnSet)
	ON_BN_CLICKED(IDC_GET, OnGet)
	//}}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
	SetDlgItemText( IDC_BAG, _T("str=hello\r\ninteger=100") );

	GetDlgItem( IDC_SET )->EnableWindow( FALSE );
	GetDlgItem( IDC_GET )->EnableWindow( FALSE );
	GetDlgItem( IDC_END )->EnableWindow( FALSE );
	GetDlgItem( IDC_BEGIN )->EnableWindow( TRUE );
	GetDlgItem( IDC_STR )->EnableWindow( FALSE );
	GetDlgItem( IDC_INTEGER )->EnableWindow( FALSE );

	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::OnBegin() 
{
	HRESULT hr = m_spObj.CreateInstance( _T("Simple18.Property.1") );
	if( FAILED( hr ) )
	{
		AfxMessageBox( _T("没有注册组件?没有初始化?") );
		return;
	}

	m_PropertyBag.SetEditWnd( (CEdit *)GetDlgItem( IDC_BAG ) );

	CComQIPtr < IPersistPropertyBag > spBag = m_spObj;
	if( !spBag )
	{
		AfxMessageBox( _T("组件没有提供 IPersistPropertyBag 接口") );
		return;
	}
	spBag->Load( &m_PropertyBag, NULL );

	GetDlgItem( IDC_SET )->EnableWindow( TRUE );
	GetDlgItem( IDC_GET )->EnableWindow( TRUE );
	GetDlgItem( IDC_END )->EnableWindow( TRUE );
	GetDlgItem( IDC_BEGIN )->EnableWindow( FALSE );
	GetDlgItem( IDC_STR )->EnableWindow( TRUE );
	GetDlgItem( IDC_INTEGER )->EnableWindow( TRUE );
	GetDlgItem( IDC_BAG )->EnableWindow( FALSE );
	GetDlgItem( IDC_STR )->SetFocus();
	((CEdit *)GetDlgItem( IDC_STR ))->SetSel( 0, -1 );
}

void CUseDlg::OnEnd() 
{
	SetDlgItemText( IDC_BAG, NULL );	// 清屏
	m_PropertyBag.SetEditWnd( (CEdit *)GetDlgItem( IDC_BAG ) );
	
	CComQIPtr < IPersistPropertyBag > spBag = m_spObj;
	if( !spBag )
	{
		AfxMessageBox( _T("组件没有提供 IPersistPropertyBag 接口") );
		return;
	}
	spBag->Save( &m_PropertyBag, TRUE, TRUE );
	
	m_spObj.Release();

	GetDlgItem( IDC_SET )->EnableWindow( FALSE );
	GetDlgItem( IDC_GET )->EnableWindow( FALSE );
	GetDlgItem( IDC_END )->EnableWindow( FALSE );
	GetDlgItem( IDC_BEGIN )->EnableWindow( TRUE );
	GetDlgItem( IDC_STR )->EnableWindow( FALSE );
	GetDlgItem( IDC_INTEGER )->EnableWindow( FALSE );
	GetDlgItem( IDC_BAG )->EnableWindow( TRUE );
	GetDlgItem( IDC_BAG )->SetFocus();
}

void CUseDlg::OnSet() 
{
	UpdateData();

	BSTR bstr = m_str.AllocSysString();
	m_spObj->put_str( bstr );
	::SysFreeString( bstr );

	m_spObj->Putinteger( m_integer );

	m_str.Empty();
	m_integer = 0;
	UpdateData( FALSE );

	GetDlgItem( IDC_STR )->SetFocus();
}

void CUseDlg::OnGet() 
{
	BSTR bstr;
	m_spObj->get_str( &bstr );
	m_str = CString( bstr );
	::SysFreeString( bstr );

	m_integer = m_spObj->Getinteger();

	UpdateData( FALSE );

	GetDlgItem( IDC_STR )->SetFocus();
	((CEdit *)GetDlgItem( IDC_STR ))->SetSel( 0, -1 );
}