簡體   English   中英

WPF玻璃窗口沒有邊框,沒有重新調整焦點

[英]WPF glass window with no border and no resize out of focus

我正在嘗試使用DmwAPIDwmEnableBlurBehindWindow方法創建一個Aero glass 無邊框 且不可調整大小的 WPF窗口。 但是,由於某些奇怪的原因,此窗口的玻璃顏色看起來好像窗口沒有焦點 正如您在接下來的三張圖片中看到的那樣,帶有邊框的正常窗口( 例如圖1和圖2 )工作得很好並且按預期做出反應(焦點為深藍色,失焦時為白色(=非活動))。

使用ToolWindow的DwmEnableBlurBehindWindowDwmEnableBlurBehindWindow與ToolWindow失焦

允許調整大小的無邊框窗口顯示相同的行為:

DwmEnableBlurBehindWindow具有無邊框但可調整大小的窗口

然而, 不可調整大小的無邊界窗口在它處於活動狀態和非活動狀態時總是看起來好像沒有聚焦(如上圖3所示 )。 它總是看起來發白:

DwmEnableBlurBehindWindow具有無邊框和不可調整大小的窗口

這是我如何設置玻璃樣式的一些示例代碼:

public MainWindow()
{
    InitializeComponent();

    WindowStyle = WindowStyle.None;
    ResizeMode = ResizeMode.NoResize;

    Height = 200;

    Background = Brushes.Transparent;
    Loaded += MainWindow_Loaded;
}
void MainWindow_Loaded(object sender, RoutedEventArgs e)
{
    var windowInteropHelper = new WindowInteropHelper(this);
    var handle = windowInteropHelper.Handle;
    var mainWindowSrc = HwndSource.FromHwnd(handle);

    if (mainWindowSrc != null)
        if (mainWindowSrc.CompositionTarget != null)
            mainWindowSrc.CompositionTarget.BackgroundColor = Color.FromArgb(0, 0, 0, 0);
    var glassParams = new DwmApi.DwmBlurbehind
    {
        dwFlags = DwmApi.DwmBlurbehind.DWM_BB_ENABLE,
        fEnable = true,
        hRegionBlur = IntPtr.Zero
    };

    IntPtr dis = new IntPtr(2);
    DwmApi.DwmSetWindowAttribute(mainWindowSrc.Handle,
                DwmApi.DwmWindowAttribute.DWMWA_LAST,
                dis,
                sizeof(uint));

    DwmApi.DwmEnableBlurBehindWindow(
        handle,
        glassParams
        );
}

我試着聚焦窗口,但這似乎並沒有影響行為。 關於如何解決這個問題的任何想法或指示?

我在dwm apis中工作不多,所以我可能錯了,但我認為默認的dwm渲染策略是使用WindowStyle來計算渲染。 如果您禁用該策略,它的行為與您期望的一樣。

添加以下位來修復你的藍調xD

const int DWMWA_NCRENDERING_POLICY = 2;
int DWMNCRP_DISABLED = 2;

DwmSetWindowAttribute(hwnd, DWMWA_NCRENDERING_POLICY, ref DWMNCRP_DISABLED, sizeof(int));

有關此伏都教的詳細信息,請查看以下資源:

DwmSetWindowAttribute函數

DWMWINDOWATTRIBUTE枚舉

MainWindow.xaml

<Window x:Class="dwm.MainWindow"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        Title="MainWindow" Height="350" Width="525"/>

MainWindow.xaml.cs

using System;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;

namespace dwm
{
    public partial class MainWindow
    {
        [DllImport("dwmapi.dll", PreserveSig = false)]
        public static extern void DwmEnableBlurBehindWindow(IntPtr hwnd, ref DwmBlurbehind blurBehind);

        [DllImport("dwmapi.dll", PreserveSig = true)]
        private static extern int DwmSetWindowAttribute(IntPtr hwnd, int attr, ref int attrValue, int attrSize);

        public MainWindow()
        {
            InitializeComponent();
        }

        protected override void OnSourceInitialized(EventArgs e)
        {
            base.OnSourceInitialized(e);

            var bb = new DwmBlurbehind
            {
                dwFlags = CoreNativeMethods.DwmBlurBehindDwFlags.DwmBbEnable,
                Enabled = true
            };

            WindowStartupLocation = WindowStartupLocation.CenterScreen;
            Background = Brushes.Transparent;
            ResizeMode = ResizeMode.NoResize;
            WindowStyle = WindowStyle.None;

            Focus();

            var hwnd = new WindowInteropHelper(this).Handle;

            HwndSource.FromHwnd(hwnd).CompositionTarget.BackgroundColor = Colors.Transparent;

            DwmEnableBlurBehindWindow(hwnd, ref bb);

            const int dwmwaNcrenderingPolicy = 2;
            var dwmncrpDisabled = 2;

            DwmSetWindowAttribute(hwnd, dwmwaNcrenderingPolicy, ref dwmncrpDisabled, sizeof(int));
        }

        [StructLayout(LayoutKind.Sequential)]
        public struct DwmBlurbehind
        {
            public CoreNativeMethods.DwmBlurBehindDwFlags dwFlags;
            public bool Enabled;
            public IntPtr BlurRegion;
            public bool TransitionOnMaximized;
        }

        public static class CoreNativeMethods
        {
            public enum DwmBlurBehindDwFlags
            {
                DwmBbEnable = 1,
                DwmBbBlurRegion = 2,
                DwmBbTransitionOnMaximized = 4
            }
        }
    }
}

屏幕截圖有焦點

屏幕截圖有焦點

沒有焦點的截圖

沒有焦點的Screenoshot

測試環境

操作系統:Windows 8 - x64

主題:標准窗戶

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM