如何使WPF应用程序在aero peek中可见
本文关键字:peek aero 何使 WPF 应用程序 | 更新日期: 2023-09-27 18:01:21
我已经在WPF中开发了一个应用程序,现在它已经完成了99%,我想实现的最后一件事是应用程序即使在显示桌面和aero peek中也能保持可见,就像Windows 7中的小工具一样。我知道Stack Overflow中也有类似的问题,但在我的Windows 10 64位操作系统中没有一个问题。该应用程序采用32位处理器架构。请帮助我提供详细的答案和在Windows 10 64位工作的代码。我使用的语言是c#
只需调用StayVisible
函数,您的窗口将保持可见,即使在AeroPeek模式
大部分代码来自这里。
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;
}
}