LoginSignup
0
0

More than 1 year has passed since last update.

WPF MouseMove で画面外に出てもマウスの動きを取得したい

Last updated at Posted at 2022-12-08

はじめに

学校の専科でWPFの授業があります。
DirectX11をWPFで制御できるようにするツールを制作しているのですが、

3Dのカメラ操作がしずらい!

というのも、マウスが画面から出ると動かせなくなります。
これをなんとか対処したいと考え約2時間で解決したので備忘録として記事にします。

image.png

問題点

マウスの動きがMouseMove のイベントがある部分の UI上に限定されてしまっている。

解決法

  • MouseMove の最中はマウスをキャプチャする
  • MouseUp でキャプチャから開放する

キャプチャ? 開放? 何言ってんの? はい。

マウスのキャプチャとは

マウスの位置によらず、
コントロールが全てのマウス入力コマンドを受け取るようにマウスを捕捉すること

だそうです。↓のサイトから引用しました。
https://atmarkit.itmedia.co.jp/ait/articles/1103/01/news124.html

解決していく

MouseMove を使用している場所に
MouseUp イベントを追加する

image.png

image.png

MouseMove

	UIElement el = (UIElement)sender;
	
	// Point mousePosition = e.GetPosition(host); ↓に置き換える
	Point mousePosition = e.MouseDevice.GetPosition(el);

    // ~~~ マウスの処理 ~~~

    el.CaptureMouse();

MouseUp

	UIElement el = (UIElement)sender;
    el.ReleaseMouseCapture();

懸念点

  • ドラッグアンドドロップの挙動になんか在るっぽいっていう記事をどこかでみた
    • けど、自分のドラッグアンドドロップには影響なかった。
0
0
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
0
0