2009-07-03 72 views
4

我無法在一個簡單的窗體中在一個組框中繪製一條線。在Winforms中繪製一條線

這裏是我的代碼:

public partial class Form1 : Form 
    { 
     public Form1() 
     { 
      InitializeComponent();       
     } 

     protected override void OnPaint(PaintEventArgs e) 
     { 
      base.OnPaint(e);    
      DrawLShapeLine(groupBox1.CreateGraphics(), 10, 10, 20, 40); 
     } 

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

如果我附上DrawLShapeLine方法按鈕單擊事件,它繪製精細,但它不會對形式的負載吸取。

請指教。

回答

3

連接GroupBoxPaint事件的事件處理程序,而是從該事件處理程序中調用DrawLShapeLine。然後,您應該使用在事件參數提供的Graphics對象:

private void groupBox1_Paint(object sender, PaintEventArgs e) 
{ 
    DrawLShapeLine(e.Graphics, 10, 10, 20, 40); 
} 

當你的代碼看起來現在將嘗試在GroupBox作畫當表單需要畫。任何其他場合都可以繪製組合框,這將使您繪製的線條消失。

0

我不確定其他事情是否正在進行,但是您應該在GroupBox的Paint事件上繪製線條,而不是Form的。

21

快速&髒:

如何爲1個像素的寬度創建面板,並給它一個的backgroundColor?

+0

這不會做對角線壽。 – 2009-07-03 07:40:42

1

添加一個沒有文字,3D邊框和高度爲2的標籤(您必須在屬性頁面中設置高度,而不是在GUI中)!