C# 绘图问题 使用C# GDI+绘图中,如何实现以下效果?随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 把以前做的一个东西裁剪了一下,供参考 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); } } 用gdi的异或原理画图吧,这样容易擦除。注意,不是gdi++ 这是个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; }} lz如果是这样移动一个图中的部分的话,建立lz最好不要这么搞...会出现闪烁...效果很差...我以前也用GDI+做过流程图.. http://blog.csdn.net/null1/archive/2008/10/22/3124630.aspx 菜鸟请教,类的实例化问题 【】求高校人事档案管理系统的需求!! Developer GridControl问题。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。。 一个奇怪的问题,请求帮助!!! 用C#实现网页预取的问题! 如何实现对XML数据的查询啊? 请较自定义公式的实现方法!谢谢 怎样判断当前窗体?我的窗体不是MDIFORM vss 历史记录 突然没了,怎么办? 很简单的问题:关于confirm对话框 我的机器上同时有VS2005和VS2008.想删除VS2005,会出现问题吗,如何删VS2005? 如何调用API
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);
}
}
注意,不是gdi++
{
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;
}
}
我以前也用GDI+做过流程图..