简体   繁体   English

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

[英]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.

相关问题 使用Visual Studio 2010继承Windows窗体类 - Inheriting Windows Form Classes with Visual Studio 2010 在Visual Studio 2010中识别Windows应用程序窗体的helpcontextid - identify helpcontextid in visual studio 2010 for windows application form Windows Form Application C锐视觉工作室2010 - Windows Form Application C sharp visual studio 2010 配置系统无法在Visual Studio 2010 Windows窗体中初始化 - configuration system failed to initialize in visual studio 2010 windows form Visual Studio 2010 - 发布Windows窗体或WPF应用程序 - Visual Studio 2010 - Publishing a Windows Form or WPF application 如何将XtraReport连接到Visual Studio 2010上的Windows窗体 - How do I connect XtraReport to windows form on Visual Studio 2010 如何在Visual Studio 2010中创建一个没有标准形式的安装程序 - How to create an installer with no standard form in the visual studio 2010 在Visual Studio 2010 Express中部署Windows应用程序 - Deploying windows application in Visual studio 2010 Express 带有Visual Studio 2005解决方案的Windows服务未随Visual Studio 2010打开 - Windows Service with Visual Studio 2005 solution not opening with Visual Studio 2010 Visual Studio 2010表单设计器-GroupBox - Visual Studio 2010 form designer - groupbox
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM