I edited answer of ang elus a bit, it works for me now, maybe it would for you.
using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Forms;
using MouseEventArgs = System.Windows.Forms.MouseEventArgs;
using ScrollEventArgs = System.Windows.Forms.ScrollEventArgs;
using ScrollEventType = System.Windows.Forms.ScrollEventType;
namespace WpfApp1
{
public static class Win32Api
{
[StructLayout(LayoutKind.Sequential)]
public class POINT
{
public int X;
public int Y;
}
[StructLayout(LayoutKind.Sequential)]
public class MouseHookStruct
{
public POINT pt;
public int hwnd;
public int wHitTestCode;
public int dwExtraInfo;
}
public delegate int HookProc(int nCode, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int SetWindowsHookEx(int idHook, HookProc lpfn, IntPtr hInstance, int threadId);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern bool UnhookWindowsHookEx(int idHook);
[DllImport("user32.dll", CharSet = CharSet.Auto, CallingConvention = CallingConvention.StdCall)]
public static extern int CallNextHookEx(int idHook, int nCode, IntPtr wParam, IntPtr lParam);
}
public class MouseHook
{
private Win32Api.POINT point;
private Win32Api.POINT POINT
{
//get { return point; }
set
{
if (point != value)
{
point = value;
if (MouseMoveEvent != null)
{
var e = new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0);
MouseMoveEvent(this, e);
//MouseClickEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
//MouseDownEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
//MouseUpEvent(this, new MouseEventArgs(MouseButtons.None, 0, point.X, point.Y, 0));
}
}
}
}
private int hHook;
private const int WM_MOUSEMOVE = 0x200;
private const int WM_LBUTTONDOWN = 0x201;
private const int WM_RBUTTONDOWN = 0x204;
private const int WM_MBUTTONDOWN = 0x207;
private const int WM_LBUTTONUP = 0x202;
private const int WM_RBUTTONUP = 0x205;
private const int WM_MBUTTONUP = 0x208;
private const int WM_LBUTTONDBLCLK = 0x203;
private const int WM_RBUTTONDBLCLK = 0x206;
private const int WM_MBUTTONDBLCLK = 0x209;
private const int WM_MOUSEWHEEL = 0x20A;
public const int WH_MOUSE_LL = 14;
public Win32Api.HookProc hProc;
public MouseHook()
{
this.POINT = new Win32Api.POINT();
}
public int SetHook()
{
hProc = new Win32Api.HookProc(MouseHookProc);
hHook = Win32Api.SetWindowsHookEx(WH_MOUSE_LL, hProc, IntPtr.Zero, 0);
return hHook;
}
public void UnHook()
{
Win32Api.UnhookWindowsHookEx(hHook);
}
private IntPtr wParam;
private IntPtr lParam;
private int MouseHookProc(int nCode, IntPtr wParam, IntPtr lParam)//, LPARAM Addtl_Info_L
{
this.wParam = wParam;
this.lParam = lParam;
Win32Api.MouseHookStruct MyMouseHookStruct = (Win32Api.MouseHookStruct)Marshal.PtrToStructure(this.lParam, typeof(Win32Api.MouseHookStruct));
if (nCode < 0)
{
return Win32Api.CallNextHookEx(hHook, nCode, this.wParam, this.lParam);
}
else
{
if (MouseClickEvent != null)
{
MouseButtons button = MouseButtons.None;
int clickCount = 0;
switch ((Int32)this.wParam)
{
case WM_LBUTTONDOWN:
button = MouseButtons.Left;
clickCount = 1;
if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_RBUTTONDOWN:
button = MouseButtons.Right;
clickCount = 1;
if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_MBUTTONDOWN:
button = MouseButtons.Middle;
clickCount = 1;
if (MouseDownEvent != null) MouseDownEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_LBUTTONUP:
button = MouseButtons.Left;
clickCount = 1;
if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_RBUTTONUP:
button = MouseButtons.Right;
clickCount = 1;
if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_MBUTTONUP:
button = MouseButtons.Middle;
clickCount = 1;
if (MouseUpEvent != null) MouseUpEvent(this, new MouseEventArgs(button, clickCount, point.X, point.Y, 0));
break;
case WM_MOUSEWHEEL:
MSLLHOOKSTRUCT mouseData = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(this.lParam, typeof(MSLLHOOKSTRUCT));
Console.WriteLine("out " + mouseData.mouseData + " " + mouseData.dwExtraInfo + " " + mouseData.flags + " " + mouseData.pt);
if (MousescrollEvent != null) MousescrollEvent(this, new ScrollEventArgs(ScrollEventType.SmallIncrement, mouseData.pt.Y < 1e-316?1:-1));
break;
default:
break;
}
var e = new MouseEventArgs(button, clickCount, point.X, point.Y, 0);
MouseClickEvent(this, e);
}
this.POINT = new Win32Api.POINT { X=MyMouseHookStruct.pt.X, Y=MyMouseHookStruct.pt.Y };
return Win32Api.CallNextHookEx(hHook, nCode, this.wParam, this.lParam);
}
}
[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public Point pt; //<-this is what needed (Y part to be precise)
public int mouseData; //<-this is time difference
public int flags; //<-this and lower, always constant, don't know what is it
public int time;
public long dwExtraInfo;
}
public delegate void MouseMoveHandler(object sender, MouseEventArgs e);
public event MouseMoveHandler MouseMoveEvent;
public delegate void MouseClickHandler(object sender, MouseEventArgs e);
public event MouseClickHandler MouseClickEvent;
public delegate void MouseDownHandler(object sender, MouseEventArgs e);
public event MouseDownHandler MouseDownEvent;
public delegate void MouseUpHandler(object sender, MouseEventArgs e);
public event MouseUpHandler MouseUpEvent;
public delegate void MousescrollHandler(object sender, ScrollEventArgs e);
public event MousescrollHandler MousescrollEvent;
}
}
And this is test window
using System.Windows;
using System.Windows.Forms;
namespace WpfApp1
{
public partial class MainWindow : Window
{
MouseHook mhM = new MouseHook();
public MainWindow()
{
InitializeComponent();
mhM.SetHook();
mhM.MouseClickEvent += (object sender, MouseEventArgs e) =>
{ label1.Content = "" + e.Button + " " + e.Clicks + " " + e.X + " " + e.Y; };
mhM.MousescrollEvent += (object sender, ScrollEventArgs e) =>
{ label2.Content = "" + e.NewValue; };
Timer timer = new Timer();
timer.Interval = 1000000;
timer.Tick += Timer_Tick;
timer.Start();
}
private void Timer_Tick(object sender, System.EventArgs e)
{
mhM.UnHook();
}
}
}