功能需求:
鼠标左键点下,拖动出一个矩形的区域。要考虑鼠标不停的移动产生的矩形是不同的,要消除前一个,始终显示一个矩形框!!!有点类似拖拽选中的意思!!!|-----------|
| |
| |
|-----------| 就是这个图的意思,是个矩形,但是鼠标移动。矩形的大小也跟着变化,上下左右移动都要能体现出变化。
我这个代码可以体现出变化。但是就是有遗留的线条。比如我拖了一个大的了。我现在先把他拖下了。就会有遗留下的大的!!!帮我看看代码那里有问题!!!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 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;}
解决方案 »
- 求教:有一个Student类,从SQLSERVER数据库中读取多个值包括名字班级等等
- 线程的疑惑
- DataGridView 追加行如何实现?
- 请问一个有关自动更新的问题!
- 发布异常的问题,up给分
- 水晶报表10在winfrom中如何熟练使用
- 请求一本好书,C# 面向对象编程思想
- [南京程序员俱乐部]Blog大交换:http://218.2.112.243:1050/BBS/ShowForum.aspx?ForumID=60
- 请教如何在listBox中检索到自己想要的信息,可能很简单也可能很难,不会,大家看看,很有用的?
- 使用sqlInsertCommand.ExecuteNonQuery()方法后怎样得知所插入记录的ID号?
- DeflateStream的压缩 和解压
- vs.net环境中能否用水晶报表api编程
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);
}
}
}
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;
}
}
}
[email protected]有好的处理方法的话。我在给你加100分。说到做到!!!!!