📄 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)
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 CUseDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUseDlg)
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(CUseDlg, CDialog)
//{{AFX_MSG_MAP(CUseDlg)
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADVISE, OnAdvise)
ON_BN_CLICKED(IDC_UNADVISE, OnUnadvise)
//}}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
m_dwCookie = 0;
HRESULT hr = m_spObj.CreateInstance( _T("Simple15.DispConnect.1") );
if( FAILED( hr ) )
{
AfxMessageBox( _T("没有注册还是没有初始化?") );
CDialog::OnCancel();
}
GetDlgItem( IDC_UNADVISE )->EnableWindow( FALSE );
GetDlgItem( IDC_ADVISE )->EnableWindow( TRUE );
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::OnAdvise()
{
// 得到连接点容器接口
CComQIPtr<IConnectionPointContainer> spContainer( m_spObj );
if( !spContainer )
{
AfxMessageBox( _T("组件没有提供连接点功能") );
return;
}
// 得到连接点接口
spContainer->FindConnectionPoint(
__uuidof(_IDispConnectEvents),
&m_spCP );
if( !m_spCP )
{
AfxMessageBox( _T("没有找到连接点接口") );
return;
}
// CComPtr<IUnknown> spUnk; //得到接受器接口
// m_sink.QueryInterface( IID_IUnknown, (LPVOID *)&spUnk ); //m_sink是接受器对象
// hr=spCP->Advise( pUnk, &m_dwCookie); //连接,m_dwCookie 是接受器cookie
// 上面这段程序,其实可以简化为:
HRESULT hr = m_spCP->Advise( &m_sink, &m_dwCookie );
if( FAILED( hr ) )
{
AfxMessageBox( _T("连接失败") );
}
m_sink.SetResultWnd( &m_edit );
AfxMessageBox( _T("已经连接成功") );
GetDlgItem( IDC_UNADVISE )->EnableWindow( TRUE );
GetDlgItem( IDC_ADVISE )->EnableWindow( FALSE );
}
void CUseDlg::OnUnadvise()
{
if( m_spCP )
{
m_spCP->Unadvise( m_dwCookie );
m_spCP.Release();
}
AfxMessageBox( _T("已经断开连接") );
GetDlgItem( IDC_UNADVISE )->EnableWindow( FALSE );
GetDlgItem( IDC_ADVISE )->EnableWindow( TRUE );
}
void CUseDlg::OnOK()
{
UpdateData();
m_spObj->Add( m_n1, m_n2 );
}
void CUseDlg::OnCancel()
{
if( m_spCP )
m_spCP->Unadvise( m_dwCookie );
CDialog::OnCancel();
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -