[英]How to get mouse position on screen in WPF?
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个需要点:
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.Position
doesn'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.不会从特定控件中获取鼠标位置。
Using MouseDown
event 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);
编辑:您可以使用Mouse.Capture(YourControl);
鼠标事件捕获到特定控件Mouse.Capture(YourControl);
so it will capture the mouse events even if it is not on that control.所以即使它不在那个控件上,它也会捕获鼠标事件。 Here is the link这是链接
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.GetPosition
returns a Point, and PointToScreen
converts the point to the screen coordinate请注意Mouse.GetPosition
返回一个 Point, PointToScreen
将点转换为屏幕坐标
EDIT:编辑:
You can use the Mouse.Capture(SepcificControl);
您可以使用Mouse.Capture(SepcificControl);
. . From MSDN来自MSDN
Captures mouse input to the specified element.捕获鼠标输入到指定元素。
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();
}
}
Why complicate things?为什么要把事情复杂化? Just pass null
to get screen coordinates:只需传递null
即可获取屏幕坐标:
private void MouseCordinateMethod(object sender, MouseEventArgs e)
{
var screenPos = e.GetPosition(null);
// ...
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.