Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 466
Satumba Avatar asked Apr 19 '09 12:04

Satumba


2 Answers

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>
like image 161
Satumba Avatar answered Oct 05 '22 11:10

Satumba


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
like image 22
Nir Avatar answered Oct 05 '22 12:10

Nir