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

📄 painter2.cpp

📁 简单的MFC应用程序
💻 CPP
字号:
#include<afxwin.h>
#include"resource.h"
#include<afxtempl.h>
#include<afxext.h>
class GraphicObject: public CObject
{
public:
	int shapenum;
	BOOL fill;
	COLORREF FillColor ,LineColor;
	int width;
	CPoint StartPnt,EndPnt;
	GraphicObject(){}
	GraphicObject(int shapenum,BOOL fill,COLORREF FillColor,COLORREF LineColor,int width,CPoint StartPnt,CPoint EndPnt)
		:shapenum(shapenum),fill(fill),FillColor(FillColor),LineColor(LineColor),width(width),StartPnt(StartPnt),EndPnt(EndPnt){}
	GraphicObject(GraphicObject & g)
		: shapenum(g.shapenum),fill(g.fill),FillColor(g.FillColor),LineColor(g.LineColor),width(g.width),StartPnt(g.StartPnt),EndPnt(g.EndPnt){}
	GraphicObject & operator =(GraphicObject & g)
	{
		shapenum=g.shapenum;
		fill=g.fill;
		FillColor=g.FillColor;
		LineColor=g.LineColor;
		width=g.width;
		StartPnt=g.StartPnt;
		EndPnt=g.EndPnt;
		return *this;
	}
};





class Shape
{
protected:
	CPoint StartPnt,EndPnt;
	int shapenum;
	friend class MyView;
public:
	Shape(CPoint StartPnt,CPoint EndPnt,int shapenum)
		: StartPnt(StartPnt),EndPnt(EndPnt),shapenum(shapenum) {}
	Shape(Shape & s)
		: StartPnt(s.StartPnt),EndPnt(s.EndPnt),shapenum(s.shapenum=0){}
	Shape(){}
	Shape & operator=(Shape &s)
	{
		StartPnt=s.StartPnt;
		EndPnt=s.EndPnt;
		return *this;
	}
	virtual void draw(CDC & aDC,COLORREF color,COLORREF fcolor,int width,BOOL Filled=false){}
	int GetShapeNum()
	{
		return shapenum;}
	void SetPoint(CPoint SPnt,CPoint EPnt)
	{
		StartPnt=SPnt;
		EndPnt=EPnt;
	}

};
class Line :public Shape
{
public:
	friend class MyView;
	Line(){shapenum=0;}
	Line(CPoint StartPnt,CPoint EndPnt)
		:Shape(StartPnt,EndPnt,0){}
	Line(Line & l): Shape(l.StartPnt,l.EndPnt,0){}
	Line & operator=(Line & l)
	{
		StartPnt=l.StartPnt;
		EndPnt=l.EndPnt;
		return *this;
	}
	void draw(CDC &dc,COLORREF color,COLORREF fcolor,int  width,BOOL Filled=false)
	{
		CPen pen(PS_SOLID,width,color);
		CPen *oldPen=dc.SelectObject(&pen);
		dc.MoveTo(StartPnt);
		dc.LineTo(EndPnt);
		dc.SelectObject(oldPen);
	}
};
class ellipse :public Shape
{
public:
	ellipse(){ shapenum=1;}
	ellipse(CPoint StartPnt,CPoint EndPnt)
		:Shape(StartPnt,EndPnt,1){}
	ellipse(ellipse & e): Shape(e.StartPnt,e.EndPnt,1){}
	ellipse & operator =(ellipse &e)
	{
		StartPnt=e.StartPnt;
		EndPnt=e.EndPnt;
		return *this;
	}
	void draw(CDC &dc,COLORREF color,COLORREF fcolor,int width,BOOL Filled=false)
	{
		CRect rect(StartPnt,EndPnt);
		CPen pen(PS_SOLID,width,color);
		CPen *oldPen=dc.SelectObject(&pen);
		dc.SelectStockObject(NULL_BRUSH);
		dc.Ellipse(rect);
		dc.SelectObject(oldPen);
	}
};
class rectangle: public Shape
{
public:
	rectangle(){shapenum=2;}
	rectangle(CPoint StartPnt,CPoint EndPnt)
		:Shape(StartPnt,EndPnt,2){}
	rectangle(rectangle &r)
		: Shape(r.StartPnt,r.EndPnt,2){}
	rectangle & operator =(rectangle &r)
	{
		StartPnt=r.StartPnt;
		EndPnt=r.EndPnt;
		return *this;
	}
	void draw(CDC &dc,COLORREF color,COLORREF fcolor,int width,BOOL Filled=false)
	{
		CRect rect(StartPnt,EndPnt);
		CPen pen(PS_SOLID,width,color);
		CPen *oldPen=dc.SelectObject(&pen);
		dc.SelectStockObject(NULL_BRUSH);
		dc.Rectangle(rect);
		dc.SelectObject(oldPen);
	}
};
class MyDocument : public CDocument
{

private:
	CArray<GraphicObject,GraphicObject> gArray;
public:
	void AddObject(GraphicObject &g)
	{ gArray.Add(g);}
	GraphicObject & GetObject(int i){return gArray[i];}
	int GetSize()
	{ return gArray.GetSize();}



	DECLARE_DYNCREATE(MyDocument)
	DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyDocument ,CDocument)
BEGIN_MESSAGE_MAP(MyDocument,CDocument)
	END_MESSAGE_MAP()
class MyFrame : public CFrameWnd
{
protected:
	CMenu * menu;
public:
	CToolBar toolbar;
	CStatusBar statusbar;
	MyFrame(){}
	~MyFrame(){}
	afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct)
	{
		if (CFrameWnd::OnCreate(lpCreateStruct))
		     return 1;
		toolbar.Create(this);
		toolbar.LoadToolBar(IDR_MyFrame);
		toolbar.EnableDocking(CBRS_ALIGN_ANY);
		toolbar.SetBarStyle(toolbar.GetBarStyle() | CBRS_TOOLTIPS | CBRS_FLYBY |CBRS_SIZE_DYNAMIC);
		EnableDocking(CBRS_ALIGN_ANY);
		DockControlBar(&toolbar);
		static UINT indicators[]=
		{
			ID_SEPARATOR,
			IDS_Color,
			IDS_Shape
		};
		statusbar.Create(this);
		statusbar.SetIndicators(indicators,sizeof(indicators)/sizeof(UINT));
		return 0;
	}
	DECLARE_DYNCREATE(MyFrame)
	DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyFrame,CFrameWnd)
BEGIN_MESSAGE_MAP(MyFrame,CFrameWnd)
	ON_WM_CREATE()
	END_MESSAGE_MAP()
class MyView :public CView
{
private:
	COLORREF lcolor,fcolor;
	Shape * aShape;

	Shape* rdShape;
	int width;
public:
	MyView()
	{
		lcolor=RGB(255,0,0);
		aShape=new Line;
		fcolor=RGB(0,0,0);
		width=2;
	}
	~MyView(){}
	afx_msg void OnEllipse()
	{
		aShape=new ellipse;
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(2,"Ellipse");
	}
	afx_msg void OnRect()
	{
		aShape=new rectangle;
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(2,"Rectangle");
	}
	afx_msg void OnLine()
	{
		aShape=new Line;
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(2,"Line");
	}


	
	afx_msg void OnDraw(CDC * aDC)
	{ 
	    MyDocument *doc=(MyDocument *)GetDocument();
		int num=doc->GetSize();
		CView::OnDraw(aDC);
		int i;
		for (i=0;i<num;++i)
		{
			GraphicObject * object=&(doc->GetObject(i));
			switch(object->shapenum){
            case 0:
                 rdShape=new Line;
				 break;
			case 1:
				rdShape=new ellipse;
				break;
			case 2:
				rdShape=new rectangle;
				break;
			};
			rdShape->SetPoint(object->StartPnt,object->EndPnt);
			rdShape->draw((*aDC),object->LineColor,object->FillColor,object->width);
            delete rdShape;
		}
	
	
	
	}
	afx_msg void OnLButtonDown(UINT,CPoint point)
	{
		SetCapture();
		if (this==GetCapture())
			(*aShape).StartPnt=(*aShape).EndPnt=point;
	}
	afx_msg void OnMouseMove(UINT,CPoint point)
	{
		if (this==GetCapture())
		{
			CClientDC aDC(this);
			aDC.SetROP2(R2_NOT);
			(*aShape).draw(aDC,lcolor,fcolor,2);
			(*aShape).EndPnt=point;
			(*aShape).draw(aDC,lcolor,fcolor,2);
		}
	}
	afx_msg void OnLButtonUp(UINT,CPoint point)
	{
		if (this==GetCapture())
		{
			CClientDC aDC(this);
			(*aShape).EndPnt=point;
			(*aShape).draw(aDC,lcolor,fcolor,width);

			GraphicObject object(aShape->GetShapeNum(),true,fcolor,lcolor,width,aShape->StartPnt,aShape->EndPnt);
			MyDocument *doc=(MyDocument *)GetDocument();
			doc->AddObject(object);
			ReleaseCapture();
		}
	}
	afx_msg void OnRed()
	{
		lcolor=RGB(255,0,0);
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(1,"Red");
	}
	afx_msg void OnBlue()
	{
		lcolor=RGB(0,0,255);
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(1,"Blue");
	}
	afx_msg void OnGreen()
	{
		lcolor=RGB(0,255,0);
		((MyFrame *)GetParentFrame())->statusbar.SetPaneText(1,"Green");
	}
	afx_msg void OnUpdateEllipse(CCmdUI *aCmdUI)
	{
		aCmdUI->SetCheck((*aShape).shapenum==1);}
	afx_msg void OnUpdateRect(CCmdUI *aCmdUI)
	{
		aCmdUI->SetCheck((*aShape).shapenum==2);}
	afx_msg void OnUpdateLine(CCmdUI *aCmdUI)
	{
		aCmdUI->SetCheck((*aShape).shapenum==0);}
	afx_msg void OnUpdateRed(CCmdUI * aCmdUI)
	{
		aCmdUI->SetCheck(lcolor==RGB(255,0,0));}
	afx_msg void OnUpdateGreen(CCmdUI *aCmdUI)
	{
		aCmdUI->SetCheck(lcolor==RGB(0,255,0));}
	afx_msg void OnUpdateBlue(CCmdUI *aCmdUI)
	{
		aCmdUI->SetCheck(lcolor==RGB(0,0,255));}
	DECLARE_DYNCREATE(MyView)
		DECLARE_MESSAGE_MAP()
};
IMPLEMENT_DYNCREATE(MyView,CView)
BEGIN_MESSAGE_MAP(MyView,CView)
	ON_WM_LBUTTONDOWN()
	ON_WM_MOUSEMOVE()
	ON_WM_LBUTTONUP()
	ON_COMMAND(IDM_Red,OnRed)
	ON_COMMAND(IDM_Green,OnGreen)
	ON_COMMAND(IDM_Blue,OnBlue)
	ON_COMMAND(IDM_Line,OnLine)
	ON_COMMAND(IDM_Ellipse,OnEllipse)
	ON_COMMAND(IDM_Rect,OnRect)
	ON_UPDATE_COMMAND_UI(IDM_Red,OnUpdateRed)
	ON_UPDATE_COMMAND_UI(IDM_Green,OnUpdateGreen)
	ON_UPDATE_COMMAND_UI(IDM_Blue,OnUpdateBlue)
	ON_UPDATE_COMMAND_UI(IDM_Line,OnUpdateLine)
	ON_UPDATE_COMMAND_UI(IDM_Ellipse,OnUpdateEllipse)
	ON_UPDATE_COMMAND_UI(IDM_Rect,OnUpdateRect)
	END_MESSAGE_MAP()
class MyApp:  public CWinApp
{
public:
	BOOL InitInstance()
	{
		CDocument *aDOC;
		CSingleDocTemplate *aDocTemplate;
		aDocTemplate =new CSingleDocTemplate(IDR_MyFrame,RUNTIME_CLASS(MyDocument),RUNTIME_CLASS(MyFrame),RUNTIME_CLASS(MyView));
		AddDocTemplate(aDocTemplate);
		aDOC=aDocTemplate->CreateNewDocument();
		CFrameWnd *Frame=aDocTemplate->CreateNewFrame(aDOC,NULL);
		m_pMainWnd =Frame;
		Frame->ShowWindow(SW_SHOW);
		return true;
	}
}a_app;
        



⌨️ 快捷键说明

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