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

📄 graphicsdemo1view.cpp

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

#include "stdafx.h"
#include "GraphicsDemo1.h"

#include "GraphicsDemo1Doc.h"
#include "GraphicsDemo1View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo1View

IMPLEMENT_DYNCREATE(CGraphicsDemo1View, CView)

BEGIN_MESSAGE_MAP(CGraphicsDemo1View, CView)
	//{{AFX_MSG_MAP(CGraphicsDemo1View)
	ON_COMMAND(ID_ROP2CODE_R2BLACKPEN, OnRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2BLACKPEN, OnUpdateRop2code)
	ON_COMMAND(ID_COLOR_SELECTBORDERCOLOR, OnColorSelectcolor)
	ON_COMMAND(ID_DRAWPRIMITIVE_BORDEREDELLIPSE, OnDrawprimitive)
	ON_UPDATE_COMMAND_UI(ID_DRAWPRIMITIVE_BORDEREDELLIPSE, OnUpdateDrawprimitive)
	ON_COMMAND(ID_ROP2CODE_R2COPYPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MASKNOTPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MASKPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MASKPENNOT, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MERGENOTPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MERGEPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2MERGEPENNOT, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOP, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOT, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOTCOPYPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOTMASKPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOTMERGEPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2NOTXORPEN, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2WHITE, OnRop2code)
	ON_COMMAND(ID_ROP2CODE_R2XORPEN, OnRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2COPYPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MASKNOTPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MASKPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MASKPENNOT, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MERGENOTPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MERGEPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2MERGEPENNOT, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOP, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOT, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOTCOPYPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOTMASKPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOTMERGEPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2NOTXORPEN, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2WHITE, OnUpdateRop2code)
	ON_UPDATE_COMMAND_UI(ID_ROP2CODE_R2XORPEN, OnUpdateRop2code)
	ON_COMMAND(ID_COLOR_SELECTFILLCOLOR, OnColorSelectcolor)
	ON_COMMAND(ID_DRAWPRIMITIVE_BORDEREDRECTANGLE, OnDrawprimitive)
	ON_COMMAND(ID_DRAWPRIMITIVE_FILLEDELLIPSE, OnDrawprimitive)
	ON_COMMAND(ID_DRAWPRIMITIVE_FILLEDRECTANGLE, OnDrawprimitive)
	ON_COMMAND(ID_DRAWPRIMITIVE_LINE, OnDrawprimitive)
	ON_UPDATE_COMMAND_UI(ID_DRAWPRIMITIVE_BORDEREDRECTANGLE, OnUpdateDrawprimitive)
	ON_UPDATE_COMMAND_UI(ID_DRAWPRIMITIVE_FILLEDELLIPSE, OnUpdateDrawprimitive)
	ON_UPDATE_COMMAND_UI(ID_DRAWPRIMITIVE_FILLEDRECTANGLE, OnUpdateDrawprimitive)
	ON_UPDATE_COMMAND_UI(ID_DRAWPRIMITIVE_LINE, OnUpdateDrawprimitive)
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo1View construction/destruction

CGraphicsDemo1View::CGraphicsDemo1View()
{

	// Set the initial raster operation
	// to R2_COPYPEN.
	m_nROP2Code = R2_COPYPEN;

	// Set the initial fill color to red.
	m_FillColor = RGB( 255, 0, 0 );

	// Set the initial border color to blue.
	m_BorderColor = RGB( 0, 0, 255 );

	// Set the initial draw operation to line.
	m_nDrawPrimitive = ID_DRAWPRIMITIVE_LINE;

}

CGraphicsDemo1View::~CGraphicsDemo1View()
{
}

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo1View drawing

void CGraphicsDemo1View::OnDraw(CDC* pDC)
{
	CGraphicsDemo1Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);

	// OnDraw() doesn't do much. Just display the
	// instructions "Click Mouse in Window".
	pDC->TextOut( 10, 10, "Click Mouse in Window", 21 );
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo1View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo1View message handlers

// Store the actual GDI ROP codes in an
// array so that we can easily set the
// ROP code depending on the value of
// m_nROP2Code.
static int nROP2Code[] = {
R2_BLACK, R2_WHITE, R2_NOP, R2_NOT,
R2_COPYPEN, R2_NOTCOPYPEN, R2_MERGEPENNOT,
R2_MASKPENNOT, R2_MERGENOTPEN, R2_MASKNOTPEN,
R2_MERGEPEN, R2_NOTMERGEPEN, R2_MASKPEN,
R2_NOTMASKPEN, R2_XORPEN, R2_NOTXORPEN };

// This array contains the IDs for the
// menu items so that we can easily
// scan the list and obtain a zero-based
// index into this list. That way, we'll
// have a value from 0-whatever that lets
// us know which menu item the user selected.
static int nMenuID[] = {
ID_ROP2CODE_R2BLACKPEN, ID_ROP2CODE_R2WHITE,
ID_ROP2CODE_R2NOP, ID_ROP2CODE_R2NOT,
ID_ROP2CODE_R2COPYPEN, ID_ROP2CODE_R2NOTCOPYPEN,
ID_ROP2CODE_R2MERGEPENNOT, ID_ROP2CODE_R2MASKPENNOT,
ID_ROP2CODE_R2MERGENOTPEN, ID_ROP2CODE_R2MASKNOTPEN,
ID_ROP2CODE_R2MERGEPEN, ID_ROP2CODE_R2NOTMERGEPEN,
ID_ROP2CODE_R2MASKPEN, ID_ROP2CODE_R2NOTMASKPEN,
ID_ROP2CODE_R2XORPEN, ID_ROP2CODE_R2NOTXORPEN };

void CGraphicsDemo1View::OnRop2code() 
{

	// Obtain the menu ID that was selected.
	int nID = (UINT) LOWORD( GetCurrentMessage()->wParam );

	// Loop through and match the menu ID with the
	// menu ID in the static list. Use this match
	// to get a zero-based value for the menu selection.
	for( int i=0; i<sizeof(nMenuID)/sizeof(int); i++ ){
		if( nID == nMenuID[i] ){
			m_nROP2Code = nROP2Code[i];
			break;
			}
		}

}

void CGraphicsDemo1View::OnUpdateRop2code(CCmdUI* pCmdUI) 
{

	// Loop through and match the ID of this menu
	// item with the ID of the menu item in the static
	// list. Then, see if m_nROP2Code matches the loop
	// value. If so, set the check; if not, don't set
	// the check.
	for( int i=0; i<sizeof(nMenuID)/sizeof(int); i++ ){
		if( (int) pCmdUI->m_nID == nMenuID[i] ){
			pCmdUI->SetCheck( m_nROP2Code == nROP2Code[i] );
			break;
			}
		}
	
}

void CGraphicsDemo1View::OnColorSelectcolor() 
{

	// Get the ID of the menu selection.
	int nID = (UINT) LOWORD( GetCurrentMessage()->wParam );

	CColorDialog ColorDialog;

	// Invoke the common color selector
	// dialog box.
	if( ColorDialog.DoModal() == IDOK ){
		// We've set either the fill color or the
		// border color.
		if( nID == ID_COLOR_SELECTFILLCOLOR )
			m_FillColor = ColorDialog.GetColor();
		else m_BorderColor = ColorDialog.GetColor();
		}

}

void CGraphicsDemo1View::OnDrawprimitive() 
{

	// Get the ID for the selected menu
	// item and store it in m_nDrawPrimitive.
	m_nDrawPrimitive =
		(UINT) LOWORD( GetCurrentMessage()->wParam );

}

void CGraphicsDemo1View::OnUpdateDrawprimitive(
	CCmdUI* pCmdUI) 
{

	// If this ID matches the ID in m_nDrawPrimitive,
	// set the menu item check.
	pCmdUI->SetCheck( m_nDrawPrimitive ==
		(int) pCmdUI->m_nID );

}

void CGraphicsDemo1View::OnLButtonDown(UINT nFlags,
	CPoint point) 
{

	// Create a DC for the drawing operations.
	CClientDC ClientDC( this );

	// Generate four random points based on
	// the mouse position.
	int x1 = point.x - ( rand() & 0x7f );
	int y1 = point.y - ( rand() & 0x7f );
	int x2 = point.x + ( rand() & 0x7f );
	int y2 = point.y + ( rand() & 0x7f );

	// Create a CRect object with which we'll
	// perform some of our draw operations.
	CRect Rect( x1, y1, x2, y2 );

	// Create a CBrush object based on the fill color.
	CBrush Brush( m_FillColor );

	// Create a CPen object based on the border color.
	CPen Pen( PS_SOLID, 1, m_BorderColor );

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

	// Select the newly-created CPen object into the
	// DC and remember the CPen object that got
	// selected out.
	CPen *pOldPen = (CPen *) ClientDC.SelectObject( &Pen );

	// Set the ROP code.
	ClientDC.SetROP2( m_nROP2Code );

	// For the filled rectangle and ellipse,
	// we create a pen based on the fill color.
	// This causes the rectangle to draw with
	// the same color in the border and fill
	// interior.
	CPen MatchPen( PS_SOLID, 1, m_FillColor );

	switch( m_nDrawPrimitive ){

		case ID_DRAWPRIMITIVE_LINE:
			// For a simple line we use
			// the four integer coordinates
			// instead of the CRect object.
			ClientDC.MoveTo( x1, y1 );
			ClientDC.LineTo( x2, y2 );
			break;

		case ID_DRAWPRIMITIVE_FILLEDRECTANGLE:
			// For the filled rectangle, we create
			// a pen based on the fill color. This
			// causes the rectangle to draw with
			// the same color in the border and fill
			// interior.
			ClientDC.SelectObject( &MatchPen );

		case ID_DRAWPRIMITIVE_BORDEREDRECTANGLE:
			// For both the bordered rectangle and
			// the filled rectangle, we make the same
			// call to the Rectangle() function. The
			// only difference is that for the filled
			// rectangle we made sure we set to a
			// pen of a color that matches the fill color.
			ClientDC.Rectangle( &Rect );
			break;

		case ID_DRAWPRIMITIVE_FILLEDELLIPSE:
			// For the filled ellipse, we create
			// a pen based on the fill color. This
			// causes the rectangle to draw with
			// the same color in the border and fill
			// interior.
			ClientDC.SelectObject( &MatchPen );

		case ID_DRAWPRIMITIVE_BORDEREDELLIPSE:
			// For both the bordered ellipse and
			// the filled ellipse, we make the same
			// call to the Rectangle() function. The
			// only difference is that for the filled
			// rectangle we made sure we set to a
			// pen of a color that matches the fill color.
			ClientDC.Ellipse( &Rect );
			break;

		}

	// Select the old CPen and CBrush objects
	// back into the DC.
	ClientDC.SelectObject( pOldPen );
	ClientDC.SelectObject( pOldPen );

	CView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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