1、示例环境:VS2010,MFC对话框工程,主要是对话框的OnPaint函数中

2、首先,一般大多数人比较熟悉的CPen的用法如下:

     CPaintDC dc(this);CPen pen1(PS_SOLID, 1, RGB(0, 0, 0));CPen* pOldPen = dc.SelectObject(&pen1);dc.MoveTo(50, 50);dc.LineTo(500, 50);dc.SelectObject(pOldPen);

3、会发现想要绘制粗细大于1的虚线(或是其它非PS_SOLID风格线条)时,绘制出来的都是实线:

<pre name="code" class="cpp">        CPaintDC dc(this);CPen pen1(PS_DASH, 2, RGB(0, 0, 0));CPen* pOldPen = dc.SelectObject(&pen1);dc.MoveTo(50, 50);dc.LineTo(500, 50);pen1.DeleteObject();dc.SelectObject(pOldPen);

4、下面介绍另一种CPen对象的创建方法,以解决上述问题:

(1)对话框OnPaint函数中添加示例代码:

     CPaintDC dc(this);LOGBRUSH logBrush;logBrush.lbStyle = BS_SOLID;logBrush.lbColor = RGB(0, 0, 0);CPen pen2(PS_DASH | PS_GEOMETRIC | PS_ENDCAP_ROUND, 2, &logBrush);CPen* pOldPen = dc.SelectObject(&pen2);dc.MoveTo(50, 50);dc.LineTo(500, 50);pen2.DeleteObject();dc.SelectObject(pOldPen);

(2)这样粗细大于1的虚线就画出来了:

5、下面是MSDN中的一段相关内容,英文好的话可以读一读:

CPen::CreatePen

This method initializes a pen with the specified style, width, and color. The pen can be subsequently selected as the current pen for any device context.

BOOL CreatePen (
int nPenStyle,
int nWidth,
COLORREF crColor ); 

Parameters

nPenStyle
Specifies the style for the pen. For a list of possible values, see the nPenStyle parameter in the CPen::CPen constructor.
nWidth
Specifies the width of the pen.

  • For the first version of CreatePen, if this value is zero, the width in device units is always 1 pixel, regardless of the mapping mode.
  • For the second version of CreatePen, if nPenStyle is PS_GEOMETRIC, the width is given in logical units. If nPenStyle is PS_COSMETIC, the width must be set to 1.
crColor
Contains an RGB color for the pen.

Return Value

Nonzero, or the handle of a logical pen, if successful; otherwise, it is zero.

Remarks

Pens that have a width greater than 1 pixel should always have either the PS_NULL, PS_SOLID, or PS_INSIDEFRAME style.

If a pen has the PS_INSIDEFRAME style and a color that does not match a color in the logical color table, the pen is drawn with a dithered color. The PS_SOLID pen style cannot be used to create a pen with a dithered color. The style PS_INSIDEFRAME is identical to PS_SOLID if the pen width is less than or equal to 1.

The width of a cosmetic pen is always 1; the width of a geometric pen is always specified in world units. After an application creates a logical pen, it can select that pen into a device context by calling the CDC::SelectObject function. After a pen is selected into a device context, it can be used to draw lines and curves.

  • If nPenStyle is PS_COSMETIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in style units. A style unit is defined by the device in which the pen is used to draw a line.
  • If nPenStyle is PS_GEOMETRIC and PS_USERSTYLE, the entries in the lpStyle array specify lengths of dashes and spaces in logical units.
  • If nPenStyle is PS_ALTERNATE, the style unit is ignored and every other pixel is set.

When an application no longer requires a given pen, it should call the CGdiObject::DeleteObject method or destroy the CPen object so the resource is no longer in use. An application should not delete a pen when the pen is selected in a device context.

Windows CE does not support the following overloaded implementation of the CPen::CreatePen method:

BOOL CreatePen(
int nPenStyle,
int nWidth,
const LOGBRUSH* pLogBrush,
int nStyleCount = 0,
const DWORD* lpStyle = NULL ); 

In Windows CE version 1.0, only solid pens can draw wide lines.

Example

CPen myPen1, myPen2;// Create a solid red pen of width 2.
myPen1.CreatePen(PS_SOLID, 2, RGB(255,0,0));// Create a geometric pen.
LOGBRUSH logBrush;
logBrush.lbStyle = BS_SOLID;
logBrush.lbColor = RGB(0,255,0);
myPen2.CreatePen(PS_DOT|PS_GEOMETRIC|PS_ENDCAP_ROUND, 2, &logBrush);

Requirements

  Windows CE versions: 1.0 and later  
  Header file: Declared in Afxwin.h
  Platform: H/PC Pro, Palm-size PC, Pocket PC

6、最后,介绍一些CPen风格,以作备份:

(1)画笔的样式:

PS_SOLID      -- 实线画笔
PS_DASH       -- 虚线画笔, 只有当画笔宽度为1或更小(以设备单位计算)时才有效
PS_DOT        -- 点线画笔, 只有当画笔宽度为1或更小(以设备单位计算)时才有效
PS_DASHDOT    -- 虚线和点交替, 只有当画笔宽度为1或更小(以设备单位计算)时才有效
PS_DASHDOTDOT -- 创建一支虚线和两点交替的画笔。只有当画笔宽度为1或更小(以设备单位计算)时才有效
PS_NULL       -- 空画笔
PS_INSIDEFRAME-- 创建一支画笔,该画笔在Windows GDI输出函数所产生的封闭形状的框架内画线,此输出函数指定一个限定矩形(例如,Ellipse,Rectangle,RoundRect,Pie和Chord成员函数),当此风格用于没有指定限定矩形的Windows GDI输出函数(例如LineTo成员函数)时,此画笔的绘制区域不受框架的限制

PS_GEOMETRIC -- 几何画笔
PS_COSMETIC  -- 装饰画笔
PS_ALTERNATE -- 创建一支交替设置像素的画笔(此风格只用于装饰画笔)  
PS_USERSTYLE -- 创建一支使用用户提供的风格数组的画笔

(2)笔帽的样式:

PS_ENDCAP_ROUND   -- 尾帽是圆的
PS_ENDCAP_SQUARE  -- 尾帽是方的
PS_ENDCAP_FLAT    -- 尾帽是平面的(注: 没有笔帽)

(3)连接的样式:

PS_JOIN_BEVEL -- 连接是斜截式的
PS_JOIN_MITER -- 当连接在::SetMiterLimit函数所设置的当前限制之内时, 连接是斜接式的. 如果连接超出这个限制则成为斜截式的
PS_JOIN_ROUND -- 连接是圆的

CPen绘制线条宽度大于1 的虚线相关推荐

  1. 使用钢笔绘制线条和形状

    一.如何:使用钢笔绘制线条 若要绘制线条,需要 Graphics 对象和 Pen 对象.Graphics 对象提供 DrawLine 方法,而 Pen 对象则存储线条的特征,如颜色和宽度. 示例 下面 ...

  2. GDI基础函数使用绘制线条和图形

    1. 绘制一个像素点:SetPixel(). 绘制直线:MoveTo(),LineTo(). 绘制多个首尾相连的线:Polyline(). 绘制矩形:FrameRect(),Rectangle(),F ...

  3. GDI基础(1):绘制线条和图形

    1. 绘制一个像素点:SetPixel(). 绘制直线:MoveTo(),LineTo(). 绘制多个首尾相连的线:Polyline(). 绘制矩形:FrameRect(),Rectangle(),F ...

  4. python PyQt5中文教程☞【第十节】PyQt5绘图(绘制文本drawText()、画点drawPoints()、设置颜色、QPen(画笔)绘制线条、QBrush(笔刷)绘制纹理

    引用文章:http://code.py40.com/pyqt5/32.html 文章目录 绘制文本 画点 PyQt5颜色 QPen(画笔) QBrush(笔刷) 总结:一发现有事件触发就会更新QWid ...

  5. iOS 动画绘制线条颜色渐变的折线图

    效果图 .................... 概述 现状 折线图的应用比较广泛,为了增强用户体验,很多应用中都嵌入了折线图.折线图可以更加直观的表示数据的变化.网络上有很多绘制折线图的demo,有 ...

  6. canvas 边界模糊_Canvas绘制线条模糊的解决方案

    标签: 前段时间,做一个跨平台app项目,需要绘制分时图和K线图.找了很多开源的js的图表库,包括echarts.highcharts等等,都不是很满意,原因有2: 1.太臃肿,我实际上只要一个分时和 ...

  7. gnuplot绘图程序中对线型(linetype)、点型(pointtype)、线条宽度(linewidth)、点大小(pointsize)、图样

    作者:magj77 原作网址:http://blog.sina.com.cn/s/blog_6a5f098201013tvb.html 在NS2的gnuplot绘图程序中对线型(linetype).点 ...

  8. VC++ 绘制线条 OnLButtonDown函数(DrawView.cpp) 利用SDK全局函数实现画线功能 利用MFC的CDC类实现画线功能 利用MFC的CClientDC类实现画线功能

    目录 绘制线条 OnLButtonDown函数(DrawView.cpp) 利用SDK全局函数实现画线功能 利用MFC的CDC类实现画线功能 利用MFC的CClientDC类实现画线功能 接上:VC+ ...

  9. android 编辑画线,如何在android中用手指在imageview上绘制线条

    您应该覆盖视图的onDraw方法. screen_drawing_room.xml android:id="@+id/rlid" android:layout_width=&quo ...

最新文章

  1. linux误删视频恢复吗,linux 误删文件恢复
  2. Python变量本质
  3. 循环语句练习题2(打印三角形和菱形)
  4. python语法类似php吗_深入学习PHP还是Python
  5. Google 程序员消灭 Bug 的 5 大法宝!
  6. (附源码)Flutter Demo
  7. C语言水仙花数和总数,C语言与水仙花数
  8. 理解 Linux 配置文件【转】
  9. VC6.0(VC++6.0)下载地址和安装教程(图解)
  10. mysql alter 添加注释_我们如何使用MySQL ALTER TABLE命令在列上添加注释?
  11. 代码 生成迅雷链接启动迅雷下载
  12. python爬虫教材推荐 豆瓣_学习python爬虫推荐书籍
  13. 思科网络综合实验(服务器,三层交换机,路由器)
  14. Unity3d+倾斜摄影技术应用
  15. 微信公众平台素材编辑与自动回复图文教程
  16. re学习笔记(0) CG-CTF-re-3 py交易
  17. 图片或者其他文件在数据库的存储方式详解
  18. 快速实施中小企业ERP 不再繁冗而复杂
  19. 服务器常见错误代码 总结
  20. 【腾讯云 Finops Crane 集训营】Finops Crane-英雄之剑

热门文章

  1. mysql获取时间部分_mysql中取日期的一部分
  2. Java、JSP、框架基于JavaEE的数字化养老院管理系统900
  3. tf 风格迁移 任意内容 固定风格 vgg19
  4. 微信小程序的入门笔记(一)
  5. 微信支付收款码快速开通方法
  6. android高德地图改变绘制点标记位置,绘制点标记-在地图上绘制-开发指南-Android 室内地图SDK | 高德地图API...
  7. wms仓储管理的特点与难点是什么?
  8. 用C#实现模拟双色球中奖控制台应用程序
  9. 2005年MBA考试英语试题
  10. 做产品的你可知道,抖音在如何吸引着你?