C#光标突出显示/关注者

Sha*_*air 3 .net c# highlighting cursor

如何突出显示系统光标?像许多屏幕录制应用程序一样.理想情况下,我想在它周围显示一个光环.谢谢

Chr*_*lor 6

对于纯托管解决方案,以下代码将在当前鼠标光标位置的桌面上绘制一个椭圆.

Point pt = Cursor.Position; // Get the mouse cursor in screen coordinates

using (Graphics g = Graphics.FromHwnd(IntPtr.Zero))
{        
  g.DrawEllipse(Pens.Black, pt.X - 10, pt.Y - 10, 20, 20);
}
Run Code Online (Sandbox Code Playgroud)

通过使用计时器,您可以每20ms更新鼠标位置,并绘制新的空心(椭圆).

还有其他更有效的方法我可以想到,但它们需要使用系统挂钩的无人值守的代码.有关详细信息,请查看SetWindowsHookEx.

更新:以下是我在评论中描述的解决方案示例,这只是粗略的,可以用于测试目的.

  public partial class Form1 : Form
  {
    private HalloForm _hallo;
    private Timer _timer;

    public Form1()
    {
      InitializeComponent();
      _hallo = new HalloForm();
      _timer = new Timer() { Interval = 20, Enabled = true };
      _timer.Tick += new EventHandler(Timer_Tick);
    }

    void Timer_Tick(object sender, EventArgs e)
    {
      Point pt = Cursor.Position;
      pt.Offset(-(_hallo.Width / 2), -(_hallo.Height / 2));
      _hallo.Location = pt;

      if (!_hallo.Visible)
      {
        _hallo.Show();
      }
    }    
  }

  public class HalloForm : Form
  {        
    public HalloForm()
    {
      TopMost = true;
      ShowInTaskbar = false;
      FormBorderStyle = FormBorderStyle.None;
      BackColor = Color.LightGreen;
      TransparencyKey = Color.LightGreen;
      Width = 100;
      Height = 100;

      Paint += new PaintEventHandler(HalloForm_Paint);
    }

    void HalloForm_Paint(object sender, PaintEventArgs e)
    {      
      e.Graphics.DrawEllipse(Pens.Black, (Width - 25) / 2, (Height - 25) / 2, 25, 25);
    }
  }
Run Code Online (Sandbox Code Playgroud)