Juddering mouse with global mouse hook issue

  • Thread starter Thread starter scocia888
  • Start date Start date
S

scocia888

Guest
I am running UIA application using background MTA threads for the UIA processing and uia event handlers (structural change, focus change). I am running the UIA processing on various events being fired including uia events like structural change.I also run the processing on global events like window location change and a mouse click using the global mouse hook below. On getting the results of the UIA processing I am drawing markers on the display in the main thread in the respective event handlers. With uia events and other global events like changing of active window and window location changed as examples everything works fine if not starting the mousehook. However on running the UIA processing and then drawing the markers on the display after a mouse click when running the mousehook I am getting jittery mouse movement when drawing my markers, sometimes the mouse locks before moving again.

I noticed the hookcallback is called with every mouse movement as well as mouse clicks. Is it possible to ignore the mouse movements as opposed to filtering mouse movements (which I have done in the code below)? Or any other suggestions as to how I can stop the mouse juddering when my main thread puts the markers on the screen after a mouse click?


private void MouseHook_MouseAction(object sender, EventArgs e)
{
//do my stuff here
}


MouseHook.MouseAction += new EventHandler(MouseHook_MouseAction);
MouseHook.Start();

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.InteropServices;
using System.Diagnostics;
using System.Threading;

namespace SpeechStart_Pro
{
public static class MouseHook
{
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SetWindowsHookEx(int idHook,
LowLevelMouseProc lpfn, IntPtr hMod, uint dwThreadId);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
[return: MarshalAs(UnmanagedType.Bool)]
private static extern bool UnhookWindowsHookEx(IntPtr hhk);

[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr CallNextHookEx(IntPtr hhk, int nCode,
IntPtr wParam, IntPtr lParam);

[DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr GetModuleHandle(string lpModuleName);

public static event EventHandler MouseAction = delegate { };

private const int WH_MOUSE_LL = 14;

private enum MouseMessages
{
WM_LBUTTONDOWN = 0x0201,
// WM_LBUTTONUP = 0x0202,
// WM_MOUSEMOVE = 0x0200,
// WM_MOUSEWHEEL = 0x020A,
WM_RBUTTONDOWN = 0x0204,
// WM_RBUTTONUP = 0x0205*/
}

[StructLayout(LayoutKind.Sequential)]
private struct POINT
{
public int x;
public int y;
}

[StructLayout(LayoutKind.Sequential)]
private struct MSLLHOOKSTRUCT
{
public POINT pt;
public uint mouseData;
public uint flags;
public uint time;
public IntPtr dwExtraInfo;
}

public static void Start()
{
_hookID = SetHook(_proc);

}
public static void stop()
{
UnhookWindowsHookEx(_hookID);
}

private static LowLevelMouseProc _proc = HookCallback;
private static IntPtr _hookID = IntPtr.Zero;

private static IntPtr SetHook(LowLevelMouseProc proc)
{
using (Process curProcess = Process.GetCurrentProcess())
using (ProcessModule curModule = curProcess.MainModule)
{
IntPtr hook = SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle("user32"), 0);
if (hook == IntPtr.Zero) throw new System.ComponentModel.Win32Exception();
return hook;
}
}

private delegate IntPtr LowLevelMouseProc(int nCode, IntPtr wParam, IntPtr lParam);

private static IntPtr HookCallback(
int nCode, IntPtr wParam, IntPtr lParam)
{
if (nCode >= 0 && (MouseMessages.WM_LBUTTONDOWN == (MouseMessages)wParam || MouseMessages.WM_RBUTTONDOWN == (MouseMessages)wParam))
{

MSLLHOOKSTRUCT hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
MouseAction(null, new EventArgs());
}
return CallNextHookEx(_hookID, nCode, wParam, lParam);
}

}
}

Continue reading...
 

Similar threads

Back
Top