如何在aero peek中看到WPF应用程序?

时间:2016-08-07 13:33:17

标签: c# wpf

我已经在WPF中开发了一个应用程序,现在它完成了99%,我要实现的最后一件事就是应用程序即使在显示桌面以及空中查看也能保持可见,就像Windows 7中的小工具一样。我知道在Stack Overflow中有类似的问题,但它们都不能在我的Windows 10 64位操作系统中运行。该应用程序是32位处理器架构。请帮助我详细解答和在Windows 10 64位中运行的代码。我正在使用的语言是C#。

1 个答案:

答案 0 :(得分:1)

只需拨打StayVisible功能,即使在AeroPeek模式下,您的窗口仍然可见。

大部分代码来自here

    public void StayVisible() {
        var helper = new WindowInteropHelper(this);
        helper.EnsureHandle();

        if (!DwmIsCompositionEnabled()) return;

        var status = Marshal.AllocCoTaskMem(sizeof(uint));
        Marshal.Copy(new[] {(int) DwmncRenderingPolicy.DWMNCRP_ENABLED}, 0, status, 1);
        DwmSetWindowAttribute(helper.Handle,
            DwmWindowAttribute.DWMWA_EXCLUDED_FROM_PEEK,
            status,
            sizeof(uint));
    }

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

    [DllImport("dwmapi.dll", PreserveSig = true)]
    private static extern int DwmSetWindowAttribute(IntPtr hwnd,
        DwmWindowAttribute dwmAttribute,
        IntPtr pvAttribute,
        uint cbAttribute);

    [Flags]
    private 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
    }

    private enum DwmncRenderingPolicy {
        DWMNCRP_USEWINDOWSTYLE,
        DWMNCRP_DISABLED,
        DWMNCRP_ENABLED,
        DWMNCRP_LAST
    }

更新.NET 3.5

如果您使用的是.NET 3.5,则需要一个简单的扩展方法来使EnsureHandle方法可用。或者你将.net升级到4.0 - 它就在你身上。

I found it here

using System;
using System.Reflection;
using System.Windows;
using System.Windows.Interop;

/// Provides NetFX 4.0 EnsureHandle method for
/// NetFX 3.5 WindowInteropHelper class.
public static class WindowInteropHelperExtensions {
    public static IntPtr EnsureHandle(this WindowInteropHelper helper) {
        if (helper == null) throw new ArgumentNullException("helper");
        if (helper.Handle != IntPtr.Zero) return helper.Handle;

        var window = (Window) typeof(WindowInteropHelper).InvokeMember("_window",
            BindingFlags.GetField | BindingFlags.Instance | BindingFlags.NonPublic,
            null, helper, null);

        typeof(Window).InvokeMember("SafeCreateWindow",
            BindingFlags.InvokeMethod | BindingFlags.Instance | BindingFlags.NonPublic,
            null, window, null);

        return helper.Handle;
    }
}