VCmfc简易画图板代码
时间: 2023-07-02 13:06:05 浏览: 205
以下是一个基于VC++ MFC的简易画图板代码示例:
首先,创建一个MFC应用程序,选择“单文档”类型,然后在资源编辑器中添加一个菜单项“画图板”。
在CMainFrame类中添加以下代码:
```cpp
#include "MyView.h"
class CMainFrame : public CFrameWnd
{
public:
CMainFrame();
protected:
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
afx_msg void OnPaint();
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnDrawLine();
afx_msg void OnDrawRectangle();
afx_msg void OnDrawEllipse();
DECLARE_MESSAGE_MAP()
private:
MyView m_wndView;
};
CMainFrame::CMainFrame()
{
Create(NULL, _T("Drawing Board"), WS_OVERLAPPEDWINDOW, CRect(0, 0, 800, 600));
}
int CMainFrame::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CFrameWnd::OnCreate(lpCreateStruct) == -1)
return -1;
if (!m_wndView.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW, CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
{
TRACE0("Failed to create view window\n");
return -1;
}
return 0;
}
void CMainFrame::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect;
GetClientRect(&rect);
dc.FillSolidRect(&rect, RGB(255, 255, 255));
}
void CMainFrame::OnSize(UINT nType, int cx, int cy)
{
CFrameWnd::OnSize(nType, cx, cy);
if (m_wndView.GetSafeHwnd())
m_wndView.MoveWindow(0, 0, cx, cy);
}
void CMainFrame::OnDrawLine()
{
m_wndView.SetDrawType(DRAW_LINE);
}
void CMainFrame::OnDrawRectangle()
{
m_wndView.SetDrawType(DRAW_RECTANGLE);
}
void CMainFrame::OnDrawEllipse()
{
m_wndView.SetDrawType(DRAW_ELLIPSE);
}
BEGIN_MESSAGE_MAP(CMainFrame, CFrameWnd)
ON_WM_CREATE()
ON_WM_PAINT()
ON_WM_SIZE()
ON_COMMAND(ID_DRAW_LINE, &CMainFrame::OnDrawLine)
ON_COMMAND(ID_DRAW_RECTANGLE, &CMainFrame::OnDrawRectangle)
ON_COMMAND(ID_DRAW_ELLIPSE, &CMainFrame::OnDrawEllipse)
END_MESSAGE_MAP()
```
在MyView类中添加以下代码:
```cpp
enum DRAW_TYPE {
DRAW_NONE = 0,
DRAW_LINE,
DRAW_RECTANGLE,
DRAW_ELLIPSE
};
class MyView : public CView
{
protected:
MyView();
DECLARE_DYNCREATE(MyView)
public:
virtual void OnDraw(CDC* pDC);
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
virtual ~MyView();
void SetDrawType(DRAW_TYPE type) { m_nDrawType = type; }
protected:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnMouseMove(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
DECLARE_MESSAGE_MAP()
private:
DRAW_TYPE m_nDrawType;
CPoint m_ptStart;
CPoint m_ptEnd;
};
MyView::MyView()
{
m_nDrawType = DRAW_NONE;
}
MyView::~MyView()
{
}
BOOL MyView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void MyView::OnDraw(CDC* pDC)
{
CDocument* pDoc = GetDocument();
// TODO: add draw code here
}
void MyView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_ptStart = point;
CView::OnLButtonDown(nFlags, point);
}
void MyView::OnMouseMove(UINT nFlags, CPoint point)
{
if (nFlags & MK_LBUTTON)
{
CDC* pDC = GetDC();
CPen pen;
pen.CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
pDC->SelectObject(&pen);
pDC->SetROP2(R2_NOTXORPEN);
switch (m_nDrawType)
{
case DRAW_LINE:
pDC->MoveTo(m_ptStart);
pDC->LineTo(m_ptEnd);
m_ptEnd = point;
pDC->MoveTo(m_ptStart);
pDC->LineTo(m_ptEnd);
break;
case DRAW_RECTANGLE:
pDC->Rectangle(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
m_ptEnd = point;
pDC->Rectangle(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
break;
case DRAW_ELLIPSE:
pDC->Ellipse(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
m_ptEnd = point;
pDC->Ellipse(m_ptStart.x, m_ptStart.y, m_ptEnd.x, m_ptEnd.y);
break;
default:
break;
}
ReleaseDC(pDC);
}
CView::OnMouseMove(nFlags, point);
}
void MyView::OnLButtonUp(UINT nFlags, CPoint point)
{
m_ptEnd = point;
CView::OnLButtonUp(nFlags, point);
}
BEGIN_MESSAGE_MAP(MyView, CView)
ON_WM_LBUTTONDOWN()
ON_WM_MOUSEMOVE()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
```
在资源编辑器中为菜单项添加ID_DRAW_LINE、ID_DRAW_RECTANGLE和ID_DRAW_ELLIPSE命令ID。
最后,在CWinApp类的InitInstance()函数中添加以下代码:
```cpp
BOOL CMyApp::InitInstance()
{
CWinApp::InitInstance();
CMainFrame* pFrame = new CMainFrame;
m_pMainWnd = pFrame;
pFrame->LoadFrame(IDR_MAINFRAME, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, NULL);
m_pMainWnd->ShowWindow(SW_SHOW);
m_pMainWnd->UpdateWindow();
return TRUE;
}
```
现在,你就可以编译并运行这个简易画图板了,它可以画线、矩形和椭圆。
阅读全文