【问题标题】:Get mouse position on screen (no application window) WPF获取屏幕上的鼠标位置(无应用程序窗口)WPF
【发布时间】:2020-07-06 11:28:08
【问题描述】:

我创建的应用程序将在后台运行。单击按钮时,我想在鼠标所在的位置显示窗口。我必须相对于整个屏幕而不是某个窗口。

在所有帖子中都会有人插入使用:

System.Windows.Forms.Control.MousePosition;

但我不能在 wpf 中使用它。

对我来说,它甚至可以从命令行或 power shell 中获取。

【问题讨论】:

  • 为了使用System.Windows.Forms.Control.MousePosition,您需要将程序集引用添加到System.DrawingSystem.Windows.Forms

标签: c# wpf


【解决方案1】:

我知道有两种解决方案,一种可能更糟糕的是根据寡妇位置和光标窗口位置来计算它。

另一个,使用PInvoke

static class Cursor {
    internal struct POINT {
        public int X;
        public int Y;
    }

    [DllImport( "user32.dll" )]
    static extern bool GetCursorPos ( out POINT lpPoint );

    [DllImport( "user32.dll" )]
    static extern bool SetCursorPos ( int X, int Y );

    public static POINT Position {
        get {
            GetCursorPos( out POINT point );
            return point;
        }
        set {
            SetCursorPos( value.X, value.Y );
        }
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-25
    • 2010-10-12
    • 2018-10-26
    • 2017-11-19
    • 1970-01-01
    相关资源
    最近更新 更多