vc++编写一个绘图程序
时间: 2023-10-10 20:04:46 浏览: 117
下面是一个简单的VC++绘图程序示例,可以绘制直线和圆形:
首先,创建一个MFC应用程序,选择“单文档”和“视图”选项,并在“视图”选项中选择“绘图视图”:
在“绘图视图.h”中添加以下代码:
```
class CDrawView : public CView
{
protected: // create from serialization only
CDrawView();
DECLARE_DYNCREATE(CDrawView)
// Attributes
public:
CDrawDoc* GetDocument();
// Operations
public:
// Overrides
public:
virtual void OnDraw(CDC* pDC); // overridden to draw this view
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
// Implementation
public:
virtual ~CDrawView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
DECLARE_MESSAGE_MAP()
};
```
在“绘图视图.cpp”中添加以下代码:
```
#include "stdafx.h"
#include "Draw.h"
#include "DrawDoc.h"
#include "DrawView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CDrawView
IMPLEMENT_DYNCREATE(CDrawView, CView)
BEGIN_MESSAGE_MAP(CDrawView, CView)
END_MESSAGE_MAP()
// CDrawView construction/destruction
CDrawView::CDrawView()
{
// TODO: add construction code here
}
CDrawView::~CDrawView()
{
}
BOOL CDrawView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CDrawView drawing
void CDrawView::OnDraw(CDC* pDC)
{
CDrawDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: add draw code for native data here
pDC->Ellipse(100, 100, 200, 200); // draw a circle
pDC->MoveTo(300, 100); // move to (300, 100)
pDC->LineTo(400, 200); // draw a line to (400, 200)
}
// CDrawView printing
BOOL CDrawView::OnPreparePrinting(CPrintInfo* pInfo)
{
// default preparation
return DoPreparePrinting(pInfo);
}
void CDrawView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add extra initialization before printing
}
void CDrawView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: add cleanup after printing
}
// CDrawView diagnostics
#ifdef _DEBUG
void CDrawView::AssertValid() const
{
CView::AssertValid();
}
void CDrawView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CDrawDoc* CDrawView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CDrawDoc)));
return (CDrawDoc*)m_pDocument;
}
#endif //_DEBUG
```
编译并运行程序,就可以看到一个包含一个圆形和一条直线的窗口。
阅读全文