16

它在特定控件内工作,但不能解决特定控件。

如何直接从屏幕(没有平台调用)独立于任何控件获取鼠标位置和使用鼠标事件?

2点需要:

  1. 当鼠标不在控件内而是在屏幕上时的鼠标事件。
  2. 鼠标不在控件内,而是在屏幕上时的鼠标位置。

它应该在不使用 Platform Invoke 的情况下解决。

接下来的两个不起作用:

System.Windows.Input.Mouse.GetPosition(this)

不会将鼠标位置移出特定控件。

System.Windows.Forms.Cursor.Position.X

System.Windows.Forms.Cursor.Position不起作用,因为它在 WPF 应用程序中没有类型,但它在 Windows 窗体应用程序中有效。

IntelliSense 获得 System.Windows.Forms.Cursor.Position,但它没有获得任何类型的位置,因此我无法获得:

Position.X    
Position.Y

Point pointToWindow = Mouse.GetPosition(this);

Point pointToScreen = PointToScreen(pointToWindow);

不会将鼠标位置移出特定控件。

4

4 回答 4

17

使用MouseDown控件的事件你可以试试这个:

var point = e.GetPosition(this.YourControl);

编辑: 您可以使用将鼠标事件捕获到特定控件,Mouse.Capture(YourControl);因此即使它不在该控件上,它也会捕获鼠标事件。这是链接

于 2015-04-23T11:27:15.793 回答
11

您可以使用PointToScreen

将表示 Visual 的当前坐标系的 Point 转换为屏幕坐标中的 Point。

像这样的东西:

private void MouseCordinateMethod(object sender, MouseEventArgs e)
{
    var relativePosition = e.GetPosition(this);
    var point= PointToScreen(relativePosition);
    _x.HorizontalOffset = point.X;
    _x.VerticalOffset = point.Y;
}

请注意,Mouse.GetPosition返回一个点,PointToScreen并将点转换为屏幕坐标

编辑:

您可以使用Mouse.Capture(SepcificControl);. 来自 MSDN

捕获对指定元素的鼠标输入。

于 2015-04-23T11:29:22.230 回答
1

我几乎没有新发现,

代码如下,首先构建并运行 Window ,

然后只需在窗口上滚动一次鼠标即可调用鼠标位置的无限屏幕检测。

(因此,在问题的第二点中,我没有找到检测鼠标事件失控的方法,但类似地使用了无限线程。)

但是我只是在WPF项目中使用了一点技巧来启用Windows.Forms,只需在纯方法中使用Forms代码,然后在事件代码块中引用该方法。

.

这是代码:

添加两个对项目的引用:

System.Drawing 
System.Windows.Forms

Xaml 部分:

<Window x:Class="Test.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:g="clr-namespace:Gma.UserActivityMonitor;assembly=Gma.UserActivityMonitor"
        Title="MainWindow" Height="350" Width="525" 
        MouseWheel="MainWindow_OnMouseWheel">
    <Grid>
       <TextBlock Name="TBK" /> 
    </Grid>
</Window>

班级代码:

public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        public void KeepReportMousePos()
        {
            //Endless Report Mouse position
            Task.Factory.StartNew(() =>
            {
                while(true){
                    this.Dispatcher.Invoke(
                        DispatcherPriority.SystemIdle,
                        new Action(() =>
                        {
                            GetCursorPos();
                        }));
                }
            });
        }
        public void GetCursorPos()
        {
            //get the mouse position and show on the TextBlock
            System.Drawing.Point p = System.Windows.Forms.Cursor.Position;
            TBK.Text = p.X + " " + p.Y;
        }

        private void MainWindow_OnMouseWheel(object sender, MouseWheelEventArgs e)
        {
            //invoke mouse position detect when wheel the mouse
            KeepReportMousePos();
        }
    }
于 2016-07-21T03:06:30.810 回答
1

为什么要把事情复杂化?只需通过null获取屏幕坐标:

private void MouseCordinateMethod(object sender, MouseEventArgs e)
{
    var screenPos = e.GetPosition(null);
    // ...
}
于 2020-04-02T16:25:56.897 回答