public static IntPtr SetWindowsHookEx(HookType hookType, HookProc lpfn, IntPtr hMod, uint dwThreadId) { // TODO: SetWindowsHookEx currently ignores the module and thread. int handle = PInvoke.AllHooks.Count + 1; List <Delegate> hooks = PInvoke.Hooks[hookType]; PInvoke.AllHooks.Add(Tuple.Create <HookType, Delegate, int>(hookType, lpfn, hooks.Count)); hooks.Add(lpfn); XnaToFnaHelper.Log($"[PInvokeHooks] Added global hook #{handle} of type {hookType}"); return((IntPtr)handle); }
public static bool ClipCursor(ref Rectangle rect) { unsafe { fixed(Rectangle *rect_ = &rect) if ((long)rect_ == 0) { // MSDN: If this parameter is NULL, the cursor is free to move anywhere on the screen. XnaToFnaHelper.Log($"[CursorEvents] Cursor released from ClipCursor"); MouseEvents.Clip = null; return(true); } } XnaToFnaHelper.Log($"[CursorEvents] Game tries to ClipCursor inside {rect}"); MouseEvents.Clip = rect; return(true); }
public static uint GetWindowThreadProcessId(IntPtr hWnd, ref uint lpdwProcessId) { Form form = Control.FromHandle(hWnd) as Form; if (form == null) { XnaToFnaHelper.Log($"[PInvokeHooks] Called GetWindowThreadProcessId for non-existing hWnd {hWnd}"); form = GameForm.Instance; } // Yes, that's required, as DLC Quest passes IntPtr.Zero unsafe { fixed(uint *lpdwProcesId_ = &lpdwProcessId) if ((long)lpdwProcesId_ != 0) { lpdwProcessId = 0; // Optional } } return((uint)(form?.ThreadId ?? 0)); }
public static int SetWindowLong(IntPtr hWnd, int nIndex, int dwNewLong) { // All other nIndex values seem to be style-related. if (nIndex == -4) { Form form = Control.FromHandle(hWnd)?.Form; if (form == null) { return(0); } IntPtr prevHook = form.WindowHookPtr; form.WindowHookPtr = (IntPtr)dwNewLong; form.WindowHook = Marshal.GetDelegateForFunctionPointer(form.WindowHookPtr, typeof(WndProc)); XnaToFnaHelper.Log($"[PInvokeHooks] Window hook set on ProxyForms.Form #{form.GlobalIndex}"); return((int)prevHook); } return(0); }
public static IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam) { // This gets called when Duck Game dies... // TODO: Find more games using SendMessage. Form form = Control.FromHandle(hWnd) as Form; if (form == null) { XnaToFnaHelper.Log($"[PInvokeHooks] Called GetWindowThreadProcessId for non-existing hWnd {hWnd}"); form = GameForm.Instance; } if (Msg == 16 /*WM_CLOSE*/) { form.Close(); // TODO: What's the proper return value for SendMessage on WM_CLOSE? return(IntPtr.Zero); } return(IntPtr.Zero); }