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