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 に答える