I wonder if it's possible to add a line to the design in a windows form? 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?
There isn't a built-in control for WinForms to do this. You can use the GroupBox
control though, set the Text
property to an empty string, and set it's height to 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. 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.