C# 如何在 WPF 中获取鼠标在屏幕上的位置?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/29822020/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-09 04:18:10  来源:igfitidea点击:

How to get mouse position on screen in WPF?

c#.netwpfc#-4.0mouse-position

提问by Ziya Ceferov

It works within a specific control, but it doesn't work out the specific control.

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

How to get mouse position and use mouse events independently of any control just directly from screen (without Platform Invoke)?

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

2 needed points:

2个需要点:

  1. Mouse events when mouse is not within a control, but on a screen.
  2. Mouse position when mouse is not within a control, but on a screen.
  1. 当鼠标不在控件内,而是在屏幕上时的鼠标事件。
  2. 当鼠标不在控件内,而是在屏幕上时的鼠标位置。

It should be solved without using Platform Invoke.

它应该在不使用平台调用的情况下解决。

Next two don't work:

接下来两个不起作用:

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

Doesn't get mouse position out a specific control.

不会从特定控件中获取鼠标位置。

System.Windows.Forms.Cursor.Position.X

System.Windows.Forms.Cursor.Positiondoesn't work because it has no types in a WPF app, but it works in a Windows Forms app.

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

IntelliSense gets System.Windows.Forms.Cursor.Position, but it doesn't get any type of Position, hence I can't get:

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

Position.X    
Position.Y

and

Point pointToWindow = Mouse.GetPosition(this);

Point pointToScreen = PointToScreen(pointToWindow);

Doesn't get mouse position out a specific control.

不会从特定控件中获取鼠标位置。

采纳答案by Hossein Narimani Rad

Using MouseDownevent of a control you can try this:

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

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

EDIT:You can capture mouse event to a specific control using Mouse.Capture(YourControl);so it will capture the mouse events even if it is not on that control. Here is the link

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

回答by Rahul Tripathi

You can use PointToScreen

您可以使用PointToScreen

Converts a Point that represents the current coordinate system of the Visual into a Point in screen coordinates.

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

Something like this:

像这样的东西:

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

Do note that Mouse.GetPositionreturns a Point, and PointToScreenconverts the point to the screen coordinate

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

EDIT:

编辑:

You can use the Mouse.Capture(SepcificControl);. From MSDN

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

Captures mouse input to the specified element.

捕获鼠标输入到指定元素。

回答by yu yang Jian

I have little new found,

我几乎没有新发现,

Code is below, fisrt build and run the Window ,

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

then just wheel your mouse one time on the window to invoke the endless screen detect of the Mouse Position.

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

(Thus I didn't find the way to detect mouse event out of the control in the second point of the question, but similar use an endless thread.)

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

But I just use a little skill to enable Windows.Forms in WPF Project, by simply use the Forms code in pure method, then refer that method in the Event Code Block.

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

.

.

Here's the Code:

这是代码:

Add two references to project:

向项目添加两个引用:

System.Drawing 
System.Windows.Forms

Xaml part:

Xml部分:

<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>

Class Code:

班级代码:

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();
        }
    }

回答by l33t

Why complicate things? Just pass nullto get screen coordinates:

为什么要把事情复杂化?只需传递null即可获取屏幕坐标:

private void MouseCordinateMethod(object sender, MouseEventArgs e)
{
    var screenPos = e.GetPosition(null);
    // ...
}