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

📄 psheetformview.cpp

📁 < MFC经典问答>>是微软类库经常遇到的问题
💻 CPP
字号:
// PSheetFormView.cpp : implementation file
//

#include "stdafx.h"
#include "PropertySheetInSplitter.h"

#include "DrawDoc.h"

#include "PSheetFormView.h"


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

/////////////////////////////////////////////////////////////////////////////
// CPSheetFormView

IMPLEMENT_DYNCREATE(CPSheetFormView, CFormView)

CPSheetFormView::CPSheetFormView()
	: CFormView(CPSheetFormView::IDD)
{
	//{{AFX_DATA_INIT(CPSheetFormView)
	//}}AFX_DATA_INIT
}

CPSheetFormView::~CPSheetFormView()
{
}

void CPSheetFormView::DoDataExchange(CDataExchange* pDX)
{
	CFormView::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPSheetFormView)
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPSheetFormView, CFormView)
	//{{AFX_MSG_MAP(CPSheetFormView)
	ON_BN_CLICKED(IDC_APPLY, OnApply)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPSheetFormView diagnostics

#ifdef _DEBUG
void CPSheetFormView::AssertValid() const
{
	CFormView::AssertValid();
}

void CPSheetFormView::Dump(CDumpContext& dc) const
{
	CFormView::Dump(dc);
}

CDrawDoc* CPSheetFormView::GetDocument() // non-debug version is inline
{
	ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
	return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// EkCreateEmbeddedPropertySheet

BOOL EkCreateEmbeddedPropertySheet(	CWnd* pParent, CPropertySheet* pPSheet,
									DWORD dwStyle = WS_CHILD | WS_VISIBLE, DWORD dwExStyle = 0 )
{
	ASSERT_VALID( pParent );
	ASSERT_VALID( pPSheet );

	// 1 - Create the embedded property sheet window
	if( !pPSheet->Create( pParent,  dwStyle, dwExStyle ) )
	{
		TRACE0( "Embedded property sheet creation failed\n" );
		return FALSE;
	}

	// 2 - Add WS_TABSTOP and WS_EX_CONTROLPARENT to the property sheet styles
	pPSheet->ModifyStyle( 0, WS_TABSTOP );
	pPSheet->ModifyStyleEx ( 0, WS_EX_CONTROLPARENT );

	// 3 - Add WS_EX_CONTROLPARENT to the parent window styles
	pParent->ModifyStyleEx ( 0, WS_EX_CONTROLPARENT );

	return TRUE;
}

/////////////////////////////////////////////////////////////////////////////
// EkPositionEmbeddedPropertySheet

void EkPositionEmbeddedPropertySheet(	CWnd* pParent, CPropertySheet* pPSheet,
										CRect rcNewPosition )
{
	ASSERT_VALID( pParent );
	ASSERT_VALID( pPSheet );

	// 1 - Get current coordinates of tab control
	// and property sheet window
	CTabCtrl* pTabCtrl = pPSheet->GetTabControl();
	ASSERT( pTabCtrl != NULL );

	CRect rcTabCtrl;
	pTabCtrl->GetWindowRect( &rcTabCtrl );
	pParent->ScreenToClient( &rcTabCtrl );

	CRect rcPSheet;
	pPSheet->GetWindowRect( &rcPSheet );
	pParent->ScreenToClient( &rcPSheet );

	// 2 - Calculate margin between property sheet
	// and tab control
	int dcx = rcPSheet.Width() - rcTabCtrl.Width();
	int dcy = rcPSheet.Height() - rcTabCtrl.Height();

	// 3 - Move and resize property sheet window
	// (also moves the tab window because it is a child
	// of the property sheet window)
	pPSheet->MoveWindow( 	rcNewPosition .left, rcNewPosition.top,
							rcNewPosition .Width(), rcNewPosition.Height() );

	// 4 - Resize tab control window to restore
	// right / bottom margins
	pTabCtrl->SetWindowPos(	NULL,
							0, 0,
							rcNewPosition.Width() - dcx,
							rcNewPosition.Height() - dcy, 
							SWP_NOZORDER | SWP_NOMOVE | SWP_NOACTIVATE );

	// 5 - Activate each property page to prevent drawing
	// problem
	int nCurrentPage = pPSheet->GetActiveIndex();
	for( int i = 0; i < pPSheet->GetPageCount(); ++i )
	{
		pPSheet->SetActivePage( i );
	}

	pPSheet->SetActivePage( nCurrentPage );
}

void EkPositionEmbeddedPropertySheet(	CWnd* pParent, CPropertySheet* pPSheet,
										UINT nIDPSheetArea )
{
	ASSERT_VALID( pParent );
	ASSERT_VALID( pPSheet );

	// 1 - Retrieve property sheet destination position
	CRect rcNewPosition;
	CWnd* pWndNewArea = pParent->GetDlgItem( nIDPSheetArea );
	if( pWndNewArea == NULL )
	{
		ASSERT( FALSE );	// Invalid nIDPSheetArea
		return;
	}

	pWndNewArea->GetWindowRect( &rcNewPosition );
	pParent->ScreenToClient( &rcNewPosition );

	// 2 - Call overloaded function
	EkPositionEmbeddedPropertySheet( pParent, pPSheet, rcNewPosition );
}

/////////////////////////////////////////////////////////////////////////////
// CPSheetFormView message handlers

void CPSheetFormView::OnUpdate(CView* pSender, LPARAM lHint, CObject* pHint) 
{
	// 1 - Get the current data from our document
	CDrawDoc* pDoc = GetDocument();

	m_psheet.m_CoordsPPage.m_nX = pDoc->m_point.x;
	m_psheet.m_CoordsPPage.m_nY = pDoc->m_point.y;
	m_psheet.m_ShapePPage.m_nShape = pDoc->m_shape;

	// 2 - Show the current data in the form view controls
	UpdateData( FALSE );

	// 3 - Show the current data in the active
	// property page
	if( m_psheet.GetSafeHwnd() != NULL )
	{
		m_psheet.GetActivePage()->UpdateData( FALSE );
	}
}

void CPSheetFormView::OnApply() 
{
	// 1 - Get the data from our form view controls
	UpdateData( TRUE );

	// 2 - Get the data from the active property page
	m_psheet.GetActivePage()->UpdateData( TRUE );
	
	// 3 - Update the document state
	CDrawDoc* pDoc = GetDocument();

	pDoc->m_point.x = m_psheet.m_CoordsPPage.m_nX;
	pDoc->m_point.y = m_psheet.m_CoordsPPage.m_nY;
	pDoc->m_shape = static_cast< CDrawDoc::SHAPE >( m_psheet.m_ShapePPage.m_nShape );

	// 4 - Tell the document something happened
	pDoc->SetModifiedFlag( TRUE );

	// 5 - Synchronize all other views
	pDoc->UpdateAllViews( this );
}

void CPSheetFormView::OnInitialUpdate() 
{
	// 1 - Let base class do default processing
	CFormView::OnInitialUpdate();

	// 2 - Create and position embedded property sheet
	EkCreateEmbeddedPropertySheet( this, &m_psheet );
	EkPositionEmbeddedPropertySheet( this, &m_psheet, IDC_PSHEET_AREA );

	// 3 - Resize parent frame to fit the Form View dialog template
	GetParentFrame()->RecalcLayout();
	ResizeParentToFit( FALSE );
	ResizeParentToFit( TRUE );

	// 4 - Set focus to first control with WS_TABSTOP style on form view
	CWnd* pChild = GetWindow( GW_CHILD );
	while( pChild != NULL && ( pChild->GetStyle() & WS_TABSTOP ) == 0 )
	{
		pChild = pChild->GetNextWindow( GW_HWNDNEXT );
	}

	if( pChild != NULL )
	{
		pChild->SetFocus();
	}

}

⌨️ 快捷键说明

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