[英]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.