Windows程序设计__孙鑫C++Lesson10《图形绘制及各类对话框》
来源:互联网 发布:数学编程 编辑:程序博客网 时间:2024/05/16 11:17
Windows程序设计__孙鑫C++Lesson10《图形绘制及各类对话框》
本节要点:
1.图形绘制
2.图形绘制参数设置
3.对话框控件的着色
4.自绘制button的创建
5.贴图操作(将位图显示到视图)
//**************************************************************************
1.图形绘制
点,直线,矩形,椭圆
实验代码:
//**************************************************************************
void CGraphicView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CClientDC dc(this);
//画笔线型无需switch判断 看其定义
//画笔线宽是按用户设置线宽设置
//画笔颜色从颜色对话框选择
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);
CPen* pOldPen=dc.SelectObject(&pen);
switch(m_nDrawType)
{
case 0:
dc.SetPixel(point,RGB(255,0,0));
break;
case 1:
dc.MoveTo(m_ptOrigin);
dc.LineTo(point);
break;
case 2:
dc.Rectangle(CRect(m_ptOrigin,point));
break;
case 3:
dc.Ellipse(CRect(m_ptOrigin,point));
break;
default:
break;
}
dc.SelectObject(pOldPen);
CView::OnLButtonUp(nFlags, point);
}
//**************************************************************************
绘图效果如下:
2.图形绘制参数设置
(1)线型设置及示例功能
//通过关联成员变量设置线型
void CGraphicView::OnSet()
{
// TODO: Add your command handler code here
CDlgSetting 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;
}
}
//通过在示例控件上以用户选择重画直线作为示例
void CDlgSetting::OnPaint()
{
CPaintDC dc(this); // device context for painting
// TODO: Add your message handler code here
UpdateData();
CPen pen(m_nLineStyle,m_nLineWidth,m_clr);//参数由重用户选择
CPen* pOldPen=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);
dc.SelectObject(pOldPen);
// Do not call CDialog::OnPaint() for painting messages
}
(2)颜色选择
//使用系统颜色对话框
void CGraphicView::OnColordlg()
{
// TODO: Add your command handler code here
CColorDialog ColorDlg;
//ColorDlg.m_cc.Flags=CC_RGBINIT;//出错 因为先前已有标记被清除
ColorDlg.m_cc.Flags |=CC_FULLOPEN | CC_RGBINIT;
ColorDlg.m_cc.rgbResult = m_clr;
if( IDOK==ColorDlg.DoModal() )
{
m_clr=ColorDlg.m_cc.rgbResult;
}
}
(4)字体选择
//使用系统字体对话框
void CGraphicView::OnFont()
{
// TODO: Add your command handler code here
CFontDialog FontDlg;
LOGFONT lf;
if( IDOK==FontDlg.DoModal() )
{
if(m_font.m_hObject )
m_font.DeleteObject();
memcpy(&lf, FontDlg.m_cf.lpLogFont, sizeof(LOGFONT));
m_font.CreateFontIndirect(&lf);//关联字体
m_strFontName=lf.lfFaceName;
}
Invalidate();
}
//***************************************************************************8
3.对话框控件的着色
//对WM_CTLCOLOR消息进行处理
HBRUSH CDlgSetting::OnCtlColor(CDC* pDC, CWnd* pWnd, UINT nCtlColor)
{
HBRUSH hbr = CDialog::OnCtlColor(pDC, pWnd, nCtlColor);
// TODO: Change any attributes of the DC here
if(IDC_LINE_STYLE==pWnd->GetDlgCtrlID())//线型控件着色
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkMode(TRANSPARENT);
return m_brush;
}
if(IDC_LINE_WIDTH==pWnd->GetDlgCtrlID())//线宽控件着色
{
pDC->SetTextColor(RGB(255,0,0));
pDC->SetBkColor(RGB(0,0,255));
return m_brush;
}
if(IDC_TEXT==pWnd->GetDlgCtrlID())//测试文本框,字体选择
pDC->SelectObject(&m_font);
// TODO: Return a different brush if the default is not desired
return hbr;
}
//着色的对话框控件如下图:
4.自绘制button的创建
插入一个CTestButton类,基类选择CButton,重载虚函数
virtual void DrawItem( LPDRAWITEMSTRUCT lpDrawItemStruct );//可从MSDN拷贝一份示例代码
5.贴图操作(将位图显示到视图)
贴图操作步骤:
step1:创建位图
step2:创建兼容DC
step3:将位图选到兼容DC中
step4:将兼容DC的位图贴到当前DC中
实验代码如下:
//*******************************************************************
//背景擦出时调用
BOOL CGraphicView::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CBitmap bmp;
if(bmp.LoadBitmap(IDB_BITMAP1))//加载位图
{
BITMAP bmpInfo;
bmp.GetBitmap(&bmpInfo);//获取位图信息
CDC dcCompatible;
dcCompatible.CreateCompatibleDC(pDC);//创建兼容DC
CBitmap* pOldBitmap = dcCompatible.SelectObject(&bmp);//将位图选到兼容DC中
CRect rect;
GetClientRect(&rect);
pDC->BitBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,0,0,SRCCOPY);//1:1比例拷贝
//pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcCompatible,
//0,0,bmpInfo.bmWidth,bmpInfo.bmHeight,NOTSRCCOPY);//拉伸拷贝
dcCompatible.SelectObject(pOldBitmap);
return TRUE;
}
else
return CView::OnEraseBkgnd(pDC);
}
//*******************************************************************
BitBlt中dwRop中参数设置为 SRCCOPY(不作更改的拷贝) ,贴图效果如下图:
BitBlt中dwRop中参数设置为 NOTSRCCOPY(反转效果) ,贴图效果如下图:
//******************************************************************************
本节小结:
1.掌握几种系统对话框的使用,尤其是如何设置其初值和获取返回值
2.掌握对话框上控件的着色
3.掌握兼容DC的贴图操作(这里有一个疑问 为什么叫兼容DC 和谁兼容 为什么要兼容?待解决)
- Windows程序设计__孙鑫C++Lesson10《图形绘制及各类对话框》
- Windows程序设计__孙鑫C++Lesson11《图形绘制及保存》
- Windows程序设计__孙鑫C++Lesson7《对话框操作1》
- Windows程序设计__孙鑫C++Lesson8《对话框操作2》
- 孙鑫MFC学习心得:Lesson10:图形的绘制
- Windows程序设计__孙鑫C++Lesson1《Windows内部运行原理》
- Windows程序设计__孙鑫C++Lesson2《掌握C++》 .
- Windows程序设计__孙鑫C++Lesson4《绘图操作》
- Windows程序设计__孙鑫C++Lesson5《文本操作》
- Windows程序设计__孙鑫C++Lesson6《菜单操作》
- Windows程序设计__孙鑫C++Lesson9《应用程序外观修改》
- Windows程序设计__孙鑫C++Lesson12《文件操作》
- Windows程序设计__孙鑫C++Lesson13《文档与串行化》
- Windows程序设计__孙鑫C++Lesson14《网络编程》
- Windows程序设计__孙鑫C++Lesson17《进程间的通信》
- Windows程序设计__孙鑫C++Lesson18《ActiveX 控件》
- Windows程序设计__孙鑫C++Lesson19《动态链接库》
- Windows程序设计__孙鑫C++Lesson20《HOOK和数据库访问》
- nios ii 之 LCD 1602
- 【數列遞推】爭奪基友
- 测试经理能力要求
- 做完一个小任务的总结
- 由于嵌入式项目版本较多不好管理,于是笔者写了一个“版本小助手”,和大家分享一下,附上程序
- Windows程序设计__孙鑫C++Lesson10《图形绘制及各类对话框》
- Ultimate Pentesting VM
- where 1=1用处
- 多线程与WPF 4.5
- Linux文件查找命令find,xargs详述
- Linux 连接详解
- 工作时候自己写的一个用户采集python
- 20120130工作记录
- 工作需要的网站用户密码破解