Loading a WPF Window without showing it

前端 未结 12 820
独厮守ぢ
独厮守ぢ 2020-12-12 16:14

I create a global hot key to show a window by PInvoking RegisterHotKey(). But to do this I need that window\'s HWND, which doesn\'t exist until the

12条回答
  •  盖世英雄少女心
    2020-12-12 16:56

    I've created extension method for showing invisible window, next Show calls will behave OK.

    public static class WindowHelper
    {
        public static void ShowInvisible(this Window window)
        {
            // saving original settings
            bool needToShowInTaskbar = window.ShowInTaskbar;
            WindowState initialWindowState = window.WindowState;
    
            // making window invisible
            window.ShowInTaskbar = false;
            window.WindowState = WindowState.Minimized;
    
            // showing and hiding window
            window.Show();
            window.Hide();
    
            // restoring original settings
            window.ShowInTaskbar = needToShowInTaskbar;
            window.WindowState = initialWindowState;
        }
    }
    

提交回复
热议问题