c# – 如何在对象上画一条虚线?

前端之家收集整理的这篇文章主要介绍了c# – 如何在对象上画一条虚线?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我在 Windows窗体上绘制一条控件,如下所示:
// Get Graphics object from chart
            Graphics graph = e.ChartGraphics.Graphics;

            PointF point1 = PointF.Empty;
            PointF point2 = PointF.Empty;

            // Set Maximum and minimum points
            point1.X = -110;
            point1.Y = -110;
            point2.X = 122;
            point2.Y = 122;

            // Convert relative coordinates to absolute coordinates.
            point1 = e.ChartGraphics.GetAbsolutePoint(point1);
            point2 = e.ChartGraphics.GetAbsolutePoint(point2);

            // Draw connection line
            graph.DrawLine(new Pen(Color.Yellow,3),point1,point2);

我想知道是否可以画一条虚线(虚线)而不是一条常规的实线?

解决方法

一旦您定义了破折号的 figure out the formatting,这很简单:
float[] dashValues = { 5,2,15,4 };
Pen blackPen = new Pen(Color.Black,5);
blackPen.DashPattern = dashValues;
e.Graphics.DrawLine(blackPen,new Point(5,5),new Point(405,5));

浮点数组中的数字表示不同颜色的短划线长度.所以,对于一个2像素(黑色)的简单破折号,每个两个,你的aray将看起来像:{2,2}然后重复模式.如果您想要5幅宽度为2像素的虚线,则可以使用{5,2}

在你的代码中,它将如下所示:

// Get Graphics object from chart
Graphics graph = e.ChartGraphics.Graphics;

PointF point1 = PointF.Empty;
PointF point2 = PointF.Empty;

// Set Maximum and minimum points
point1.X = -110;
point1.Y = -110;
point2.X = 122;
point2.Y = 122;

// Convert relative coordinates to absolute coordinates.
point1 = e.ChartGraphics.GetAbsolutePoint(point1);
point2 = e.ChartGraphics.GetAbsolutePoint(point2);

// Draw (dashed) connection line
float[] dashValues = { 4,2 };
Pen dashPen= new Pen(Color.Yellow,3);
dashPen.DashPattern = dashValues;
graph.DrawLine(dashPen,point2);
原文链接:https://www.f2er.com/csharp/95714.html

猜你在找的C#相关文章