动态绘制矩形 [英] Rectangle drawing dynamically

查看:105
本文介绍了动态绘制矩形的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在Form上用鼠标动态绘制Rectangle,但是需要的结果不存在。我想在窗体上抓取鼠标,这样就可以绘制矩形了。

有人请帮助我。 .....

I want to draw the Rectangle by mouse dynamically on Form but required results are not there.Means I want to grab the mouse on the form so rectangle can be draw.
Somebody Please help me......

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace rect
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }
        Point startPos;      // mouse-down position
        Point currentPos;    // current mouse position
        bool drawing;        // busy drawing
        List<Rectangle> rectangles = new List<Rectangle>();  // previous rectangles

        private Rectangle getRectangle()
        {
            return new Rectangle(
                Math.Min(startPos.X, currentPos.X),
                Math.Min(startPos.Y, currentPos.Y),
                Math.Abs(startPos.X - currentPos.X),
                Math.Abs(startPos.Y - currentPos.Y));
        }

        private void canevas_MouseDown(object sender, MouseEventArgs e)
        {
            currentPos = startPos = e.Location;
            drawing = true;
        }

        private void canevas_MouseMove(object sender, MouseEventArgs e)
        {
            currentPos = e.Location;
            if (drawing)
                Invalidate();
        }

        private void canevas_MouseUp(object sender, MouseEventArgs e)
        {
            if (drawing)
            {
                drawing = false;
                var rc = getRectangle();
                if (rc.Width > 0 && rc.Height > 0) rectangles.Add(rc);
                Invalidate();
            }
        }

        private void canevas_Paint(object sender, PaintEventArgs e)
        {
            if (rectangles.Count > 0) e.Graphics.DrawRectangles(Pens.Black, rectangles.ToArray());
            if (drawing) e.Graphics.DrawRectangle(Pens.Red, getRectangle());
        }
    
        private void rectangleShape1_Click(object sender, EventArgs e)
        {
            getRectangle();
        }
    }
}

推荐答案

以下代码执行您想要执行的操作。



The following code performs what you want to do.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Windows.Forms;

namespace CodeProject
    {

    // *************************************** class DynamicRectangles
    
    public partial class DynamicRectangles : Form
        {

        Point               current_mouse_position;
        Point               mouse_down_position;
        bool                drawing = false;
        List < Rectangle >  rectangles = new List < Rectangle > ( );

        // ***************************************** DynamicRectangles
        
        public DynamicRectangles ( )
            {
            
            InitializeComponent ( );
            }

        // ***************************************** current_rectangle
        
        private Rectangle current_rectangle ( )
            {
            return new Rectangle(
                Math.Min ( mouse_down_position.X, 
                           current_mouse_position.X ),
                Math.Min ( mouse_down_position.Y, 
                           current_mouse_position.Y ),
                Math.Abs ( mouse_down_position.X - 
                           current_mouse_position.X ),
                Math.Abs ( mouse_down_position.Y - 
                           current_mouse_position.Y ) );
            }

        // *********************************************** OnMouseDown
        
        protected override void OnMouseDown ( MouseEventArgs e )
            {
            
            base.OnMouseDown ( e );

            drawing = true;

            current_mouse_position = e.Location;
            mouse_down_position = e.Location;
            }

        // *********************************************** OnMouseMove
        
        protected override void OnMouseMove ( MouseEventArgs e )
            {
            
            base.OnMouseMove ( e );

            current_mouse_position = e.Location;
            if ( drawing )
                {
                Invalidate ( );
                }
            }

        // ************************************************* OnMouseUp
        
        protected override void OnMouseUp ( MouseEventArgs e )
            {
            Rectangle  rectangle;
            base.OnMouseUp ( e );

            drawing = false;

            rectangle = current_rectangle ( );
            if ( ( rectangle.Width > 0 ) && ( rectangle.Height > 0 ) )
                {
                rectangles.Add ( rectangle );
                }
            Invalidate ( );
            }

        // *************************************************** OnPaint
        
        protected override void OnPaint ( PaintEventArgs e )
            {
            
            base.OnPaint ( e );

            if ( rectangles.Count > 0 )
                {
                e.Graphics.DrawRectangles ( new Pen ( Color.Black ),
                                            rectangles.ToArray ( ) );
                }
            if ( drawing )
                {
                e.Graphics.DrawRectangle ( new Pen ( Color.Red ), 
                                           current_rectangle ( ) );
                }
            }
            
        } // class DynamicRectangles

    } // namespace CodeProject





我对你的代码有一些看法。



1.来自MSDN



On ...方法允许派生类在不附加委托的情况下处理事件。

这是在派生类中处理事件的首选技术。



我建议您使用On ...形式的事件处理程序声明。您需要在代码中输入 protected override ,Intellisence将为您提供一个列表,您可以从中选择所需内容。



2.我忽略了canevas [sic]变量。

3.我忽略了事件处理程序rectangleShape1_Click。



希望有帮助。



Gus



I have a couple of observations about your code.

1. From MSDN

The On... methods allow derived classes to handle the event without attaching a delegate.
This is the preferred technique for handling the event in a derived class.

I suggest that you use the On... form of event handler declaration. All you need to enter is protected override in your code and Intellisence will provide you with a list from which you can choose what you want.

2. I am ignoring the canevas [sic] variable.
3. I am ignoring the event handler rectangleShape1_Click.

Hope that helps.

Gus


这篇关于动态绘制矩形的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆