7
我要滾動ScrollViewer控件,而不是專注於應用程序窗口。我發現在Windows窗體的一些解決方案,但不是在WPF因爲一些我需要的東西都沒有。WPF - 滾動沒有焦點
也許這裏有人可以幫助我解決我的問題?
在此先感謝。
編輯:
我要滾動鼠標滾輪的ScrollViewer控件,而鼠標在ScrollViewer中,也同時不集中的應用程序窗口。
我要滾動ScrollViewer控件,而不是專注於應用程序窗口。我發現在Windows窗體的一些解決方案,但不是在WPF因爲一些我需要的東西都沒有。WPF - 滾動沒有焦點
也許這裏有人可以幫助我解決我的問題?
在此先感謝。
編輯:
我要滾動鼠標滾輪的ScrollViewer控件,而鼠標在ScrollViewer中,也同時不集中的應用程序窗口。
我找到了解決辦法。下面一個例子:
XAML
<Window x:Class="Project.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" Height="300" Width="500">
<ScrollViewer x:Name="ScrollViewer">
<StackPanel>
<Border Background="Blue" Width="100" Height="100" />
<Border Background="Red" Width="100" Height="100" />
<Border Background="Blue" Width="100" Height="100" />
<Border Background="Red" Width="100" Height="100" />
<Border Background="Blue" Width="100" Height="100" />
<Border Background="Red" Width="100" Height="100" />
</StackPanel>
</ScrollViewer>
</Window>
代碼隱藏
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Input;
using System.Windows.Interop;
namespace Project
{
public partial class MainWindow : Window
{
private const int WH_MOUSE_LL = 14;
private const int WM_MOUSEWHEEL = 0x020A;
private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);
private IntPtr _handle;
public MainWindow()
{
InitializeComponent();
_handle = new WindowInteropHelper(this).Handle;
SetWindowsHookEx(WH_MOUSE_LL, MouseChanged, _handle, 0);
ScrollViewer.PreviewMouseWheel += ScrollViewer_MouseWheel;
Unloaded += MainWindow_Unloaded;
}
private void MainWindow_Unloaded(object sender, RoutedEventArgs e)
{
UnhookWindowsHookEx(_handle);
}
private void ScrollViewer_MouseWheel(object sender, MouseWheelEventArgs e)
{
e.Handled = true;
}
private IntPtr MouseChanged(int nCode, IntPtr wParam, IntPtr lParam)
{
if (ScrollViewer.IsMouseOver && WM_MOUSEWHEEL == (int)wParam)
{
MouseData mouseData = (MouseData)Marshal.PtrToStructure(lParam, typeof(MouseData));
ScrollViewer.ScrollToVerticalOffset(ScrollViewer.VerticalOffset - mouseData.Z/200000.0);
}
return CallNextHookEx(_handle, nCode, wParam, lParam);
}
[StructLayout(LayoutKind.Sequential)]
private struct MouseData
{
public int X;
public int Y;
public int Z;
}
[DllImport("user32.dll")]
private static extern IntPtr SetWindowsHookEx(int idHook, LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);
[DllImport("user32.dll")]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);
[DllImport("user32.dll")]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode, IntPtr wParam, IntPtr lParam);
}
}
你要滾動的'ScrollViewer' *編程*,或者你希望用戶能夠即使它不是前景窗口,也可以用鼠標滾動它? –
看到我的編輯,我認爲它會回答你的問題。 – Werewolve