public static IntPtr SimulateAltKeyPress(IntPtr handle) { Pinvokes.SendMessage(handle, (int)WM.SyskeyDown, 0x12, 0x20380001); Pinvokes.SendMessage(handle, (int)WM.SyskeyUp, 0x12, 0xC0380001); return(IntPtr.Zero); }
// This static method is required because legacy OSes do not support // SetWindowLongPtr public static IntPtr SetWindowLongPtr(IntPtr hWnd, int nIndex, long dwNewLong) { if (IntPtr.Size == 8) { return(Pinvokes.SetWindowLongPtr64(hWnd, nIndex, dwNewLong)); } return(new IntPtr(Pinvokes.SetWindowLong32(hWnd, nIndex, (int)dwNewLong))); }
public static Size GetClientSize(Process proc) { Pinvokes.RECT rect; Pinvokes.GetWindowRect(proc.MainWindowHandle, out rect); Size size = new Size(rect); return(size); }
private void SimulateBorderScrolling(POINT origin) { POINT point = origin; ScreenToClient(LatestRa3.MainWindowHandle, ref point); Size size = WindowInvocation.GetClientSize(LatestRa3); if (!size.IsPointInArea(origin.X, origin.Y)) { return; } // I wanted to make it cool but it somewhat looks not so cool. :( void Check(bool condition, Keys key, ref bool keyNeedsUp) { uint scanCode = MapVirtualKeyEx((uint)key, MAPVK_VK_TO_VSC, IntPtr.Zero); uint goodScanCode = scanCode << 16; // scancode is from 16-23, byte uint extendedMessage = goodScanCode | 1 // Key repeating | 1 << 24; // is extended key = true if (condition) { Pinvokes.PostMessage(LatestRa3.MainWindowHandle, (int)WM.KeyDown, 0, extendedMessage); keyNeedsUp = true; } else { if (keyNeedsUp) { Pinvokes.PostMessage(LatestRa3.MainWindowHandle, (int)WM.KeyUp, 0, extendedMessage); keyNeedsUp = false; } } /* * Inputs.SendMessage(LatestRa3.MainWindowHandle, (int)WM.KeyDown, 0x25, 0x14b0001); * Inputs.SendMessage(LatestRa3.MainWindowHandle, (int)WM.KeyUp, 0x25, 0xc14b0001); * //Messaging.InvokeKeyPress(LatestRa3.MainWindowHandle, (uint) Keys.Left); * Console.WriteLine("Invoked keypress");*/ } // Left Check(point.X <= Constants.Ra3InnerScrollBorderSize, Keys.Left, ref needsKeyUp1); // Right Check(size.Width - point.X <= Constants.Ra3InnerScrollBorderSize, Keys.Right, ref needsKeyUp2); // Up Check(point.Y <= Constants.Ra3InnerScrollBorderSize, Keys.Up, ref needsKeyUp3); // Down Check(size.Height - point.Y <= Constants.Ra3InnerScrollBorderSize, Keys.Down, ref needsKeyUp4); }
public static uint WriteProcessMemory(IntPtr handle, IntPtr address, byte[] buffer, int size) { bool result = Pinvokes.WriteProcessMemory(handle, address, buffer, size, out var numberOfBytesWritten); if (!result) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(numberOfBytesWritten); }
public static bool CloseHandle(IntPtr handle) { bool result = Pinvokes.CloseHandle(handle); if (!result) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(true); }
public static IntPtr OpenProcess(Pinvokes.ProcessAccessFlags processAccess, bool bInheritHandle, int processId) { IntPtr handle = Pinvokes.OpenProcess(processAccess, bInheritHandle, processId); if (handle == IntPtr.Zero) { throw new Win32Exception(Marshal.GetLastWin32Error()); } return(handle); }
public static int SendMessage(IntPtr hwnd, int msg, uint wParam, uint lParam) { int lastError = 0; if (Pinvokes.SendMessage(hwnd, msg, wParam, lParam) != IntPtr.Zero) { lastError = Marshal.GetLastWin32Error(); string lastErrorMessage = new Win32Exception(lastError).Message; Logger.Error($"SendMessage failed with {lastError}: \"{lastErrorMessage}\""); } return(lastError); }
public static Process GetForegroundProcess() { IntPtr handle = Pinvokes.GetForegroundWindow(); Pinvokes.GetWindowThreadProcessId(handle, out var procId); if (procId == -1) { return(null); } return(Process.GetProcessById(procId)); }
public static bool LockToProcess(Process process) { Pinvokes.RECT windowRect, clientRect; Pinvokes.GetWindowRect(process.MainWindowHandle, out windowRect); Pinvokes.GetClientRect(process.MainWindowHandle, out clientRect); int borderThickness = ((windowRect.Right - windowRect.Left) - clientRect.Right) / 2; int topBorderThickness = (windowRect.Bottom - windowRect.Top) - clientRect.Bottom; Pinvokes.RECT lockPosition; lockPosition.Left = windowRect.Left + borderThickness; lockPosition.Right = clientRect.Right + windowRect.Left + borderThickness; lockPosition.Top = windowRect.Top + topBorderThickness - borderThickness; lockPosition.Bottom = clientRect.Bottom + windowRect.Top + topBorderThickness - borderThickness; return(Pinvokes.ClipCursor(ref lockPosition)); }
public static void DropBorder(Process process) { long style = Pinvokes.GetWindowLong(process.MainWindowHandle, (int)GwlIndex.GWL_STYLE); style &= ~((long)WindowStyles.WS_CAPTION | (long)WindowStyles.WS_MAXIMIZE | (long)WindowStyles.WS_MINIMIZE | (long)WindowStyles.WS_SYSMENU); /* long standardStyle = (long) WindowStyles.WS_POPUP | (long) WindowStyles.WS_MINIMIZE | (long) WindowStyles.WS_VISIBLE | * (long) WindowStyles.WS_CLIPSIBLINGS | (long) WindowStyles.WS_SYSMENU | 8; * * long extendedStyle = (long) WindowStyles.WS_EX_LEFT | (long) WindowStyles.WS_EX_LTRREADING | * (long) WindowStyles.WS_EX_RIGHTSCROLLBAR | (long) WindowStyles.WS_EX_TOPMOST; * * long standardStyle = (long)0xb4080008; * long extendedStyle = (long)8; * SetWindowLongPtr(process.MainWindowHandle, (int)GwlIndex.GWL_STYLE, standardStyle); * SetWindowLongPtr(process.MainWindowHandle, (int)GwlIndex.GWL_EXSTYLE, extendedStyle);*/ SetWindowLongPtr(process.MainWindowHandle, (int)GwlIndex.GWL_STYLE, style); }
public static void ResizeWindow(Process process) { // TODO: Drop this reference to winforms and PInvoke it? Screen currentOccupiedScreen = Screen.FromHandle(process.MainWindowHandle); Pinvokes.RECT procRect; Pinvokes.GetWindowRect(process.MainWindowHandle, out procRect); // TODO: Repair this: It seems to have a bit too much size like 1926x1102 which makes everything broken. int width = procRect.Right - procRect.Left; int height = procRect.Bottom - procRect.Top; Pinvokes.SetWindowPos( process.MainWindowHandle , 0 , currentOccupiedScreen.Bounds.X , currentOccupiedScreen.Bounds.Y , currentOccupiedScreen.Bounds.Width , currentOccupiedScreen.Bounds.Height , (uint)WindowSizing.SWP_FRAMECHANGED ); }
private void FindRa3Process() { var ra3Procs = Process.GetProcessesByName(Constants.Ra3ProcessName); if (ra3Procs.Length <= 0) { Logger.Debug("RA3 *not* found at startup."); return; } if (Pinvokes.IsWindow(ra3Procs[0].MainWindowHandle)) { Logger.Debug("RA3 found at startup."); LatestRa3 = ra3Procs[0]; return; } Task.Delay(1000).ContinueWith(_ => { Logger.Debug("RA3 found but no window yet. Retry."); FindRa3Process(); }); }
private void ProcessWatcherOnProcessStarted(object sender, ProcessArgs e) { Console.WriteLine($"[{e.Process.ProcessName}] LAUNCHED"); if (e.Process.ProcessName != Constants.Ra3ProcessName) { return; } Logger.Debug($"Found ra3 process - Has Window: {Pinvokes.IsWindow(e.Process.MainWindowHandle)}"); // Right after process creation, ra3 doesnt have a window and it can be assumed that the process is not fully loaded. // This is a workaround which will probably remain here forever :/ if (!Pinvokes.IsWindow(e.Process.MainWindowHandle)) { Task.Delay(1000).ContinueWith(_ => { ProcessWatcherOnProcessStarted(sender, e); }); return; } if (LatestRa3 == null || LatestRa3.HasExited) { LatestRa3 = e.Process; } }
public static IntPtr InvokeMouse(IntPtr handle, int msg, uint x, uint y) { return(Pinvokes.SendMessage(handle, msg, 0, MakeLParam(x, y))); }
public static IntPtr InvokeSysKeyUp(IntPtr handle, uint key) { return(Pinvokes.SendMessage(handle, (int)WM.SyskeyUp, key, 1)); }
public static IntPtr InvokeKeyDown(IntPtr handle, uint key) { return(Pinvokes.SendMessage(handle, (int)WM.KeyDown, key, 0)); }