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

📄 graphicsdemo2view.cpp

📁 VC++编程宝典,电子工业出版社出版,源代码,第一部分
💻 CPP
字号:
// GraphicsDemo2View.cpp : implementation of
// the CGraphicsDemo2View class
//

#include "stdafx.h"
#include "GraphicsDemo2.h"

#include "GraphicsDemo2Doc.h"
#include "GraphicsDemo2View.h"

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo2View

IMPLEMENT_DYNCREATE(CGraphicsDemo2View, CView)

BEGIN_MESSAGE_MAP(CGraphicsDemo2View, CView)
	//{{AFX_MSG_MAP(CGraphicsDemo2View)
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo2View construction/destruction

CGraphicsDemo2View::CGraphicsDemo2View()
{

	// The clipping regions upper
	// left corner will be at 10, 10.
	m_nRgnX = m_nRgnY = 10;

}

CGraphicsDemo2View::~CGraphicsDemo2View()
{
}

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo2View drawing

void CGraphicsDemo2View::OnDraw(CDC* pDC)
{
	CGraphicsDemo2Doc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
}

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo2View diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CGraphicsDemo2View message handlers

void CGraphicsDemo2View::OnMouseMove(UINT nFlags,
	CPoint point) 
{

	// Form an array of the star definition.
	// We don't make it static since it's
	// altered by addition each time OnMouseMove()
	// is called.
	POINT Point[] = {
		-59, -81, 0, 100, 59, -81,
		-95, 31, 95, 31 };

	// Loop through and add the mouse
	// coordinates to the star definition
	// vertices.
	for( int i=0; i<5; i++ ){
		Point[i].x += point.x;
		Point[i].y += point.y;
		}

	// Create a CBrush object with random red, green,
	// and blue hues.
	CBrush Brush( RGB( ( rand() & 0xff ),
		( rand() & 0xff ), ( rand() & 0xff ) ) );

	// Create a CPen object with random red, greeen,
	// and blue hues.
	CPen Pen( PS_SOLID, 1, RGB( ( rand() & 0xff ),
		( rand() & 0xff ), ( rand() & 0xff ) ) );

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

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

	// Select the newly-created CPen object into the DC
	// and remember the old CPen object that was selected
	// out.
	CPen *pOldPen = (CPen *) ClientDC.SelectObject( &Pen );
	
	// Create a rectangular region with which to
	// clip all draw operations.
	CRgn Rgn;
	Rgn.CreateRectRgn( m_nRgnX, m_nRgnY,
		m_nRgnX + 200, m_nRgnY + 200 );

	// Select the newly-created region into the DC.
	ClientDC.SelectClipRgn( &Rgn );

	// Use the GDI Polygon() function to draw the
	// polygon into the DC.
	ClientDC.Polygon( Point,
		sizeof( Point ) / sizeof( POINT ) );

	// Set the clipping region to none.
	ClientDC.SelectClipRgn( NULL );

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

	CView::OnMouseMove(nFlags, point);
}

void CGraphicsDemo2View::OnLButtonDown(UINT nFlags,
	CPoint point) 
{
	
	// Move the clipping region. Do this by taking
	// the mouse coodinates at which the left
	// button was clicked. Subtract 100 to arrive at
	// the upper left corner of the clipping region.
	m_nRgnX = point.x - 100;
	if( m_nRgnX < 0 ) m_nRgnX = 0;
	m_nRgnY = point.y - 100;
	if( m_nRgnY < 0 ) m_nRgnY = 0;

	CView::OnLButtonDown(nFlags, point);
}

⌨️ 快捷键说明

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