C#绘图中,如何实现橡皮线的效果。

linxinru1976 2009-05-05 04:00:39
MFC里面要用到SetROP2,但是C#里面没有。实现类似橡皮线的效果。具体来说,如何实现,随着鼠标的移动画矩形线(及时删除原来的矩形框),松开鼠标时显示最后状态下的矩形框?
...全文
437 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
bin_1987_huang 2009-05-05
  • 打赏
  • 举报
回复
4楼的方法很好 ,
不过这个简易画板也很不错,有画直线,画椭圆,画矩形,等等
http://download.csdn.net/source/1149352
CqCoder 2009-05-05
  • 打赏
  • 举报
回复
4楼
正解
gomoku 2009-05-05
  • 打赏
  • 举报
回复
这是个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;
}
}
zgke 2009-05-05
  • 打赏
  • 举报
回复
不知道你要怎么画 最简单的方法

ControlPaint.DrawFocusRectangle


你可以参考 这个是画一次记录下 矩形 下次画的时候在绘制一个透明的矩形
http://blog.csdn.net/zgke/archive/2009/03/04/3956936.aspx

walkghost 2009-05-05
  • 打赏
  • 举报
回复
UP楼上的。
cpio 2009-05-05
  • 打赏
  • 举报
回复
GDI+的话,没有这个功能

如果要用,就得通过API调用GDI接口
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Text; using System.Windows.Forms; namespace DoubleBufferDraw { public partial class DrawLine : Form { class LineObj { private Point m_start; private Point m_end; public LineObj(Point start, Point end) { this.m_start = start; this.m_end = end; } public void Draw(Graphics g, Pen pen) { g.DrawLine(pen, m_start, m_end); } } private Point m_startPoint = Point.Empty; List lineList = new List(); public DrawLine() { InitializeComponent(); } private void drawLine(Graphics graphics, Point startPoint, Point endPoint) { BufferedGraphicsContext context = BufferedGraphicsManager.Current; BufferedGraphics bg = context.Allocate(graphics, this.ClientRectangle); bg.Graphics.Clear(this.BackColor); foreach (LineObj line in this.lineList) { line.Draw(bg.Graphics, SystemPens.ControlText); } bg.Graphics.DrawLine(SystemPens.ControlText, startPoint, endPoint); bg.Render(); bg.Dispose(); bg = null; } protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); foreach (LineObj line in this.lineList) { line.Draw(e.Graphics, SystemPens.ControlText); } } protected override void OnMouseDown(MouseEventArgs e) { base.OnMouseDown(e); this.m_startPoint = new Point(e.X, e.Y); } protected override void OnMouseMove(MouseEventArgs e) { base.OnMouseMove(e); if (e.Button == MouseButtons.Left) { this.drawLine(this.CreateGraphics(), this.m_startPoint, new Point(e.X, e.Y)); } } protected override void OnMouseUp(MouseEventArgs e) { base.OnMouseUp(e); LineObj line = new LineObj(this.m_startPoint, e.Location); this.lineList.Add(line); } } }

111,126

社区成员

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

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

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