使用C# GDI+绘图中,如何实现以下效果?随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框

解决方案 »

  1.   

    把以前做的一个东西裁剪了一下,供参考
        public partial class Form1 : Form
        {
            private bool isDrawing = false;
            Rectangle currentShape;
            private BufferedGraphics bg;
            public Form1()
            {
                InitializeComponent();
            }        private void Form1_Load(object sender, EventArgs e)
            {
                this.BackColor = Color.White;
                bg = BufferedGraphicsManager.Current.Allocate(this.CreateGraphics(), this.ClientRectangle);        }
            private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                if (!isDrawing)
                {
                    isDrawing = true;
                    currentShape = new Rectangle(e.Location, 0, 0);
                }
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    isDrawing = false;
                    currentShape = null;
                }
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (isDrawing)
                {
                    Rectangle rectangle = (Rectangle)currentShape;
                    rectangle.RectangleWidth = e.X - rectangle.Location.X;
                    rectangle.RectangleHight = e.Y - rectangle.Location.Y;
                    bg.Graphics.Clear(Color.White);
                    rectangle.Draw(bg.Graphics);
                    bg.Render();
                }        }
        }    class Rectangle
        {        private Point location;
            public Point Location
            {
                get { return location; }
                set { location = value; }
            }        private int rectangleHeight;        public int RectangleHight
            {
                get { return rectangleHeight; }
                set { rectangleHeight = value; }
            }
            private int rectangleWidth;        public int RectangleWidth
            {
                get { return rectangleWidth; }
                set { rectangleWidth = value; }
            }        public Rectangle(Point location, int rectangleWidth, int rectangleHeight)
            {
                this.location = location;
                this.rectangleWidth = rectangleWidth;
                this.rectangleHeight = rectangleHeight;
            }        public void Draw(Graphics gfx)
            {
                Point actualLocation = new Point(
                    location.X + (rectangleWidth >= 0 ? 0 : rectangleWidth),
                    location.Y + (rectangleHeight >= 0 ? 0 : rectangleHeight)
                    );
                int actualWidth = (rectangleWidth >= 0 ? rectangleWidth : -rectangleWidth);
                int actualHeight = (rectangleHeight >= 0 ? rectangleHeight : -rectangleHeight);
                gfx.DrawRectangle(Pens.Red, actualLocation.X, actualLocation.Y, actualWidth, actualHeight);
            }
        }
      

  2.   

    用gdi的异或原理画图吧,这样容易擦除。
    注意,不是gdi++
      

  3.   

    这是个DrawReversibleFrame的例子:public partial class Form1 : Form
    {
        Point startPoint = new Point(-1, -1);
        Rectangle lastRect = Rectangle.Empty;
        List<Rectangle> rectangles = new List<Rectangle>();    public Form1()
        {
            InitializeComponent();
        }    protected override void OnMouseDown(MouseEventArgs e)
        {
            this.startPoint = this.PointToScreen(e.Location);
        }    protected override void OnMouseMove(MouseEventArgs e)
        {
            if (this.startPoint.X >= 0)
            {
                Point current = this.PointToScreen(e.Location);
                Size size = new Size(current.X - startPoint.X, current.Y - startPoint.Y);            ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //擦旧
                lastRect = new Rectangle(startPoint, size);
                ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //画新
            }
        }    protected override void OnMouseUp(MouseEventArgs e)
        {
            if (this.startPoint.X >= 0)
            {
                ControlPaint.DrawReversibleFrame(lastRect, this.BackColor, FrameStyle.Dashed);  //擦
                this.startPoint.X = -1;            this.rectangles.Add(Normalize(this.RectangleToClient(lastRect)));
                this.lastRect = Rectangle.Empty;
                this.Invalidate();
            }
        }    protected override void OnPaint(PaintEventArgs e)
        {
            foreach (Rectangle rect in this.rectangles)
            {
                e.Graphics.DrawRectangle(Pens.Black, rect);
            }
        }    private Rectangle Normalize(Rectangle rect)
        {
            if (rect.Width < 0) { rect.X += rect.Width; rect.Width = -rect.Width; }
            if (rect.Height < 0) { rect.Y += rect.Height; rect.Height = -rect.Height; }
            return rect;
        }
    }
      

  4.   

    lz如果是这样移动一个图中的部分的话,建立lz最好不要这么搞...会出现闪烁...效果很差...
    我以前也用GDI+做过流程图..
      

  5.   

    http://blog.csdn.net/null1/archive/2008/10/22/3124630.aspx