学校新课程计算机图形学,觉得光靠老师的讲授有点吃力,自己找了一门网课看:(正好这个老师还是我们教材的编写老师)
跟着学校老师的进度上网课听了几节,正打算自己一顿操作猛如虎绝知此事要躬行操作一下,打开一看,好家伙!VS里面没有MFC······
https://jingyan.baidu.com/article/77b8dc7f8bdf216174eab689.html
似乎VS2019还有亿点点不一样
https://blog.csdn.net/weixin_42818344/article/details/98448372
运行效果:
在TestView.h中,定义起点和终点
打开类向导,在TestView中添加消息响应函数:
添加消息处理函数:
效果如图所示:(画出来确实是直线,但是不知道为什么截图放出来就成了虚线,也许这就是传说中的“走样”?····)
加上更改画笔之后:
既然两个点可以绘制直线,那么也可以作为矩形的对角点:
也可以作为椭圆外接矩形的对角点,注意到椭圆之间是可以相互遮挡的,说明他们有填充
用画刷填充椭圆
椭圆边界红色,填充绿色,说明画刷和画笔互不影响。
代码:(创建名为Test的MFC文件,直接覆盖TestView.和TestView.cpp即可)
TestView.h:
// TestView.h: CTestView 类的接口
//
#pragma once
class CTestView : public CView
{
protected: // 仅从序列化创建
CTestView() noexcept;
DECLARE_DYNCREATE(CTestView)
// 特性
public:
CTestDoc* GetDocument() const;
// 操作
public:
// 重写
public:
virtual void OnDraw(CDC* pDC); // 重写以绘制该视图
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);
// 实现
public:
virtual ~CTestView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
CPoint p0, p1;//定义起点p0和终点p1
// 生成的消息映射函数
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
};
#ifndef _DEBUG // TestView.cpp 中的调试版本
inline CTestDoc* CTestView::GetDocument() const
{ return reinterpret_cast<CTestDoc*>(m_pDocument); }
#endif
TestView.cpp
// TestView.cpp: CTestView 类的实现
//
#include "pch.h"
#include "framework.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Test.h"
#endif
#include "TestDoc.h"
#include "TestView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CTestView
IMPLEMENT_DYNCREATE(CTestView, CView)
BEGIN_MESSAGE_MAP(CTestView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
END_MESSAGE_MAP()
// CTestView 构造/析构
CTestView::CTestView() noexcept
{
// TODO: 在此处添加构造代码
}
CTestView::~CTestView()
{
}
BOOL CTestView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// CTestView 绘图
void CTestView::OnDraw(CDC* /*pDC*/)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: 在此处为本机数据添加绘制代码
}
// CTestView 打印
BOOL CTestView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CTestView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CTestView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
// CTestView 诊断
#ifdef _DEBUG
void CTestView::AssertValid() const
{
CView::AssertValid();
}
void CTestView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CTestDoc* CTestView::GetDocument() const // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CTestDoc)));
return (CTestDoc*)m_pDocument;
}
#endif //_DEBUG
// CTestView 消息处理程序
void CTestView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
p0 = point;
CView::OnLButtonDown(nFlags, point);
}
void CTestView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
p1 = point;
CDC* pDC = GetDC();//定义pDC,便于调用MoveTo和LineTo两个函数画线
CPen NewPen, * pOldPen;//定义画笔
NewPen.CreatePen(PS_SOLID, 3, RGB(255, 0, 0));//设置画笔属性:实线、宽度为3,颜色RGB红色
pOldPen = pDC->SelectObject(&NewPen);//画笔选入设备描述表绘制直线
/*pDC->MoveTo(p0);
pDC->LineTo(p1);*/
/*pDC->Rectangle(CRect(p0, p1));*/
CBrush NewBrush,* pOldBrush;//新建画刷
NewBrush.CreateSolidBrush(RGB(0, 128, 0));//设置属性,颜色为绿色
pOldBrush = pDC->SelectObject(&NewBrush);//选入设备描述表
pDC->Ellipse(CRect(p0, p1));//绘制椭圆
ReleaseDC(pDC);//释放pDC
NewPen.DeleteObject();//删除画笔
NewBrush.DeleteObject();//删除画刷
CView::OnLButtonUp(nFlags, point);
}