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.
7,488 questions
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
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?
I think he's referring to the fact that when under tablet mode, mouse cursor is converted to ripple (hence disappear).
Since touch API works differently then cursor API, I don't think there is a way to lock the points. (remember, at least on Surface device there will be at least 3 points detectable on touch mode, and most touch screen on tablet device support at least 2 points so they can zoom)