.NET Framework 4.0 and drawing on Aero Glass issue

别等时光非礼了梦想. 提交于 2019-12-04 08:47:03

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.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!