WPF中,如何将Vista Aero效果扩展到整个窗口

                                                  WPF中,如何将Vista Aero效果扩展到整个窗口
                                                                                           
周银辉
效果图:
AeroDemo.png


有不少示例介绍了如何将Vista Aero效果扩展到整个窗口,但大都是针对Windows Form应用程序,而不是WPF(即前者针对的是Form类,后者是针对的Window类),比如 http://www.cnblogs.com/zhouyinhui/archive/2007/05/30/765416.html
其实与其类似,都是调用dwmapi,只不过Window类没有直接给我们提供句柄,我们需要这样的代码来找到其句柄:
None.gif IntPtr hwnd  =   new  System.Windows.Interop.WindowInteropHelper(window).Handle;
然后将窗口的背景设置为透明:
None.gif window.Background  =  Brushes.Transparent;
None.gif            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor 
=  Colors.Transparent;
最后调用
None.gif  DwmApi.DwmExtendFrameIntoClientArea(hwnd, margins);
注意,我们应该在窗口被显示之后(SourceInitialized之后)再调用我们的函数否则会引发异常。

参考代码:
None.gif   public  partial  class  Window1 : System.Windows.Window
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif
InBlock.gif        
public Window1()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            InitializeComponent();
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        
protected override void OnSourceInitialized(EventArgs e)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
base.OnSourceInitialized(e);
InBlock.gif            DWMLib.AeroHelper.ExtendGlassFrame(
thisnew Thickness(-1));
InBlock.gif
ExpandedSubBlockEnd.gif        }

InBlock.gif
ExpandedBlockEnd.gif    }

None.gif public   class  AeroHelper
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        
public static bool ExtendGlassFrame(Window window, Thickness margin)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
if (!DwmApi.DwmIsCompositionEnabled())
InBlock.gif                
return false;
InBlock.gif
InBlock.gif            IntPtr hwnd 
= new WindowInteropHelper(window).Handle;
InBlock.gif            
if (hwnd == IntPtr.Zero)
InBlock.gif                
throw new InvalidOperationException("The Window must be shown before extending glass.");
InBlock.gif
InBlock.gif            
// Set the background to transparent from both the WPF and Win32 perspectives
InBlock.gif
            window.Background = Brushes.Transparent;
InBlock.gif            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor 
= Colors.Transparent;
InBlock.gif
InBlock.gif            DWMLib.DwmApi.MARGINS margins 
= new DWMLib.DwmApi.MARGINS((int)margin.Left, (int)margin.Top, (int)margin.Right, (int)margin.Bottom);
InBlock.gif            DwmApi.DwmExtendFrameIntoClientArea(hwnd, margins);
InBlock.gif
InBlock.gif            
return true;
ExpandedSubBlockEnd.gif        }

InBlock.gif
None.gif public   class  DwmApi
ExpandedBlockStart.gifContractedBlock.gif    
dot.gif {
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmEnableBlurBehindWindow(IntPtr hWnd, DWM_BLURBEHIND pBlurBehind);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmExtendFrameIntoClientArea(IntPtr hWnd, MARGINS pMargins);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern bool DwmIsCompositionEnabled();
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmGetColorizationColor(
InBlock.gif            
out int pcrColorization,
InBlock.gif            [MarshalAs(UnmanagedType.Bool)]
out bool pfOpaqueBlend);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmEnableComposition(bool bEnable);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern IntPtr DwmRegisterThumbnail(IntPtr dest, IntPtr source);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmUnregisterThumbnail(IntPtr hThumbnail);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmUpdateThumbnailProperties(IntPtr hThumbnail, DWM_THUMBNAIL_PROPERTIES props);
InBlock.gif
InBlock.gif        [DllImport(
"dwmapi.dll", PreserveSig = false)]
InBlock.gif        
public static extern void DwmQueryThumbnailSourceSize(IntPtr hThumbnail, out Size size);
InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
public class DWM_THUMBNAIL_PROPERTIES
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public uint dwFlags;
InBlock.gif            
public RECT rcDestination;
InBlock.gif            
public RECT rcSource;
InBlock.gif            
public byte opacity;
InBlock.gif            [MarshalAs(UnmanagedType.Bool)]
InBlock.gif            
public bool fVisible;
InBlock.gif            [MarshalAs(UnmanagedType.Bool)]
InBlock.gif            
public bool fSourceClientAreaOnly;
InBlock.gif
InBlock.gif            
public const uint DWM_TNP_RECTDESTINATION = 0x00000001;
InBlock.gif            
public const uint DWM_TNP_RECTSOURCE = 0x00000002;
InBlock.gif            
public const uint DWM_TNP_OPACITY = 0x00000004;
InBlock.gif            
public const uint DWM_TNP_VISIBLE = 0x00000008;
InBlock.gif            
public const uint DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
public class MARGINS
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int cxLeftWidth, cxRightWidth, cyTopHeight, cyBottomHeight;
InBlock.gif
InBlock.gif            
public MARGINS(int left, int top, int right, int bottom)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                cxLeftWidth 
= left;
InBlock.gif                cyTopHeight 
= top;
InBlock.gif                cxRightWidth 
= right;
InBlock.gif                cyBottomHeight 
= bottom;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
public class DWM_BLURBEHIND
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public uint dwFlags;
InBlock.gif            [MarshalAs(UnmanagedType.Bool)]
InBlock.gif            
public bool fEnable;
InBlock.gif            
public IntPtr hRegionBlur;
InBlock.gif            [MarshalAs(UnmanagedType.Bool)]
InBlock.gif            
public bool fTransitionOnMaximized;
InBlock.gif
InBlock.gif            
public const uint DWM_BB_ENABLE = 0x00000001;
InBlock.gif            
public const uint DWM_BB_BLURREGION = 0x00000002;
InBlock.gif            
public const uint DWM_BB_TRANSITIONONMAXIMIZED = 0x00000004;
ExpandedSubBlockEnd.gif        }

InBlock.gif
InBlock.gif        [StructLayout(LayoutKind.Sequential)]
InBlock.gif        
public struct RECT
ExpandedSubBlockStart.gifContractedSubBlock.gif        
dot.gif{
InBlock.gif            
public int left, top, right, bottom;
InBlock.gif
InBlock.gif            
public RECT(int left, int top, int right, int bottom)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
dot.gif{
InBlock.gif                
this.left = left;
InBlock.gif                
this.top = top;
InBlock.gif                
this.right = right;
InBlock.gif                
this.bottom = bottom;
ExpandedSubBlockEnd.gif            }

ExpandedSubBlockEnd.gif        }

ExpandedBlockEnd.gif    }

Demo, 要正确运行Demo,你应该使用开启Aero效果的Vista版本
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值