microsoft / microsoft-ui-xaml

Windows UI Library: the latest Windows 10 native controls and Fluent styles for your applications
MIT License
6.36k stars 678 forks source link

Working with Mica #6186

Closed ghost1372 closed 2 years ago

ghost1372 commented 3 years ago

I know Mica is not supported yet. However, with the help of some PInvoke functions, Mica is activated on WPF apps image

so I tried to do the same in WinUI3 preview 3

[DllImport("dwmapi.dll")]
        public static extern int DwmSetWindowAttribute(IntPtr hwnd, DwmWindowAttribute dwAttribute, ref int pvAttribute, int cbAttribute);

        [Flags]
        public enum DwmWindowAttribute : uint
        {
            DWMWA_USE_IMMERSIVE_DARK_MODE = 20,
            DWMWA_MICA_EFFECT = 1029
        }
        public static void EnableMica(IntPtr source, bool darkThemeEnabled)
        {
            int trueValue = 0x01;
            int falseValue = 0x00;

            // Set dark mode before applying the material, otherwise you'll get an ugly flash when displaying the window.
            if (darkThemeEnabled)
                DwmSetWindowAttribute(source, DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref trueValue, Marshal.SizeOf(typeof(int)));
            else
                DwmSetWindowAttribute(source, DwmWindowAttribute.DWMWA_USE_IMMERSIVE_DARK_MODE, ref falseValue, Marshal.SizeOf(typeof(int)));

            DwmSetWindowAttribute(source, DwmWindowAttribute.DWMWA_MICA_EFFECT, ref trueValue, Marshal.SizeOf(typeof(int)));
        }

public MainWindow()
        {
            this.InitializeComponent();
            IntPtr hwnd = WinRT.Interop.WindowNative.GetWindowHandle(this);
            EnableMica(hwnd, true);
        }

TitleBar activates Mica well, but not Windows content

image

Is there a solution?😁

nahomebssa commented 3 years ago

Try setting the window style to WS_EX_NOREDIRECTIONBITMAP.

ghost1372 commented 3 years ago

@nahomebssa Window does not have a style property, How should I set the style?

seven-mile commented 3 years ago

@nahomebssa Window does not have a style property, How should I set the style?

He probably means the Win32 "Extended Window Style", which can be set during calling CreateWindow or be set by calling SetWindowLongPtr at runtime. For WinUI3, you can hardly change the behavior of CreateWindow, while SetWindowLongPtr doesn't support all styles. I'm not sure if it works for WS_EX_NOREDIRECTIONBITMAP.

But it seems you should somehow set the background of ClientArea to transparency... Why WPF can handle it automatically?

StephenLPeters commented 3 years ago

@jevansaks FYI, looks pretty interesting!

StephenLPeters commented 3 years ago

@codendone FYI as well

Link1J commented 3 years ago

But it seems you should somehow set the background of ClientArea to transparency... Why WPF can handle it automatically?

@seven-mile It doesn't, the example sets the background to transparent. Like you recommended, and is the exact thing WinUI 2 does. https://github.com/Difegue/Mica-WPF-Sample/blob/c579d038f762053e84b03f24acf0c8fc24e13d31/MicaTest/MainWindow.xaml#L9

Lukespacewalker commented 3 years ago

Window in WinUI 3 doesn't have Background property. How can I set it to transpararent?

seven-mile commented 3 years ago

Window in WinUI 3 doesn't have Background property. How can I set it to transpararent?

I think that's why Mica is still not out?

ghost1372 commented 3 years ago

Window in WinUI 3 doesn't have Background property. How can I set it to transpararent?

I think that's why Mica is still not out?

I changed the Windows style as mentioned above but nothing happened😫

jonasnordlund commented 2 years ago

I read that this method may no longer be supported since Windows 11 Build 22494.1000? I can't confirm this myself though.

dongle-the-gadget commented 2 years ago

I read that this method may no longer be supported since Windows 11 Build 22494.1000? I can't confirm this myself though.

Can't confirm on 22494 either, but it surely doesn't work for 22504+

ghost1372 commented 2 years ago

@seven-mile FYI, Mica Sample available in WASDK-Sample Repo, but only for C++, can you convert it to c#?

dongle-the-gadget commented 2 years ago

Doesn't look like it's going to work very well in WPF since it requires the use of a Visual layer, thus creating the airspace bug.

IcySnex commented 2 years ago

You need to set the background of your application to transparent.

You can do this via Win32 API calls. I have created a sample project here: https://github.com/IcySnex/WinUI3-Transparent-Mica-Acrylic-Blurred. You can also look at a video here: https://www.youtube.com/watch?v=mfS8PT9Z3u8. But be aware that you need to have at least the WindowsAppSDK version 1.1 - preview 1.

dongle-the-gadget commented 2 years ago

That method depends on a private API that has been removed since 22494+ (in 22523 and later it's replaced. The proper way is to use Windows.UI.Composition.Compositor, however this approach requires either a swapchain or a HwndHost (or some other way idk), the latter causes the airspace problem.