private static void BringToFront(string processName) { var p = Process.GetProcessesByName(processName).FirstOrDefault(); if (p == null) { return; } var desktopWindow = JwLibHelperNativeMethods.GetDesktopWindow(); if (desktopWindow == IntPtr.Zero) { return; } var found = false; var prevWindow = IntPtr.Zero; while (!found) { var nextWindow = JwLibHelperNativeMethods.FindWindowEx(desktopWindow, prevWindow, null, null); if (nextWindow != IntPtr.Zero) { JwLibHelperNativeMethods.GetWindowThreadProcessId(nextWindow, out var procId); if (procId == p.Id) { found = true; IntPtr mainWindow = p.MainWindowHandle; if (JwLibHelperNativeMethods.IsIconic(mainWindow)) { const int swRestore = 9; JwLibHelperNativeMethods.ShowWindow(mainWindow, swRestore); } JwLibHelperNativeMethods.SetForegroundWindow(nextWindow); } prevWindow = nextWindow; } else { break; } } }
private static bool BringToFront(string processName) { var p = Process.GetProcessesByName(processName).FirstOrDefault(); if (p == null) { return(false); } var desktopWindow = JwLibHelperNativeMethods.GetDesktopWindow(); if (desktopWindow == IntPtr.Zero) { return(false); } bool found = false; var prevWindow = IntPtr.Zero; while (!found) { var mainWindow = JwLibHelperNativeMethods.FindWindowEx(desktopWindow, prevWindow, MainWindowClassName, null); if (mainWindow == IntPtr.Zero) { break; } var sb = new StringBuilder(256); JwLibHelperNativeMethods.GetWindowText(mainWindow, sb, 256); if (sb.ToString().StartsWith(JwLibCaptionPrefix)) { JwLibHelperNativeMethods.SetForegroundWindow(mainWindow); found = true; } prevWindow = mainWindow; } return(found); }