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

create a DropShadow behind a control #10295

Open
tpoint75 opened this issue Jan 10, 2025 · 1 comment
Open

create a DropShadow behind a control #10295

tpoint75 opened this issue Jan 10, 2025 · 1 comment
Labels
needs-triage Issue needs to be triaged by the area owners

Comments

@tpoint75
Copy link

Describe the bug

I cant create a DropShadow behind a control. The shadow is always in front of it!

void ApplyDropShadow(FrameworkElement const& element) {

        // Get the Compositor and Visual for the element
        auto visual = ElementCompositionPreview::GetElementVisual(element);
        auto compositor = visual.Compositor();

        // Create a DropShadow
        DropShadow dropShadow = compositor.CreateDropShadow();
        dropShadow.Color(winrt::Windows::UI::ColorHelper::FromArgb(255, 0, 255, 255)); 
        dropShadow.BlurRadius(30.0f);
        dropShadow.Opacity(0.8f);
        dropShadow.Offset({ -5.0f, -5.0f, 0.0f });

        // Create a SpriteVisual and set the shadow
        SpriteVisual shadowVisual = compositor.CreateSpriteVisual();
        shadowVisual.Shadow(dropShadow);

        // Set the size of the shadow to match the element
        shadowVisual.Size({ element.ActualSize().x + 10, element.ActualSize().y + 10 });

        // Place the container visual behind the XAML element
        ElementCompositionPreview::SetElementChildVisual(element, shadowVisual);
}

Steps to reproduce the bug

execute code

Expected behavior

No response

Screenshots

No response

NuGet package version

None

Packaging type

No response

Windows version

No response

IDE

No response

Additional context

No response

@microsoft-github-policy-service microsoft-github-policy-service bot added the needs-triage Issue needs to be triaged by the area owners label Jan 10, 2025
@tpoint75 tpoint75 changed the title Bug title create a DropShadow behind a control Jan 10, 2025
@RDMacLachlan RDMacLachlan transferred this issue from microsoft/WindowsAppSDK Jan 13, 2025
@castorix
Copy link

I cannot test in C++, but it is the same code as in C#; a way is to render the control with RenderTargetBitmap (similar to in this test #10062), then add the shadow (and Mask depending on the control (GetAlphaMask))
A test with an Image control, by adapting your code :

Image

I also found other methods with another control, like at https://github.com/microsoft/uwp-experiences/blob/master/apps/News/News/Helpers/Composition/CompositionShadow.xaml.cs

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs-triage Issue needs to be triaged by the area owners
Projects
None yet
Development

No branches or pull requests

2 participants