0

我想在 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);
    }
}

}

任何帮助将不胜感激,

4

1 回答 1

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

        }

}
于 2013-04-02T09:09:52.363 回答