在c#里面怎样动态的画一个矩形???????????

解决方案 »

  1.   

    System.Drawing.Graphics.DrawRectangle() 方法
      

  2.   

    using System.Drawing;
    using System.Windows.Forms;class Test : Form
    {
      Rectangle r = Rectangle.Empty;  protected override void OnMouseDown(MouseEventArgs e)
      {
        r.X = e.X;
        r.Y = e.Y;
      }  protected override void OnMouseUp(MouseEventArgs e)
      {
        if(e.X < r.X)
        {
          r.Width = r.X - e.X;
          r.X     = e.X;
        }
        else
        {
          r.Width = e.X - r.X;
        }    if(e.Y < r.Y)
        {
          r.Height = r.Y - e.Y;
          r.Y      = e.Y;
        }
        else
        {
          r.Height = e.Y - r.Y;
        }
        Invalidate(new Rectangle(r.X, r.Y, r.Width+1, r.Height+1));
      }  protected override void OnPaint(PaintEventArgs e)
      {
        base.OnPaint(e);
        e.Graphics.DrawRectangle(new Pen(Color.Blue, 0), r);
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  3.   

    // 这个会随鼠标移动而画, 单击确定顶点.using System.Drawing;
    using System.Windows.Forms;class Test : Form
    {
      int x0, y0, x1, y1;
      Rectangle r = Rectangle.Empty;  protected override void OnMouseDown(MouseEventArgs e)
      {
        x0 = e.X;
        y0 = e.Y;
      }  protected override void OnMouseMove(MouseEventArgs e)
      {
        x1 = e.X;
        y1 = e.Y;
        Invalidate();
      }  protected override void OnPaint(PaintEventArgs e)
      {
        base.OnPaint(e);
        if (x0 < x1) { r.X = x0; r.Width  = x1 - x0; }
        else         { r.X = x1; r.Width  = x0 - x1; }
        if (y0 < y1) { r.Y = y0; r.Height = y1 - y0; }
        else         { r.Y = y1; r.Height = y0 - y1; }
        e.Graphics.DrawRectangle(new Pen(Color.Blue, 0), r);
      }  static void Main()
      {
        Application.Run(new Test());
      }
    }
      

  4.   

    还想请教你一问题~可否给个联系方式~~ msn 或电子邮箱
      

  5.   

    继续使用 System.Drawing.Graphics.DrawRectangle() 方法
      

  6.   

    用 System.Drawing.Graphics.DrawRectangle() 画矩形就可以了.