English 中文(简体)
如何在物体上画虚线?
原标题:How to draw a dashed line over an object?

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

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

最佳回答

一旦你找出定义短划线的格式

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

Pen的公共属性定义为

public DashStyle DashStyle { get; set; }

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

DashPattern将执行此操作。在此处查找示例

在更现代的C#中:

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

要回答有关使用后面的代码生成虚线的问题,请执行以下操作:

        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”,它为绘制的线生成破折号。此处提供了更多信息:形状.StrokeDashArray





相关问题
Bring window to foreground after Mutex fails

I was wondering if someone can tell me what would be the best way to bring my application to the foreground if a mutex was not able to be created for a new instance. E.g.: Application X is running ...

How to start WinForm app minimized to tray?

I ve successfully created an app that minimizes to the tray using a NotifyIcon. When the form is manually closed it is successfully hidden from the desktop, taskbar, and alt-tab. The problem occurs ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

Handle DataTable.DataRow cell change event

I have a DataTable that has several DataColumns and DataRow. Now i would like to handle an event when cell of this DataRow is changed. How to do this in c#?

Apparent Memory Leak in DataGridView

How do you force a DataGridView to release its reference to a bound DataSet? We have a rather large dataset being displayed in a DataGridView and noticed that resources were not being freed after the ...

ALT Key Shortcuts Hidden

I am using VS2008 and creating forms. By default, the underscore of the character in a textbox when using an ampersand is not shown when I run the application. ex. "&Goto Here" is not ...

WPF-XAML window in Winforms Application

I have a Winforms application coded in VS C# 2008 and want to insert a WPF window into the window pane of Winforms application. Could you explain me how this is done.

热门标签