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

📄 demodlg.cpp

📁 这是MFC经典问答书的光盘内容
💻 CPP
字号:
// DemoDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ExpandingDialog.h"
#include "DemoDlg.h"

#include "DrawDoc.h"

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

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg dialog


CDemoDlg::CDemoDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDemoDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDemoDlg)
	m_nX = 0;
	m_nY = 0;
	m_nShape = -1;
	//}}AFX_DATA_INIT
}


void CDemoDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDemoDlg)
	DDX_Control(pDX, IDC_DRAW_AREA, m_DrawArea);
	DDX_Control(pDX, IDC_SHAPE, m_cmbShape);
	DDX_Control(pDX, IDC_Y, m_edtY);
	DDX_Control(pDX, IDC_X, m_edtX);
	DDX_Text(pDX, IDC_X, m_nX);
	DDX_Text(pDX, IDC_Y, m_nY);
	DDX_CBIndex(pDX, IDC_SHAPE, m_nShape);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDemoDlg, CDialog)
	//{{AFX_MSG_MAP(CDemoDlg)
	ON_WM_PAINT()
	ON_CBN_SELCHANGE(IDC_SHAPE, OnSelchangeShape)
	ON_BN_CLICKED(IDC_EXPAND, OnExpand)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDemoDlg message handlers

void CDemoDlg::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// 1 - Get placeholder coordinates
	CRect rect;		// Placeholder rectangle
	m_DrawArea.GetWindowRect( &rect );
	ScreenToClient( &rect );

	// 2 - Draw inside placeholder rectangle
	// Draw current shape
	CBrush brShape( RGB( 255, 0, 255 ) );
	CBrush* pOldBrush = dc.SelectObject( &brShape );

	int nShape = m_cmbShape.GetCurSel();

	switch( nShape )
	{
	default :
		ASSERT( FALSE );  // We should never get here !
		break;

	case CDrawDoc::SQUARE :
		dc.Rectangle( rect );
		break;

	case CDrawDoc::CIRCLE :
		dc.Ellipse( rect );
		break;
	}

	CPen pen( PS_SOLID, 2, RGB( 0, 0, 0 ) );
	CPen* pOldPen = dc.SelectObject( &pen );

	int x = ( rect.left + rect.right ) / 2;
	int y = ( rect.top + rect.bottom ) / 2;

	dc.MoveTo( x-3, y-3 );
	dc.LineTo( x+3, y+3 );

	dc.MoveTo( x-3, y+3 );
	dc.LineTo( x+3, y-3 );

	dc.SelectObject( pOldPen );
	dc.SelectObject( pOldBrush );
	
	// Do not call CDialog::OnPaint() for painting messages
}

void CDemoDlg::RedrawArea()
{
	// 1 - Get placeholder coordinates
	CRect rect;		// Placeholder rectangle
	m_DrawArea.GetWindowRect( &rect );
	ScreenToClient( &rect );

	// 2 - Force update of the drawing area
	InvalidateRect( rect );
	UpdateWindow();
}

void CDemoDlg::OnSelchangeShape() 
{
	RedrawArea();
}

void EkExpandDialog( CDialog* pDlg, UINT nSmallID, UINT nLargeID, UINT nButtonID )
{
	ASSERT_VALID( pDlg );

	// 1 - Get the More/Less button control
	CWnd* pWndButton = pDlg->GetDlgItem( nButtonID );
	ASSERT_VALID( pWndButton );

	// 2 - Load button resource string and parse More/Less parts
	CString strButton;
	VERIFY( strButton.LoadString( nButtonID ) );

	CString strMore, strLess;
	AfxExtractSubString( strMore, strButton, 0, _T( '\n' ) );
	AfxExtractSubString( strLess, strButton, 1, _T( '\n' ) );

	// 3 - Find out if we need to expand or collapse the dialog
	CString strCaption;
	pWndButton->GetWindowText( strCaption );
	BOOL bExpand = ( strCaption == strMore );	// Collapse by default

	// 4 - Get current dialog window rectangle
	CRect rcDialog;
	pDlg->GetWindowRect( &rcDialog );

	int nNewHeight = -1;

	if( bExpand )
	{
		// 5a - Change More/Less button caption
		pWndButton->SetWindowText( strLess );

		// 6a - Calculate new dialog height
		CWnd* pWndLarge = pDlg->GetDlgItem( nLargeID );
		ASSERT_VALID( pWndLarge );

		CRect rcLarge;
		pWndLarge->GetWindowRect( &rcLarge );

		nNewHeight = rcLarge.top-rcDialog.top;
	}
	else
	{
		// 5b - Change More/Less button caption
		pWndButton->SetWindowText( strMore );

		// 6b - Calculate new dialog height
		CWnd* pWndSmall = pDlg->GetDlgItem( nSmallID );
		ASSERT_VALID( pWndSmall );

		CRect rcSmall;
		pWndSmall->GetWindowRect( &rcSmall );

		nNewHeight = rcSmall.top-rcDialog.top;
	}

	// 7 - Set new dialog height
	ASSERT( nNewHeight > 0 );
	pDlg->SetWindowPos( NULL, 0, 0,
						rcDialog.Width(), nNewHeight,
						SWP_NOMOVE | SWP_NOZORDER );

	// 8 - Set the enabled state for each control depending on whether
	// the control is currently visible or not
	CWnd* pWndControl = pDlg->GetWindow( GW_CHILD );
	while( pWndControl != NULL )
	{
		CRect rcControl;
		pWndControl->GetWindowRect( &rcControl );
		pWndControl->EnableWindow( rcControl.top <= rcDialog.top + nNewHeight );
		pWndControl = pWndControl->GetWindow( GW_HWNDNEXT );
	}

	// 9 - Check if a control still has the focus
	// (can lose it if the active control becomes disabled)
	CWnd* pWndActiveControl = CWnd::GetFocus();
	if( pWndActiveControl == NULL )
	{
		// 10 - Set focus to "first" control on dialog
		CWnd* pWndFirstControl = pDlg->GetNextDlgTabItem( NULL );
		ASSERT_VALID( pWndFirstControl );
		ASSERT( pWndFirstControl->IsWindowEnabled() );
		pWndFirstControl->SetFocus();
	}
}

void CDemoDlg::OnExpand() 
{
	// Expand or collapse the dialog box
	EkExpandDialog( this, IDC_SMALL, IDC_LARGE, IDC_EXPAND );	
}

BOOL CDemoDlg::OnInitDialog() 
{
	CDialog::OnInitDialog();
	
	// Initially collapse the dialog box
	EkExpandDialog( this, IDC_SMALL, IDC_LARGE, IDC_EXPAND );	
	
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

⌨️ 快捷键说明

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