📄 usedlg.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_NEW, OnNew)
ON_BN_CLICKED(IDC_CONTINUE, OnContinue)
ON_BN_CLICKED(IDC_NEXT, OnNext)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUseDlg message handlers
LPCOLESTR lpwFileName = L"c:\\prime.stg";
LPCOLESTR lpwStreamName = L"persist";
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
GetDlgItem( IDC_NEXT )->EnableWindow( FALSE );
HRESULT hr = m_spPrime.CreateInstance( _T("Simple17.Prime") );
if( FAILED( hr ) )
{
AfxMessageBox( _T("注册了否?初始化了否?") );
m_spPrime.Release();
return TRUE;
}
CComQIPtr < IPersistStreamInit > spPSI;
spPSI = m_spPrime;
if( !spPSI )
{
AfxMessageBox( _T("没有找到持续性接口") );
m_spPrime.Release();
}
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::OnNew()
{
CComPtr < IStorage > spStorage;
CComPtr < IStream > spStream;
HRESULT hr = ::StgCreateDocfile(
lpwFileName,
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0,
&spStorage );
if( FAILED( hr ) )
{
AfxMessageBox( _T("不能建立复合文件") );
return;
}
hr = spStorage->CreateStream(
lpwStreamName,
STGM_CREATE | STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0,
0,
&spStream );
if( FAILED( hr ) )
{
AfxMessageBox( _T("不能建立流") );
return;
}
CComQIPtr < IPersistStreamInit > spPSI;
spPSI = m_spPrime;
if( !spPSI )
AfxMessageBox( _T("组件没有提供持续性接口") );
else
{
spPSI->InitNew();
GetDlgItem( IDC_NEXT )->EnableWindow( TRUE );
}
}
void CUseDlg::OnContinue()
{
CComPtr < IStorage > spStorage;
CComPtr < IStream > spStream;
HRESULT hr = ::StgOpenStorage(
lpwFileName,
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE,
NULL,
0,
&spStorage );
if( FAILED( hr ) )
{
AfxMessageBox( _T("不能打开复合文件") );
return;
}
spStorage->OpenStream(
lpwStreamName,
NULL,
STGM_READ | STGM_SHARE_EXCLUSIVE,
0,
&spStream );
if( FAILED( hr ) )
{
AfxMessageBox( _T("不能打开流读写") );
return;
}
CComQIPtr < IPersistStreamInit > spPSI;
spPSI = m_spPrime;
if( !spPSI )
AfxMessageBox( _T("组件没有提供持续性接口") );
else
{
spPSI->Load( spStream );
GetDlgItem( IDC_NEXT )->EnableWindow( TRUE );
}
}
void CUseDlg::OnNext()
{
long nPrime = m_spPrime->GetNext();
SetDlgItemInt( IDC_PRIME, nPrime );
}
void CUseDlg::OnCancel()
{
if( m_spPrime )
{
// 这里本应该取得 IPersistStreamInt 指针,但我们的处理却是
// 取得 IPersistStream 指针,无所谓啦,反正 IPersistStreamInit
// 指针是从 IPersistStream 继承来的,而且我们这里也不需要调用
// IPersistStreamInit::InitNew() 函数
CComQIPtr < IPersistStream > spPS;
spPS = m_spPrime;
HRESULT hr = spPS->IsDirty();
if( S_OK == hr ) // 数据已经改变,则需要保存
{
CComPtr < IStorage > spStorage;
CComPtr < IStream > spStream;
hr = ::StgOpenStorage(
lpwFileName,
NULL,
STGM_READWRITE | STGM_SHARE_EXCLUSIVE,
NULL,
0,
&spStorage );
if( SUCCEEDED( hr ) )
{
spStorage->OpenStream(
lpwStreamName,
NULL,
STGM_WRITE | STGM_SHARE_EXCLUSIVE,
0,
&spStream );
if( SUCCEEDED( hr ) )
spPS->Save( spStream, TRUE );
}
}
}
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -