K.K.Vinay Kumar K.K.Vinay Kumar - 3 months ago 22
C# Question

How to make WPF application visible in aero peek?

I've developed an application in WPF now it is 99% complete last thing I want to implement is application to stay visible even in show desktop as well as in aero peek just like the way the gadgets in Windows 7 used to stay. I know there are similar questions asked in Stack Overflow but none of them work in my Windows 10 64 bit OS. The application is 32 bit processor architecture. Please help me with detailed answer and an code that works in Windows 10 64 bit. Language I'm using is C#.

Answer

just call the StayVisible function and your window will stay visible, even in AeroPeek mode.

Most of the code is from 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
    }