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

📄 penbrushdemoview.cpp

📁 < VC++编程宝典>>配套源代码, 对于想精通VC++的朋友很有帮助!
💻 CPP
字号:
// PenBrushDemoView.cpp : implementation of
// the CPenBrushDemoView class
//

#include "stdafx.h"
#include "PenBrushDemo.h"

#include "PenBrushDemoDoc.h"
#include "PenBrushDemoView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CPenBrushDemoView

IMPLEMENT_DYNCREATE(CPenBrushDemoView, CView)

BEGIN_MESSAGE_MAP(CPenBrushDemoView, CView)
	//{{AFX_MSG_MAP(CPenBrushDemoView)
	ON_COMMAND(ID_OPERATIONTYPE_LINES, OnOperationtype)
	ON_COMMAND(ID_OPERATIONTYPE_RECTANGLES, OnOperationtype)
	ON_UPDATE_COMMAND_UI(ID_OPERATIONTYPE_LINES, OnUpdateOperationtype)
	ON_UPDATE_COMMAND_UI(ID_OPERATIONTYPE_RECTANGLES, OnUpdateOperationtype)
	ON_COMMAND(ID_LINEWIDTH_ONE, OnLinewidth)
	ON_COMMAND(ID_LINEWIDTH_TWO, OnLinewidth)
	ON_COMMAND(ID_LINEWIDTH_FOUR, OnLinewidth)
	ON_COMMAND(ID_LINEWIDTH_EIGHT, OnLinewidth)
	ON_COMMAND(ID_LINEWIDTH_SIXTEEN, OnLinewidth)
	ON_UPDATE_COMMAND_UI(ID_LINEWIDTH_ONE, OnUpdateLinewidth)
	ON_UPDATE_COMMAND_UI(ID_LINEWIDTH_TWO, OnUpdateLinewidth)
	ON_UPDATE_COMMAND_UI(ID_LINEWIDTH_FOUR, OnUpdateLinewidth)
	ON_UPDATE_COMMAND_UI(ID_LINEWIDTH_EIGHT, OnUpdateLinewidth)
	ON_UPDATE_COMMAND_UI(ID_LINEWIDTH_SIXTEEN, OnUpdateLinewidth)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPenBrushDemoView construction/destruction

CPenBrushDemoView::CPenBrushDemoView()
{

	// Set the initial operation type to line.
	m_nOperationType = 0;

	// Lines will have a width of 1 initially.
	m_nLineWidth = 0;

}

CPenBrushDemoView::~CPenBrushDemoView()
{
}

BOOL CPenBrushDemoView::PreCreateWindow(CREATESTRUCT& cs)
{
	return CView::PreCreateWindow(cs);
}

/////////////////////////////////////////////////////////////////////////////
// CPenBrushDemoView drawing

void CPenBrushDemoView::OnDraw(CDC* pDC)
{
	CPenBrushDemoDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	int i;

	// Keep the text strings describing
	// the line styles in a static array.
	static char *szLineStyles[] = {
		"PS_SOLID", "PS_DASH", "PS_DOT", "PS_DASHDOT",
		"PS_DASHDOTDOT", "PS_NULL", "PS_INSIDEFRAME" };

	// Keep the line style definitions in a
	// static array.
	static int nLineStyles[] = {
		PS_SOLID, PS_DASH, PS_DOT, PS_DASHDOT,
		PS_DASHDOTDOT, PS_NULL, PS_INSIDEFRAME };

	// Keep the text strings describing
	// the brush styles in a static array.
	static char *szBrushStyles[] = {
		"HS_BDIAGONAL", "HS_CROSS", "HS_DIAGCROSS",
		"HS_FDIAGONAL", "HS_HORIZONTAL", "HS_VERTICAL" };

	// Keep the brush style definitions in a
	// static array.
	static int nBrushStyles[] = {
		HS_BDIAGONAL, HS_CROSS, HS_DIAGCROSS,
		HS_FDIAGONAL, HS_HORIZONTAL, HS_VERTICAL };

	switch( m_nOperationType ){
		case 0:
			for( i=0; i<7; i++ ){

				// Display the text string describing
				// the line style.
				pDC->TextOut( 10, 10 + i * 35,
					szLineStyles[i] );

				// Create the pen based on the
				// style and width
				CPen Pen( nLineStyles[i],
					1 << m_nLineWidth,
					RGB( 0, 0, 0 ) );

				// Select the CPen object into the DC
				// and remember the CPen object that's
				// selected out.
				CPen *pOldPen = pDC->SelectObject( &Pen );

				// Draw the line.
				pDC->MoveTo( 150, 17 + i * 35 );
				pDC->LineTo( 300, 17 + i * 35 );

				// Select the old CPen object back into
				// the DC.
				pDC->SelectObject( pOldPen );
				}
			break;

		case 1:
			// Create a CPen object based on the line width
			// that the user selected.
			CPen Pen( PS_INSIDEFRAME, 1 << m_nLineWidth,
				RGB( 0, 0, 0 ) );

			// Select the newly-created CPen object into
			// the DC and remember the old CPen object
			// that's selected out.
			CPen *pOldPen = pDC->SelectObject( &Pen );

			for( i=0; i<6; i++ ){

				// Display the text string describing
				// the brush style.
				pDC->TextOut( 10, 23 + i * 55,
					szBrushStyles[i] );

				// Create a CBrush object based on the selected
				// brush style.
				CBrush Brush( nBrushStyles[i], RGB( 0, 0, 0 ) );

				// Select the newly-created CBrush object into
				// the DC and remember the CBrush object that's
				// selected out.
				CBrush *pOldBrush = pDC->SelectObject( &Brush );

				// Create a CRect object with the coordinates
				// of the destination rectangle.
				CRect Rect( 150, 10 + i * 55, 300, 60 + i * 55 );

				// Draw the rectangle.
				pDC->Rectangle( Rect );

				// Select the old CBrush object into the DC.
				pDC->SelectObject( pOldBrush );
				}

			// Select the old CPen object into the DC.
			pDC->SelectObject( pOldPen );
			break;
		}

}

/////////////////////////////////////////////////////////////////////////////
// CPenBrushDemoView diagnostics

#ifdef _DEBUG
void CPenBrushDemoView::AssertValid() const
{
	CView::AssertValid();
}

void CPenBrushDemoView::Dump(CDumpContext& dc) const
{
	CView::Dump(dc);
}

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

/////////////////////////////////////////////////////////////////////////////
// CPenBrushDemoView message handlers

void CPenBrushDemoView::OnOperationtype() 
{

	m_nOperationType =
		LOWORD( GetCurrentMessage()->wParam -
			ID_OPERATIONTYPE_LINES );
	
	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

void CPenBrushDemoView::OnUpdateOperationtype(
	CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck( (int) pCmdUI->m_nIndex ==
		m_nOperationType );

}

void CPenBrushDemoView::OnLinewidth() 
{

	m_nLineWidth =
		LOWORD( GetCurrentMessage()->wParam -
			ID_LINEWIDTH_ONE );

	InvalidateRect( NULL, TRUE );
	UpdateWindow();

}

void CPenBrushDemoView::OnUpdateLinewidth(CCmdUI* pCmdUI) 
{

	pCmdUI->SetCheck( (int) pCmdUI->m_nIndex ==
		m_nLineWidth );

}

⌨️ 快捷键说明

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