C# 鼠标移动时重新绘制图形路径
我已经在PaintEvent上使用图形路径绘制了圆形矩形 我已经添加了mouseevent,以了解光标是否在g.p.上C# 鼠标移动时重新绘制图形路径,c#,winforms,graphicspath,C#,Winforms,Graphicspath,我已经在PaintEvent上使用图形路径绘制了圆形矩形 我已经添加了mouseevent,以了解光标是否在g.p.上 void Round_MouseMove(object sender, MouseEventArgs e) { Point mousePt = new Point(e.X, e.Y); if (_path != null) if (_path.IsVisible(e.Location))
void Round_MouseMove(object sender, MouseEventArgs e)
{
Point mousePt = new Point(e.X, e.Y);
if (_path != null)
if (_path.IsVisible(e.Location))
MessageBox.Show("GraphicsPath has been hovered!");
}
问题:是否有一种方法可以调整或重画(先隐藏上一页,然后绘制新的)graphicsPath运行时?调用
使表单无效
,以便执行OnPaint(PaintEventArgs e)
检查以下示例:
public sealed partial class GraphicsPathForm : Form
{
private bool _graphicsPathIsVisible;
private readonly Pen _pen = new Pen(Color.Red, 2);
private readonly Brush _brush = new SolidBrush(Color.FromArgb(249, 214, 214));
private readonly GraphicsPath _graphicsPath = new GraphicsPath();
private Rectangle _rectangle = new Rectangle(10, 30, 100, 100);
public GraphicsPathForm()
{
InitializeComponent();
_graphicsPath.AddRectangle(_rectangle);
}
protected override void OnPaint(PaintEventArgs e)
{
var g = e.Graphics;
g.CompositingQuality = CompositingQuality.HighQuality;
g.InterpolationMode = InterpolationMode.Bilinear;
g.SmoothingMode = SmoothingMode.AntiAlias;
g.DrawPath(_pen, _graphicsPath);
if (_graphicsPathIsVisible)
g.FillPath(_brush, _graphicsPath);
base.OnPaint(e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
var isVisible = _graphicsPath.IsVisible(e.Location);
if (isVisible == _graphicsPathIsVisible)
return;
const int zoom = 5;
if (isVisible)
{
if (!_graphicsPathIsVisible)
{
_rectangle.Inflate(zoom, zoom);
_graphicsPath.Reset();
_graphicsPath.AddRectangle(_rectangle);
}
}
else
{
if (_graphicsPathIsVisible)
{
_rectangle.Inflate(-zoom, -zoom);
_graphicsPath.Reset();
_graphicsPath.AddRectangle(_rectangle);
}
}
_graphicsPathIsVisible = isVisible;
Invalidate();
base.OnMouseMove(e);
}
}
我希望它能有所帮助。Winforms渲染的一般规则:仅在Paint
事件或OnPaint()中绘制override;在某处跟踪您想要绘制的内容(例如位图、点列表、对象等);当您希望重新绘制屏幕时(即,当您跟踪要绘制的内容发生更改时),调用Invalidate()
。从你的问题来看,我猜你违反了所有这些规则(对于新程序员来说,这是一个常见的错误)。因此,第一步是理解这些规则,学习解释这些规则存在的原因以及如何在其中工作的教程和文档。我看到了这些规则,并且它起了作用!泰。。。它充满了每一个鼠标移动的颜色。。。但如果悬停,矩形的大小是可以改变的吗?@Elegiac是的,当然可以。您需要将矩形存储在变量中,更改大小并再次添加到\u graphicsPath
。我将尝试修改我的示例。