[Visual C++系列]2.3 使用点,刷子,笔进行绘图
2011-08-06 10:35
148 查看
2.3 使用点,刷子,笔进行绘图
在Windows中画点的方法很简单,只需要调用COLORREF CDC::SetPixel( int x, int y, COLORREF crColor )就可以在指定点画上指定颜色,同时返回原来的颜色。COLORREF CDC::GetPixel( int x, int y)可以得到指定点的颜色。在Windows中应该少使用画点的函数,因为这样做的执行效率比较低。
刷子和画笔在Windows作图中是使用最多的GUI对象,本节在讲解刷子和画笔使用方法的同时也讲述一写基本作图函数。
在画点或画线时系统使用当前DC中的画笔,所以在创建画笔后必须将其选入DC才会在绘图时产生效果。画笔可以通过CPen对象来产生,通过调用CPen::CreatePen( int nPenStyle, int nWidth, COLORREF crColor )来创建。其中nPenStyle指名画笔的风格,可取如下值:
PS_SOLID 实线 Creates a solid pen.
PS_DASH 虚线,宽度必须为一 Creates a dashed pen. Valid only when the pen width is 1 or less, in device units.
PS_DOT 点线,宽度必须为一 Creates a dotted pen. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOT 点划线,宽度必须为一 Creates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOTDOT 双点划线,宽度必须为一 Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units.
PS_NULL 空线,使用时什么也不会产生 Creates a null pen.
PS_ENDCAP_ROUND 结束处为圆形 End caps are round.
PS_ENDCAP_SQUARE 结束处为方形 End caps are square.
nWidth和crColor为线的宽度和颜色。
刷子是在画封闭曲线时用来填充的颜色,例如当你画圆形或方形时系统会用当前的刷子对内部进行填充。刷子可利用CBrush对象产生。通过以下几种函数创建刷子:
BOOL CreateSolidBrush( COLORREF crColor ); 创建一种固定颜色的刷子
BOOL CreateHatchBrush( int nIndex, COLORREF crColor ); 创建指定颜色和网格的刷子,nIndex可取以下值:
HS_BDIAGONAL Downward hatch (left to right) at 45 degrees
HS_CROSS Horizontal and vertical crosshatch
HS_DIAGCROSS Crosshatch at 45 degrees
HS_FDIAGONAL Upward hatch (left to right) at 45 degrees
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
BOOL CreatePatternBrush( CBitmap* pBitmap ); 创建以8*8位图为模板的刷子
在选择了画笔和刷子后就可以利用Windows的作图函数进行作图了,基本的画线函数有以下几种
CDC::MoveTo( int x, int y ); 改变当前点的位置
CDC::LineTo( int x, int y ); 画一条由当前点到参数指定点的线
CDC::BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); 画弧线
CDC::BOOL Polyline( LPPOINT lpPoints, int nCount ); 将多条线依次序连接
基本的作图函数有以下几种:
CDC::BOOL Rectangle( LPCRECT lpRect ); 矩形
CDC::RoundRect( LPCRECT lpRect, POINT point ); 圆角矩形
CDC::Draw3dRect( int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight ); 3D边框
CDC::Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); 扇形
CDC::Ellipse( LPCRECT lpRect ); 椭圆形
CDC::Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd );
CDC::Polygon( LPPOINT lpPoints, int nCount ); 多边形
对于矩形,圆形或类似的封闭曲线,系统会使用画笔绘制边缘,使用刷子填充内部。如果你不希望填充或是画出边缘,你可以选入空刷子(NULL_PEN)或是(NULL_BRUSH)空笔。
下面的代码创建一条两象素宽的实线并选入DC。并进行简单的作图:
在Windows中画点的方法很简单,只需要调用COLORREF CDC::SetPixel( int x, int y, COLORREF crColor )就可以在指定点画上指定颜色,同时返回原来的颜色。COLORREF CDC::GetPixel( int x, int y)可以得到指定点的颜色。在Windows中应该少使用画点的函数,因为这样做的执行效率比较低。
刷子和画笔在Windows作图中是使用最多的GUI对象,本节在讲解刷子和画笔使用方法的同时也讲述一写基本作图函数。
在画点或画线时系统使用当前DC中的画笔,所以在创建画笔后必须将其选入DC才会在绘图时产生效果。画笔可以通过CPen对象来产生,通过调用CPen::CreatePen( int nPenStyle, int nWidth, COLORREF crColor )来创建。其中nPenStyle指名画笔的风格,可取如下值:
PS_SOLID 实线 Creates a solid pen.
PS_DASH 虚线,宽度必须为一 Creates a dashed pen. Valid only when the pen width is 1 or less, in device units.
PS_DOT 点线,宽度必须为一 Creates a dotted pen. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOT 点划线,宽度必须为一 Creates a pen with alternating dashes and dots. Valid only when the pen width is 1 or less, in device units.
PS_DASHDOTDOT 双点划线,宽度必须为一 Creates a pen with alternating dashes and double dots. Valid only when the pen width is 1 or less, in device units.
PS_NULL 空线,使用时什么也不会产生 Creates a null pen.
PS_ENDCAP_ROUND 结束处为圆形 End caps are round.
PS_ENDCAP_SQUARE 结束处为方形 End caps are square.
nWidth和crColor为线的宽度和颜色。
刷子是在画封闭曲线时用来填充的颜色,例如当你画圆形或方形时系统会用当前的刷子对内部进行填充。刷子可利用CBrush对象产生。通过以下几种函数创建刷子:
BOOL CreateSolidBrush( COLORREF crColor ); 创建一种固定颜色的刷子
BOOL CreateHatchBrush( int nIndex, COLORREF crColor ); 创建指定颜色和网格的刷子,nIndex可取以下值:
HS_BDIAGONAL Downward hatch (left to right) at 45 degrees
HS_CROSS Horizontal and vertical crosshatch
HS_DIAGCROSS Crosshatch at 45 degrees
HS_FDIAGONAL Upward hatch (left to right) at 45 degrees
HS_HORIZONTAL Horizontal hatch
HS_VERTICAL Vertical hatch
BOOL CreatePatternBrush( CBitmap* pBitmap ); 创建以8*8位图为模板的刷子
在选择了画笔和刷子后就可以利用Windows的作图函数进行作图了,基本的画线函数有以下几种
CDC::MoveTo( int x, int y ); 改变当前点的位置
CDC::LineTo( int x, int y ); 画一条由当前点到参数指定点的线
CDC::BOOL Arc( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); 画弧线
CDC::BOOL Polyline( LPPOINT lpPoints, int nCount ); 将多条线依次序连接
基本的作图函数有以下几种:
CDC::BOOL Rectangle( LPCRECT lpRect ); 矩形
CDC::RoundRect( LPCRECT lpRect, POINT point ); 圆角矩形
CDC::Draw3dRect( int x, int y, int cx, int cy, COLORREF clrTopLeft, COLORREF clrBottomRight ); 3D边框
CDC::Chord( LPCRECT lpRect, POINT ptStart, POINT ptEnd ); 扇形
CDC::Ellipse( LPCRECT lpRect ); 椭圆形
CDC::Pie( LPCRECT lpRect, POINT ptStart, POINT ptEnd );
CDC::Polygon( LPPOINT lpPoints, int nCount ); 多边形
对于矩形,圆形或类似的封闭曲线,系统会使用画笔绘制边缘,使用刷子填充内部。如果你不希望填充或是画出边缘,你可以选入空刷子(NULL_PEN)或是(NULL_BRUSH)空笔。
下面的代码创建一条两象素宽的实线并选入DC。并进行简单的作图:
{ ... CPen pen; pen.CreatePen(PS_SOLID,2,RGB(128,128,128)); CPen* pOldPen=(CPen*)dc.SelectObject(&pen); dc.SelectStockObject(NULL_BRUSH);//选入空刷子 dc.Rectangle(CRect(0,0,20,20));//画矩形 ... }
相关文章推荐
- 使用点,刷子,笔进行绘图
- 使用Eclipse RCP进行桌面程序开发(五):2D绘图
- CaffeOnSpark安装和使用教程系列二:单节点使用CaffeOnSpark进行MNIST数据集的测试
- 使用Java的Graphics类进行绘图的方法详解
- VS2010与.NET4系列 22.使用VS2010进行框选择和多行编辑
- 在Qt Widget中使用QPainter之外的引擎进行绘图
- JBoss 系列十九:使用JGroups构建块RspFilter对群组通信返回消息进行过滤
- 使用快速排序算法对列表进行排序——Leetcode系列(四)
- Python进阶(三十九)-数据可视化の使用matplotlib进行绘图分析数据
- Jenkins系列——使用checkstyle进行代码规范检查【升级版】
- 使用插入排序算法对列表进行排序——Leetcode系列(五)
- 使用迭代法对二叉树进行后序遍历——Leetcode系列(六)
- WEB打印系列教程之四--使用ScriptX进行复杂的WEB打印设置、使用jatoolsPrinter控件进行WEB打印设置
- 简单的QT绘图程序(把全部的点都记录下来,然后在paintEvent里使用drawLine函数进行绘制,貌似效率很低。。。)
- 《Visual C++ 2010入门教程》系列五:合理组织项目、使用外部工具让工作更有效(转)
- 使用Core Graphics 进行iOS绘图
- Bing Maps进阶系列九:使用MapCruncher进行地图切片并集成进Bing Maps
- 说说如何使用 canvas 进行 2D 绘图
- 使用VS进行工作流开发系列博客6-Developing Workflows in VS: Part 5 - Code Your Workflow
- 一步一步教你使用AgileEAS.NET基础类库进行应用开发-系列目录