在WPF应用程序中禁用Aero Peek

问题描述

| 我想在WPF应用程序中禁用Aero Peek(当用户将鼠标置于“显示桌面”按钮上时,我的应用程序必须可见)。我使用以下PInvoke签名:
[Flags]
public enum DwmWindowAttribute : uint
{
    DWMWA_NCRENDERING_ENABLED = 1,DWMWA_NCRENDERING_POLICY,DWMWA_TRANSITIONS_FORCEdisABLED,DWMWA_ALLOW_NCPAINT,DWMWA_CAPTION_BUTTON_BOUNDS,DWMWA_NONCLIENT_RTL_LAYOUT,DWMWA_FORCE_ICONIC_REPRESENTATION,DWMWA_FLIP3D_POLICY,DWMWA_EXTENDED_FRAME_BOUNDS,DWMWA_HAS_ICONIC_BITMAP,DWMWA_disALLOW_PEEK,DWMWA_EXCLUDED_FROM_PEEK,DWMWA_LAST
}

[Flags]
public enum DWMNCRenderingPolicy : uint
{
    UseWindowStyle,disabled,Enabled,Last
}

[DllImport(\"dwmapi.dll\",PreserveSig=false)]
[return: MarshalAs(UnmanagedType.Bool)]
public static extern bool DwmIsCompositionEnabled();

[DllImport(\"dwmapi.dll\",PreserveSig=false)]
public static extern Int32 DwmSetwindowAttribute(IntPtr hwnd,DwmWindowAttribute dwmAttribute,IntPtr pvAttribute,uint cbAttribute);
和这种用法
    var helper = new WindowInteropHelper(this);
    helper.EnsureHandle();

    if (API.DwmIsCompositionEnabled())
    {
        var status = Marshal.AllocCoTaskMem(sizeof(uint));
        Marshal.copy(new[] {(int) API.DWMNCRenderingPolicy.Enabled},status,1);
        API.DwmSetwindowAttribute(helper.Handle,API.DwmWindowAttribute.DWMWA_EXCLUDED_FROM_PEEK,sizeof (uint));
    }
在我的64位系统(Windows 7 Professional)中,仅当我运行64位应用程序时,它才有效。如果我以WOW64模式运行32位应用程序,则会收到异常: \“对PInvoke函数\'XXX :: DwmSetwindowAttribute \的调用已使堆栈不平衡。这可能是因为托管的PInvoke签名与非托管的目标签名不匹配。请检查PInvoke签名的调用约定和参数是否与目标匹配非托管签名。\“ 你怎么看待这件事?解决办法是什么?     

解决方法

        我更改签名:
[DllImport(\"dwmapi.dll\",PreserveSig = true)]
public static extern int DwmSetWindowAttribute(IntPtr hwnd,DwmWindowAttribute dwmAttribute,IntPtr pvAttribute,uint cbAttribute);
和用法:
if (API.DwmIsCompositionEnabled())
{
    var status = Marshal.AllocHGlobal(sizeof(int));
    Marshal.WriteInt32(status,1); // true
    API.DwmSetWindowAttribute(helper.Handle,API.DwmWindowAttribute.DWMWA_EXCLUDED_FROM_PEEK,status,sizeof(int));
}