All about borderless windows in WPF

In one of my projects, I needed a borderless window in WPF.

At first I thought this was a piece of cake, a matter of setting the border brush to null or transparent and maybe, just to be sure, put the border thickness to zero.

Well…Things turned out to be a bit more complicated than that.

It all depends on the scenario and the user’s need.

Borderless & Non-Resizable Window

Probably the easiest scenario is when you just need a borderless window even if it is non-resizable.

In that case, set WindowStyle property to None and set the ResizeMode property to NoResize.

code1

BorderlessNonREsizable

Borderless & Resizable Window

If you need a borderless and resizable window at the same time, you should set WindowStyle property to None, set AllowsTransparency to True, ResizeMode to CanResizeWithGrip and Background to Transparent.

code2

borderlessResizable

So far, all this looks pretty simple and easy, right? But in real life applications, you will have children elements inside your window.

If you have a native window as a child, let’s say a WebBrowser, hosted in the window with AllowsTransparency set to True, the child window will not appear at all. This is due to the fundamental “airspace” issue. In particular, composition involving different rendering technologies supports transparency only with entire top-level windows, not child windows.

I took the same code used below and added a WebBrowser to the window, The WebBrowser is visible in the non-resizable window only.

code3

Luckily there is a workaround to that

Borderless & Resizable Window Hosting native windows

As mentioned in the workaround above, we need to create an ‘overlay’ window containing the child and set the borderless resizable window as its owner.

Note that the Overlay window has to be moved/resized in response to the owner window moving.

code4 

code5

The class Win32 to do all the calculations:

code6

code7

BorderlessResizableWithChild

 It’s possible I missed a scenario or two , so I would love to hear about them!

Advertisements

About rabeb

Rabeb is a software engineer , fan of the latest .Net technologies, playing with Metro style applications for Win8 at work . In her spare time,she plays with Metro Style apps for Windows Phone.

Posted on August 2, 2013, in WPF, XAML and tagged , , . Bookmark the permalink. 1 Comment.

  1. You are a saint, thank you. This worked for my airspace issue when parenting my WPF window to the windows desktop, and my app is functioning perfect.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: