Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

.NET Framework 4.0 and drawing on Aero Glass issue

In my application I have a form which I tweak by using the DWM API's method DwmExtendFrameIntoClientArea to extend the height of the Aero Glass Title Bar so that part of my form client area is drawn on the Aero frame. To achieve this effect, I also draw a black rectangle on the part of the client area which is 'over' the glass frame so that it appears transparent, as many online articles suggest. This worked pretty well under Windows Vista/Windows 7 but as I downloaded VS 2010 and used the .NET Framework 4.0 as my trarget framework to build my application, this approach no longer works. The problem is that the black rectangle is visible, i.e. the black color is no longer considered transparent when drawing on Aero glass. Does anyone have an idea what might be wrong with that and how it can be overcome?

like image 736
WorldIntruder Avatar asked Nov 14 '22 11:11

WorldIntruder


1 Answers

The answer to this question is described here: http://msdn.microsoft.com/en-us/magazine/cc163435.aspx#S6 with solutions for C#.

Excerpt from the linked page (in case the link is down):

Using glass as a background on your window is a bit tricky. If you render anything naturally opaque (such as GDI functions), you'll get your item rendered on glass, though sometimes with unexpected results. If you want to actually blend rendering into the glass surface, you'll need to take advantage of functionality that utilizes the alpha channel of colors, such as GDI+, Windows Presentation Foundation, or the Windows XP Theme API.

One particular gotcha is that rendering a GDI item in black uses the bit pattern 0x00000000-which also happens to be a completely transparent black if you are using an alpha channel. This means that if you draw with a black GDI brush or pen you'll get a transparent color, not a black one. The biggest problem this presents is when you try to use the default text color in a control of a text label that sits on the glass area. Since the default text color is usually black, the DWM will consider this to be transparent and the text will be written in the glass incorrectly.

And the solution for WinForms:

Happily, there are a number of ways around this problem. Using owner-draw controls is one. Rendering to a bitmap that has an alpha channel is another. Fortunately, the easiest way to get text on controls is to let the .NET Framework 2.0 use GDI+ for you. This is easily accomplished by setting the UseCompatibleTextRendering property on your controls. By default, this property is set to false so that controls written for previous versions of the .NET Framework will render the same. But if you set it to true, your text will come out looking correct. You can set the property globally with the Application.SetUseCompatibleTextRenderingDefault method. If you're using Visual Studio® 2005, the template code will include a call to set compatible text-rendering to false in the main routine before your form is created. You can just edit this to set it to true as shown below and all your controls will look correct when written on a glass window.

static void Main()
{
    Application.EnableVisualStyles();

    Application.SetCompatibleTextRenderingDefault(true); // this line fixes an issue

    Application.Run(new GlassForm());
}
like image 121
Gman Avatar answered Mar 13 '23 02:03

Gman