Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Working with Mica #6186

Closed
ghost1372 opened this issue Oct 28, 2021 · 15 comments
Closed

Working with Mica #6186

ghost1372 opened this issue Oct 28, 2021 · 15 comments
Labels
area-Materials Reveal, Acrylic, lighting, etc. team-CompInput Issue for IXP (Composition, Input) team

Comments

@ghost1372
Copy link
Contributor

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
Copy link

Try setting the window style to WS_EX_NOREDIRECTIONBITMAP.

@ghost1372
Copy link
Contributor Author

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

@seven-mile
Copy link

@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?

@btueffers btueffers transferred this issue from microsoft/WindowsAppSDK Oct 28, 2021
@ghost ghost added the needs-triage Issue needs to be triaged by the area owners label Oct 28, 2021
@StephenLPeters
Copy link
Contributor

@jevansaks FYI, looks pretty interesting!

@StephenLPeters StephenLPeters added area-Materials Reveal, Acrylic, lighting, etc. team-CompInput Issue for IXP (Composition, Input) team labels Oct 28, 2021
@StephenLPeters
Copy link
Contributor

@codendone FYI as well

@Link1J
Copy link

Link1J commented Oct 31, 2021

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
Copy link

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

@seven-mile
Copy link

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
Copy link
Contributor Author

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
Copy link

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
Copy link

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
Copy link
Contributor Author

ghost1372 commented Apr 16, 2022

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

@dongle-the-gadget
Copy link

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
Copy link

IcySnex commented Apr 20, 2022

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
Copy link

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.

@ghost ghost removed the needs-triage Issue needs to be triaged by the area owners label Jun 6, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area-Materials Reveal, Acrylic, lighting, etc. team-CompInput Issue for IXP (Composition, Input) team
Projects
None yet
Development

No branches or pull requests

9 participants