LoginSignup
3
3

More than 3 years have passed since last update.

WPFでカーソルの位置を取得する(C#)

Last updated at Posted at 2019-10-11

はじめに

ウィンドウ外にカーソルがあってもその位置を取得する方法です。
調べてもすぐに出てこなかったため、備忘録として残します。

動作環境

  • Visual Studio Community 2019 - 16.3.2
  • .NET Framework - 4.7.03190

方法

System.Windows.Formsを使用します。まずはソリューションエクスプローラーから参照を右クリックし、参照の追加を選択します。
キャプチャ.PNG

検索欄から"Forms"等と入力して、System.Windows.FormsチェックしてOKを選択します。
キャプチャ2.PNG

あとはSystem.Windows.Forms.Cursor.Positionで場所が取得できます。

System.Drawing.Point p = System.Windows.Forms.Cursor.Position;

// Labelを作成し、そこに出力するサンプル
label.Content = "(x, y) = ("+ p.X +", "+ p.Y +")"; // p.X, p.Yで取得できます。

System.Drawing.Pointで怒られたら同じく参照の追加からSystem.Drawing.Pointを持ってくると大丈夫です。

3
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
3
3