第十课笔记

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);

 

具体细节:

 

  1. void CHuiTuView::OnLButtonDown(UINT nFlags, CPoint point)   
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.       
  5.     m_yuandian=point;  
  6.     CView::OnLButtonDown(nFlags, point);  
  7. }  
void CHuiTuView::OnLButtonDown(UINT nFlags, CPoint point) {	// TODO: Add your message handler code here and/or call default		m_yuandian=point;	CView::OnLButtonDown(nFlags, point);}


 

  1. void CHuiTuView::OnLButtonUp(UINT nFlags, CPoint point)   
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.       
  5.     CClientDC cdcc(this);  
  6.       
  7.     HBRUSH hbrush=(HBRUSH)GetStockObject(NULL_BRUSH);  
  8.     CBrush *oldbrush=cdcc.SelectObject(CBrush::FromHandle(hbrush));  
  9.   
  10.     CPen cpen(m_xianleixing,m_xiandaxiao,RGB(255,0,0));  
  11.     CPen *oldpen=cdcc.SelectObject(&cpen);  
  12.   
  13.     switch(m_huastyle)  
  14.     {  
  15.     case 0:  
  16.   
  17.         cdcc.SetPixel(point,RGB(255,0,0));  
  18.         break;  
  19.           
  20.     case 1:  
  21.         cdcc.MoveTo(m_yuandian);  
  22.         cdcc.LineTo(point);  
  23.         break;  
  24.           
  25.     case 2:  
  26.         cdcc.Rectangle(&CRect(m_yuandian,point));  
  27.         break;  
  28.           
  29.     case 3:  
  30.         cdcc.Ellipse(&CRect(m_yuandian,point));  
  31.         break;  
  32.           
  33.     default:  
  34.         break;  
  35.     }  
  36.       
  37.     cdcc.SelectObject(oldbrush);  
  38.     cdcc.SelectObject(oldpen);  
  39.     CView::OnLButtonUp(nFlags, point);  
  40. }  
void CHuiTuView::OnLButtonUp(UINT nFlags, CPoint point) {	// TODO: Add your message handler code here and/or call default		CClientDC cdcc(this);		HBRUSH hbrush=(HBRUSH)GetStockObject(NULL_BRUSH);	CBrush *oldbrush=cdcc.SelectObject(CBrush::FromHandle(hbrush));	CPen cpen(m_xianleixing,m_xiandaxiao,RGB(255,0,0));	CPen *oldpen=cdcc.SelectObject(&cpen);	switch(m_huastyle)	{	case 0:		cdcc.SetPixel(point,RGB(255,0,0));		break;			case 1:		cdcc.MoveTo(m_yuandian);		cdcc.LineTo(point);		break;			case 2:		cdcc.Rectangle(&CRect(m_yuandian,point));		break;			case 3:		cdcc.Ellipse(&CRect(m_yuandian,point));		break;			default:		break;	}		cdcc.SelectObject(oldbrush);	cdcc.SelectObject(oldpen);	CView::OnLButtonUp(nFlags, point);}


 

  1. void CHuiTuView::OnSetting()   
  2. {  
  3.     // TODO: Add your command handler code here   
  4.     CHuituDlg cdlg;  
  5.   
  6.     cdlg.m_xiankuai=m_xiandaxiao;  
  7.     cdlg.m_leixing=m_xianleixing;  
  8.     if(IDOK==cdlg.DoModal())  
  9.     {  
  10.         UpdateData();  
  11.         m_xiandaxiao=cdlg.m_xiankuai;  
  12.         m_xianleixing=cdlg.m_leixing;  
  13.     }  
  14.   
  15. }  
void CHuiTuView::OnSetting() {	// TODO: Add your command handler code here	CHuituDlg cdlg;	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;	}}


 

  1. void CHuiTuView::OnDian()   
  2. {  
  3.     // TODO: Add your command handler code here   
  4.     m_huastyle=0;  
  5.       
  6. }  
  7.   
  8. void CHuiTuView::OnLine()   
  9. {  
  10.     // TODO: Add your command handler code here   
  11.     m_huastyle=1;  
  12. }  
  13.   
  14. void CHuiTuView::OnJuxing()   
  15. {  
  16.     // TODO: Add your command handler code here   
  17.     m_huastyle=2;  
  18. }  
  19.   
  20. void CHuiTuView::OnYuan()   
  21. {  
  22.     // TODO: Add your command handler code here   
  23.     m_huastyle=3;  
  24. }  
void CHuiTuView::OnDian() {	// TODO: Add your command handler code here	m_huastyle=0;	}void CHuiTuView::OnLine() {	// TODO: Add your command handler code here	m_huastyle=1;}void CHuiTuView::OnJuxing() {	// TODO: Add your command handler code here	m_huastyle=2;}void CHuiTuView::OnYuan() {	// TODO: Add your command handler code here	m_huastyle=3;}


 

edit挂链一个UINT整形,类型radio关联一个int

 

调用系统的调色板

1菜单添加一个“颜色”选项 ID_COLOR

2为这个COLOR添加事件

  1. void CHuiTuView::OnColor()   
  2. {  
  3.     // TODO: Add your command handler code here   
  4.   
  5.     CColorDialog  ccdlg;  
  6.     ccdlg.m_cc.Flags|= CC_FULLOPEN | CC_RGBINIT;  
  7.       
  8.     ccdlg.m_cc.rgbResult=m_yanse;  
  9.     if(IDOK==ccdlg.DoModal())  
  10.     {  
  11.         m_yanse=ccdlg.m_cc.rgbResult;  
  12.     }  
  13. }  
void CHuiTuView::OnColor() {	// TODO: Add your command handler code here	CColorDialog  ccdlg;	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,然后画图的时候,用这个去创建就行了

 

创建字体对话框也是一样的步骤

  1. void CHuiTuView::OnZiti()   
  2. {  
  3.     // TODO: Add your command handler code here   
  4.       
  5.     CFontDialog cfdlg;  
  6.       
  7.     if(IDOK==cfdlg.DoModal())  
  8.     {  
  9.         if(m_ziti.m_hObject)  
  10.         {  
  11.         m_ziti.DeleteObject();  
  12.           
  13.         }  
  14.         m_ziti.CreateFontIndirect(cfdlg.m_cf.lpLogFont);  
  15.         m_zitimingzi=cfdlg.m_cf.lpLogFont->lfFaceName;  
  16.     }  
  17.     Invalidate();  
  18. }  
void CHuiTuView::OnZiti() {	// TODO: Add your command handler code here		CFontDialog cfdlg;		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();}


 

一个选择线条的示例

  1. void CHuituDlg::OnChangeXiankuan()   
  2. {  
  3.     // TODO: If this is a RICHEDIT control, the control will not   
  4.     // send this notification unless you override the CDialog::OnInitDialog()   
  5.     // function and call CRichEditCtrl().SetEventMask()   
  6.     // with the ENM_CHANGE flag ORed into the mask.   
  7.       
  8.     // TODO: Add your control notification handler code here   
  9.   
  10.     Invalidate();  
  11.       
  12. }  
  13.   
  14. void CHuituDlg::OnRadio1()   
  15. {  
  16.     // TODO: Add your control notification handler code here   
  17.     Invalidate();  
  18. }  
  19.   
  20. void CHuituDlg::OnRadio2()   
  21. {  
  22.     // TODO: Add your control notification handler code here   
  23.     Invalidate();     
  24. }  
  25.   
  26. void CHuituDlg::OnRadio3()   
  27. {  
  28.     // TODO: Add your control notification handler code here   
  29.     Invalidate();  
  30. }  
  31.   
  32. void CHuituDlg::OnPaint()   
  33. {  
  34.     CPaintDC dc(this); // device context for painting   
  35.       
  36.     // TODO: Add your message handler code here   
  37.       
  38.     UpdateData();  
  39.     CClientDC ccdc(this);  
  40.     CPen cpen(m_leixing,m_xiankuai,RGB(255,0,0));//可以设置一个成员变量接收来自调色板的颜色   
  41.     ccdc.SelectObject(&cpen);  
  42.   
  43.     CRect crect;  
  44.     GetDlgItem(ID_SHILI)->GetWindowRect(&crect);  
  45.       
  46.     ScreenToClient(&crect);  
  47.     TEXTMETRIC tm;  
  48.     ccdc.GetTextMetrics(&tm);  
  49.     ccdc.MoveTo(crect.left+20,crect.top+crect.Height()/2);  
  50.     ccdc.LineTo(crect.right-20,crect.top+crect.Height()/2);  
  51.   
  52.   
  53.   
  54.       
  55.     // Do not call CDialog::OnPaint() for painting messages   
  56. }  
void CHuituDlg::OnChangeXiankuan() {	// 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();	}void CHuituDlg::OnRadio1() {	// TODO: Add your control notification handler code here	Invalidate();}void CHuituDlg::OnRadio2() {	// TODO: Add your control notification handler code here	Invalidate();	}void CHuituDlg::OnRadio3() {	// TODO: Add your control notification handler code here	Invalidate();}void CHuituDlg::OnPaint() {	CPaintDC dc(this); // device context for painting		// TODO: Add your message handler code here		UpdateData();	CClientDC ccdc(this);	CPen cpen(m_leixing,m_xiankuai,RGB(255,0,0));//可以设置一个成员变量接收来自调色板的颜色	ccdc.SelectObject(&cpen);	CRect crect;	GetDlgItem(ID_SHILI)->GetWindowRect(&crect);		ScreenToClient(&crect);	TEXTMETRIC tm;	ccdc.GetTextMetrics(&tm);	ccdc.MoveTo(crect.left+20,crect.top+crect.Height()/2);	ccdc.LineTo(crect.right-20,crect.top+crect.Height()/2);		// Do not call CDialog::OnPaint() for painting messages}


当进行数据交互是要UpdateData

能够立即交互

 

在对话框中添加WM_CTLCOLOR消息

  1. HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.       
  5.     // TODO: Change any attributes of the DC here   
  6.       
  7.     // TODO: Return a different brush if the default is not desired   
  8.   
  9.   
  10.     return  m_myBrush;  
  11.     //return hbr;   
  12. }  
HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);		// TODO: Change any attributes of the DC here		// TODO: Return a different brush if the default is not desired	return  m_myBrush;	//return hbr;}

m_mBrush.CreateSolidBrush(RGB(0,255,0))
返回自己的画刷,对话框中的控件绘制的时候 就会触发这个消息

 

  1. HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)   
  2. {  
  3.     HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);  
  4.       
  5.     // TODO: Change any attributes of the DC here   
  6.       
  7.     // TODO: Return a different brush if the default is not desired   
  8.       
  9.     if(pWnd->GetDlgCtrlID()==ID_LEIXINGKUANGKUANG)  
  10.     {  
  11.         pDC->SetBkColor(RGB(255,0,0));  
  12.         pDC->SetTextColor(RGB(255,255,255));  
  13.         return m_myBrush;  
  14.     }  
  15.   
  16.     if(pWnd->GetDlgCtrlID()==ID_XIANKUAN)  
  17.     {  
  18.         pDC->SetBkColor(RGB(255,0,0));  
  19.         pDC->SetTextColor(RGB(255,255,255));  
  20.         return m_myBrush;  
  21.     }  
  22.   
  23.     if(pWnd->GetDlgCtrlID()==ID_MFC)  
  24.     {  
  25.         pDC->SelectObject(&mfcziti);  
  26.     }  
  27.   
  28.     if(pWnd->GetDlgCtrlID()==IDOK)  
  29.     {  
  30.         pDC->SetTextColor(RGB(255,0,0));  
  31.         pDC->SetBkMode(TRANSPARENT);  
  32.     }  
  33.   
  34.     //return  m_myBrush;   
  35.     return hbr;  
  36. }  
HBRUSH CHuituDlg::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor) {	HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);		// TODO: Change any attributes of the DC here		// TODO: Return a different brush if the default is not desired		if(pWnd->GetDlgCtrlID()==ID_LEIXINGKUANGKUANG)	{		pDC->SetBkColor(RGB(255,0,0));		pDC->SetTextColor(RGB(255,255,255));		return m_myBrush;	}	if(pWnd->GetDlgCtrlID()==ID_XIANKUAN)	{		pDC->SetBkColor(RGB(255,0,0));		pDC->SetTextColor(RGB(255,255,255));		return m_myBrush;	}	if(pWnd->GetDlgCtrlID()==ID_MFC)	{		pDC->SelectObject(&mfcziti);	}	if(pWnd->GetDlgCtrlID()==IDOK)	{		pDC->SetTextColor(RGB(255,0,0));		pDC->SetBkMode(TRANSPARENT);	}	//return  m_myBrush;	return hbr;}


现在没有办法改变BUTTon的属性,只能通过它的虚函数DrawItem去改变

下面新建一个CBUTTOn的派生类,然后去覆写DrawItem函数

然后关联控制,将OK关联到新建的派生类

 

 

如果还想改变按钮的背景颜色或其它的,可以使用组建重用,即利用别人写好的现成的类

把类和头文件都添加项目目录里面,然后将按钮关联到 组件类,然后可以在OnInitDlg中新添加一些属性。这里引用牛人写的CButtonST类

  1. BOOL CHuituDlg::OnInitDialog()   
  2. {  
  3.     CDialog::OnInitDialog();  
  4.       
  5.     // TODO: Add extra initialization here   
  6.     m_paisheng.SetActiveBgColor(RGB(255,0,0));  
  7.     m_paisheng.SetActiveFgColor(RGB(0,255,0));  
  8.     return TRUE;  // return TRUE unless you set the focus to a control   
  9.                   // EXCEPTION: OCX Property Pages should return FALSE   
  10. }  
BOOL CHuituDlg::OnInitDialog() {	CDialog::OnInitDialog();		// TODO: Add extra initialization here	m_paisheng.SetActiveBgColor(RGB(255,0,0));	m_paisheng.SetActiveFgColor(RGB(0,255,0));	return TRUE;  // return TRUE unless you set the focus to a control	              // EXCEPTION: OCX Property Pages should return FALSE}


 

位图:

在CXXView中 添加WM_ERASEBKGND消息

  1. BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC)   
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.   
  5.     //MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的   
  6.   
  7.   
  8.     CBitmap cbitmap;  
  9.     cbitmap.LoadBitmap(IDB_BITMAP1);  
  10.   
  11.   
  12.     CDC memDC;  
  13.     memDC.CreateCompatibleDC(pDC);  
  14.     memDC.SelectObject(&cbitmap);  
  15.   
  16.     CRect crect;  
  17.     GetClientRect(&crect);  
  18.     pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);  
  19.   
  20.           
  21.     return TRUE;  
  22.     //return CView::OnEraseBkgnd(pDC);   
  23. }  
BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC) {	// TODO: Add your message handler code here and/or call default	//MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的	CBitmap cbitmap;	cbitmap.LoadBitmap(IDB_BITMAP1);	CDC memDC;	memDC.CreateCompatibleDC(pDC);	memDC.SelectObject(&cbitmap);	CRect crect;	GetClientRect(&crect);	pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);			return TRUE;	//return CView::OnEraseBkgnd(pDC);}


1:1复制的

 

下面还有个可以拉伸的

CBitmap有个成员方法

这样就可以获取位图的宽度和高度了

  1. BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC)   
  2. {  
  3.     // TODO: Add your message handler code here and/or call default   
  4.   
  5.     //MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的   
  6.   
  7.   
  8.     CBitmap cbitmap;  
  9.     cbitmap.LoadBitmap(IDB_BITMAP1);  
  10.   
  11.     BITMAP bitmap;  
  12.     cbitmap.GetBitmap(&bitmap);  
  13.   
  14.     CDC memDC;  
  15.     memDC.CreateCompatibleDC(pDC);  
  16.     memDC.SelectObject(&cbitmap);  
  17.   
  18.     CRect crect;  
  19.     GetClientRect(&crect);  
  20.     //pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);   
  21.     pDC->StretchBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);  
  22.           
  23.           
  24.     return TRUE;  
  25.     //return CView::OnEraseBkgnd(pDC);   
  26. }  
BOOL CHuiTuView::OnEraseBkgnd(CDC* pDC) {	// TODO: Add your message handler code here and/or call default	//MessageBeep(0);这句用来测试,擦除是什么时候触发的,在调用ONPaint之前触发的	CBitmap cbitmap;	cbitmap.LoadBitmap(IDB_BITMAP1);	BITMAP bitmap;	cbitmap.GetBitmap(&bitmap);	CDC memDC;	memDC.CreateCompatibleDC(pDC);	memDC.SelectObject(&cbitmap);	CRect crect;	GetClientRect(&crect);	//pDC->BitBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,SRCCOPY);	pDC->StretchBlt(0,0,crect.Width(),crect.Height(),&memDC,0,0,bitmap.bmWidth,bitmap.bmHeight,SRCCOPY);					return TRUE;	//return CView::OnEraseBkgnd(pDC);}


上面的代码可以写在OnPaint或OnDraw中
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值