绘制矩形C#Mouse_down工作Mouse_up没有

时间:2013-04-02 08:42:22

标签: c# forms draw rectangles

我想在一个表单上绘制一个矩形,mouse_down事件工作正常,但我如何编写mouse_up。我应该如何确定起始坐标。

http://oi49.tinypic.com/2wcnof8.jpg

    Rectangle rect = new Rectangle();
    Color rastercolor = Color.Black;

    private void drawGrid(Color rastercolor)
    {
        Graphics gfx = this.CreateGraphics();
        Pen pen = new Pen(rastercolor);
        for (int i = 0; i <= 2000; i = i + 20)
        {
            gfx.DrawLine(pen, 0, i, 2000, i);
        }
        for (int x = 0; x < 2000; x = x + 20)
        {
            gfx.DrawLine(pen, x, 0, x, 2000);
        }
    }

    protected override void OnMouseDown(MouseEventArgs e)
    {
        rect = new Rectangle(e.X, e.Y, 0, 0);

        if (e.Button == MouseButtons.Middle)
        {
            ColorDialog coldial = new ColorDialog();
            coldial.ShowDialog();
            Color rastercolor = coldial.Color;
            tekenGrid(rastercolor);
        }
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        if (e.Button == MouseButtons.Left)
        {
            rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
        }
    }


    protected override void OnPaint(PaintEventArgs e)
    {
        tekenGrid(rastercolor);
        using (Pen pen = new Pen(Color.Red, 2))
        {
            e.Graphics.DrawRectangle(pen, rect);
        }
    }

    private void Frm1_MouseUp(object sender, MouseEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        Pen pen = new Pen(Color.Red, 2);
        g.DrawRectangle(pen, rect);
    }

    private void Frm1_Load(object sender, EventArgs e)
    {

    }

    private void Frm1_MouseDown(object sender, MouseEventArgs e)
    {
        Graphics g = this.CreateGraphics();
        Pen pen = new Pen(Color.Blue, 2);
        g.DrawRectangle(pen, rect);
    }
}

}

非常感谢任何帮助,

1 个答案:

答案 0 :(得分:0)

不确定这是否是你的意思。请尝试此代码。

 public partial class Form1 : Form
 {
    public Form1()
    {
        InitializeComponent();
        this.DoubleBuffered = true;
    }

    Rectangle rect = new Rectangle(); 
       Color _rastercolor = Color.Black;
       private Pen _pen=new Pen(Color.Red,2);

        private void tekenGrid(Color rastercolor,  Graphics gfx)
        {

            Pen pen = new Pen(rastercolor);
            for (int i = 0; i <= 2000; i = i + 20)
            {
                gfx.DrawLine(pen, 0, i, 2000, i);
            }
            for (int x = 0; x < 2000; x = x + 20)
            {
                gfx.DrawLine(pen, x, 0, x, 2000);
            }
        }

        protected override void OnMouseDown(MouseEventArgs e)
        {
            rect = new Rectangle(e.X, e.Y, 0, 0);

            if (e.Button == MouseButtons.Middle)
            {
                ColorDialog coldial = new ColorDialog();
                coldial.ShowDialog();
                Color rastercolor = coldial.Color;
                tekenGrid(rastercolor,this.CreateGraphics());
            }
            else
            base.OnMouseDown(e);
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                rect = new Rectangle(rect.Left, rect.Top, e.X - rect.Left, e.Y - rect.Top);
                this.Refresh();
            }

            base.OnMouseMove(e);
        }


        protected override void OnPaint(PaintEventArgs e)
        {
            tekenGrid(_rastercolor,e.Graphics);
                e.Graphics.DrawRectangle(_pen, rect);
        }

        private void Frm1_MouseUp(object sender, MouseEventArgs e)
        {
            _pen = new Pen(Color.Red, 2);
            this.Refresh();
        }

        private void Frm1_Load(object sender, EventArgs e)
        {

        }

        private void Frm1_MouseDown(object sender, MouseEventArgs e)
        {
           _pen = new Pen(Color.Blue, 2);

        }

}