I wrote an article especially for you to know how to handle horizontal scrolling of touchpad in a WPF application. That is Support Horizontal Scrolling of TouchPad in WPF Application - walterlv
We need to fetch WM_MOUSEHWHEEL
message from our WPF window. Yes! That mouse wheel message. We fetch vertical data from it before, but we now fetch horizontal data from it.
At first, we should hook the window message.
protected override void OnSourceInitialized(EventArgs e)
{
var source = PresentationSource.FromVisual(_board);
((HwndSource) source)?.AddHook(Hook);
}
private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
// Handle window message here.
return IntPtr.Zero;
}
Next, handle WM_MOUSEHWHEEL
:
const int WM_MOUSEHWHEEL = 0x020E;
private IntPtr Hook(IntPtr hwnd, int msg, IntPtr wParam, IntPtr lParam, ref bool handled)
{
switch (msg)
{
case WM_MOUSEHWHEEL:
int tilt = (short) HIWORD(wParam);
OnMouseTilt(tilt);
return (IntPtr) 1;
}
return IntPtr.Zero;
}
/// <summary>
/// Gets high bits values of the pointer.
/// </summary>
private static int HIWORD(IntPtr ptr)
{
var val32 = ptr.ToInt32();
return ((val32 >> 16) & 0xFFFF);
}
/// <summary>
/// Gets low bits values of the pointer.
/// </summary>
private static int LOWORD(IntPtr ptr)
{
var val32 = ptr.ToInt32();
return (val32 & 0xFFFF);
}
private void OnMouseTilt(int tilt)
{
// Write your horizontal handling codes here.
}
You can write horizontal scrolling code in OnMouseTilt
method.
Better yet, you could pack all the codes above in a more common class and raise a MouseTilt
event just like raising MouseWheel
event.