Satumba
Satumba

Reputation: 880

Create a transparent hole inside a window's background - WPF

I have a window with these values:

WindowState="Maximized"
AllowsTransparency="True"
Opacity="0.5"
WindowStyle="None"

This window is coming on top of other window (as a pop-up) with content on it on a specific location.

I have a new requirement. This window have to show a rectangle area from the window below. In other words, i have to set a "hole" in this window which will be totally transparent (without the opacity value). Until this moment i couldn't figure out a way to make this transparent hole.

Hope to get an idea...

Upvotes: 8

Views: 8809

Answers (2)

Nir
Nir

Reputation: 29594

try to avoid AllowsTransparency=true, it is very buggy and slow.

you can PInvoke SetWindowRgn to create a a window of any shape:

  1. Use CreateRectRgn twice, once for the window bounding rectangle and once for the hole.
  2. Use CombineRgn with RGN_AND as the 4th parameter to get a region with an hole in it
  3. Call SetWindowRgn to apply the region to the window
  4. Don't forget to delete all the regions except for the one you passed to SetWindowRgn

Upvotes: 4

Satumba
Satumba

Reputation: 880

I found a kind of solution for it:

this is the pop-up window that on top of another window, and containing a hole to the other window in a desired place:

Window's header:

    WindowState="Maximized"
    AllowsTransparency="True"
    WindowStyle="None"

Window's content:

<Window.Background >
    <SolidColorBrush x:Name="BackgroundBrush" Color="WhiteSmoke" Opacity="0" ></SolidColorBrush>
</Window.Background>
<Canvas x:Name="ContectHolder" >
    <Path Stroke="Black" Fill="WhiteSmoke" Opacity="0.8">
        <Path.Data>
            <CombinedGeometry GeometryCombineMode="Exclude">
                <CombinedGeometry.Geometry1  >
                    <RectangleGeometry Rect="0,0,2000,2000"  />
                </CombinedGeometry.Geometry1>
                <CombinedGeometry.Geometry2>
                    <RectangleGeometry Rect="75,75,400,900" />
                </CombinedGeometry.Geometry2>
            </CombinedGeometry>
        </Path.Data>
    </Path>
</Canvas>

Upvotes: 12

Related Questions