鹿我所录的博客

记录我在学习C#中的点点滴滴,记录下以备后来人借鉴。

 

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

posted on 2014-01-04 14:07  鹿我所录  阅读(8336)  评论(0)    收藏  举报

导航