利用对话框设置参数画DDA直线
1.先插入一个对话框
2.选择合适部件拖到对话框中
3.修改文本名称
4.双击对话框的空白处添加对话框类
5.在属性中依次修改
6.添加变量
7.设置成这样就完成了
8.添加确定处理函数
void dlg_line::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
UpdateData(true);
CDialog::OnOK();
}
9.添加#include "dlg_line.h"
10.在CaView添加函数内容
void CaView::OnDda()
{
// TODO: 在此添加命令处理程序代码
dlg_line dlg;
dlg.DoModal();
CDC* pDC = GetDC();
CRect rect;
GetClientRect(&rect);
//重定义坐标轴
// 画坐标轴
pDC->MoveTo(0, rect.Height() / 2);
pDC->LineTo(rect.Width(), rect.Height() / 2);
pDC->MoveTo(rect.Width() / 2, 0);
pDC->LineTo(rect.Width() / 2, rect.Height());
//移动坐标原点到客户区中心
pDC->SetMapMode(MM_ISOTROPIC);
pDC->SetViewportExt(rect.right, rect.bottom);
pDC->SetViewportOrg(rect.right / 2, rect.bottom / 2);
pDC->SetWindowOrg(0, 0);
pDC->SetWindowExt(1000, -1000);
pDC->TextOut(450, 18, _T("DDA画线法成功了!"));//在屏幕(450,18)的位置显示一个白色底色的黑字:DDA画线法成功了!
double dx, dy, e, x, y;
dx = dlg.x2 - dlg.x1;
dy = dlg.y2 - dlg.y1;
e = (fabs(dx) > fabs(dy) ? fabs(dx) : fabs(dy));
dx /= e;
dy /= e;
x = dlg.x1;
y = dlg.y1;
for (int i = 1; i <= e; ++i) {
pDC->SetPixel(int(x + 0.5), int(y + 0.5), RGB(255, 0, 0));
x += dx;
y += dy;
}
}
11.运行