none
Restoring a hidden window and getting focus to it RRS feed

  • Question

  • Hi All,

     

    I have a WPF application where I need to hide a window when it is minimized. I have set the ShowInTaskbar property of this window to false and used the StateChanged event for that and I hide the window when it is minimized. This is working fine.

     

    I need to show this minimized (and hidden) form again on some other event on another window which is a parent window of the hidden window. I am setting the WindowState property of the child window to NORMAL and showing the hidden window. Here is my code for this:

     

    child.WindowState = WindowState.Normal;

    child.Show();

    child.Focus();

     

    The above code is not showing the window in normal mode but is displaying an icon just above the Start button of my system. I need to manually click the restore button now to see it in Normal mode. When I try setting the ShowInTaskbar property to true, it is showing in the taskbar when the above code is executed. But it is not getting on focus. I need to manually click on this to show it.

     

    But when I set the WindowState property to Maximized instead of Normal, it is working fine. I mean, the window is getting maximized and shown. Here is the code change.

     

    child.WindowState = WindowState.Maximized; //WindowState.Normal;

    child.Show();

    child.Focus();

     

    Why does this differ from Maximized to Normal? Is there any other property or method to restore the window to its original size when it is being shown after hiding?

     

    Thanks in advance for any help,

    Ravi Kumar

    Tuesday, April 1, 2008 5:57 AM

Answers

  • Here is an workable sample:

    Code Snippet

    <Window x:Class="AnswerHarness2.HideAndShowWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="HideAndShowWindow" Height="300" Width="300">
        <StackPanel>
          <Button Width="120" Height="30" Content="Show" x:Name="btnCreate"/>
          <Button Width="120" Height="30" Content="Hidden" x:Name="btnShow"/>
        </StackPanel>
    </Window>
    public partial class HideAndShowWindow : Window
    {
        private Window childWindow = null;
        private const int SW_RESTORE = 9;

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public HideAndShowWindow()
        {
            InitializeComponent();
            btnCreate.Click += delegate
            {
                childWindow = new Window();
                childWindow.Content = new TextBox();
                childWindow.Visibility = Visibility.Visible;
                childWindow.Owner = this;
                childWindow.StateChanged += (sender, e) =>
                {
                    if (childWindow.WindowState == WindowState.Minimized)
                    {
                        childWindow.Visibility = Visibility.Hidden;
                    }
                };
            };

            btnShow.Click += delegate
            {
                if (childWindow != null)
                {
                    childWindow.Visibility = Visibility.Visible;
                    ShowWindow(new WindowInteropHelper(childWindow).Handle, SW_RESTORE);
                }
            };
        }
    }


    Hope this helps
    Monday, April 7, 2008 10:05 AM

All replies

  • Instead of messing around with ShowInTaskbar property, you can toggle Window.Visibility to show and hide child Window, this should give you a correct behaviour.

    Hope this helps


    Thursday, April 3, 2008 2:30 AM
  • I think I did not convey the problem correctly. The problem is not with the ShowInTaskbar property. Anyway, I have already tried setting the Visibility property to show/hide the child window. But it did not work.

     

    I have found a work around for this, which is as follows:

     

    I have implemented the StateChange event handler for the child window and when it gets maximized, I am restoring it back to Normal state. The code is working correctly. But the problem now is, the user could be able to see it coming back from maximized state. If there is any other alternative, please let me know.

     

    Thanks and Regards,

    Ravi Kumar

    Thursday, April 3, 2008 5:17 AM
  • Toggling Visibility property works pretty well for me, could you please elaborate why it doesn't work for you, if possible please provide a small repro code?

    Thanks
    Thursday, April 3, 2008 7:08 AM
  • My first post was pretty clear and elaborate. Anyway, let me provide you the source code and explain about the problem in detail. I have an application, where I am opening a child window from a parent window. I call my parent window as ParentWindow.cs and the child as Child Window.cs. I have two buttons on parent window, one is to show the child window and the other one is to restore it back.

     

    Here is the code for opening the child window (on click of button1) in ParentWindow.cs:

    private ChildWindow child = null;

    private void button1_Click(object sender, RoutedEventArgs e)

    {

              child = new ChildWindow();

              child.Visibility = Visibility.Visible;

    }

     

    Once the child window is shown, I can minimize it. When I minimize the child window, I need to hide it. To implement this, I have implemented the StateChanged event handler of child window. Here is the code of it in ChildWindow.cs.

    private void ChildWindow_StateChanged(object sender, EventArgs e)

    {

              if (this.WindowState == WindowState.Minimized)

              {

                     this.Visibility = Visibility.Hidden;

              }

    }

     

    This is hiding the child window. If I click on button 2 on the parent window, the hidden child window should become visible and should be on focus. To implement this, I have written the following code in ParentWindow.cs:

    private void button2_Click(object sender, RoutedEventArgs e)

    {

             child.WindowState = WindowState.Normal; //WindowState.Maximized;

             child.Visibility = Visibility.Visible;

             child.Focus();

    }

     

    When I hit button 2, the child window is becoming visible but it is in Minimized form only. It is not coming to Normal mode. But if I change the first line of the above event to Maximized, it is working fine. I mean, the child window is becoming visible in Maximized mode. I wonder why this is behaving differently for Normal and Maximized modes.

     

    I tried to use Show() and Hide() methods instead of setting Visibility but it is not working. Also, I have set the WindowState to Normal after setting Visibility property to Visible (swapped the first two lines in the above event) but it seem to make no difference.

    Thursday, April 3, 2008 11:15 AM
  • Here is an workable sample:

    Code Snippet

    <Window x:Class="AnswerHarness2.HideAndShowWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="HideAndShowWindow" Height="300" Width="300">
        <StackPanel>
          <Button Width="120" Height="30" Content="Show" x:Name="btnCreate"/>
          <Button Width="120" Height="30" Content="Hidden" x:Name="btnShow"/>
        </StackPanel>
    </Window>
    public partial class HideAndShowWindow : Window
    {
        private Window childWindow = null;
        private const int SW_RESTORE = 9;

        [DllImport("user32.dll")]
        static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);

        public HideAndShowWindow()
        {
            InitializeComponent();
            btnCreate.Click += delegate
            {
                childWindow = new Window();
                childWindow.Content = new TextBox();
                childWindow.Visibility = Visibility.Visible;
                childWindow.Owner = this;
                childWindow.StateChanged += (sender, e) =>
                {
                    if (childWindow.WindowState == WindowState.Minimized)
                    {
                        childWindow.Visibility = Visibility.Hidden;
                    }
                };
            };

            btnShow.Click += delegate
            {
                if (childWindow != null)
                {
                    childWindow.Visibility = Visibility.Visible;
                    ShowWindow(new WindowInteropHelper(childWindow).Handle, SW_RESTORE);
                }
            };
        }
    }


    Hope this helps
    Monday, April 7, 2008 10:05 AM
  • This is what I am looking for. Thanks a lot for your help.

     

    Tuesday, April 8, 2008 5:54 AM
  • Thanks. We will make sure to log a bug about this.

     

    Wednesday, April 9, 2008 9:30 PM
  • hi,have you solved the problem?Now i encounter  the same problem,can you tell me your solution to this problem?thanks a lot!
    Tuesday, November 15, 2011 9:27 AM