Example #1
0
        public static bool ForceForegroundWindow(int handle)
        {
            var foregroundWnd = User32.GetForegroundWindow();

            if (handle == foregroundWnd)
            {
                return(true);
            }

            int ret;

            var threadId1 = User32.GetWindowThreadProcessId(foregroundWnd, 0);
            var threadId2 = User32.GetWindowThreadProcessId(handle, 0);

            if (threadId1 != threadId2)
            {
                User32.AttachThreadInput(threadId1, threadId2, 1);
                ret = User32.SetForegroundWindow(handle);
                User32.AttachThreadInput(threadId1, threadId2, 0);
            }
            else
            {
                ret = User32.SetForegroundWindow(handle);
            }

            var showWindowMode = User32.IsIconic(handle) == 0 ? User32.Restore : User32.Show;

            User32.ShowWindow(handle, showWindowMode);
            User32.SetActiveWindow(handle);
            User32.BringWindowToTop(handle);

            return(ret != 0);
        }
Example #2
0
        private void BringCurrentToFront(int parentPid, int currentPid)
        {
            foreach (var process in Process.GetProcessesByName("fam"))
            {
                if (process.Id == currentPid)
                {
                    continue;
                }

                var processParentPid = GetParentProcessId(process);
                if (processParentPid != parentPid)
                {
                    _trace.TraceInformation("Found a previous process with PID {0}, but it does not appear to be attached to PID {1}. Ignoring...", process.Id, parentPid);
                    continue;
                }

                _trace.TraceInformation("Found a previous process with PID {0}. Getting windows...", process.Id);

                var windows = GetProcessWindows(process.Id);
                foreach (var window in windows)
                {
                    _trace.TraceInformation("Calling BringWindowToTop with HWND {0}.", window);
                    User32.BringWindowToTop(window);
                }

                return;
            }

            ConsoleNeeded();
            Terminal.WriteLine("Unable to start the familiar. Please check logs for details.");

            _trace.TraceError("Unable to find previous process.");
        }
        void KeyCommandEditor_Shown(object sender, EventArgs e)
        {
            User32.BringWindowToTop(this.Handle);

            if (_cmd == OPMShortcut.CmdOutOfRange)
            {
                DialogResult = DialogResult.Cancel;
                Close();
            }
        }
Example #4
0
        /// <summary>
        /// Forces the window to the foreground by attaching to the foreground window thread
        /// </summary>
        private void ForceForeground()
        {
            // This is required as there's a few restrictions on when this can be called
            // Per https://msdn.microsoft.com/en-us/library/windows/desktop/ms633539%28v=vs.85%29.aspx

            var targetThread = User32.GetWindowThreadProcessId(User32.GetForegroundWindow(), IntPtr.Zero);
            var appThread    = Kernel32.GetCurrentThreadId();
            var attached     = false;

            try
            {
                if (targetThread == appThread)
                {
                    // already attached
                    return;
                }

                attached = User32.AttachThreadInput(targetThread, appThread, true);

                if (!attached)
                {
                    // hmm
                    Close();
                    return;
                }

                var ourHandle = new WindowInteropHelper(this).Handle;

                // force us to the forground
                User32.BringWindowToTop(ourHandle);
                User32.SetFocus(ourHandle);
            }
            finally
            {
                if (attached)
                {
                    // unattach
                    User32.AttachThreadInput(targetThread, appThread, false);
                }
            }
        }
Example #5
0
 public static void ShowWindow(IntPtr hWnd)
 {
     if (User32.GetForegroundWindow() != hWnd)
     {
         IntPtr dwMyThreadID    = User32.GetWindowThreadProcessId(User32.GetForegroundWindow(), IntPtr.Zero);
         IntPtr dwOtherThreadID = User32.GetWindowThreadProcessId(hWnd, IntPtr.Zero);
         if (dwMyThreadID != dwOtherThreadID)
         {
             User32.AttachThreadInput(dwMyThreadID, dwOtherThreadID, true);
             User32.BringWindowToTop(hWnd);
             User32.SetForegroundWindow(hWnd);
             IntPtr hMain = User32.GetWindow(hWnd, WinBase.GW_ENABLEDPOPUP);
             User32.SetFocus(hMain);
             User32.AttachThreadInput(dwMyThreadID, dwOtherThreadID, false);
         }
         else
         {
             User32.SetForegroundWindow(hWnd);
         }
     }
 }
Example #6
0
        public static void ActivateTopLevelWindow(Control control)
        {
            //	Prevent stupidity.
            if (control == null || !control.IsHandleCreated)
            {
                return;
            }

            //	Find the form that contains the control.  If it's found, we get off easy, and can
            //	activate it natively through .NET.
            Form form = control.FindForm();

            if (form != null)
            {
                form.Activate();
            }
            else
            {
                User32.BringWindowToTop(control.Handle);
            }
        }
 void MessageDisplay_Shown(object sender, EventArgs e)
 {
     //this.TopMost = true;
     User32.BringWindowToTop(this.Handle);
 }
Example #8
0
 public void BringWindowToTop(long handle)
 {
     User32.BringWindowToTop((System.IntPtr)handle);
 }
Example #9
0
 public void RegainTrayPriority()
 {
     User32.BringWindowToTop(windowHandle);
 }
Example #10
0
        public override void ActivateForm(Window form, Window window, IntPtr hwnd)
        {
            var fHandle = (PresentationSource.FromVisual(form) as HwndSource).Handle;
            var wHandle = window == null ? IntPtr.Zero : (PresentationSource.FromVisual(window) as HwndSource).Handle;

            if (window == null || wHandle != fHandle)
            {
                IntPtr Dummy = IntPtr.Zero;

                IntPtr hWnd = fHandle;
                if (User32.IsIconic(hWnd))
                {
                    User32.ShowWindowAsync(hWnd, SW_RESTORE);
                }
                else
                {
                    //User32.ShowWindowAsync(hWnd, SW_SHOW);
                    form.ShowActivated = true;
                    form.Show();
                }
                User32.SetForegroundWindow(hWnd);
                form.Activate();
                form.Topmost = true;
                form.Topmost = false;

                // Code from Karl E. Peterson, www.mvps.org/vb/sample.htm
                // Converted to Delphi by Ray Lischner
                // Published in The Delphi Magazine 55, page 16
                // Converted to C# by Kevin Gale
                IntPtr foregroundWindow = User32.GetForegroundWindow();
                if (foregroundWindow != hWnd)
                {
                    uint foregroundThreadId = User32.GetWindowThreadProcessId(foregroundWindow, Dummy);
                    uint thisThreadId       = User32.GetWindowThreadProcessId(hWnd, Dummy);

                    if (User32.AttachThreadInput(thisThreadId, foregroundThreadId, true))
                    {
                        form.Activate();
                        form.Topmost = true;
                        form.Topmost = false;
                        User32.BringWindowToTop(hWnd);                         // IE 5.5 related hack
                        User32.SetForegroundWindow(hWnd);
                        User32.AttachThreadInput(thisThreadId, foregroundThreadId, false);
                    }
                }

                if (User32.GetForegroundWindow() != hWnd)
                {
                    // Code by Daniel P. Stasinski
                    // Converted to C# by Kevin Gale
                    IntPtr Timeout = IntPtr.Zero;
                    User32.SystemParametersInfo(SPI_GETFOREGROUNDLOCKTIMEOUT, 0, Timeout, 0);
                    User32.SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Dummy, SPIF_SENDCHANGE);
                    User32.BringWindowToTop(hWnd);                     // IE 5.5 related hack
                    User32.SetForegroundWindow(hWnd);
                    form.Activate();
                    form.Topmost = true;
                    form.Topmost = false;
                    User32.SystemParametersInfo(SPI_SETFOREGROUNDLOCKTIMEOUT, 0, Timeout, SPIF_SENDCHANGE);
                }

                Shell32.FlashWindow(fHandle, 0);
            }
        }