一開始我認為應該使用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過程中,就可以實時顯示鼠標所在位置,無論鼠標在哪個位置。