prevent the program from hiding the mouse cursor

MainSilent 1 Reputation point
2020-12-07T18:10:44.13+00:00

I'm working on a touchpad program and i avoided mouse position changes by this code:

public partial class MouseHooker
{
    public MouseHooker()
    {
        _hookID = SetHook(_proc);
    }

    private static readonly 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)
        {
            return SetWindowsHookEx(WH_MOUSE_LL, proc, GetModuleHandle(curModule.ModuleName), 0);
        }
    }

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

    const uint TOUCH_FLAG = 0xFF515700;
    private const int WH_MOUSE_LL = 14;

    private static IntPtr HookCallback(int nCode, IntPtr wParam, IntPtr lParam)
    {
        var hookStruct = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
        uint extraInfo = (uint)hookStruct.dwExtraInfo.ToInt32();

        if (nCode >= 0 && (extraInfo & TOUCH_FLAG) == TOUCH_FLAG)
        {
            return new IntPtr(1);
        }
        return CallNextHookEx(_hookID, nCode, wParam, lParam);
    }

    [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;
    }

    [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)]
    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);
}

it works fine and the mouse position won't change, but the problem is when i touch my program mouse cursor hides, i tried Cursor.show() and SetCursorPos but none of them worked.

How can i prevent the program from hiding the mouse cursor?

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,234 questions
{count} votes