Getting the 'Vista' minimize with Windowless applications

Here's the problem. Launch Blend, and create a window that has AllowsTransparency set to true (which sets the WindowStyle to None). Looks good, yes? Now, add a button, and in the Click handler, set the WindowState property to Minimized.

Run the application on Vista, and click your minimize button. You'll notice that the window just vanishes! It doesn't get the nice minimize behavior like you'd expect (You know, the animation. Minimize any other app, and you'll see what I mean).

One of my teammates, Brian, was trying to get this to work, and he did some research. Apparently, if you set this.WindowStyle to WindowStyle.SingleBorderWindow, it will minimize correctly. He was pretty excited about this, and brought it to me. I'm not completely sure why it works, because when you restore the window, it's still the "none" WindowStyle, but it does work.

Problem is, if you run the minimize behavior a second time, it fails with an InvalidOperationException, and your app comes crashing down.

We tried resetting the WindowStyle back to None after Minimizing it, but that caused a different InvalidOp.

So, we played with it a bit, and it seems (oddly enough) that if you set the property ONCE after the window has been created, that you get the minimizing behavior. You just can't set it twice.

So, try this method:

private void MinimizeIt(object sender, RoutedEventArgs e)

{

      if (this.WindowStyle != WindowStyle.SingleBorderWindow)

      {

            this.WindowStyle = WindowStyle.SingleBorderWindow;

      }

      this.WindowState = WindowState.Minimized;

}

And from our experimentation, this would consistently cause the window to minimize in the "Vista" way, didn't cause a crash, and oddly enough, didn't change the appearance at all. Oddly enough, for grins, I tried putting that line in the Window1.Loaded event handler (Well, without the If statement), and it gives me the InvalidOperation exception.

Comments