[英]how to highlight the mouse pointer position using c#
我需要一个简单的鼠标指针荧光笔,以鼠标指针为中心的圆形。 在下面的代码中使用Invalidate()会导致沿着路径的后方圆圈闪烁。 它们几乎不引人注意。 而且,在我休息鼠标时,它不会绘制圆圈。
我应该考虑在鼠标指针休息位置绘制圆圈的事件(尝试其他鼠标事件)?
有没有办法刷新绘图而不使用invalidate()?
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
SolidBrush myBrush = new SolidBrush(Color.Yellow);
Graphics gg = this.CreateGraphics();
Point p = new Point();
p = e.Location;
int radius = 10;
float x = p.X - radius;
float y = p.Y - radius;
float width = 5 * radius;
float height = 5 * radius;
gg.FillEllipse(myBrush, x, y, width, height);
gg.dispose();
Invalidate();
}
首先打开DoubleBuffered = true;
然后这应该做:
private void Form1_MouseMove(object sender, MouseEventArgs e)
{
Invalidate();
}
private void Form1_Paint(object sender, PaintEventArgs e)
{
float radius = 10f;
Point pt = PointToClient(Cursor.Position);
e.Graphics.SmoothingMode = SmoothingMode.HighQuality;
e.Graphics.FillEllipse(Brushes.Yellow, pt.X - radius,
pt.Y - radius, radius * 2, radius * 2);
}
由于您尝试在背景上突出显示某个点,因此您可能需要使用半透明颜色,如下所示:
using (SolidBrush brush = new SolidBrush(Color.FromArgb(160, Color.Yellow)))
e.Graphics.FillEllipse(brush, pt.X - radius, pt.Y - radius, radius * 2, radius * 2);
您可能希望使用alpha的其他值而不是160。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.