[英]Create lines in windows form with visual studio 2010?
I wonder if it's possible to add a line to the design in a windows form? 我想知道是否可以在Windows窗体中为设计添加一条线? I can't find any tool for this in the toolbox? 我在工具箱中找不到任何工具? Or is there some other way to do this in visual studio or in code? 或者在visual studio或代码中有其他方法可以做到这一点吗?
There isn't a built-in control for WinForms to do this. WinForms没有内置的控件来执行此操作。 You can use the GroupBox
control though, set the Text
property to an empty string, and set it's height to 2
. 您可以使用GroupBox
控件,将Text
属性设置为空字符串,并将其高度设置为2
。 This will mimic a embossed line. 这将模仿浮雕线。 Otherwise, you need to create a custom control and paint the line yourself. 否则,您需要创建自定义控件并自行绘制线条。
For a custom control, here's an example. 对于自定义控件,这是一个示例。
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms;
namespace WindowsFormsApplication12
{
public partial class Line : Control
{
public Line() {
InitializeComponent();
}
private Color m_LineColor = Color.Black;
/// <summary>
/// Gets or sets the color of the divider line
/// </summary>
[Category("Appearance")]
[Description("Gets or sets the color of the divider line")]
public Color LineColor {
get {
return m_LineColor;
}
set {
m_LineColor = value;
Invalidate();
}
}
protected override void OnPaint(PaintEventArgs pe) {
using (SolidBrush brush = new SolidBrush(LineColor)) {
pe.Graphics.FillRectangle(brush, pe.ClipRectangle);
}
}
}
}
It simply fills the ClientRectangle
with the specified LineColor
, so the height and width of the line is that of the control itself. 它只是用指定的LineColor
填充ClientRectangle
,因此该行的高度和宽度是控件本身的高度和宽度。 Adjust accordingly. 相应调整。
public void DrawLShapeLine(System.Drawing.Graphics g, int intMarginLeft, int intMarginTop, int intWidth, int intHeight)
{
Pen myPen = new Pen(Color.Black);
myPen.Width = 2;
// Create array of points that define lines to draw.
int marginleft = intMarginLeft;
int marginTop = intMarginTop;
int width = intWidth;
int height = intHeight;
int arrowSize = 3;
Point[] points =
{
new Point(marginleft, marginTop),
new Point(marginleft, height + marginTop),
new Point(marginleft + width, marginTop + height),
// Arrow
new Point(marginleft + width - arrowSize, marginTop + height - arrowSize),
new Point(marginleft + width - arrowSize, marginTop + height + arrowSize),
new Point(marginleft + width, marginTop + height)
};
g.DrawLines(myPen, points);
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
DrawLShapeLine(e.Graphics, 10, 10, 20, 40);
}
See the following link for more 请参阅以下链接了解更多信息
Drawing a line in Winforms 在Winforms中绘制一条线
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.