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#.
1 Answers
1
votes
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
}
Update .NET 3.5
If you are using .NET 3.5 you need a litte extension method to make the EnsureHandle method available. Or you upgrade .net to 4.0 - it's on you.
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;
}
}