如何在对象上绘制虚线?

发布于 2024-11-09 05:43:43 字数 743 浏览 0 评论 0原文

我在 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);

我想知道是否可以绘制虚线(点)而不是常规实线?

I am drawing a line on a control on my Windows form like this:

            // 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);

I would like to know if it is possible to draw a dashed (dotted) line instead of a regular solid line?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

冷情 2024-11-16 05:43:43

一旦您找出定义破折号的格式,事情就非常简单了

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));

: float 数组中的数字代表不同颜色的破折号长度。因此,对于每个亮 2 个像素(黑色)和灭 2 个像素的简单破折号,您的阵列将如下所示: {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, point1, point2);

It's pretty simple once you figure out the formatting that defines the dashes:

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));

The numbers in the float array represent dash lengths of different colors. So for a simple dash of 2 pixels on (black) and two off each your aray would look like: {2,2} The pattern then repeats. If you wanted 5-wide dashes with a space of 2 pixels you would use {5,2}

In your code it would look like:

// 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, point1, point2);
娇俏 2024-11-16 05:43:43

我认为你可以通过改变用来画线的笔来实现这一点。
因此,将示例中的最后两行替换为:

        var pen = new Pen(Color.Yellow, 3);
        pen.DashStyle = DashStyle.Dash;
        graph.DrawLine(pen, point1, point2);

I think you can accomplish this by changing the pen you use to draw your line.
So, replace the last 2 lines in your example with:

        var pen = new Pen(Color.Yellow, 3);
        pen.DashStyle = DashStyle.Dash;
        graph.DrawLine(pen, point1, point2);
姜生凉生 2024-11-16 05:43:43

Pen 有一个公共属性,它被定义为

public DashStyle DashStyle { get; set; }

如果您想绘制虚线,您可以设置 DasStyle.Dash

Pen has a public property that is defined as

public DashStyle DashStyle { get; set; }

you can set DasStyle.Dash if you want to draw a dashed line.

我偏爱纯白色 2024-11-16 05:43:43

在更现代的 C# 中:

var dottedPen = new Pen(Color.Gray, width: 1) { DashPattern = new[] { 1f, 1f } };

In more modern C#:

var dottedPen = new Pen(Color.Gray, width: 1) { DashPattern = new[] { 1f, 1f } };
野稚 2024-11-16 05:43:43

要回答有关使用隐藏代码生成虚线的问题:

        Pen dashPenTest = new(Brushes.DodgerBlue, 1);

        Line testLine = new()
        {
            Stroke = dashPenTest.Brush, //Brushes.Aqua,
            StrokeThickness = dashPenTest.Thickness,//1,
            StrokeDashArray = new DoubleCollection() { 8,4 },
            X1 = 0,
            X2 = canvas.Width,
            Y1 = 10,
            Y2 = 10
        }; 
        canvas.Children.Add(testLine);

这个答案利用了 xaml 中画布的生成:

        <Canvas x:Name ="canvas" Background="White" Height="300" Width="300">

这里重要的方法是“StrokeDashArray”,它为绘制的线生成虚线。此处提供了更多信息: Shape.StrokeDashArray

To answer this question regarding the generation of a dashed line using the code-behind:

        Pen dashPenTest = new(Brushes.DodgerBlue, 1);

        Line testLine = new()
        {
            Stroke = dashPenTest.Brush, //Brushes.Aqua,
            StrokeThickness = dashPenTest.Thickness,//1,
            StrokeDashArray = new DoubleCollection() { 8,4 },
            X1 = 0,
            X2 = canvas.Width,
            Y1 = 10,
            Y2 = 10
        }; 
        canvas.Children.Add(testLine);

This answer make use of the generation of a canvas in the xaml:

        <Canvas x:Name ="canvas" Background="White" Height="300" Width="300">

The important method here is the "StrokeDashArray" that generates the dashes for the line drawn. More information is given here: Shape.StrokeDashArray

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文