C# 光标突出显示/跟随者

发布于 2024-08-28 04:40:54 字数 52 浏览 5 评论 0原文

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

How to highlight the system cursor? Like many screen recording applications do. Ideally, I'd like to display a halo around it.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

無處可尋 2024-09-04 04:40:54

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

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);
}

例如,通过使用计时器,您可以每 20 毫秒更新一次鼠标位置并绘制新的空心(椭圆)。

我能想到其他更有效的方法,但它们需要使用系统挂钩的未修改代码。有关详细信息,请查看 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);
    }
  }

For a purely managed solution, the following code will draw an ellipse on the desktop at the current mouse cursor position.

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);
}

By using a timer you can update the mouse position every 20ms for example and draw the new hallow (ellipse).

There are other more efficient ways that I can think of, but they would require unamanged code using system hooks. Take a look at SetWindowsHookEx for more info on this.

Update: Here is a sample of the solution I described in my comments, this is just rough and ready for testing purposes.

  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);
    }
  }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文