Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Window transparency in WPF vs Winforms

Why is it that I have to set the WindowStyle property to None on a WPF form to get transparency, but in Winforms I can do it on any form, and retain borders, standard buttons, etc? Clearly the API supports this, so I'm not clear on what's special about WPF that would make this an issue.

I'm guessing that WPF is jumping through some DirectX or OpenGL hoops, while Winforms is just setting the alpha for the window via the API, but I could be way off base.

like image 253
3Dave Avatar asked May 17 '26 23:05

3Dave


1 Answers

Agreed, this is heavy handed:

   private void VerifyConsistencyWithAllowsTransparency(WindowStyle style)
   {
       if (AllowsTransparency && style != WindowStyle.None)
       {
           throw new InvalidOperationException(SR.Get(SRID.MustUseWindowStyleNone));
       }
   }

WPF uses the exact same mechanism to implement this as Windows Forms, layered windows. There is no obvious reason it wouldn't work the same way in WPF. The code snippet, lifted from Window.cs, simply rules it out. There is however one hint from the UsesPerPixelOpacity property:

When you enable per-pixel opacity, the system no longer draws the non-client area. This is because the intended purpose of UsesPerPixelOpacity is to show non-rectangular top-level UI that works in interoperation scenarios, and showing the rectangular non-client area defeats that purpose.

"interoperation scenarios", I guess.

like image 142
Hans Passant Avatar answered May 19 '26 15:05

Hans Passant