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过程中,就可以实时显示鼠标所在位置,无论鼠标在哪个位置。


免责声明!

本站转载的文章为个人学习借鉴使用,本站对版权不负任何法律责任。如果侵犯了您的隐私权益,请联系本站邮箱yoyou2525@163.com删除。



 
粤ICP备18138465号  © 2018-2025 CODEPRJ.COM