WPF で現在のマウス画面の座標を取得するにはどうすればよいですか?
-
26-09-2019 - |
質問
画面上の現在のマウスの調整を取得するにはどうすればよいですか?私だけが知っています Mouse.GetPosition()
要素のmousePositionを取得するのですが、要素を使わずに座標を取得したいです。
解決
レイチェルの答えにフォローアップする。
ここでは、WPFでマウスのスクリーン座標を取得することが可能な2つの方法があります。
1.Using Windowsフォーム。 System.Windows.Formsへの参照を追加します。
public static Point GetMousePositionWindowsForms()
{
System.Drawing.Point point = Control.MousePosition;
return new Point(point.X, point.Y);
}
2.UsingのWin32
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
internal static extern bool GetCursorPos(ref Win32Point pt);
[StructLayout(LayoutKind.Sequential)]
internal struct Win32Point
{
public Int32 X;
public Int32 Y;
};
public static Point GetMousePosition()
{
Win32Point w32Mouse = new Win32Point();
GetCursorPos(ref w32Mouse);
return new Point(w32Mouse.X, w32Mouse.Y);
}
他のヒント
または純粋なWPFの使用中の PointToScreenするます。
サンプルヘルパーメソッド:
// Gets the absolute mouse position, relative to screen
Point GetMousePos(){
return _window.PointToScreen(Mouse.GetPosition(_window))
}
は、画面やアプリケーションへの相対座標をしたいですか?
それはアプリケーション内だ場合だけ使用します:
Mouse.GetPosition(Application.Current.MainWindow);
でない場合は、私はあなたがSystem.Windows.Forms
と使用への参照を追加することができます信じています:
System.Windows.Forms.Control.MousePosition;
あなたは、彼らが確実に動作しないことがあります。あなたが現在の画面、すべてのモニターで構成されていない表示領域全体を基準にしたマウス位置を取得するために変換を使用する必要があるためです。このような何か...(ここで、 "これは" WPFウィンドウです)。
var transform = PresentationSource.FromVisual(this).CompositionTarget.TransformFromDevice;
var mouse = transform.Transform(GetMousePosition());
public System.Windows.Point GetMousePosition()
{
System.Drawing.Point point = System.Windows.Forms.Control.MousePosition;
return new System.Windows.Point(point.X, point.Y);
}
の形式を使用するか、任意のDLLをインポートすることなく、この作品ます:
using System.Windows;
using System.Windows.Input;
/// <summary>
/// Gets the current mouse position on screen
/// </summary>
private Point GetMousePosition()
{
// Position of the mouse relative to the window
var position = Mouse.GetPosition(Window);
// Add the window position
return new Point(position.X + Window.Left, position.Y + Window.Top);
}
TimerDispatcher (WPF タイマーのアナログ) と Windows の「フック」を組み合わせて使用して、オペレーティング システムからカーソル位置を取得できます。
[DllImport("user32.dll")]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool GetCursorPos(out POINT pPoint);
ポイントはライトです struct
. 。これには X、Y フィールドのみが含まれます。
public MainWindow()
{
InitializeComponent();
DispatcherTimer dt = new System.Windows.Threading.DispatcherTimer();
dt.Tick += new EventHandler(timer_tick);
dt.Interval = new TimeSpan(0,0,0,0, 50);
dt.Start();
}
private void timer_tick(object sender, EventArgs e)
{
POINT pnt;
GetCursorPos(out pnt);
current_x_box.Text = (pnt.X).ToString();
current_y_box.Text = (pnt.Y).ToString();
}
public struct POINT
{
public int X;
public int Y;
public POINT(int x, int y)
{
this.X = x;
this.Y = y;
}
}
このソリューションは、パラメータの読み取りが頻繁または低すぎる問題も解決するため、自分で調整できるようになります。ただし、次のことを表す 1 つの引数による WPF メソッドのオーバーロードについて覚えておいてください。 ticks
ない milliseconds
.
TimeSpan(50); //ticks
あなたは1つのライナーを探しているなら、これは良くありません。
new Point(Mouse.GetPosition(mWindow).X + mWindow.Left, Mouse.GetPosition(mWindow).Y + mWindow.Top)
私はコントロールで使用するためにこれを追加します。
private void Control_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
var mousepos = new List<double> { e.GetPosition(ControlName).X, e.GetPosition(ControlName).Y };
}
Mouse.GetPosition(mWindow)
は、あなたの好みのパラメータに対するマウスの位置を与えます。
mWindow.PointToScreen()
画面に点の相対位置を変換します。
mWindow.PointToScreen(Mouse.GetPosition(mWindow))
は(実際には、任意のクラスはmWindow
から派生この機能を持っています)System.Windows.Media.Visual
がウィンドウであると仮定すると、画面にあなたのマウス位置を与えるので、あなたがWPFウィンドウクラス内でこれを使用している場合は、、this
は動作するはずです。