加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > Windows > 正文

windows-7 – .NET Framework 4.0和Aero Glass问题的绘图

发布时间:2020-12-14 02:18:39 所属栏目:Windows 来源:网络整理
导读:在我的应用程序中,我有一个表单,我通过使用DWM API的方法DwmExtendFrameIntoClientArea来调整,以扩展Aero Glass标题栏的高度,以便在Aero框架上绘制我的表单客户区的一部分.为了达到这个效果,我还在客户区域的一部分绘制了一个黑色矩形,它在玻璃框架上方,因此
在我的应用程序中,我有一个表单,我通过使用DWM API的方法DwmExtendFrameIntoClientArea来调整,以扩展Aero Glass标题栏的高度,以便在Aero框架上绘制我的表单客户区的一部分.为了达到这个效果,我还在客户区域的一部分绘制了一个黑色矩形,它在玻璃框架上方,因此它看起来很透明,正如许多在线文章所暗示的那样.这在 Windows Vista / Windows 7下运行良好但是当我下载VS 2010并使用.NET Framework 4.0作为我的trarget框架来构建我的应用程序时,这种方法不再有效.问题是黑色矩形是可见的,即在Aero玻璃上绘制时黑色不再被认为是透明的.有没有人知道这可能有什么问题以及如何克服它?

解决方法

这里描述了这个问题的答案: http://msdn.microsoft.com/en-us/magazine/cc163435.aspx#S6带有C#的解决方案.

从链接页面摘录(如果链接断开):

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.

和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());
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读