zoukankan      html  css  js  c++  java
  • GDI+编程(画笔/画刷/路径/区域)

    构造Graphics对象

    Graphics类是GDI+程序设计的核心,Graphics类能够完成大部分的绘图,文本输出,几何图形的填充及坐标系统的转换等各种操作.在功能上,它与GDI的设备环境(DC)一致.

    Graphics类有下列几种构造函数:

    1. static Graphics* FromHDC(HDG hdc);
    2. static Graphics* FromHDC(HDG hdc, HANDLE hDevice);
    3. static Graphics* FromHWND(HWND hWnd, BOOL icm);
    4. static Graphics* FromImage(Image* image);

    hdc:设备环境句柄.

    hWnd:窗口句柄.

    icm:是否使用色彩配置文件校正色彩.

    image:图像对象.

    Hdevice:设备句柄.

    --------------------------------------------------------------------------------------------------

    画笔

    GDI+通过Pen类来定义一个画笔,Pen类的构造函数有以下两种:

    1. Pen(color, width)
    2. Pen(brush, width)

    第二种为使用指定的画刷来构造一个画笔.

    画笔的线型

    Pen类成员函数SetDashStyle(设置画笔线程)可以设置画笔的线型.GDI+在DashStyle(线程)枚举中提供了5个常见的画笔线型风格.

    1. enum DashStyle{
    2.     DashStyleSolid = 0,       // 实线
    3.     DashStyleDash = 1,        // 虚线
    4.     DashStyleDot = 2,         // 点线
    5.     DashStyleDashDot = 3,     // 点划线
    6.     DashStyleDashDotDot = 4,  // 双点划线
    7.     DashStyleCustom = 5       // 自定义线(略...)
    8. };

    画笔的对齐方式

    Pen类成员函数SetAlignment(画笔对齐)可以设置画笔的对齐方式.PenAlignment枚举有两种对齐方式:

    1. enum PenAlignment{
    2.     PenAlignmentCenter = 0,   // 居中(默认)
    3.     PenAlignmentInset = 1,    // 嵌入
    4. };

    画笔的缩放及旋转

    例子:

    1. Pen pen(Color(255,0,0,0),4);  // 定义一支宽度为4的画笔
    2. pen.ScaleTransform(1,6);      //  将画笔垂直方向上扩充6倍,水平不变.
    3. graphics.DrawEllipse(&pen,10,10,100,100);    // 画一个圆
    4. pen.RotateTransform(60,MatrixOrderAppend);   // 将画笔旋转60度
    5. graphics.DrawEllipse(&pen,120,10,100,100);   // 画一个圆
    6. pen.RotateTransform(60,MatrixOrderAppend);   // 将画笔再旋转60度
    7. graphics.DrawEllipse(&pen,230,10,100,100);   // 画一个圆

    画笔的线帽属性

    线帽(LineCap)就是线条首尾的外观,默认情况下为方形.设置线帽函数有两个:

    1. SetSTARTCap(起点线帽)
    2. SetEndCap(终点线帽)

    不同外观的线帽由LineCap枚举列出:

    1. enum LineCap{
    2.     LineCapFlat = 0,
    3.     LineCapSquare = 1,
    4.     LineCapRound = 2,
    5.     LineCapTriangle = 3,
    6.     LineCapNoAnchor = 0x10,
    7.     LineCapSquareAnchor = 0x11,
    8.     LineCapRoundAnchor = 0x12,
    9.     LineCapDiamondAnchor = 0x13,
    10.     LineCapArrowAnchor = 0x14,
    11.     LineCapCustom = 0xff
    12. };

    线帽图:http://photo.blog.sina.com.cn/photo/7c773cc5gbcfd89aa2dcc

    直线的连接点属性

    直线连接点是两条端点重合或重叠的直线形成的公共区域.

    函数为SetLineJoin(连接点),连接点LineJoin枚举为:

    1. enum LineJoin{
    2.     LineJoinMiter = 0,         // 斜接
    3.     LineJoinBevel = 1,         // 斜切
    4.     LineJoinRound = 2,         // 圆形
    5.     LineJoinMiterClipped = 3,  // 剪裁斜接
    6. };

    --------------------------------------------------------------------------------------------------

    直线

    6种绘制直线函数:

    1. DrawLine(Pen* pen, Point& pt1, Point& pt2)
    2. DrawLine(Pen* pen, PointF& pt1, PointF& pt2)
    3. DrawLine(Pen* pen, INT x1, INT y1, INT x2, INT y2)
    4. DrawLine(Pen* pen, REAL x1, REAL y1, REAL x2, REAL y2)
    5. DrawLines(Pen* pen, Point* points, INT count)
    6. DrawLines(Pen* pen, PointF* points, INT count)

    pt1,pt2:线段起止点位置.

    x1,y1,x2,y2:线段起止点坐标.

    points:直线端点数组.

    count:线段定义点总数.

    例子:

    1. Graphics graphics(hWnd);                     // 在窗口中创建Graphics对象
    2. Pen blackPen(Color(255,0,0,0),3);            // 创建画笔
    3. graphics.DrawLine(&blackPen,10,10,100,100);  // 绘制线条

    -------------------------------------------------

    矩形

    6种绘制矩形函数:

    1. DrawRectangle(Pen* pen, Rect& rect)
    2. DrawRectangle(Pen* pen, RectF& rect)
    3. DrawRectangle(Pen* pen, INT x, INT y, INT width, INT height)
    4. DrawRectangle(Pen* pen, REAL x, REAL y, REAL width, REAL height)
    5. DrawRectangles(Pen* pen, Rect& rect, INT count)
    6. DrawRectangles(Pen* pen, RectF& rect, INT count)

    -------------------------------------------------

    椭圆

    4种绘制椭圆函数:

    1. DrawEllipse(Pen* pen, Rect& rect)
    2. DrawEllipse(Pen* pen, RectF& rect)
    3. DrawEllipse(Pen* pen, INT x, INT y, INT width, INT height)
    4. DrawEllipse(Pen* pen, REAL x, REAL y, REAL width, REAL height)

    -------------------------------------------------

    简单曲线和贝塞尔曲线

    普通曲线分DrawCurve和DrawClosedCurve(封闭曲线)两种.

    常见的调用方法:

    1. DrawCurve(Pen* pen, Point* points, INT count)
    2. DrawCurve(Pen* pen, Point* points, INT count, REAL tension)

    tension:曲线的弯曲强度,弯曲强度越小,曲线的弯曲程度也越小.

    例子:

    1. Graphics graphics(hWnd);
    2. Pen blackPen(Color(255,0,0,0),3);
    3. Point point1(50, 50);
    4. Point point2(200, 50);
    5. Point point3(80, 150);
    6. Point point4(125, 0);
    7. Point point5(155, 150);
    8. Point point6(50, 50);
    9. Point points[6] = {point1,point2,point3,point4,point5,point6};
    10. graphics.DrawCurve(&blackPen,points,6);

    贝塞尔曲线(由1个起点2个控制点和1点终点组成):

    1. DrawBezier(Pen* pen, Point& pt1, Point& pt2, Point& pt3, Point& pt4)

    -------------------------------------------------

    多边形

    3条或更多条直边组成的闭合图形,函数如下:

    1. DrawPolygon(Pen* pen, Point* points, INT count)
    2. DrawPolygon(Pen* pen, PointF* points, INT count)

    -------------------------------------------------

    孤线

    1. DrawArc(Pen* pen, Rect& rect, REAL startAngle, REAL sweepAngle)
    2. DrawArc(Pen* pen, RectF& rect, REAL startAngle, REAL sweepAngle)
    3. DrawArc(Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
    4. DrawArc(Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)

    startAngle:从x轴到孤线的起始点沿顺时针方向度量的角(以度为单位).

    sweepAngle:从startAngle参数到孤线的结束点沿顺时针方向度量的角(以度为单位).

    -------------------------------------------------

    扇形

    1. DrawPie(Pen* pen, Rect& rect, REAL startAngle, REAL sweepAngle)
    2. DrawPie(Pen* pen, RectF& rect, REAL startAngle, REAL sweepAngle)
    3. DrawPie(Pen* pen, INT x, INT y, INT width, INT height, REAL startAngle, REAL sweepAngle)
    4. DrawPie(Pen* pen, REAL x, REAL y, REAL width, REAL height, REAL startAngle, REAL sweepAngle)

    --------------------------------------------------------------------------------------------------

    画刷

    通过SolidBrush类来定义一个单色画刷,只有一种构造函数:

    1. SolidBrush greenBrush(Color(255,0,255,0));

    使用单色画刷填充图形区域时,GDI+提供了以下几种填充函数:

    1. FillClosedCurve:填充闭合曲线.
    2. FillEllipse:填充椭圆.
    3. FillPath:填充路径.
    4. FillPie:填充扇形.
    5. FillPolygon:填充多边形.
    6. FillRectangle:填充矩形.
    7. FillRectangles:填充矩形集.
    8. FillRegion:填充区域.

    上述8个填充函数同样适用于其他类型的画刷.

    -------------------------------------------------

    影线画刷

    通过HatchBrush类来定义一个影线画刷,其构造函数为:

    1. HatchBrush(HatchStyle hatchStyle, const Color& foreColor, const Color& backColor);

    hatchStyle:影线画刷的类型,HatchStyle枚举提供了53种风格的影线画刷类型.

    foreColor:影线画刷线条的前景色.

    backColor:影线画刷线条的背景色.

    例子:

    1. Color black(255,0,0,0);         // 前景为黑色.
    2. Color white(255,255,255,255);   // 背景为白色.
    3. for(int i=0;i<53;i++)
    4.     HatchBrush brush(HatchStyle(i), black, white);  // 创建53种影线画刷

    画刷绘制原点

    1. SetRenderingOrigin(INT x, INT y);

    -------------------------------------------------

    纹理画刷

    创造一个纹理画刷,需要进行以下3步工作.

    (1)定制纹理画刷使用的基本图案(图形).

    (2)设定画刷图片的大小.

    (3)确定基本图案的排列方式.

    使用TextureBrush类来构造纹理画刷.TextureBrush类的构造函数有以下7种:

    1. TextureBrush(Image* image, Rect& dstRect, ImageAttributes* imageAttributes)
    2. TextureBrush(Image* image, RectF& dstRect, ImageAttributes* imageAttributes)
    3. TextureBrush(Image* image, WrapMode wrapMode)
    4. TextureBrush(Image* image, WrapMode wrapMode, Rect& dstRect)
    5. TextureBrush(Image* image, WrapMode wrapMode, RectF& dstRect)
    6. TextureBrush(Image* image,WrapMode wrapMode,INT dstX,INT dstY,INT dstWidth,INT dstHeight)
    7. TextureBrush(Image* image,WrapMode wrapMode,REAL dstX,REAL dstY,REAL dstWidth,REAL dstHeight)

    image:纹理画刷使用的基本图案.

    dstRect:用于指定图案中用于画刷中的矩形区域.它的大小不能超过基本图案的范围.

    wrapMode:指定在画刷中如何排列基本图案.

    imageAttributes:用于指定基本图案的附加特征参数.

    对画刷图像缩放为1.5倍:

    1. SetTransform(&Matrix(1.5f,0.0f,0.0f,1.5f,0.0f,0.0f));

    纹理画刷的排列方式

    GDI+的WrapMode枚举提供了5种不同的图案排列方式:

    1. enum WrapMode{
    2.     WrapModeTile = 0,         // 简单的平铺方式(默认)
    3.     WrapModeTileFlipX = 1,    // 每隔一列的图像会左右翻转
    4.     WrapModeTileFlipY = 2,    // 每隔一行的图像会上下翻转
    5.     WrapModeTileFlipXY = 3,   // 在水平和垂直方向上同时翻转图片
    6.     WrapModeClamp = 4         // 不平铺,只包含一幅位于左上角的图像
    7. };

    可以在构造函数中指定,也可以通过SetWrapMode(排列方式)函数修改画刷的图片排列方式.

    纹理画刷的变换

    1. RotateTransform(REAL angle);         // 旋转变换(度)
    2. ScaleTransform(REAL sx,REAL sy);     // 缩放变换(倍)
    3. TranslateTransform(REAL dx,REAL dy); // 平移变换(像素)

    -------------------------------------------------

    线性渐变画刷

    线性渐变画刷LinearGradientBrush类的构造函数有以下6种:

    1. LinearGradientBrush(Point& point1, Point& point2, Color& color1, Color& color2)
    2. LinearGradientBrush(PointF& point1, PointF& point2, Color& color1, Color& color2)
    3. LinearGradientBrush(Rect& rect,Color& color1,Color& color2,REAL angle,BOOL isAngleScalable)
    4. LinearGradientBrush(RectF& rect,Color& color1,Color& color2,REAL angle,BOOL isAngleScalable)
    5. LinearGradientBrush(Rect& rect ,Color& color1, Color& color2, LinearGradientMode mode)
    6. LinearGradientBrush(RectF& rect ,Color& color1, Color& color2, LinearGradientMode mode)

    point1,point2:起点,终点的坐标.

    color1,color2:起点,终点的色彩.

    rect:画刷的矩形定义区间.

    isAngleScalable:画刷是否需要旋转.

    angle:旋转角度,isAngleScalable为真时,该参数才有效.

    mode:渐变线的填充方向.

    线性渐变画刷填充方式

    与纹理画刷的排列方式SetWrapMode(排列方式)相似.

    多色线性渐变画刷的实现

    1. SetInterpolationColors(Color* presetColors, Real* blendPositions, INT count);

    presetColors:包含多种渐变色的数组.第一及最后一个变量定义了渐变的起止点色彩.

    blendPositions:包含色彩合成位置的数组.第一及最后一个变量必须是0.0f(0%)和1.0f(100%).

    count:参与渐变的色彩总数.

    3色渐变例子:

    1. LinearGradientBrush brush(Point(0,0),Point(50,50),Color(255,0,0,0),Color(255,255,255,255));
    2. Color color[3] = {Color(255,255,0,0),Color(255,0,255,0),Color(255,0,0,255)};
    3. REAL real[3] = {0.0f,0.3f,1.0f};
    4. brush.SetInterpolationColors(color,real,3);
    5. graphics.FillRectangle(&brush,0,0,200,200);

    -------------------------------------------------

    路径渐变画刷

    路径渐变画刷PathGradientBrush类的构造函数有以下3种:

    1. PathGradientBrush(GraphicsPath* path)
    2. PathGradientBrush(Point* points, INT count, WrapMode wrapMode)
    3. PathGradientBrush(PointF* points, INT count, WrapMode wrapMode)

    path:由GraphicsPath类定义的路径.

    points:用数组形式表示的组成路径的各个端点的坐标.

    count:构造路径的端点总数.

    wrapMode:渐变画刷在目标区域中的排列方式.

    同线性渐变画刷相比,路径渐变画刷的渐变方向是从路径中央到路径边缘呈发散壮的渐变.所以,除了指定路径对象外,还要指定路径的中心点色彩和路径的边界色彩.

    4边形例子:

    1. Point points[]={Point(0,0),Point(100,0),Point(100,100),Point(0,100)};
    2. GraphicsPath path;
    3. path.AddLines(points, 4);
    4. // 构造路径渐变画刷
    5. PathGradientBrush PGBrush(&path);
    6. // 设置中心点色彩
    7. PGBrush.SetCenterColor(Color(255,255,0,0));
    8. // 设置每个端点的色彩
    9. Color colors[]={Color(255,0,0,255),Color(255,0,0,255),Color(255,0,0,255),Color(255,0,0,255)};
    10. int count = 4;
    11. PGBrush.SetSurroundColors(colors, &count);
    12. // 填充目标路径
    13. graphics.FillPath(&PGBrush, &path);

    更改路径渐变画刷的中心点

    默认情况下,路径的中心点是定义路径的各个点坐标的平均值.调用PathGradientBrush类的成员函数SetCenterPoint可以对路径渐变画刷的中心点进行调整.

    圆形例子:

    1. GraphicsPath path;
    2. path.AddEllipse(0,0,200,200);
    3. // 构造路径渐变画刷
    4. PathGradientBrush PGBrush(&path);
    5. // 设置中心点色彩
    6. PGBrush.SetCenterColor(Color(255,255,255,255));
    7. // 设置每个端点的色彩
    8. Color colors[]={Color(255,0,0,0)};
    9. int count = 1;
    10. PGBrush.SetSurroundColors(colors, &count);
    11. // 设置中心点
    12. PGBrush.SetCenterPoint(Point(50,50));
    13. // 填充目标路径
    14. graphics.FillEllipse(&PGBrush, 0,0,200,200);

    多色路径渐变与多色线性渐变方法一样.

    路径渐变的3种变换与"纹理画刷的变换"一样.

    --------------------------------------------------------------------------------------------------

    输出文本

    1. DrawString(string, length, font, layoutRect, stringFormat, brush) 
    2. DrawString(string, length, font, origin, brush) 
    3. DrawString(string, length, font, origin, stringFormat, brush) 

    string:输出的文本内容,双字节型.

    length:文本长度.该值如果设成-1,表明string的最后一个字符是NULL.

    font:字体.

    layoutRect:文本输出的矩形区域.

    origin:文本输出的起点,类型为PointF.

    stringFormat:文本输出格式.

    输出文本时使用的画刷.

    例子:

    1. Graphics graphics(hWnd);
    2. // 双字节数组
    3. WCHAR string[20];
    4. wcscpy(string,L"文本");
    5. // 字体
    6. Gdiplus::Font font(L"宋体",16);
    7. // 文本输出的矩形区域
    8. RectF rect(0.0f,0.0f,200.0f,50.0f);
    9. // 文本输出格式
    10. StringFormat format;
    11. format.SetAlignment(StringAlignmentNear);
    12. // 文本画刷
    13. SolidBrush brush(Color(255,0,0,0));
    14. // 文本显示
    15. graphics.DrawString(string,-1,&font,rect,&format,&brush);

    测量字符串(略)

    分栏显示文本(略)

    字符串的去尾(略)

    文本的剪裁输出(略)

    测量文本的局部输出区域(略)

    格式化文本输出(略)

    控制文本输出方向(略)

    设置文本对齐方式(略)

    使用制表位(略)

    显示快捷键前导字符(略)

    --------------------------------------------------------------------------------------------------

    路径

    GDI+使用GraphicsPath类来定义路径,有3种构造函数:

    1. GraphicsPath(fillMode)
    2. GraphicsPath(Point* points, BYTE* types, INT count, FillMode fillMode)
    3. GraphicsPath(PointF* points, BYTE* types, INT count, FillMode fillMode)

    fillMode:填充模式.该参数为FillMode枚举列出的两个成员FillModeAlternate,FillModeWinding之一.

    points:定义子路径的点.

    types:points数组成员的类型描述.这些点的类型有PathPointType枚举列出的点,直线的端点,曲线的端点,控制点3种.

    1. enum PathPointType{
    2.     PathPointTypeStart = 0,           // 子路径的起点
    3.     PathPointTypeLine = 1,            // 直线的起点或终点
    4.     PathPointTypeBezier = 3,          // 贝塞尔曲线的端点或控制点
    5.     PathPointTypePathTypeMode = 0x07, // 路径点类型掩码
    6.     PathPointTypeDashMode = 0x10,     //
    7.     PathPointTypePathMarker = 0x20,   // 标记点
    8.     PathPointTypeCloseSubpath = 0x80, // 封闭子路径的最后一点
    9.     PathPointTypeBezier3 = 3,         //
    10. };

    向路径中添加几何图形

    路径轮廓的定义除了可以在构造函数中通过定义端点坐标及类型实现之外,还可以在路径建立完成后,通过向路径追加线条或几何图形的方式实现.

    1. 成员函数名                  函数功能
    2. AddArc                      // 添加孤线
    3. AddBezier,AddBeziers        // 添加贝塞尔曲线
    4. AddClosedCurve              // 添加封闭曲线
    5. AddCurve                    // 添加曲线
    6. AddEllipse                  // 添加椭圆
    7. AddLine,AddLines            // 添加直线
    8. AddPath                     // 添加路径
    9. AddPie                      // 添加扇形
    10. AddPolygon                  // 添加多边形
    11. AddRectangle,AddRectangles  // 添加矩形
    12. AddString                   // 添加字符串

    开放图形与封闭图形

    1. CloseFigure()       // 封闭图形
    2. CloseAllFigures()   // 封闭所有图形

    添加子路径(略)

    提取子路径的信息(略)

    --------------------------------------------------------------------------------------------------

    区域

    --------------------------------------------------------------------------------------------------

    转自:http://blog.sina.com.cn/s/blog_7c773cc50100yvye.html

  • 相关阅读:
    Webdynpro Debug
    Smartforms SpoolId(转)
    BAPI_ACC_DOCUMENT_POST相关增强的实现
    angular factory service provider
    angularjs directive指令 link在渲染完成之后执行
    angularjs ui-router传值
    angularjs 常用 工具包
    angularJs-destroy事件
    angularjs 取消/中止 ajax请求
    angular-ui-router中的$stateProvider设置
  • 原文地址:https://www.cnblogs.com/baoku/p/10676272.html
Copyright © 2011-2022 走看看