Exemple #1
0
        /// <summary>
        ///     Shows a window.
        /// </summary>
        /// <param name="hWnd"> The window to show. </param>
        /// <remarks>
        ///     <para>
        ///         Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />.
        ///     </para>
        /// </remarks>
        public static void ShowWindow(IntPtr hWnd)
        {
            if (hWnd == IntPtr.Zero)
            {
                return;
            }

            WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShow);
        }
Exemple #2
0
        /// <summary>
        ///     Moves a window to a screen.
        /// </summary>
        /// <param name="hWnd"> The window to move. </param>
        /// <param name="screen"> The screen or null to move to the primary screen. </param>
        /// <remarks>
        ///     <para>
        ///         Nothing happens if <paramref name="hWnd" /> is <see cref="IntPtr.Zero" />.
        ///     </para>
        /// </remarks>
        public static void MoveWindowToScreen(IntPtr hWnd, Screen screen)
        {
            if (hWnd == IntPtr.Zero)
            {
                return;
            }

            screen = screen ?? Screen.PrimaryScreen;

            WINDOWINFO info = new WINDOWINFO();

            info.cbSize = (uint)Marshal.SizeOf(info);
            WindowsWindow.GetWindowInfo(hWnd, ref info);

            WINDOWPLACEMENT placement = new WINDOWPLACEMENT();

            placement.Length = (uint)Marshal.SizeOf(placement);
            WindowsWindow.GetWindowPlacement(hWnd, ref placement);

            bool isMaximized = placement.ShowCmd == WindowsWindow.SwShowmaximized;
            bool isMinimized = placement.ShowCmd == WindowsWindow.SwShowminimized;
            bool isNormal    = placement.ShowCmd == WindowsWindow.SwShownormal;

            int width  = info.rcWindow.Right - info.rcWindow.Left;
            int height = info.rcWindow.Bottom - info.rcWindow.Top;

            if (isMaximized || isMinimized)
            {
                WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShownormal);
            }

            WindowsWindow.SetWindowPos(hWnd, IntPtr.Zero, screen.WorkingArea.Left, screen.WorkingArea.Top, 0, 0,
                                       WindowsWindow.SwpNozorder | WindowsWindow.SwpNosize);

            if (isNormal)
            {
                WindowsWindow.SetWindowPos(hWnd, IntPtr.Zero,
                                           screen.WorkingArea.Left + ((screen.WorkingArea.Width - width) / 2),
                                           screen.WorkingArea.Top + ((screen.WorkingArea.Height - height) / 2), 0, 0,
                                           WindowsWindow.SwpNozorder | WindowsWindow.SwpNosize);
            }
            else if (isMaximized)
            {
                WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShowmaximized);
            }
            else if (isMinimized)
            {
                WindowsWindow.ShowWindow(hWnd, (int)WindowsWindow.SwShowminimized);
            }
        }