功能需求:
鼠标左键点下,拖动出一个矩形的区域。要考虑鼠标不停的移动产生的矩形是不同的,要消除前一个,始终显示一个矩形框!!!有点类似拖拽选中的意思!!!|-----------|
|           |
|           |
|-----------|  就是这个图的意思,是个矩形,但是鼠标移动。矩形的大小也跟着变化,上下左右移动都要能体现出变化。
我这个代码可以体现出变化。但是就是有遗留的线条。比如我拖了一个大的了。我现在先把他拖下了。就会有遗留下的大的!!!帮我看看代码那里有问题!!!1  public Rectangle saveoldselectrect = new Rectangle();  //保存上一次左键选择的矩形
 private void Form1_MouseMove(object sender, MouseEventArgs e)
{
                 int x1, x2, y1, y2;                if (MousedownPoint.X > e.X)
                {
                    x1 = e.X;
                    x2 = MousedownPoint.X;                }
                else
                {
                    x1 = MousedownPoint.X;
                    x2 = e.X;                }                if (MousedownPoint.Y > e.Y)
                {
                    y1 = e.Y;
                    y2 = MousedownPoint.Y;                }
                else
                {
                    y1 = MousedownPoint.Y;
                    y2 = e.Y;
                }                x1 = Math.Max(0, x1);
                y1 = Math.Max(0, y1);
                Rectangle Newrect = new Rectangle(x1, y1, (x2 - x1), (y2 - y1));//这个就是左键选定的矩形!!!                if (Newrect.Right > this.ClientRectangle.Right)
                    Newrect.Width = this.ClientRectangle.Right - Newrect.Left;
                if (Newrect.Bottom > this.ClientRectangle.Bottom)
                    Newrect.Height = this.ClientRectangle.Bottom - Newrect.Top;                if (Newrect.Width <= 0 || Newrect.Height <= 0)
                    return;
                              Rectangle ScreenRect = new Rectangle();
                ScreenRect.X = Math.Min(saveoldselectrect.X, Newrect.X);
                ScreenRect.Y = Math.Min(saveoldselectrect.Y, Newrect.Y);
                if (saveoldselectrect.X + saveoldselectrect.Width > Newrect.X + Newrect.Width)
                {
                    ScreenRect.Width = saveoldselectrect.Width;
                }
                else
                {
                    ScreenRect.Width = Newrect.Width;
                }                if (saveoldselectrect.Y + saveoldselectrect.Height > Newrect.X + Newrect.Height)
                {
                    ScreenRect.Height = saveoldselectrect.Height;
                }
                else
                {
                    ScreenRect.Height = Newrect.Height;                }
             
                ScreenRect.X = ScreenRect.X - 1;
                ScreenRect.Y = ScreenRect.Y - 1;
                ScreenRect.Width = ScreenRect.Width + 2;
                ScreenRect.Height = ScreenRect.Height + 2;
                ScreenRect.X = Math.Max(this.ClientRectangle.X, ScreenRect.X);
                ScreenRect.Y = Math.Max(this.ClientRectangle.Y, ScreenRect.Y);
                if (ScreenRect.Right > this.ClientRectangle.Right)
                    ScreenRect.Width = this.ClientRectangle.Right - ScreenRect.X;
                if (ScreenRect.Bottom > this.ClientRectangle.Bottom)
                    ScreenRect.Height = this.ClientRectangle.Bottom - ScreenRect.Y;                //获取指定矩形的bitmap
                  Bitmap smalltempbitmap = map.DrawMap.Bitmap.Clone(ScreenRect, map.DrawMap.Bitmap.PixelFormat);
                                         //画新的图形!!
                Rectangle temprect = new Rectangle(Newrect.X, Newrect.Y, Newrect.Width, Newrect.Height);
                temprect.Offset(-ScreenRect.X, -ScreenRect.Y);
                //矩形填充
                Color col = Color.FromArgb(50, 255, 181, 106);
                using (SolidBrush rectbrush = new SolidBrush(col))
                {
                  //  Graphics.FromImage(smalltempbitmap).FillRectangle(rectbrush, temprect);
                    Graphics.FromImage(smalltempbitmap).DrawRectangle(Pens.Black, temprect);
                }                using (Graphics g1 = Graphics.FromHwnd(this.Handle))
                {                 g1.DrawImage(smalltempbitmap, ScreenRect.X, ScreenRect.Y, ScreenRect.Width, ScreenRect.Height);
                 
                }
                 
                 saveoldselectrect = Newrect;}
                

解决方案 »

  1.   


    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 CSDNDragRect
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            private Point startPoint;
            private Point lastPoint;        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                this.Capture = true;
                startPoint = new Point(e.X, e.Y);
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                this.Capture = false;
            }        private Pen bgPen;
            private Pen rectPen;        private Graphics gForm;
            private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                if (this.Capture)
                {
                    Point nowPoint = new Point(e.X, e.Y);
                    drawRect(gForm, bgPen, startPoint, lastPoint);
                    drawRect(gForm, rectPen, startPoint, nowPoint);
                    lastPoint = nowPoint;
                }
            }        private void Form1_Load(object sender, EventArgs e)
            {
                bgPen = new Pen(Color.White, 3);
                rectPen = new Pen(Color.Red, 3);
                gForm = this.CreateGraphics();
            }        private void drawRect(Graphics g, Pen p, Point s, Point e)
            {
                Rectangle r = getRectangle(s, e);
                g.DrawRectangle(p, r);
            }        private Rectangle getRectangle(Point s, Point e)
            {
                int x = s.X < e.X ? s.X : e.X;
                int y = s.Y < e.Y ? s.Y : e.Y;            int w = (int)Math.Abs(s.X - e.X);
                int h = (int)Math.Abs(s.Y - e.Y);            return new Rectangle(x, y, w, h);
            }
        }
    }
      

  2.   

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;namespace WindowsApplication29
    {
        public partial class Form1 : Form
        {
            Point old;
            bool mouseDown = false;
            Graphics newG;
            public Form1()
            {
                newG = this.CreateGraphics();
                InitializeComponent();
            }        private void Form1_MouseDown(object sender, MouseEventArgs e)
            {
                old = e.Location;
                mouseDown = true;
            }        private void Form1_MouseMove(object sender, MouseEventArgs e)
            {
                newG.Clear(Color.White);
                if (mouseDown)
                {
                    
                    newG.DrawRectangle(new Pen(Color.Black), new Rectangle(old, new Size(e.X - old.X, e.Y - old.Y)));
                }
                
            }        private void Form1_MouseUp(object sender, MouseEventArgs e)
            {
                mouseDown = false;
            }
        }
    }
      

  3.   

    我的背景图是个复杂的bitmap。你这个方法貌似不行把
      

  4.   

    1楼的大哥。能不能来个背景图片。在背景图片上处理的呢。我的是这样的。我的油箱是
    [email protected]有好的处理方法的话。我在给你加100分。说到做到!!!!!