WinUI3/WPF 实现窗口可穿透
-
设置背景为透明
// 适用于 Windows App SDK 1.5 / WinUI3
private const int WS_EX_TRANSPARENT = 0x00000020;
private const int GWL_EXSTYLE = -20;
private const int GWL_STYLE = -16;
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
// 在页面启动时
IntPtr hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
int extendedStyle = GetWindowLong(hwnd, GWL_STYLE);
_ = SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);
//_ = SetWindowLong(hwnd, GWL_EXSTYLE, 0x80020); // 0x80020可以实现窗口透明
// 适用于 WPF
private const int WS_EX_TRANSPARENT = 0x00000020;
private const int GWL_EXSTYLE = -20;
[DllImport("user32.dll")]
private static extern int GetWindowLong(IntPtr hwnd, int index);
[DllImport("user32.dll")]
private static extern int SetWindowLong(IntPtr hwnd, int index, int newStyle);
// 在页面启动时
IntPtr hwnd = System.Windows.Interop.WindowInteropHelper(this).Handle;
int extendedStyle = GetWindowLong(hwnd, GWL_EXSTYLE);
_ = SetWindowLong(hwnd, GWL_EXSTYLE, extendedStyle | WS_EX_TRANSPARENT);