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

📄 vc09aview.cpp

📁 Visual C++ 6.0编程基础与范例
💻 CPP
字号:
// vc09aView.cpp : implementation of the CVc09aView class
//

#include "stdafx.h"
#include "vc09a.h"

#include "vc09aDoc.h"
#include "vc09aView.h"

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

/////////////////////////////////////////////////////////////////////////////
// CVc09aView

IMPLEMENT_DYNCREATE(CVc09aView, CView)

BEGIN_MESSAGE_MAP(CVc09aView, CView)
	//{{AFX_MSG_MAP(CVc09aView)
	ON_WM_CREATE()
	ON_COMMAND(ID_WIDTH1, OnWidth1)
	ON_UPDATE_COMMAND_UI(ID_WIDTH1, OnUpdateWidth1)
	ON_COMMAND(ID_WIDTH5, OnWidth5)
	ON_UPDATE_COMMAND_UI(ID_WIDTH5, OnUpdateWidth5)
	ON_COMMAND(ID_COLOR, OnColor)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONUP()
	ON_WM_MOUSEMOVE()
	ON_WM_SETCURSOR()
	ON_COMMAND(ID_WIDTH2, OnWidth2)
	ON_UPDATE_COMMAND_UI(ID_WIDTH2, OnUpdateWidth2)
	ON_COMMAND(ID_WIDTH3, OnWidth3)
	ON_UPDATE_COMMAND_UI(ID_WIDTH3, OnUpdateWidth3)
	ON_COMMAND(ID_WIDTH4, OnWidth4)
	ON_UPDATE_COMMAND_UI(ID_WIDTH4, OnUpdateWidth4)
	ON_COMMAND(ID_FILL, OnFill)
	ON_UPDATE_COMMAND_UI(ID_FILL, OnUpdateFill)
	ON_COMMAND(ID_LINE, OnLine)
	ON_UPDATE_COMMAND_UI(ID_LINE, OnUpdateLine)
	ON_COMMAND(ID_RECT, OnRect)
	ON_UPDATE_COMMAND_UI(ID_RECT, OnUpdateRect)
	ON_UPDATE_COMMAND_UI(ID_ELLIPSE, OnUpdateEllipse)
	ON_COMMAND(ID_ELLIRECT, OnEllirect)
	ON_UPDATE_COMMAND_UI(ID_ELLIRECT, OnUpdateEllirect)
	ON_COMMAND(ID_ELLIPSE, OnEllipse)
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CVc09aView construction/destruction

CVc09aView::CVc09aView()
{
	// TODO: add construction code here

	m_bdoing=FALSE;
	m_type=0;
	m_nwidth=1;
	m_color=RGB(0,0,255);
	m_pmdc=new CDC;
	m_pbmp=new CBitmap;
}

CVc09aView::~CVc09aView()
{
	delete m_pmdc;
	delete m_pbmp;
}

BOOL CVc09aView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

	return CView::PreCreateWindow(cs);
}



/////////////////////////////////////////////////////////////////////////////
// CVc09aView drawing

void CVc09aView::OnDraw(CDC* pDC)
{
	CVc09aDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
	// TODO: add draw code for native data here

	CBitmap* poldbmp=m_pmdc->SelectObject(m_pbmp);
	pDC->BitBlt(0,0,m_nmx,m_nmy,m_pmdc,0,0,SRCCOPY);
	m_pmdc->SelectObject(poldbmp);
}

/////////////////////////////////////////////////////////////////////////////
// CVc09aView printing

BOOL CVc09aView::OnPreparePrinting(CPrintInfo* pInfo)
{
	// default preparation
	return DoPreparePrinting(pInfo);
}

void CVc09aView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add extra initialization before printing
}

void CVc09aView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
	// TODO: add cleanup after printing
}

/////////////////////////////////////////////////////////////////////////////
// CVc09aView diagnostics

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

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

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

/////////////////////////////////////////////////////////////////////////////
// CVc09aView message handlers

int CVc09aView::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CView::OnCreate(lpCreateStruct) == -1)
		return -1;

	// TODO: Add your specialized creation code here
	m_nmx=GetSystemMetrics(SM_CXSCREEN);
	m_nmy=GetSystemMetrics(SM_CYSCREEN);

	CDC* pDC=GetDC();
	m_pmdc->CreateCompatibleDC(pDC);
	m_pbmp->CreateCompatibleBitmap(pDC,m_nmx,m_nmy);

	CBitmap* pOldbitmap=m_pmdc->SelectObject(m_pbmp);

	CBrush brush;
	brush.CreateStockObject(WHITE_BRUSH);
	CRect rect(-1,-1,m_nmx,m_nmy);
	m_pmdc->FillRect(rect,&brush);
	m_pmdc->SelectObject(pOldbitmap);
	ReleaseDC(pDC);

	return 0;
}

void CVc09aView::OnWidth1() 
{
	// TODO: Add your command handler code here
	m_nwidth=1;
}

void CVc09aView::OnUpdateWidth1(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nwidth ==1);
}

void CVc09aView::OnWidth2() 
{
	// TODO: Add your command handler code here
	m_nwidth=2;
}

void CVc09aView::OnUpdateWidth2(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nwidth ==2);
}

void CVc09aView::OnWidth3() 
{
	// TODO: Add your command handler code here
	m_nwidth=3;
}

void CVc09aView::OnUpdateWidth3(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nwidth ==3);
}

void CVc09aView::OnWidth4() 
{
	// TODO: Add your command handler code here
	m_nwidth=4;
}

void CVc09aView::OnUpdateWidth4(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nwidth ==4);
}

void CVc09aView::OnWidth5() 
{
	// TODO: Add your command handler code here
	m_nwidth=5;
}

void CVc09aView::OnUpdateWidth5(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	pCmdUI->SetCheck(m_nwidth ==5);
}

void CVc09aView::OnColor() 
{
	// TODO: Add your command handler code here
	CColorDialog dlg;
	dlg.m_cc.Flags |=CC_PREVENTFULLOPEN | CC_RGBINIT;
	dlg.m_cc.rgbResult = m_color;
	if(dlg.DoModal()==IDOK)
	{
		m_color=dlg.GetColor();
	}
}

void CVc09aView::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	if(m_bdoing)
		return;

	SetCapture();
	m_bdoing=true;
	m_pnew=point;
	m_pold=point;
	if(m_type==4)
	{
		CBrush* poldbrush;
		CBitmap* poldbmp;
		CBrush bfill;
		bfill.CreateSolidBrush(m_color);
		poldbrush=m_pmdc->SelectObject(&bfill);
		poldbmp=m_pmdc->SelectObject(m_pbmp);
		m_pmdc->ExtFloodFill(point.x,point.y,m_pmdc->GetPixel(point),FLOODFILLSURFACE);
		Invalidate(FALSE);
		m_pmdc->SelectObject(poldbrush);
		m_pmdc->SelectObject(poldbmp);
		m_bdoing=FALSE;
	}
}

void CVc09aView::OnLButtonUp(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	if(m_bdoing)
	{
		m_bdoing=FALSE;
		CBitmap* poldbmp=m_pmdc->SelectObject(m_pbmp);
		CPen pen;
		pen.CreatePen(PS_SOLID,m_nwidth,m_color);
		CPen* poldpen=m_pmdc->SelectObject(&pen);
		CBrush* poldbrush=(CBrush*)m_pmdc->SelectStockObject(NULL_BRUSH);
		CRect rect(m_pnew,m_pold);
		switch(m_type)
		{
		case 0:
			m_pmdc->MoveTo(m_pold);
			m_pmdc->LineTo(point);
			break;
		case 1:
			m_pmdc->Rectangle(rect);
			break;
		case 2:
			m_pmdc->Ellipse(rect);
			break;
		case 3:
			m_pmdc->RoundRect(m_pnew.x,m_pnew.y,m_pold.x,m_pold.y,10,10);
			break;
		case 4:
		default:
			break;
		}
		Invalidate(FALSE);
		m_pmdc->SelectObject(poldbmp);
		m_pmdc->SelectObject(poldpen);
		m_pmdc->SelectObject(poldbrush);
	}
	ReleaseCapture();
	
//	CView::OnLButtonUp(nFlags, point);
}

void CVc09aView::OnMouseMove(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	
	if(m_bdoing)
	{
		CDC* pDC=GetDC();
		CBitmap* poldbmp=m_pmdc->SelectObject(m_pbmp);
		CPen pen;
		pen.CreatePen(PS_SOLID,m_nwidth,m_color);
		CPen* poldpen=pDC->SelectObject(&pen);
		CBrush* poldbrush=(CBrush*)pDC->SelectStockObject(NULL_BRUSH);
		CRect rectold(m_pold,m_pnew);
		rectold.NormalizeRect();
		rectold.InflateRect(m_nwidth,m_nwidth);
		pDC->BitBlt(rectold.left,rectold.top,rectold.Width(),rectold.Height(),
			m_pmdc,rectold.left,rectold.top,SRCCOPY);
		CRect rectnew(m_pold,point);
		switch(m_type)
		{
		case 0:
			pDC->MoveTo(m_pold);
			pDC->LineTo(point);
			break;
		case 1:
			pDC->Rectangle(rectnew);
			break;
		case 2:
			pDC->Ellipse(rectnew);
			break;
		case 3:
			pDC->RoundRect(m_pold.x,m_pold.y,point.x,point.y,10,10);
			break;
		case 4:
		default:
			break;
		}
		m_pmdc->SelectObject(poldbmp);
		pDC->SelectObject(poldpen);
		pDC->SelectObject(poldbrush);
		ReleaseDC(pDC);
		m_pnew=point;
	}

}

BOOL CVc09aView::OnSetCursor(CWnd* pWnd, UINT nHitTest, UINT message) 
{
	// TODO: Add your message handler code here and/or call default

	switch(m_type)
	{
	case 0:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR2));
		break;

	case 1:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR4));
		break;

	case 2:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR3));
		break;

	case 3:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR5));
		break;

	case 4:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_CURSOR1));
		break;

	default:
		::SetCursor(AfxGetApp()->LoadCursor(IDC_ARROW));
		break;
	}
	return TRUE;
}


void CVc09aView::OnFill() 
{
	// TODO: Add your command handler code here
	
	m_type=4;
}

void CVc09aView::OnUpdateFill(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->SetCheck(m_type==4);
}

void CVc09aView::OnLine() 
{
	// TODO: Add your command handler code here
	
	m_type=0;
}

void CVc09aView::OnUpdateLine(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->SetCheck(m_type==0);
}

void CVc09aView::OnRect() 
{
	// TODO: Add your command handler code here
	
	m_type=1;
}

void CVc09aView::OnUpdateRect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->SetCheck(m_type==1);
}

void CVc09aView::OnEllipse() 
{
	// TODO: Add your command handler code here
	
	m_type=2;
}

void CVc09aView::OnUpdateEllipse(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->SetCheck(m_type==2);
}

void CVc09aView::OnEllirect() 
{
	// TODO: Add your command handler code here
	
	m_type=3;
}

void CVc09aView::OnUpdateEllirect(CCmdUI* pCmdUI) 
{
	// TODO: Add your command update UI handler code here
	
	pCmdUI->SetCheck(m_type==3);
}

⌨️ 快捷键说明

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