1.进入资源视图,新建对话框,编辑修改Caption或者ID属性
右击该对话框添加类 基类为 CDialog
命名为:MyCircleDialog
使用类向导,选择MyCircleDialog类,选 择成员变量 添加变量 value类型 int型
m_radius 设置 变量取值范围
m_circlex
m_circlex
2.添加菜单,设置ID 为 ID_MyCircleMenu 并通过类向导添加消息映射函数
选择类名 C***View
查找ID ID_MyCircleMenu,映射 ONCOMMAND 消息
C***View
3.添加变量并在构造函数中初始化
C***View.h
public: int m_vradius; int m_vcirclex; int m_vcircley;
C***View.cpp
m_vradius = 0; m_vcirclex = 0; m_vcircley = 0;
添加 MyCircleDialog 头文件
#include "MyCircleDialog.h"
4.在消息处理程序中添加与dialog交互 获取 输入数据的代码
// CMFCCircleDialogDemoView 消息处理程序 void CMFCCircleDialogDemoView::OnMycirclemenu() { // TODO: 在此添加命令处理程序代码 MyCircleDialog dlg; //定义一个对话框对象 if(dlg.DoModal() == IDOK) //显示对话框 { m_vradius = dlg.m_radius; m_vcirclex = dlg.m_circleX; m_vcircley = dlg.m_circleY; //接收并存储编辑框数据 Invalidate(); //刷新视图 } }
// CMFCTestView 绘制
5.在OnDraw中 添加 绘图 代码(取消CDC*参数注释,使用所有设备环境基类CDC画图,
也可以定义使用 其他设备环境类如CClientDC等)
void CMFCCircleDialogDemoView::OnDraw(CDC* pDC) { CMFCCircleDialogDemoDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); if (!pDoc) return; // TODO: 在此处为本机数据添加绘制代码 /*确定圆心,画出直角坐标系*/ CRect rect; GetWindowRect(rect); int center_x = rect.Width()/2; int center_y = rect.Height()/2; CPen myPen; //创建画笔 myPen.CreateStockObject(BLACK_PEN); //设置画笔属性 pDC->SelectObject(&myPen); //绑定画笔句柄 pDC->MoveTo(center_x,0); pDC->LineTo(center_x, rect.Height()); pDC->MoveTo(0,center_y); pDC->LineTo(rect.Width(),center_y); /*********************绘制圆****************/ //逻辑圆心坐标 /* int m_vradius = 50; int m_vcirclex = -70; int m_vcircley = 100; */ //转化为画布中圆心坐标 center_x = center_x + m_vcirclex; center_y = center_y - m_vcircley; pDC->SetPixel(center_x,center_y+m_vradius,RGB(125,125,125));//绘制圆心的点 pDC->Ellipse(center_x-3,center_y-3,center_x+3,center_y+3); //画个小圆,确定圆心位置 int p=5/4-m_vradius; //键值的初始值 int x=0; int y=m_vradius; //使用中点圆算法画圆 while(x<y){ x++; if(p<0){ p=p+2*x+3; pDC->SetPixel(center_x+x,center_y+y,RGB(125,125,125)); pDC->SetPixel(center_x+y,center_y+x,RGB(125,125,125)); pDC->SetPixel(center_x+y,center_y-x,RGB(125,125,125)); pDC->SetPixel(center_x+x,center_y-y,RGB(125,125,125)); pDC->SetPixel(center_x-x,center_y-y,RGB(125,125,125)); pDC->SetPixel(center_x-y,center_y-x,RGB(125,125,125)); pDC->SetPixel(center_x-y,center_y+x,RGB(125,125,125)); pDC->SetPixel(center_x-x,center_y+y,RGB(125,125,125)); //以对称的方式完成圆的绘制 } else{ y--; p=p+2*x+3-2*y-2; pDC->SetPixel(center_x+x,center_y+y,RGB(125,125,125)); pDC->SetPixel(center_x+y,center_y+x,RGB(125,125,125)); pDC->SetPixel(center_x+y,center_y-x,RGB(125,125,125)); pDC->SetPixel(center_x+x,center_y-y,RGB(125,125,125)); pDC->SetPixel(center_x-x,center_y-y,RGB(125,125,125)); pDC->SetPixel(center_x-y,center_y-x,RGB(125,125,125)); pDC->SetPixel(center_x-y,center_y+x,RGB(125,125,125)); pDC->SetPixel(center_x-x,center_y+y,RGB(125,125,125)); //以对称的方式完成圆的绘制 } Sleep(20); } }
时间: 2024-10-12 20:59:17