C# 如何获取鼠标在屏幕上的位置,不论程序是否为活动状态

一开始我认为应该使用HOOK来写,而且必须使用全局HOOK,结果在一次偶然的机会得到,原来其实根本没有那个必要。

直接上代码吧,一看就明白

            Point ms = Control.MousePosition;
            this.label2.Text = string.Format("{0}:{1}", ms.X, ms.Y);
            MouseButtons mb=  Control.MouseButtons;

            if (mb == System.Windows.Forms.MouseButtons.Left) this.label3.Text = "Left";
            if (mb == System.Windows.Forms.MouseButtons.Right) this.label3.Text = "Right";
            if (mb == System.Windows.Forms.MouseButtons.Middle) this.label3.Text = "Middle";

这里面 Control.MouseButtons 是关键代码

将以上代码写入到 TIMER的Tick过程中,就可以实时显示鼠标所在位置,无论鼠标在哪个位置。

原文地址:https://www.cnblogs.com/lujin49/p/3504805.html