帮助获取.Net WinForms应用程序以支持Vista Aero Glass
时间:2020-03-05 18:59:37 来源:igfitidea点击:
获得对.Net表单的玻璃支持有两个技巧。
我认为这种方法的原始资源在这里:http://blogs.msdn.com/tims/archive/2006/04/18/578637.aspx
基本上:
//reference Desktop Windows Manager (DWM API) [DllImport( "dwmapi.dll" )] static extern void DwmIsCompositionEnabled( ref bool pfEnabled ); [DllImport( "dwmapi.dll" )] static extern int DwmExtendFrameIntoClientArea( IntPtr hWnd, ref MARGINS pMarInset ); //then on form load //check for Vista if ( Environment.OSVersion.Version.Major >= 6 ) { //check for support bool isGlassSupported = false; DwmIsCompositionEnabled( ref isGlassSupported ); if ( isGlassSupported ) DwmExtendFrameIntoClientArea( this.Handle, ref margins ); ... //finally on print draw a black box over the alpha-ed area //Before SP1 you could also use a black form background
最后一步是在该区域上绘制的任何子控件似乎也将黑色视为alpha透明蒙版的问题。
例如,课程区域上方的标签栏将具有透明文本。
有没有解决的办法?
有没有更简单的方法可以做到这一点?
我正在处理的应用程序必须在XP和Vista上均可使用,我需要它们正常降级。这里有最佳做法吗?
解决方案
回答
确实没有比这更简单的方法了。这些API尚未由.NET Framework公开(因此),因此,实现此目标的唯一方法是通过某种互操作(或者WPF)。
至于使用两个Windows版本,我们都应该拥有良好的代码,因为在我们实际调用函数之前,运行时不会去寻找DLL的入口点。
回答
DannySmurf说了。通过.NET框架,我们没有对这些API的直接"托管"访问权限(几周前我亲自尝试过)。
我最终做了些讨厌的事。使用GDI +创建了自己的UI。 (按钮,圆形标签等)。无论Windows版本如何,其外观都相同。 Win.Forms确实很丑陋,但这就是XP <方面的全部功能。
回答
我不介意非托管调用是使用黑盒来模仿alpha行为以及其对顶部某些组件中的黑色元素产生的影响的破解。
回答
我认为我们忘记设置要成为玻璃区域的TransparencyKey。根据文章,
In your Windows Forms application, you simply need to set the TransparencyKey property to a color that you won't use elsewhere in the application (I use Gainsboro, for reasons that will become apparent later). Then you can create one or more panels that are docked to the margins of your form and set the background color for the panel to the transparency key. Now when you call DwmExtendFrameIntoClientArea, the glass will show within its margins wherever you've set something of the appropriate transparency key.
回答
可以使用的廉价技巧是在窗体上放置透明的Panel控件,然后将控件放在其上-黑色将变为黑色。