2009-07-03 125 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中)!