绘制基本图形和线型(StrokeStyle)的设置详解

目前,在博客园上,相对写得比较好的两个关于Direct2D的教程系列,分别是万一Direct2D系列和zddDirect2D系列。有兴趣的网友可以去看看。本系列也是介绍Direct2D的教程,是基于Windows API Code Pack 1.1的Direct2D的教程,如果要调试文中的代码的话,还得参考前文 Direct2D教程I——简介及首个例子 下载导入Windows API Code Pack 1.1的动态库

在前文 Direct2D教程I——简介及首个例子 简单介绍了Direct2D,并给了一个简单的示例。接下来,本文对一些基本的绘图命令做个介绍

绘制基本图形:直线、矩形、圆角矩形、椭圆

和GDI+类似,在RenderTarget对象下有各个绘图命令(GDI+是在Graphics对象下有各个绘图命令),下面是各个基本图形的绘图命令的函数原型

直线: 
Public Sub DrawLine(firstPoint As Direct2D1.Point2F, secondPoint As Direct2D1.Point2F, brush As Direct2D1.Brush, skrokeWidth As Single, strokeStyle AsDirect2D1.StrokeStyle)

矩形: 
Public Sub DrawRectangle(rect As Direct2D1.RectF, brush As Direct2D1.Brush, strokeWidth As Single)  
Public Sub DrawRectangle(rect As Direct2D1.RectF, brush As Direct2D1.Brush, strokeWidth As Single, strokeStyle As Direct2D1.StrokeStyle)  
Public Sub FillRectangle(rect As Direct2D1.RectF, brush As Direct2D1.Brush)

圆角矩形: 
Public Sub DrawRoundedRectangle(roundedRect As Direct2D1.RoundedRect, brush As Direct2D1.Brush, strokeWidth As Single) 
Public Sub DrawRoundedRectangle(roundedRect As Direct2D1.RoundedRect, brush As Direct2D1.Brush, strokeWidth As Single, strokeStyle As Direct2D1.StrokeStyle)  
Public Sub FillRoundedRectangle(roundedRect As Direct2D1.RoundedRect, brush As Direct2D1.Brush)

椭圆: 
Public Sub DrawEllipse(ellipse As Direct2D1.Ellipse, brush As Direct2D1.Brush, strokeWidth As Single) 
Public Sub DrawEllipse(ellipse As Direct2D1.Ellipse, brush As Direct2D1.Brush, strokeWidth As Single, strokeStyle As Direct2D1.StrokeStyle)  
Public Sub FillEllipse(ellipse As Direct2D1.Ellipse, brush As Direct2D1.Brush)

从上面的函数原型可以看出,以Draw开头的函数都是绘制函数。以Fill开头的函数都是填充函数。绘制函数的线宽由strokeWidth参数指定,绘制函数的线型由strokeStyle参数指定(默认是实线)。和GDI+不同的是,在Direct2D中,不再区分Brush(画刷)和Pen(画笔)对象,而统一用Brush(画刷)对象,在用Brush(画刷)绘制线的时候再指定线宽和线型参数。

从参数strokeWidth和参数strokeStyle来看,都带有单词stroke。熟悉PS的都知道,stroke指的是描边,在PS中描边的位置分为“外部”、“内部”、“居中”。那么在Direct2D中,这里的描边的位置在哪儿呢?我们用代码实验一下。

说明:为了代码的复用,我们把前文中的_d2DFactory和_renderTarget修饰限定词从Private改为Protected。这样在后面的例子中直接继承前文的类,也可以直接用_d2DFactory和_renderTarget这两个对象。

先给出Point2F、RectF、RoundedRect、Ellipse这四个类的原型定义,参数都简介明了,不再详述

Direct2D1.Point2F(x As Single, y As Single) 
Direct2D1.RectF(left As Single, top As Single, right As Single, buttom As Single) 
Direct2D1.RoundedRect(rect As Direct2D1.RectF, radiusX As Single, radiusY As Single) 
Direct2D1.Ellipse(point As Direct2D1.Point2F, radiusX As Single, radiusY As Single)

下面是实验示例代码

Public Class clsDirect2DSample2 
    Inherits clsDirect2DSample

Public Shadows Sub Render() 
        If Not _renderTarget Is Nothing Then

With _renderTarget 
                .BeginDraw()

Dim F1 As New Direct2D1.RectF(0, 0, 100, 50) 
                Dim F2 As New Direct2D1.RectF(200, 200, 300, 250) 
                Dim B As Direct2D1.SolidColorBrush = _renderTarget.CreateSolidColorBrush(New Direct2D1.ColorF(1, 0, 0))

_renderTarget.DrawRectangle(F1, B, 10) 
                _renderTarget.DrawRectangle(F2, B, 10)

.EndDraw() 
            End With

End If 
    End Sub

End Class

从上图的代码来看,在(0,0)位置上画了一个100*50的矩形,描边宽度10px。在(200,200)位置上画了一个矩形,描边宽度10px,如下图所示

从上面的效果可以看出两点:

一是左上角的矩形说明,Direct2D中的描边类型应该是“居中”;

二是右边的矩形说明Direct2D中的RectF定义和原本GDI+中的RectangleF定义不同,Direct2D中的RectF中后两个参数是右下角的坐标,而GDI+中的RectangleF中的后两个参数指的是矩形的宽和高。

线型(StrokeStyle)的设置

在上面的函数原型中,线型是由参数strokeStyle决定的,它是StrokeStyle类。这个类不能直接初始化,必须由D2DFactory对象的CreateStrokeStyle函数创建(Direct2D中有不少的类不能直接初始化,必须由D2DFactory或RenderTarget对象的相应的函数创建,如上面的SolidColorBrush对象就必须由RenderTarget对象的CreateSolidColorBrush函数创建)。

先看看CreateStrokeStyle函数的原型定义: 
Public Function CreateStrokeStyle(strokeStyleProperties As Direct2D1.StrokeStyleProperties) As Direct2D1.StrokeStyle 
Public Function CreateStrokeStyle(strokeStyleProperties As Direct2D1.StrokeStyleProperties, dashes() As Single) As Direct2D1.StrokeStyle

主要是通过strokeStyleProperties参数来设置线型,来看看StrokeStyleProperties结构及其参数对象的原型定义。

Direct2D1.StrokeStyleProperties(startCap As Direct2D1.CapStyle, endCap As Direct2D1.CapStyle, dashCap As Direct2D1.CapStyle, _ 
                                            lineJoin As Direct2D1.LineJoin, miterLimit As Single, _ 
                                            dashStyle As Direct2D1.DashStyle, dashOffset As Single) 
Public Enum CapStyle 
    Flat = 0 
    Square = 1 
    Round = 2 
    Triangle = 3 
End Enum 
Public Enum LineJoin 
    Miter = 0 
    Bevel = 1 
    Round = 2 
    MiterOrBevel = 3 
End Enum 
Public Enum DashStyle 
    Solid = 0 
    Dash = 1 
    Dot = 2 
    DashDot = 3 
    DashDotDot = 4 
    Custom = 5 
End Enum

其中CapStyle枚举指的是线端的线头类型,分别是Flat(平整,即无)、Square(方块)、Round(圆)、Triangle(三角)。和GDI+中的DashCap枚举类似,多了一个Square(方块),而在下面的例子中会说明Flat和Square的区别。参数startCap决定线起点的线头类型、endCap决定线终点的线头类型、dashCap决定中间划线端的线头类型。

而DashStyle枚举指的是线型,分别是Solid(实线)、Dash(划线)、Dot(点线)、DashDot(点划线)、DashDotDot(点点划线)、Custom(自定义)。和GDI+中的DashStyle一样。

参数dashOffset指的是点划线的偏移量

下面的代码是对上面的两个枚举的演示,请注意枚举CapStyle中的Flat和Square的区别。

Public Class clsDirect2DSample3 
    Inherits clsDirect2DSample

Public Shadows Sub Render() 
        If Not _renderTarget Is Nothing Then

With _renderTarget 
                .BeginDraw()

Dim B As Direct2D1.SolidColorBrush = _renderTarget.CreateSolidColorBrush(New Direct2D1.ColorF(1, 0, 0)) 
                Dim SP As New Direct2D1.StrokeStyleProperties() 
                Dim S As Direct2D1.StrokeStyle

SP.StartCap = Direct2D1.CapStyle.Flat 
                SP.EndCap = Direct2D1.CapStyle.Flat 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 20), New Direct2D1.Point2F(200, 20), B, 8, S)

SP.StartCap = Direct2D1.CapStyle.Square 
                SP.EndCap = Direct2D1.CapStyle.Square 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 40), New Direct2D1.Point2F(200, 40), B, 8, S)

SP.StartCap = Direct2D1.CapStyle.Round 
                SP.EndCap = Direct2D1.CapStyle.Round 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 60), New Direct2D1.Point2F(200, 60), B, 8, S)

SP.StartCap = Direct2D1.CapStyle.Triangle 
                SP.EndCap = Direct2D1.CapStyle.Triangle 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 80), New Direct2D1.Point2F(200, 80), B, 8, S)

SP.StartCap = Direct2D1.CapStyle.Flat 
                SP.EndCap = Direct2D1.CapStyle.Flat 
                SP.DashCap = Direct2D1.CapStyle.Round

SP.DashStyle = Direct2D1.DashStyle.Solid 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 120), New Direct2D1.Point2F(200, 120), B, 6, S)

SP.DashStyle = Direct2D1.DashStyle.Dash 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 140), New Direct2D1.Point2F(200, 140), B, 6, S)

SP.DashStyle = Direct2D1.DashStyle.Dot 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 160), New Direct2D1.Point2F(200, 160), B, 6, S)

SP.DashStyle = Direct2D1.DashStyle.DashDot 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 180), New Direct2D1.Point2F(200, 180), B, 6, S)

SP.DashStyle = Direct2D1.DashStyle.DashDotDot 
                S = _d2DFactory.CreateStrokeStyle(SP) 
                _renderTarget.DrawLine(New Direct2D1.Point2F(10, 200), New Direct2D1.Point2F(200, 200), B, 6, S)

.EndDraw() 
            End With 
        End If 
    End Sub 
End Class

上面四条线是演示枚举CapStyle的四种类型,注意第一条线是Flat,第二条线是Square。虽然都是方形,但是很明显第二条线比第一条线两边还多出一点(方形线帽)

下面五条线是演示枚举DashStyle表示的五个线型。注意:上面代码中红色的部分,DashCap不能设置为Direct2D1.CapStyle.Flat,如果设置为Flat,则所有的点都不见了;设置其他三种类型分别代表不同的点(方形点、圆点、菱形点)。

再说说枚举DashStyle中的Custom线型。设定为Custom后,必须用一个数组来指定自定义划线的线型。数组按照{实、空、实、空……}顺序来指定各部分的长度,单位是线宽。

例如{2,2}表示2线宽长的实线和2线宽长的空白组成的划线,也就是枚举DashStyle中的Dash

{0,2}表示0线宽长的实线和2线宽长的空白组成的划线,就是枚举DashStyle中的Dot。这也解释了DashCap设置为Direct2D1.CapStyle.Flat时,为何点不见了(设置为其他值时,点实际上是由线头组成的,点本身的长度为0)。

以此类推,{2,2,0,2}表示DashStyle中的DashDot,{2,2,0,2,0,2}表示DashStyle中的DashDotDot

要注意的是,只有枚举DashStyle设置为Custom时,才能传递数组,否则会直接报错的。

最后说说LineJoin枚举,指的是连接两条线的连接方式。分别是Miter(折角)、Bevel(倒角)、Round(圆角)、MiterOrBevel(折角或倒角)。和GDI+中的LineJoin枚举类似,只是MiterOrBevel好像对应GDI+中LineJoin枚举中的MiterClipped。

说说MiterOrBevel,当折角的值没有超过指定的值(系统指定)时,是Miter(折角),反之超过的话是Bevel(倒角)。

参数miterLimit是指折角的限制,低于限制时会自动添加倒角。参数miterLimit最小是1。注意的是miterLimit参数并不是配合LineJoin使用。当miterLimit设置为1的时候,下面的例子中,LineJoin枚举设置成Miter、Bevel、MiterOrBevel效果是一样的。当miterLimit设置为3的时候,LineJoin枚举设置成Miter、Bevel、MiterOrBevel效果才是不一样的

下面是LineJoin的示例,由于是演示两条线的连接,故用了一些后面才会详述的代码。不过,不影响我们理解枚举LineJoin的各个设置值的意义。

左上是Miter,右上是Bevel,左下是Round,右下是MiterOrBevel

  
Public Class clsDirect2DSample4 
    Inherits clsDirect2DSample

Public Shadows Sub Render() 
        If Not _renderTarget Is Nothing Then

With _renderTarget 
                .BeginDraw()

Dim B As Direct2D1.SolidColorBrush = _renderTarget.CreateSolidColorBrush(New Direct2D1.ColorF(1, 0, 0)) 
                Dim SP As New Direct2D1.StrokeStyleProperties() 
                Dim S As Direct2D1.StrokeStyle

Dim PG As Direct2D1.PathGeometry 
                Dim sink As Direct2D1.GeometrySink

PG = _d2DFactory.CreatePathGeometry 
                sink = PG.Open

sink.BeginFigure(New Direct2D1.Point2F(17, 17), Direct2D1.FigureBegin.Hollow)

sink.AddLine(New Direct2D1.Point2F(45, 85)) 
                sink.AddLine(New Direct2D1.Point2F(85, 45)) 
                sink.AddLine(New Direct2D1.Point2F(85, 125)) 
                sink.AddLine(New Direct2D1.Point2F(165, 17))

sink.EndFigure(Direct2D1.FigureEnd.Open) 
                sink.Close()

SP.LineJoin = Direct2D1.LineJoin.Miter 
                SP.MiterLimit = 3 
                S = _d2DFactory.CreateStrokeStyle(SP)

.DrawGeometry(PG, B, 10, S)

PG = _d2DFactory.CreatePathGeometry 
                sink = PG.Open

sink.BeginFigure(New Direct2D1.Point2F(217, 17), Direct2D1.FigureBegin.Hollow)

sink.AddLine(New Direct2D1.Point2F(245, 85)) 
                sink.AddLine(New Direct2D1.Point2F(285, 45)) 
                sink.AddLine(New Direct2D1.Point2F(285, 125)) 
                sink.AddLine(New Direct2D1.Point2F(365, 17))

sink.EndFigure(Direct2D1.FigureEnd.Open) 
                sink.Close()

SP.LineJoin = Direct2D1.LineJoin.Bevel 
                SP.MiterLimit = 3 
                S = _d2DFactory.CreateStrokeStyle(SP)

.DrawGeometry(PG, B, 10, S)

PG = _d2DFactory.CreatePathGeometry 
                sink = PG.Open

sink.BeginFigure(New Direct2D1.Point2F(17, 217), Direct2D1.FigureBegin.Hollow)

sink.AddLine(New Direct2D1.Point2F(45, 285)) 
                sink.AddLine(New Direct2D1.Point2F(85, 245)) 
                sink.AddLine(New Direct2D1.Point2F(85, 325)) 
                sink.AddLine(New Direct2D1.Point2F(165, 217))

sink.EndFigure(Direct2D1.FigureEnd.Open) 
                sink.Close()

SP.LineJoin = Direct2D1.LineJoin.Round 
                SP.MiterLimit = 3 
                S = _d2DFactory.CreateStrokeStyle(SP)

.DrawGeometry(PG, B, 10, S)

PG = _d2DFactory.CreatePathGeometry 
                sink = PG.Open

sink.BeginFigure(New Direct2D1.Point2F(217, 217), Direct2D1.FigureBegin.Hollow)

sink.AddLine(New Direct2D1.Point2F(245, 285)) 
                sink.AddLine(New Direct2D1.Point2F(285, 245)) 
                sink.AddLine(New Direct2D1.Point2F(285, 325)) 
                sink.AddLine(New Direct2D1.Point2F(365, 217))

sink.EndFigure(Direct2D1.FigureEnd.Open) 
                sink.Close()

SP.LineJoin = Direct2D1.LineJoin.MiterOrBevel 
                SP.MiterLimit = 3 
                S = _d2DFactory.CreateStrokeStyle(SP)

.DrawGeometry(PG, B, 10, S)

.EndDraw() 
            End With

End If 
    End Sub

End Class

下面是效果图

从上图能看出四种类型的不同。左上是Miter,右上是Bevel,左下是Round,右下是MiterOrBevel

绘制基本图形和线型(StrokeStyle)的设置详解的更多相关文章

  1. Jupyter自定义设置详解

    今天专门花时间总结梳理一下jupyter的一些高级设置,jupyter我已经介绍过一次基本内容了,Setup and Linux | James Chen's Blogs,尤其是如何在服务器运行jup ...

  2. 【转】Eclipse Java注释模板设置详解

    Eclipse Java注释模板设置详解   设置注释模板的入口: Window->Preference->Java->Code Style->Code Template 然后 ...

  3. Win7 NFS 设置详解 | X-Space

    Win7 NFS 设置详解 | X-Space Win7 NFS 设置详解

  4. [转]JAVA环境变量JAVA_HOME、CLASSPATH、PATH设置详解

    [转] JAVA环境变量JAVA_HOME.CLASSPATH.PATH设置详解 - dreamman的日志 - 网易博客http://blog.163.com/dreamman_yx/blog/st ...

  5. fiddler软件测试——Fiddler抓取https设置详解(图文)(摘抄)

    随笔- 8  文章- 0  评论- 0 fiddler软件测试——Fiddler抓取https设置详解(图文)   强烈推荐(原创亲测)!!!Fiddler抓取https设置详解(图文)转 本文主要说 ...

  6. fiddler软件测试——Fiddler抓取https设置详解(图文)

    强烈推荐(原创亲测)!!!Fiddler抓取https设置详解(图文)转 本文主要说明了自己在设置fiddler抓取https过程中所遇到的问题及解决步骤,特别是fiddler在设置证书的环节遇到的各 ...

  7. MySQL高可用架构之Mycat-关于Mycat安装和参数设置详解

    MySQL高可用架构之Mycat-关于Mycat安装和参数设置详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Mycat介绍 1>.什么是Mycat Mycat背后是 ...

  8. 【转载】 Eclipse注释模板设置详解

     Eclipse注释模板设置详解 网站推荐: 金丝燕网(主要内容是 Java 相关) 木秀林网(主要内容是消息队列)

  9. loadrunner 运行脚本-Run-time Settings-Browser Enmulation设置详解

    运行脚本-Run-time Settings-Browser Enmulation设置详解 by:授客 QQ:1033553122 浏览器模拟 所有Internet Vuser Header包含一个标 ...

随机推荐

  1. CSS3+HTML5特效5 - 震动的文字

    先看效果(把鼠标移上去看看) abcd 这个效果很简单,就是移动文字的位置模拟出震动的效果. Css <style> @-webkit-keyframes shake { 0%{ -web ...

  2. 剖析Jetty实现原理

    之前写一个简单易用Jetty文章.Jetty对于做JAVA Web发展的方面来说并不陌生,他是一个servlet集装箱,只有相对Tomcat这是比较简单的设计,并且也相对简单,使用灵活,我是学习和使用 ...

  3. java 中关于json的使用方法

    json在数据传输起了很大的作用,下面说说java中json的使用方法. 文章参考:http://www.codes51.com/article/detail_99574.html json串示例 [ ...

  4. 开源Dubbox

    当当网开源Dubbox https://github.com/dangdangdotcom/dubbox https://github.com/alibaba/dubbo http://www.inf ...

  5. WebService使用JSON格式传递笔记+JQuery测试

    原文WebService使用JSON格式传递笔记+JQuery测试 因为一些因素,必须改写WebService,很传统,但是很多公司还在用.. 因为XML 的关系,不想让他传递数据的时候过度肥大,所以 ...

  6. Java阅读word程序说明文件

    完成office文件操作可以帮助apache.poi包(我用poi-3.10-FINAL),导入对应的jar包(最好所有导入) 以下的程序演示了一些操作word的过程,具体的函数功能能够查看此包的官方 ...

  7. OpenStreetMap架构

    OpenStreetMap框架简介 1.OSM平台开发 OpenStreetMap(缩写OSM)地图是一个合作项目,我们的目标是创建一个免费的内容,让所有的人都可以编辑的世界地图. OSM在地图上由一 ...

  8. Asterisk 未来之路3.0_0002

    原文:Asterisk 未来之路3.0_0002 伟大的变化需要可扩展性技术 每一个现有的PBX都因为其自身的缺点变的糟糕,不管其功能如何丰富,总有一些东西会漏掉.具备非常完全功能的PBX 也不能预见 ...

  9. [DevExpress]利用LookUpEdit实现类似自动提示效果

    原文:[DevExpress]利用LookUpEdit实现类似自动提示效果 关键代码: public static void BindWithAutoCompletion(this LookUpEdi ...

  10. Xamarin移动跨平台解决方案是如何工作

    Xamarin移动跨平台解决方案是如何工作的? 概述 上一篇 C#移动跨平台开发(1)环境准备发布之后不久,无独有偶,微软宣布了开放.NET框架源代码并且会为Windows.Mac和Linux开发一个 ...