mfc继续画图孙鑫c第十讲笔记整理.docx
《mfc继续画图孙鑫c第十讲笔记整理.docx》由会员分享,可在线阅读,更多相关《mfc继续画图孙鑫c第十讲笔记整理.docx(22页珍藏版)》请在冰豆网上搜索。
mfc继续画图孙鑫c第十讲笔记整理
MFC(继续画图,孙鑫C第十讲笔记整理)
1.画图:
a.创建四个菜单,为其添加消息响应;
b.在View中添加m_DrawType,保存绘画类型;
c.增加成员变量,m_PtOrigin,当按下鼠标左键时,保存此点;
d.在OnLButtonUp中画点,线,矩形,椭圆,别忘记设置成透明画刷
2.为其添加一个设置对话框(线型和线宽)
a.创建对话框,为其创建一个新类关联它;
b.为其中的线宽关联成员变量;
c.在View中增加一个菜单,响应新的对话框;
d.添加线型选项设置,将其Group属性选中,并为单选按纽关联成员变量。
在view中增加一个线型变量m_nLineStyle
3.添加一个颜色对话框
a.实例化一个CColorDialog
b.调用DoModal方法
4.添加字体对话框,将选择的字体在View中显示出来。
a.实例化一个对象;
b.为View添加一个字体成员变量,得到用户选择的字体。
c.调用Invadate()发出重绘消息;
d.再次注意一个对象只能创建一次,故要再次创建,必须将原告的删除!
5.为设置对话框增加示例功能。
a.当控件内容改变时,发出En_change消息。
而Radio按纽则为Clicked。
需先UpdateData()。
另外还需要ScreenToClient(&rect)
6.改变对话框的背景色和控件颜色。
每个控件被绘制时都发出WM_CTlColor消息,
7.如何改变OK按纽的字体和背景?
OK按纽
a.创建一个新类,CTestBtn,基类为CButton
b.在类中增加虚函数,DrawItem,添加代码。
c.将OK按纽关联成员变量。
类型为CTestBtn,注意将OK按纽的OwnerDraw特性选中。
Cancel按纽
用新类来改变。
a.加入新文件。
b.为Cancel关联一个成员变量,类型为CSXBtn;
c.调用CSXBtn的方法。
Cancel2按纽
a.方法同上。
8.在窗口中贴图,4个步骤
1、创建位图
CBitmap
bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
2、创建兼容DC
CDC
dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);
3、将位图选到兼容DC中
dcCompatible.SelectObject(&bitmap);
4、将兼容DC中的位图贴到当前DC中。
在WM_EraseBkgnd()中调用,但不能再调用基类的擦除背景函数。
也可以在OnDraw函数中完成,但效率低,图像会闪烁,因为它先擦除背景,慢。
pDC->BitBlt(rect.left,rect.top,rect.Width(),
rect.Height(),&dcCompatible,0,0,SRCCOPY);
具体细节:
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_yuandian=point;
CView:
:
OnLButtonDown(nFlags,point);
}voidCHuiTuView:
:
OnLButtonDown(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
m_yuandian=point;
CView:
:
OnLButtonDown(nFlags,point);
}
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCcdcc(this);
HBRUSHhbrush=(HBRUSH)GetStockObject(NULL_BRUSH);
CBrush*oldbrush=cdcc.SelectObject(CBrush:
:
FromHandle(hbrush));
CPencpen(m_xianleixing,m_xiandaxiao,RGB(255,0,0));
CPen*oldpen=cdcc.SelectObject(&cpen);
switch(m_huastyle)
{
case0:
cdcc.SetPixel(point,RGB(255,0,0));
break;
case1:
cdcc.MoveTo(m_yuandian);
cdcc.LineTo(point);
break;
case2:
cdcc.Rectangle(&CRect(m_yuandian,point));
break;
case3:
cdcc.Ellipse(&CRect(m_yuandian,point));
break;
default:
break;
}
cdcc.SelectObject(oldbrush);
cdcc.SelectObject(oldpen);
CView:
:
OnLButtonUp(nFlags,point);
}voidCHuiTuView:
:
OnLButtonUp(UINTnFlags,CPointpoint)
{
//TODO:
Addyourmessagehandlercodehereand/orcalldefault
CClientDCcdcc(this);
HBRUSHhbrush=(HBRUSH)GetStockObject(NULL_BRUSH);
CBrush*oldbrush=cdcc.SelectObject(CBrush:
:
FromHandle(hbrush));CPencpen(m_xianleixing,m_xiandaxiao,RGB(255,0,0));
CPen*oldpen=cdcc.SelectObject(&cpen);switch(m_huastyle)
{
case0:
cdcc.SetPixel(point,RGB(255,0,0));
break;
case1:
cdcc.MoveTo(m_yuandian);
cdcc.LineTo(point);
break;
case2:
cdcc.Rectangle(&CRect(m_yuandian,point));
break;
case3:
cdcc.Ellipse(&CRect(m_yuandian,point));
break;
default:
break;
}
cdcc.SelectObject(oldbrush);
cdcc.SelectObject(oldpen);
CView:
:
OnLButtonUp(nFlags,point);
}
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnSetting()
{
//TODO:
Addyourcommandhandlercodehere
CHuituDlgcdlg;
cdlg.m_xiankuai=m_xiandaxiao;
cdlg.m_leixing=m_xianleixing;
if(IDOK==cdlg.DoModal())
{
UpdateData();
m_xiandaxiao=cdlg.m_xiankuai;
m_xianleixing=cdlg.m_leixing;
}
}voidCHuiTuView:
:
OnSetting()
{
//TODO:
Addyourcommandhandlercodehere
CHuituDlgcdlg;cdlg.m_xiankuai=m_xiandaxiao;
cdlg.m_leixing=m_xianleixing;
if(IDOK==cdlg.DoModal())
{
UpdateData();
m_xiandaxiao=cdlg.m_xiankuai;
m_xianleixing=cdlg.m_leixing;
}}
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnDian()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=0;
}
voidCHuiTuView:
:
OnLine()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=1;
}
voidCHuiTuView:
:
OnJuxing()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=2;
}
voidCHuiTuView:
:
OnYuan()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=3;
}voidCHuiTuView:
:
OnDian()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=0;
}voidCHuiTuView:
:
OnLine()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=1;
}voidCHuiTuView:
:
OnJuxing()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=2;
}voidCHuiTuView:
:
OnYuan()
{
//TODO:
Addyourcommandhandlercodehere
m_huastyle=3;
}
edit挂链一个UINT整形,类型radio关联一个int
调用系统的调色板
1菜单添加一个“颜色”选项
ID_COLOR
2为这个COLOR添加事件
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnColor()
{
//TODO:
Addyourcommandhandlercodehere
CColorDialogccdlg;
ccdlg.m_cc.Flags|=CC_FULLOPEN|CC_RGBINIT;
ccdlg.m_cc.rgbResult=m_yanse;
if(IDOK==ccdlg.DoModal())
{
m_yanse=ccdlg.m_cc.rgbResult;
}
}voidCHuiTuView:
:
OnColor()
{
//TODO:
AddyourcommandhandlercodehereCColorDialogccdlg;
ccdlg.m_cc.Flags|=CC_FULLOPEN|CC_RGBINIT;
ccdlg.m_cc.rgbResult=m_yanse;
if(IDOK==ccdlg.DoModal())
{
m_yanse=ccdlg.m_cc.rgbResult;
}
}
3在CXXView中定义成员变量m_yanse,然后画图的时候,用这个去创建就行了
创建字体对话框也是一样的步骤
[cpp]view
plaincopyprint?
voidCHuiTuView:
:
OnZiti()
{
//TODO:
Addyourcommandhandlercodehere
CFontDialogcfdlg;
if(IDOK==cfdlg.DoModal())
{
if(m_ziti.m_hObject)
{
m_ziti.DeleteObject();
}
m_ziti.CreateFontIndirect(cfdlg.m_cf.lpLogFont);
m_zitimingzi=cfdlg.m_cf.lpLogFont->lfFaceName;
}
Invalidate();
}voidCHuiTuView:
:
OnZiti()
{
//TODO:
Addyourcommandhandlercodehere
CFontDialogcfdlg;
if(IDOK==cfdlg.DoModal())
{
if(m_ziti.m_hObject)
{
m_ziti.DeleteObject();
}
m_ziti.CreateFontIndirect(cfdlg.m_cf.lpLogFont);
m_zitimingzi=cfdlg.m_cf.lpLogFont->lfFaceName;
}
Invalidate();
}
一个选择线条的示例
[cpp]view
plaincopyprint?
voidCHuituDlg:
:
OnChangeXiankuan()
{
//TODO:
IfthisisaRICHEDITcontrol,thecontrolwillnot
//sendthisnotificationunlessyouoverridetheCDialog:
:
OnInitDialog()
//functionandcallCRichEditCtrl().SetEventMask()
//withtheENM_CHANGEflagORedintothemask.
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}
voidCHuituDlg:
:
OnRadio1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}
voidCHuituDlg:
:
OnRadio2()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}
voidCHuituDlg:
:
OnRadio3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}
voidCHuituDlg:
:
OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
//TODO:
Addyourmessagehandlercodehere
UpdateData();
CClientDCccdc(this);
CPencpen(m_leixing,m_xiankuai,RGB(255,0,0));//可以设置一个成员变量接收来自调色板的颜色
ccdc.SelectObject(&cpen);
CRectcrect;
GetDlgItem(ID_SHILI)->GetWindowRect(&crect);
ScreenToClient(&crect);
TEXTMETRICtm;
ccdc.GetTextMetrics(&tm);
ccdc.MoveTo(crect.left+20,crect.top+crect.Height()/2);
ccdc.LineTo(crect.right-20,crect.top+crect.Height()/2);
//DonotcallCDialog:
:
OnPaint()forpaintingmessages
}voidCHuituDlg:
:
OnChangeXiankuan()
{
//TODO:
IfthisisaRICHEDITcontrol,thecontrolwillnot
//sendthisnotificationunlessyouoverridetheCDialog:
:
OnInitDialog()
//functionandcallCRichEditCtrl().SetEventMask()
//withtheENM_CHANGEflagORedintothemask.
//TODO:
AddyourcontrolnotificationhandlercodehereInvalidate();
}voidCHuituDlg:
:
OnRadio1()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}voidCHuituDlg:
:
OnRadio2()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}voidCHuituDlg:
:
OnRadio3()
{
//TODO:
Addyourcontrolnotificationhandlercodehere
Invalidate();
}voidCHuituDlg:
:
OnPaint()
{
CPaintDCdc(this);//devicecontextforpainting
//TODO:
Addyourmessagehandlercodehere
UpdateData();
CClientDCccdc(this);
CPencpen(m_leixing,m_xiankuai,RGB(255,0,0));//可以设置一个成员变量接收来自调色板的颜色
ccdc.SelectObject(&cpen);CRectcrect;
GetDlgItem(ID_SHILI)->GetWindowRect(&crect);
ScreenToClient(&crect);
TEXTMETRICtm;
ccdc.GetTextMetrics(&tm);
ccdc.MoveTo(crect.left+20,crect.top+crect.Height()/2);
ccdc.LineTo(crect.right-20,crect.top+crect.Height()/2);
//DonotcallCDialog:
:
OnPaint()forpaintingmessages
}
当进行数据交互是要UpdateData
能够立即交互
在对话框中添加WM_CTLCOLOR消息
[cpp]view
plaincopyprint?
HBRUSHCHuituDlg:
:
OnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor)
{
HBRUSHhbr=CDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
//TODO:
ChangeanyattributesoftheDChere
//TODO:
Returnadifferentbrushifthedefaultisnotdesired
returnm_myBrush;
//returnhbr;
}HBRUSHCHuituDlg:
:
OnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor)
{
HBRUSHhbr=CDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
//TODO:
ChangeanyattributesoftheDChere
//TODO:
Returnadifferentbrushifthedefaultisnotdesiredreturnm_myBrush;
//returnhbr;
}
m_mBrush.CreateSolidBrush(RGB(0,255,0))
返回自己的画刷,对话框中的控件绘制的时候
就会触发这个消息
[cpp]view
plaincopyprint?
HBRUSHCHuituDlg:
:
OnCtlColor(CDC*pDC,CWnd*pWnd,UINTnCtlColor)
{
HBRUSHhbr=CDialog:
:
OnCtlColor(pDC,pWnd,nCtlColor);
//TODO:
ChangeanyattributesoftheDChere
//TODO:
Returnadifferentbrushi