Esempio n. 1
0
        /// <summary>
        /// Activate a specified window.
        /// </summary>
        /// <param name="target">Target Window</param>
        internal static void ActivateWindow(Window target)
        {
            var handleWindow = new WindowInteropHelper(target).Handle;

            try
            {
                // Get process ID for target window's thread.
                var targetWindowId = NativeMethod.GetWindowThreadProcessId(
                    handleWindow,
                    IntPtr.Zero);
                if (targetWindowId == 0)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to get process ID for target window.");
                }

                // Get process ID for the foreground window's thread.
                var foregroundWindow = NativeMethod.GetForegroundWindow();
                if (foregroundWindow == IntPtr.Zero)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to get handle to the foreground window.");
                }

                var foregroundWindowId = NativeMethod.GetWindowThreadProcessId(
                    foregroundWindow,
                    IntPtr.Zero);
                if (foregroundWindowId == 0)
                {
                    throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to get process ID for the foreground window.");
                }

                if (targetWindowId != foregroundWindowId)
                {
                    try
                    {
                        // Attach target window's thread to the foreground window's thread.
                        var result1 = NativeMethod.AttachThreadInput(
                            foregroundWindowId,
                            targetWindowId,
                            true);
                        if (!result1)
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error(), String.Format("Failed to attach thread ({0}) to thread ({1}).", foregroundWindowId, targetWindowId));
                        }

                        // Set position of target window.
                        var result2 = NativeMethod.SetWindowPos(
                            handleWindow,
                            IntPtr.Zero,
                            0,
                            0,
                            0,
                            0,
                            NativeMethod.SWP_NOSIZE | NativeMethod.SWP_NOMOVE | NativeMethod.SWP_SHOWWINDOW);
                        if (!result2)
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error(), "Failed to set position of target window.");
                        }
                    }
                    finally
                    {
                        // Detach target window's thread from the foreground window's thread.
                        var result3 = NativeMethod.AttachThreadInput(
                            foregroundWindowId,
                            targetWindowId,
                            false);
                        if (!result3)
                        {
                            throw new Win32Exception(Marshal.GetLastWin32Error(), String.Format("Failed to detach thread ({0}) from thread ({1}).", foregroundWindowId, targetWindowId));
                        }
                    }
                }
            }
            catch (Win32Exception ex)
            {
                throw new Exception(String.Format("{0} (Code: {1}).", ex.Message.Substring(0, ex.Message.Length - 1), ex.ErrorCode), ex);
            }

            // Show and activate target window.
            if (target.WindowState == WindowState.Minimized)
            {
                target.WindowState = WindowState.Normal;
            }

            target.Show();
            target.Activate();
        }