Example #1
0
        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);
            }

            var 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);
        }
Example #2
0
        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;
                        JwLibHelperNativeMethods.SetForegroundWindow(nextWindow);
                    }

                    prevWindow = nextWindow;
                }
                else
                {
                    break;
                }
            }
        }