3

I need to get mouse position on screen NOT inside my application.

I've used Global mouse and keyboard hook here

It works fine under winforms but won't work under wpf.

I'm using version1 of the code and used the following

var activity = new UserActivityHook();
activity.OnMouseActivity += activity_OnMouseActivity;

but instead of working it give me following error:

Additional information: The specified module could not be found

under for following code

public void Start(bool InstallMouseHook, bool InstallKeyboardHook)
{
    // install Mouse hook only if it is not installed and must be installed
    if (hMouseHook == 0 && InstallMouseHook)
    {
        // Create an instance of HookProc.
        MouseHookProcedure = new HookProc(MouseHookProc);

        //install hook
        hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure,
            Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);
        //If SetWindowsHookEx fails.
        if (hMouseHook == 0)
        {
            //Returns the error code returned by the last unmanaged function called using platform invoke that has the DllImportAttribute.SetLastError flag set. 
            int errorCode = Marshal.GetLastWin32Error();
            //do cleanup
            Stop(true, false, false);
            //Initializes and throws a new instance of the Win32Exception class with the specified error. 
            throw new Win32Exception(errorCode);
        }
    }
}

Is there any alternative for WPF or am I missing something?

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Suraj Bhawal
  • 403
  • 1
  • 6
  • 19

2 Answers2

10

dotctor's answer gives the mouse position, but not the event I was looking for.

So I figured out on my own. Here are my findings for future reference.

We need to change:

//install hook
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure,
             Marshal.GetHINSTANCE(Assembly.GetExecutingAssembly().GetModules()[0]), 0);

to the following:

// Install hook
hMouseHook = SetWindowsHookEx(WH_MOUSE_LL, MouseHookProcedure, IntPtr.Zero, 0);

Alternatively you can just download the edited cs file from here then we can use the event

activity.OnMouseActivity += activity_OnMouseActivity;

under which we can use e.X and e.Y to get mouse position.

AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Suraj Bhawal
  • 403
  • 1
  • 6
  • 19
  • 1
    Link is dead, what a shame. Anything we can do to fix this? (Wayback machine doesn't work). – Issung Nov 29 '19 at 04:31
  • @JG3 sorry. I don't have the files anymore. anyways. you'll just need to edit the code as specified above and you're good to go. – Suraj Bhawal Dec 13 '19 at 12:15
1
  1. Get help from a wizard! (do it the easy way)
    add reference to System.Windows.Forms and use Control.MousePosition

  2. Become an alchemist! (combine your items)
    combine Visual.PointToScreen and Mouse.GetPosition and Application.Current.MainWindow

  3. Use an energy chakra (win32)

     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     internal static extern bool GetCursorPos(ref Win32Point pt);
    
     [StructLayout(LayoutKind.Sequential)]
     internal struct Win32Point
     {
       public Int32 X;
       public Int32 Y;
     };
    
     public static Point GetMousePosition()
     {
        var w32Mouse = new Win32Point();
        GetCursorPos(ref w32Mouse);
    
        return new Point(w32Mouse.X, w32Mouse.Y);
     }
    
AustinWBryan
  • 3,249
  • 3
  • 24
  • 42
Hamid Pourjam
  • 20,441
  • 9
  • 58
  • 74
  • Yes i can get mouse pointer.. Using the 3rd option u gave.. But what i really want is to report mouse position whenever mouse moves.. Kind of global mouse move event as I've shown in the question... Anyways thanks for the reply its the closest i can get. – Suraj Bhawal Nov 26 '14 at 03:53