[英]how to highlight the mouse pointer position using c#
I need a simple mouse pointer highlighter in the form of a circle centered at the mouse pointer. 我需要一个简单的鼠标指针荧光笔,以鼠标指针为中心的圆形。 Using Invalidate() in the below code causes rear circles along the path as flickers.
在下面的代码中使用Invalidate()会导致沿着路径的后方圆圈闪烁。 They are hardly noticeable.
它们几乎不引人注意。 Moreover, it doesn't draw the circle at the moment i rest the mouse.
而且,在我休息鼠标时,它不会绘制圆圈。
What event should I consider to draw the circle at the position the mouse pointer is rested (tried other mouse events as well)? 我应该考虑在鼠标指针休息位置绘制圆圈的事件(尝试其他鼠标事件)?
Is there a way to refresh the drawing without using 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();
}
First turn on DoubleBuffered = true;
首先打开
DoubleBuffered = true;
Then this should do: 然后这应该做:
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);
}
Since you are trying to highlight a spot on the background you may want to use a semi-transparent Color like this: 由于您尝试在背景上突出显示某个点,因此您可能需要使用半透明颜色,如下所示:
using (SolidBrush brush = new SolidBrush(Color.FromArgb(160, Color.Yellow)))
e.Graphics.FillEllipse(brush, pt.X - radius, pt.Y - radius, radius * 2, radius * 2);
You may want to play with other values for alpha than 160. 您可能希望使用alpha的其他值而不是160。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.