private static void SetAccentPolicy(IntPtr windowHandle, bool isWindowActive, bool isDarkTheme) { //DwmHelper.WindowExtendIntoClientArea(windowHandle, new MARGINS(-1, -1, -1, -1)); DwmHelper.WindowExtendIntoClientArea(windowHandle, new MARGINS { cyBottomHeight = 1 }); //DwmHelper.SetWindowAttributeValue(windowHandle, DWMWINDOWATTRIBUTE.VISIBLE_FRAME_BORDER_THICKNESS, 0); var accentPolicy = default(AccentPolicy); var accentPolicySize = Marshal.SizeOf(accentPolicy); accentPolicy.AccentFlags = 2; accentPolicy.AccentState = isWindowActive switch { true when OSVersionHelper.IsWindows10_1803_OrGreater => AccentState.ACCENT_ENABLE_ACRYLICBLURBEHIND, true when OSVersionHelper.IsWindows10_OrGreater => AccentState.ACCENT_ENABLE_BLURBEHIND, true => AccentState.ACCENT_ENABLE_TRANSPARENTGRADIENT, false => AccentState.ACCENT_ENABLE_HOSTBACKDROP }; accentPolicy.GradientColor = isDarkTheme ? 0x99000000 : 0x99FFFFFF; //ResourceHelper.GetResource<uint>(ResourceToken.BlurGradientValue); var accentPtr = Marshal.AllocHGlobal(accentPolicySize); Marshal.StructureToPtr(accentPolicy, accentPtr, false); var data = new WindowCompositionAttributeData { Attribute = WindowCompositionAttribute.WCA_ACCENT_POLICY, SizeOfData = accentPolicySize, Data = accentPtr }; SetWindowCompositionAttribute(windowHandle, ref data); Marshal.FreeHGlobal(accentPtr); }
public WindowBase() { this.ContentRendered += delegate { //添加窗口阴影 ShadowWindow.Attach(this); }; if (DwmHelper.IsDwmSupported) { DwmHelper = new DwmHelper(this); DwmHelper.AeroGlassEffectChanged += DwmHelper_AeroGlassEffectChanged; } }
private static void EnableMicaEffect(IntPtr windowHandle, bool isDarkTheme) { DwmHelper.WindowExtendIntoClientArea(windowHandle, new MARGINS { cxLeftWidth = -1, cyTopHeight = -1, cxRightWidth = -1, cyBottomHeight = -1 }); //var value = NativeMethods.DwmGetWindowAttribute(windowHandle, DWMWINDOWATTRIBUTE.CAPTION_BUTTON_BOUNDS, out RECT rect, Marshal.SizeOf<RECT>()); var trueValue = 0x01; var falseValue = 0x00; // Set dark mode before applying the material, otherwise you'll get an ugly flash when displaying the window. if (isDarkTheme) { DwmHelper.SetWindowAttributeValue(windowHandle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, trueValue); } else { DwmHelper.SetWindowAttributeValue(windowHandle, DWMWINDOWATTRIBUTE.DWMWA_USE_IMMERSIVE_DARK_MODE, falseValue); } //MICA_EFFECT = 1029, // [set] BOOL, Enables or disables the Mica window effect DwmHelper.SetWindowAttributeValue(windowHandle, (DWMWINDOWATTRIBUTE)1029, trueValue); }
public MainWindow() { InitializeComponent(); DwmHelper.DropShadowToWindow(this); }