使用visual studio 2010在Windows窗体中创建线条?

3D-*_*tiv 4 c# visual-studio-2010

我想知道是否可以在Windows窗体中为设计添加一条线?我在工具箱中找不到任何工具?或者在visual studio或代码中有其他方法可以做到这一点吗?

Dav*_*son 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);
            }
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

它只是填充ClientRectangle指定的LineColor,所以该行的高度和宽度是控件本身的高度和宽度.相应调整.