gpt4 book ai didi

c# - 当鼠标指针不在窗口中时,如何接收 MouseWheel 事件?

转载 作者:太空宇宙 更新时间:2023-11-03 15:38:59 26 4
gpt4 key购买 nike

我注意到我的 MouseWheel 事件处理程序仅在鼠标指针位于窗口中时执行。即使指针在窗口外,我也希望它运行处理程序。

我的程序中只有一个窗口,它的 XAML 结构如下:

<Window ...
MouseWheel = "Window_MouseWheel">
....
</Window>

最佳答案

为了在光标离开窗口时接收到鼠标事件,窗口需要“捕获”鼠标。它通过将自身传递给 Mouse.Capture() 来做到这一点方法。

请注意,如果您的程序窗口失去激活(即用户任务切换到其他程序),它将失去鼠标捕获。如果您想在用户稍后重新激活窗口时再次自动跟踪鼠标滚轮,则需要专门处理它(即处理 Activated 事件)。

最后请注意,启用鼠标捕获后,通过鼠标与窗口的正常交互将不起作用,Alt-F4 也不起作用。您需要为用户提供一些额外的与程序交互的机制(例如处理按键事件)。

下面是一个简单的示例程序,展示了基本思想:

XAML:

<Window x:Class="TestSO30866523CaptureMouseWheel.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
xmlns:l="clr-namespace:TestSO30866523CaptureMouseWheel"
DataContext="{Binding RelativeSource={RelativeSource Self}}"
Activated="Window_Activated"
KeyDown="Window_KeyDown"
MouseWheel="Window_MouseWheel"
Title="MainWindow" Height="350" Width="525">
<StackPanel x:Name="stackPanel1">
<TextBlock Text="The window cannot be closed while mouse is captured."/>
<TextBlock Text="Press Escape to stop capture. Press Return to resume capture."/>
<TextBlock Text="{Binding Value, StringFormat=Value: {0}}"/>
<TextBlock Text="{Binding MouseCaptured, StringFormat=Mouse is captured: {0}}"/>
</StackPanel>
</Window>

C#:

public partial class MainWindow : Window
{
public static readonly DependencyProperty ValueProperty = DependencyProperty.Register(
"Value", typeof(int), typeof(MainWindow), new PropertyMetadata(0));
public static readonly DependencyProperty MouseCapturedProperty = DependencyProperty.Register(
"MouseCaptured", typeof(bool), typeof(MainWindow));

public int Value
{
get { return (int)GetValue(ValueProperty); }
set { SetValue(ValueProperty, value); }
}

public bool MouseCaptured
{
get { return (bool)GetValue(MouseCapturedProperty); }
set { SetValue(MouseCapturedProperty, value); }
}

private readonly IInputElement _captureElement;

public MainWindow()
{
InitializeComponent();

_captureElement = this;

Mouse.AddGotMouseCaptureHandler((DependencyObject)_captureElement, stackPanel1_GotLostMouseCapture);
Mouse.AddLostMouseCaptureHandler((DependencyObject)_captureElement, stackPanel1_GotLostMouseCapture);
Mouse.Capture(_captureElement);
MouseCaptured = Mouse.Captured != null;
}

private void stackPanel1_GotLostMouseCapture(object sender, MouseEventArgs e)
{
MouseCaptured = Mouse.Captured != null;
}

private void Window_MouseWheel(object sender, MouseWheelEventArgs e)
{
Value += e.Delta;
}

private void Window_Activated(object sender, EventArgs e)
{
Mouse.Capture(_captureElement);
}

private void Window_KeyDown(object sender, KeyEventArgs e)
{
switch (e.Key)
{
case Key.Escape:
Mouse.Capture(null);
break;
case Key.Return:
Mouse.Capture(_captureElement);
break;
}
}
}

当鼠标被捕获时,无论光标放在哪里,窗口都会响应鼠标滚轮事件。当鼠标未被捕获时,只有当光标在窗口上时才响应鼠标滚轮事件。

关于c# - 当鼠标指针不在窗口中时,如何接收 MouseWheel 事件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30866523/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com