public static bool ForceForegroundWindow(IntPtr hWnd) { try { IntPtr foreground = WindowFunctions.GetForegroundWindow(); int foreThread = WindowFunctions.GetWindowThreadProcessId(foreground, 0); #pragma warning disable 618 int appThread = AppDomain.GetCurrentThreadId(); #pragma warning restore 618 if (foreThread == 0 || appThread == 0 || hWnd == IntPtr.Zero) { return(false); } if (foreThread != appThread) { WindowFunctions.AttachThreadInput(foreThread, appThread, true); } string window_title_foreground_window = WindowFunctions.GetWindowTitle(foreground); uint nuuul = 0; WindowFunctions.SystemParametersInfo(WindowFunctions.SPI_GETFOREGROUNDLOCKTIMEOUT, 0, ref nuuul, 0); WindowFunctions.SystemParametersInfo(WindowFunctions.SPI_SETFOREGROUNDLOCKTIMEOUT, 0, ref nuuul, WindowFunctions.SPIF_SENDCHANGE); System.Windows.Forms.Application.DoEvents(); WindowFunctions.BringWindowToTop(hWnd); System.Windows.Forms.Application.DoEvents(); WindowFunctions.ShowWindow(hWnd, WindowFunctions.SW_SHOW); System.Windows.Forms.Application.DoEvents(); System.Windows.Forms.Application.DoEvents(); WindowFunctions.BringWindowToTop(hWnd); System.Windows.Forms.Application.DoEvents(); if (foreThread != appThread) { WindowFunctions.AttachThreadInput(foreThread, appThread, false); } System.Threading.Thread.Sleep(200); return(true); } catch { } return(false); }
public static string GetWindowTitle(IntPtr hWnd) { if (!WindowFunctions.IsWindowVisible(hWnd)) { return(""); } int length = WindowFunctions.GetWindowTextLength(hWnd); if (length == 0) { return(""); } StringBuilder builder = new StringBuilder(length); WindowFunctions.GetWindowText(hWnd, builder, length + 1); return(builder.ToString()); }