.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 c开发者_如何学Goan be overcome?
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());
}
Forgive me if this is unhelpful, but is it possible that they've simply changed their colour palette? Quite often it seems that those windows interop type APIs can use PBGRA palettes in addition to their default palette, and it's possible that their default colour-palette may have changed from one framework version to another. It should be simple enough to test whether this is true or not.
(I found a similar problem with rendering hardware cursors, where transparent and black would be confused under certain circumstances. It eventually turned out to be that I was miscalculating the PBGRA so that one of BGR was greater than A, which may be true of your background meant-to-be-transparent-colour.)
There are other complications with using the DWM API method. I'm not entirely sure, but from memory, there is something complicated with setting the new margins - if I recall correctly, they all needed to have the same sign (+ / -) or artifacts (such as your suddenly appearing black background) sometimes appeared.
精彩评论