4

我想知道是否可以在 windows 窗体的设计中添加一行?我在工具箱中找不到任何工具?或者在视觉工作室或代码中是否有其他方法可以做到这一点?

4

2 回答 2

11

WinForms 没有内置控件来执行此操作。您可以使用该GroupBox控件,将Text属性设置为空字符串,并将其高度设置为2. 这将模仿压纹线。否则,您需要创建自定义控件并自己绘制线条。

对于自定义控件,这是一个示例。

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

它只是ClientRectangle用指定的填充LineColor,所以线条的高度和宽度就是控件本身的高度和宽度。相应调整。

于 2012-07-08T07:58:57.670 回答
0
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); 
} 

有关在 Winforms 中绘制线条的更多信息,请参阅以下链接

于 2012-07-08T08:18:23.520 回答