C# 绘图问题

linxinru1976 2009-04-26 02:48:01
使用C# GDI+绘图中,如何实现以下效果?随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框
...全文
180 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
优途科技 2009-04-27
  • 打赏
  • 举报
回复
完全可以,类似橡皮线。
null1 2009-04-27
  • 打赏
  • 举报
回复
http://blog.csdn.net/null1/archive/2008/10/22/3124630.aspx
a260881071 2009-04-27
  • 打赏
  • 举报
回复
热学沸腾56 2009-04-27
  • 打赏
  • 举报
回复
lz如果是这样移动一个图中的部分的话,建立lz最好不要这么搞...会出现闪烁...效果很差...
我以前也用GDI+做过流程图..
冰凝瞬间1986 2009-04-27
  • 打赏
  • 举报
回复
GDI+
gomoku 2009-04-27
  • 打赏
  • 举报
回复
这是个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;
}
}
aimeast 2009-04-26
  • 打赏
  • 举报
回复
用gdi的异或原理画图吧,这样容易擦除。
注意,不是gdi++
LemIST 2009-04-26
  • 打赏
  • 举报
回复
up
先用背景色重画原来的矩形,可以清除原来的.
然后再画现在的矩形.
zcandyly20211 2009-04-26
  • 打赏
  • 举报
回复
友情up!
yeaicc 2009-04-26
  • 打赏
  • 举报
回复
处理鼠标的单击事件
footprint2008 2009-04-26
  • 打赏
  • 举报
回复
[Quote=引用楼主 linxinru1976 的帖子:]
使用C# GDI+绘图中,如何实现以下效果?随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框
[/Quote]

ding^^^^^^^^^^^
yangqidong 2009-04-26
  • 打赏
  • 举报
回复
把以前做的一个东西裁剪了一下,供参考
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);
}
}

110,577

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧