How to restore a minimized Window in code-behind?

后端 未结 7 898
无人共我
无人共我 2020-12-08 06:24

This is somewhat of a mundane question but it seems to me there is no in-built method for it in WPF. There only seems to be the WindowState property which being

7条回答
  •  轮回少年
    2020-12-08 06:47

    WPF's point of view is that this is an OS feature. If you want to mess around with OS features you might have to get your hands dirty. Luckily they have provided us with the tools to do so. Here is a UN-minimize method that takes a WPF window and uses WIN32 to accomplish the effect without recording any state:

    public static class Win32
    {
        public static void Unminimize(Window window)
        {
            var hwnd = (HwndSource.FromVisual(window) as HwndSource).Handle;
            ShowWindow(hwnd, ShowWindowCommands.Restore);
        }
    
        [DllImport("user32.dll")]
        private static extern bool ShowWindow(IntPtr hWnd, ShowWindowCommands nCmdShow);
    
        private enum ShowWindowCommands : int
        {
            /// 
            /// Activates and displays the window. If the window is minimized or 
            /// maximized, the system restores it to its original size and position. 
            /// An application should specify this flag when restoring a minimized window.
            /// 
            Restore = 9,
        }
    }
    

提交回复
热议问题