// PaintView.cpp: CPaintView 类的实现
//
#include "stdafx.h"
// SHARED_HANDLERS 可以在实现预览、缩略图和搜索筛选器句柄的
// ATL 项目中进行定义,并允许与该项目共享文档代码。
#ifndef SHARED_HANDLERS
#include "Paint.h"
#endif
#include "PaintDoc.h"
#include "PaintView.h"
#define ROUND(a) int(a+0.5) //四舍五入
#define PI 3.1415926 //圆周率
#include "math.h" //数学头文件
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CPaintView
IMPLEMENT_DYNCREATE(CPaintView, CView)
BEGIN_MESSAGE_MAP(CPaintView, CView)
// 标准打印命令
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CPaintView::OnFilePrintPreview)
ON_WM_CONTEXTMENU()
ON_WM_RBUTTONUP()
ON_COMMAND(ID_32772, &CPaintView::OnDDA)
ON_COMMAND(ID_32773, &CPaintView::OnBresenham)
ON_COMMAND(ID_32774, &CPaintView::Oncircle)
ON_COMMAND(ID_32775, &CPaintView::Onellipse)
ON_COMMAND(ID_32781, &CPaintView::OnBaseShape)
ON_COMMAND(ID_32776, &CPaintView::On1)
ON_COMMAND(ID_32777, &CPaintView::On2)
ON_COMMAND(ID_32778, &CPaintView::On3)
ON_COMMAND(ID_32779, &CPaintView::On4)
ON_COMMAND(ID_32780, &CPaintView::On5)
ON_COMMAND(ID_32782, &CPaintView::OnBezier)
ON_UPDATE_COMMAND_UI(ID_32782, &CPaintView::OnUpdateBezier)
ON_WM_LBUTTONDOWN()
ON_COMMAND(ID_32783, &CPaintView::OnB)
ON_COMMAND(ID_32784, &CPaintView::Ontianchong)
ON_UPDATE_COMMAND_UI(ID_32784, &CPaintView::OnUpdatetianchong)
ON_COMMAND(ID_32785, &CPaintView::Onclear)
ON_COMMAND(ID_32786, &CPaintView::Onrectangle)
ON_COMMAND(ID_32787, &CPaintView::Onduo)
ON_COMMAND(ID_32788, &CPaintView::Onshan)
ON_COMMAND(ID_32789, &CPaintView::Onhu)
ON_COMMAND(ID_32790, &CPaintView::Onsystem)
ON_COMMAND(ID_32791, &CPaintView::OnmanyChange)
ON_COMMAND(ID_32792, &CPaintView::On32792)
END_MESSAGE_MAP()
// CPaintView 构造/析构
CPaintView::CPaintView() noexcept
{
// TODO: 在此处添加构造代码
}
CPaintView::~CPaintView()
{
}
BOOL CPaintView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: 在此处通过修改
// CREATESTRUCT cs 来修改窗口类或样式
return CView::PreCreateWindow(cs);
}
// CPaintView 绘图
void CPaintView::OnDraw(CDC* pDC)
{
CPaintDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
GetMaxX();
GetMaxY();
pDC->MoveTo(MaxX / 2, 0); //绘制坐标轴
pDC->LineTo(MaxX / 2, MaxY);
pDC->MoveTo(0, MaxY / 2);
pDC->LineTo(MaxX, MaxY / 2);
}
void CPaintView::GetMaxX() //获得屏幕宽度
{
CRect Rect;
GetClientRect(&Rect);
MaxX = Rect.right;
}
void CPaintView::GetMaxY() //获得屏幕高度
{
CRect Rect;
GetClientRect(&Rect);
MaxY = Rect.bottom;
}
// CPaintView 打印
void CPaintView::OnFilePrintPreview()
{
#ifndef SHARED_HANDLERS
AFXPrintPreview(this);
#endif
}
BOOL CPaintView::OnPreparePrinting(CPrintInfo* pInfo)
{
// 默认准备
return DoPreparePrinting(pInfo);
}
void CPaintView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加额外的打印前进行的初始化过程
}
void CPaintView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/)
{
// TODO: 添加打印后进行的清理过程
}
void CPaintView::OnRButtonUp(UINT /* nFlags */, CPoint point)
{
ClientToScreen(&point);
OnContextMenu(this, point);
}
void CPaintView::OnContextMenu(CWnd* /* pWnd */, CPoint point)
{
#ifndef SHARED_HANDLERS
theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);
#endif
}
// CPaintView 诊断
#ifdef _DEBUG
void CPaintView::AssertValid() const
{
CView::AssertValid();
}
void CPaintView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CPaintDoc* CPaintView::GetDocument() // 非调试版本是内联的
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CPaintDoc)));
return (CPaintDoc*)m_pDocument;
}
#endif //_DEBUG
// CPaintView 消息处理程序
//DDA算法画直线
void CPaintView::OnDDA()
{
RECT rect1;
float dx, dy, epsl, k, x0, x1, y0, y1;
float x, y, xIncre, yIncre;
GetClientRect(&rect1);
int width;
int height;
width = rect1.right;
height = rect1.bottom;
x0 = 500;
x1 = 100;
y0 = 500;
y1 = 100;
CClientDC dc(this);
dx = x1 - x0;
dy = y1 - y0;
x = x0;
y = y0;
if (abs((int)dx) > abs((int)dy))
epsl = abs((int)dx);
else
epsl = abs((int)dy);
xIncre = (float)(dx / epsl);
yIncre = (float)(dy / epsl);
for (k = 0; k <= epsl; k++) {
dc.SetPixel((int)(x + 0.5), height - ((int)(y + 0.5)), RGB(255, 0, 0));
x = x + xIncre;
y = y + yIncre;
}
}
//Bresenham算法画直线
void CPaintView::OnBresenham()
{
CClientDC dc(this);
int x0 = 60, y0 = 50, x1 = 300, y1 = 200, x = x0, y = y0;
double e = -0.5;
int dx = x1 - x0;
int dy = y1 - y0;
double k = dy * 1.0 / dx;
for (int i = 0; i <= dx; i++)
{
dc.SetPixel(int(x), int(y), RGB(0, 0, 0));
x = x + 1;
e = e + k;
if (e >= 0)
{
y++;
e = e - 1;
}
}
}
//系统绘制直线
void CPaintView::Onsystem()
{
CClientDC dc(this);
dc.LineTo(200,350);
}
//绘制圆
void CPaintView::Oncircle()
{
float x = 0, y, d, w, h;
float r;
RECT rect2;
GetClientRect(&rect2);
w = rect2.right;
h = rect2.bottom;
r = w / 5;
y = r;
d = 1 - r;
CClientDC dc(this);
while (x <= y) {
dc.SetPixel(x + w / 2, h - (y + h / 2), RGB(255, 0, 255));
dc.SetPixel(y + w / 2, h - (x + h / 2), RGB(255, 0, 255));
dc.SetPixel(-y + w / 2, h - (x + h / 2), RGB(255, 0, 255));
dc.SetPixel(-x + w / 2, h - (y + h / 2), RGB(255, 0, 255));
dc.SetPixel(-x + w / 2, h - (-y + h / 2), RGB(255, 0, 255));
dc.SetPixel(-y + w / 2, h - (-x + h / 2), RGB(255, 0, 255));
dc.SetPixel(y + w / 2, h - (-x + h / 2), RGB(255, 0, 255));
dc.SetPixel(x + w / 2, h - (-y + h / 2), RGB(255, 0, 255));
if (d < 0) {
d += 2 * x + 3;
}
else {
d += 2 * (x - y) + 5;
y--;
}
x++;
}
}
//绘制椭圆
void CPaintView::Onellipse()
{
RECT rect1;
int ox, oy, a, b;
GetClientRect(&rect1);
ox = 300; //圆心x坐标
oy = 200; //圆心y坐标
a = 300; //长半轴
b = 100; //短半轴
CClientDC dc(this);
float d = b * b + a * a*(-b + 0.25);
int x = 0, y = b;
int fx = a * a / sqrt((float)a*a + b * b);
while (x != fx)
{
if (d < 0)
{
d += b * b*(2 * x + 3);
}
else
{
--y;
d += b * b*(2 * x + 3) + a * a*(-2 * y + 2);
}
++x;
dc.SetPixel(ox + x, oy + y, RGB(255, 0, 0));
dc.SetPixel(ox - x, oy + y, RGB(255, 0, 0));
dc.SetPixel(ox + x, oy - y, RGB(255, 0, 0));
dc.SetPixel(ox - x, oy - y, RGB(255, 0, 0));
}
d = b * b*(x + 0.5)*(x + 0.5) + a * a*(y - 1)*(y - 1) - a * a*b*b;
while (y > 0)
{
if (d < 0)
{
++x;
d += b * b*(2 * x + 2) + a * a*(-2 * y + 3);
}
else
d += a * a*(-2 * y + 3);
--y;
dc.SetPixel(ox + x, oy + y, RGB(255, 0, 0));
dc.SetPixel(ox - x, oy + y, RGB(255, 0, 0));
dc.SetPixel(ox + x, oy - y, RGB(255, 0, 0));
dc.SetPixel(ox - x, oy - y, RGB(255, 0, 0));
}
}
//绘制矩形
void CPaintView::Onrectangle()
{
CClientDC dc(this);
dc.Rectangle(50,50,200,100);
}
//多边形
void CPaintView::Onduo()
{
CClientDC dc(this);
CPoint pt[6];
CRect rc;
GetClientRect(&rc);
dc.SetViewportOrg(rc.right / 4, rc.bottom * 3 / 4);
pt[0].x = -10, pt[0].y = 0;
pt[1].x = -40, pt[1].y = 40;
pt[2].x = 50, pt[2].y = 40;
pt[3].x = 10, pt[3].y = 0;
pt[4].x = 50, pt[4].y = -40;
pt[5].x = -40, pt[5].y = -40;
dc.Polygon(pt, 6);
}
//绘制扇形
void CPaintView::Onshan()
{
CClientDC dc(this);
CRect rc;
GetClientRect(&rc);
dc.SetViewportOrg(rc.right / 1.5, rc.bottom / 4);
dc.Pie(-90, -90, 90, 90, -200, 0, 200, 0);
}
//绘制圆弧
void CPaintView::Onhu()