vc++学习笔记10
图形的绘制,设置对话框,字体对话框,显示位图
实现画点,线,圆,矩形,
首先菜单上添加4个按钮,然后添加相应函数;
void CGraphicView::OnRect()
{
m_DrawType=3;// TODO: Add your command handler code here
}
void CGraphicView::OnEclipse()
{
m_DrawType=4;// TODO: Add your command handler code here
}
void CGraphicView::OnPoint()
{
m_DrawType=1;
}
void CGraphicView::OnLine()
{
m_DrawType=2;// TODO: Add your command handler code here
}定义两个成员变量在VIEW里面,一个表示想要画的图形类型,一个存储起始点;并在view里面予以初始化;
public: UINT m_DrawType;
添加两个消息响应函数,一个响应WM_BUTTONDOWN,一个响应WM_BUTTONUP
void CGraphicView::OnLButtonDown(UINT nFlags, CPoint point)
{
m_OrignPoint=point;
CView::OnLButtonDown(nFlags, point);
}
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
CClientDC dc(this);//定义画布
switch(m_DrawType)//判断所画图形的类型,分别响应
{
case 1:
dc.SetPixel(point,RGB(255,255,0));//画点,此函数SetPixel函数显示一个点
break;
case 2: dc.MoveTo(m_OrignPoint);
dc.LineTo(point);
break;
case 3:
dc.Rectangle(CRect(m_OrignPoint,point));//使用Rectangle的重构函数,在里面定义一个CRect区域
break;
case 4:
dc.Ellipse(CRect(m_OrignPoint,point));
break;
default:
break;
}
CView::OnLButtonUp(nFlags, point);
}
建立一个设置框;改变画笔参数;
首先建立一个对话框,然后建立相应的控件,对Edit控件建立相应的变量,保存用户设置的数据;
然后添加相应按钮,添加相应的相应函数在VIew里面,只要当用户点击ok 的时候才保存数据;所以要用IDOK==dlg.DoModal()判断一下;
void CGraphicView::OnSetting()
{
CSettingDialog dlg;
dlg.m_n_Linewidth=m_nDrawWidth;//此处是把选中的选项从新返回对话框显示,换一种方式利用static变量也可以做到
dlg.m_nLineStyle=m_nLineStyle;//同上
if (IDOK==dlg.DoModal())//点击OK键才予以保存
{
m_nDrawWidth=dlg.m_n_Linewidth;
m_nLineStyle=dlg.m_nLineStyle;
}
}
=================================================================================================================================
建立颜色对话框:使用到MFC里面自带的 CColorDialog类,以及设置的参数
可以不用提前添加资源Dialog;直接利用DoModol()
void CGraphicView::OnColor()
{
CColorDialog dlg;//颜色对话框
dlg.m_cc.Flags|=CC_RGBINIT|CC_FULLOPEN;//将自己添加的参数或上原来的参数。组合新的模式
dlg.m_cc.rgbResult=m_color;//将颜色重新返回给对话框
if(IDOK==dlg.DoModal())
{
m_color=dlg.m_cc.rgbResult;//颜色对话框里面自带的变量m_cc.rgbResult保存选中的颜色
}
}创建字体设置对话框:利用MFC自带CFontDialog,注意建立自己的成员变量保存用户选择的字体信息void CGraphicView::OnFont()
{
CFontDialog dlg;//创建字体对话框
if(IDOK==dlg.DoModal())
{
if(m_font.m_hObject) //m_hObject保存了选中字体的句柄,在创建之前必须判定是否已经创建
m_font.DeleteObject();
m_font.CreateFontIndirect(dlg.m_cf.lpLogFont); //lplogFont乃一个指针指向LPLOGFONT结构体
m_FontName=dlg.m_cf.lpLogFont->lfFaceName;//m_cf保存用户选中字体的各式名字等等信息
Invalidate();//使窗口无效,重画窗口
}
====================================================================================================================================
创建示例来保存用户选中的格式的变化;形如,

建立静态组框,然后对每个按钮添加相应消息,对每个组框都重画图,添加一样的代码,冗余,可以再每个按钮改变的时候;
对于Edit组件,选中EN_CHANGGE消息的相应函数;
对于单选按钮,添加BN_CLICKED消息的响应的函数;
void CSettingDialog::OnChangeLineWidth()
{
// TODO: If this is a RICHEDIT control, the control will not
// send this notification unless you override the CDialog::OnInitDialog()
// function and call CRichEditCtrl().SetEventMask()
// with the ENM_CHANGE flag ORed into the mask.
// TODO: Add your control notification handler code here
Invalidate();//每个按钮发生变化时,都使窗口无效,重画窗口时,发生WM_PAINT函数,从而可以通过相应此消息的函数onpaint()实现这几个按钮变化
}
void CSettingDialog::OnRadio1()
{
// TODO: Add your control notification handler code here
Invalidate();//每个按钮发生变化时,都使窗口无效,重画窗口时,发生WM_PAINT函数,从而可以通过相应此消息的函数onpaint()实现这几个按钮变化
}
void CSettingDialog::OnRadio2()
{
// TODO: Add your control notification handler code here
Invalidate();//每个按钮发生变化时,都使窗口无效,重画窗口时,发生WM_PAINT函数,从而可以通过相应此消息的函数onpaint()实现这几个按钮变化
}
void CSettingDialog::OnRadio3()
{
// TODO: Add your control notification handler code here
Invalidate();
}
void CSettingDialog::OnPaint()
{
CPaintDC dc(this); // device context for painting
UpdateData(TRUE);
CPen pen(m_nLineStyle,m_n_Linewidth,RGB(0,0,255));//此处RGB为自定义的颜色,若要动态变化此颜需要定义一个成员变量m_col保存颜色,在OnSetting()函数里面将此变量重新设置;dlg.m_col=m_color; 然后将RGB替换为m_col;
dc.SelectObject(pen);
CRect rect;
GetDlgItem(IDC_sample)->GetWindowRect(&rect);//获得Group框的矩形区域大小,GetWindowRect获得是窗口的坐标
ScreenToClient(&rect);//屏幕转为客户区域
dc.MoveTo(rect.left+20,rect.top+rect.Height()/2);
dc.LineTo(rect.right-20,rect.top+rect.Height()/2);
UpdateData(FALSE);//选中的数据必须随时更新,显示到窗口之上
}
对控件的字体,背景色的改变(除了Button)HBRUSH CSettingDialog::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
if (pWnd->GetDlgCtrlID()==IDC_LINE)
{ pDC->SetTextColor(RGB(0,255,0));//设置文本颜色
pDC->SetBkColor(TRANSPARENT);
return m_brush;//设置静态组框的颜色
}
if (pWnd->GetDlgCtrlID()==IDC_LINE_WIDTH)
{
pDC->SetTextColor(RGB(0,10,10));
pDC->SetBkColor(RGB(0,0,255));
}
if (pWnd->GetDlgCtrlID()==IDC_TEXT)//修改静态文本字体,利用添加一个字体的成员变量,之后初始化m_font.CreatePointFont(300,"华文行楷");
{
pDC->SelectObject(&m_font);//将字体格式选入
}
// TODO: Return a different brush if the default is not desired
return hbr;//m_brush是创建的成员变量,利用创建画笔函数m_brush.CreateSolidBrush(RGB(255,255,12));
}
==================================================================================================================================
在窗口中贴图:
1显示位图; 1 CBitmap bitmap;bitmap.LoadBitemap()加载自己创建的位图
2、创建兼容的DC CDC dcCompatible,dcCompatible.createCompatibleDC(pDC);
3、将位图选入兼容DC,dc.Compatibl.SelectObject(&bitmap);
4、将兼容DC位图贴到当前DC中;pDC->BitBlt(rect.left,rect.top,rect.width(),rect.hight(),......)
BOOL CGraphicView::OnEraseBkgnd(CDC* pDC) //添加消息的处理函数,响应的消息是WM_ERASEBACKGROUND
{
// TODO: Add your message handler code here and/or call default
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);//加载位图资源
CDC dcCompatible; //创建兼容位图
dcCompatible.CreateCompatibleDC(pDC);//创建兼容位图的环境
dcCompatible.SelectObject(&bitmap);//讲位图资源选入
CRect rect;
GetClientRect(&rect);//得到显示位图的客户区域
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);//将位图资源载入,此函数显示图片只能按照1:1的比例显示,也就是说显示的bitmap 并不完整;
//return CView::OnEraseBkgnd(pDC);//此处需修改,因为此处在显示位图后迅速擦除掉了;
return true;
}
另一个办法 strechBlt()函数可以将图片全展示,而不仅仅是1:1BOOL CGraphicView::OnEraseBkgnd(CDC* pDC) //添加消息的处理函数,响应的消息是WM_ERASEBACKGROUND
{
// TODO: Add your message handler code here and/or call defaul
BITMAP bmp;//BITMAP的结构体,保存了关于bitmap的各种信息,包括,长,宽,高,等等
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);//加载位图资源
bitmap.GetBitmap(&bmp);
CDC dcCompatible; //创建兼容位图
dcCompatible.CreateCompatibleDC(pDC);//创建兼容位图的环境
dcCompatible.SelectObject(&bitmap);//讲位图资源选入
CRect rect;
GetClientRect(&rect);//得到显示位图的客户区域
//pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);//将位图资源载入
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,bmp.bmWidth,bmp.bmHeight,SRCCOPY);//此函数可以讲bitmap全面的显示,可以动态变化
//return CView::OnEraseBkgnd(pDC);//此处需修改,因为此处在显示位图后迅速擦除掉了;
return true;
}
Edit By SolarJupiter