绘图控制

来源:互联网 发布:quick是什么软件 编辑:程序博客网 时间:2024/05/16 02:25
新建一个单文档程序
在主菜单添加一个子菜单,菜单项设计如下:
绘图控制 - andylanzhiyong - C++学习
 对应ID为:
IDM_DOT     //点击该菜单项画点
IDM_LINE     //点击该菜单项画线
IDM_RECTANGLE    //点击该菜单项画矩形
IDM_ELLIPSE    //点击该菜单项画椭圆
IDM_SETTING    //点击该菜单项弹出设置对话框(设置线宽样式)
IDM_COLOR     //点击该菜单项弹出颜色对话框,设置颜色
IDM_FONT    //点击该菜单项弹出字体对话框,设置字体

先来实现一种功能,当选择点、先、矩形或椭圆时,在View中画对应的图形
在View中添加一个变量,来保存用户选择的是点、先、矩形还是椭圆
UINT m_nDrawType;   //画图类型

构造函数中初始化为0:m_nDrawType=0;

为四个菜单项添加响应函数:

void CGraphicView::OnDot() 
{
// TODO: Add your command handler code here
m_nDrawType=1;   //画点类型
}

void CGraphicView::OnLine() 
{
// TODO: Add your command handler code here
m_nDrawType=2;   //画直线类型
}

void CGraphicView::OnRectangle() 
{
// TODO: Add your command handler code here
m_nDrawType=3;   //画矩形类型
}

void CGraphicView::OnEllipse() 
{
// TODO: Add your command handler code here
m_nDrawType=4;  //画椭圆类型
}


在View中添加一个变量,来保存用户选择的是点、先、矩形还是椭圆
CPoint m_ptOrigin;   //保存鼠标左键按下的起点

构造函数中初始化为0:m_ptOrigin=0;

添加左键按下消息和抬起消息
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default
m_ptOrigin=point;    //保存鼠标左键按下的起点
CView::OnLButtonDown(nFlags, point);
}

void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point) 
{
// TODO: Add your message handler code here and/or call default

CClientDC dc(this);
CPen pen(PS_SOLID,1,RGB(255,0,0));
//CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
dc.SelectObject(&pen);
CBrush *pBrush=CBrush::FromHandle((HBRUSH)GetStockObject(NULL_BRUSH));
dc.SelectObject(pBrush);
switch(m_nDrawType)
{
case 1:
dc.SetPixel(point,RGB(255,0,0));
//dc.SetPixel(point,m_clr);
break;
case 2:
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
break;
case 3:
dc.Rectangle(CRect(m_ptOrigin,point));
break;
case 4:
dc.Ellipse(CRect(m_ptOrigin,point));
break;
}

CView::OnLButtonUp(nFlags, point);
}

接下来,添加一个设置对话框,用来设置线宽和线型,根据设置画图
对话框设计如下:
绘图控制 - andylanzhiyong - C++学习
 
线宽编辑框ID:IDC_LINE_WIDTH
线型单选按钮ID:IDC_RADIO1,IDC_RADIO2,IDC_RADIO3
设置IDC_RADIO1的Group属性

为设置对话框创建一个类CSettingDlg,继承自CDialog

在CSettingDlg类中为线宽编辑框和线型单选按钮各关联一个变量:
UINTm_nLineWidth;
intm_nLineStyle;
都初始化为0

最终是在View中画图,添加变量保存用户在设置对话框中的选择
int m_nLineStyle;
UINT m_nLineWidth;
都初始化为0

为菜单项“设置”添加响应函数:
void CGraphicView::OnSetting() 
{
// TODO: Add your command handler code here
CSettingDlg dlg;
//保存上次设置的线宽
dlg.m_nLineWidth=m_nLineWidth;
//保存上次设置的线型
dlg.m_nLineStyle=m_nLineStyle;
//保存上次设置的颜色
dlg.m_clr=m_clr;
if(IDOK==dlg.DoModal())
{
//获得设置对话框中用户选择的线宽和线型
m_nLineWidth=dlg.m_nLineWidth;
m_nLineStyle=dlg.m_nLineStyle;
}
}

再实现一个功能:通过颜色对话框选择颜色来画图
View中第一个变量,接收用户在颜色对话框选择的颜色
COLORREF m_clr;

添加“颜色”菜单项响应函数:
void CGraphicView::OnColor() 
{
// TODO: Add your command handler code here

CColorDialog dlg;
//保存上次选择的颜色作为默认颜色
dlg.m_cc.Flags|=CC_RGBINIT | CC_FULLOPEN;
dlg.m_cc.rgbResult=m_clr;

if(IDOK==dlg.DoModal())
{
//获得颜色对话框中选择的颜色
m_clr=dlg.m_cc.rgbResult;
}
}


再实现一个功能:
通过用户在字体对话框中的设置,把选择的字体它的字体名字
按选择的字体类型大小输出在View

在View中定义两个变量:
CString m_strFontName;   //保存用户选择的字体的名字
CFont m_font;    //保存用户选择的字体

添加“字体”菜单项的响应函数
void CGraphicView::OnFont() 
{
// TODO: Add your command handler code here
CFontDialog dlg;
if(IDOK==dlg.DoModal())
{
if(m_font.m_hObject)     
//如果已创建字体,释放之前的字体
m_font.DeleteObject();
//创建所选择的字体
m_font.CreateFontIndirect(dlg.m_cf.lpLogFont);
//获得字体的名字
m_strFontName=dlg.m_cf.lpLogFont->lfFaceName;
//重画
Invalidate();
}
}

添加OnDraw()响应函数:
void CGraphicView::OnDraw(CDC* pDC)
{
CGraphicDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here

CFont *pOldFont=pDC->SelectObject(&m_font);
pDC->TextOut(0,0,m_strFontName);
pDC->SelectObject(pOldFont);
}

再实现一个功能:
在设置对话框中选择线宽和线型,在该对话框中马上一个画线示例
绘图控制 - andylanzhiyong - C++学习

void CSettingDlg::OnChangeLineWidth() 
{
// TODO: If this is a RICHEDIT control, the control will not
// TODO: Add your control notification handler code here
Invalidate();
}

void CSettingDlg::OnRadio1() 
{
// TODO: Add your control notification handler code here
Invalidate();
}

void CSettingDlg::OnRadio2() 
{
// TODO: Add your control notification handler code here
Invalidate();
}

void CSettingDlg::OnRadio3() 
{
// TODO: Add your control notification handler code here
Invalidate();
}
 
为CSettingDlg添加OnPaint() 响应函数
void CSettingDlg::OnPaint() 
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here

UpdateData();  
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
dc.SelectObject(&pen);

CRect rect;
//获得“示例”组框的矩形
GetDlgItem(IDC_SAMPLE)->GetWindowRect(&rect);
//坐标转换
ScreenToClient(&rect);
//“示例”组框中间位置画示例直线
dc.MoveTo(rect.left+20,rect.top+rect.Height()/2);
dc.LineTo(rect.right-20,rect.top+rect.Height()/2);

// Do not call CDialog::OnPaint() for painting messages
}

 

原创粉丝点击