I have a System.Drawing.Region object with the GetBounds() method returning
{X = 0.0 Y = 0.0 Width = 120.0 Height = 120.0}
However when I execute the Exclude() method with a rectangle:
region.Exclude(New System.Drawing.Rectangle(60, -20, 100, 160))
I would expect the Region.GetBounds method to return
{X = 0.0 Y = 0.0 Width = 60.0 Height = 120.0}
instead the Exclude() call appears to do nothing at all. Similarly with the Intersect() method
region.Intersect(New System.Drawing.Rectangle(60, -20, 100, 160))
I would expect to see
{X = 60.0 Y = 0.0 Width = 60.0 Height = 120.0}
but again there is no change. Is this correct?
Edit: Specific context
I am working with the OnPaintBackground() method on a larger project in the direction of a base Control with general transparency: What is a general solution to creating a transparent control?
Protected Overrides Sub OnPaintBackground(pevent As System.Windows.Forms.PaintEventArgs)
Dim initialClip As Region = pevent.Graphics.Clip
'Develop list of underlying controls'
Dim submarinedControls As New List(Of Control)
For Each control As Control In Parent.Controls.ToArray.Reverse
If control IsNot Me AndAlso control.Visible AndAlso Me.ClientRectangle.IntersectsWith(control.RelativeClientRectangle(Me)) Then : submarinedControls.Add(control)
Else : Exit For
End If
Next
'Prepare clip for parent draw'
pevent.Graphics.Clip = New Region(initialClip.GetRegionData)
For Each control As Control In submarinedControls
pevent.Graphics.Clip.Exclude(control.RelativeClientRectangle(Me))
Next
...
End Sub
At the 'Prepare clip for parent redraw' section, after the initial clip has been recreated, its bounds are as specified. The next step is to exclude any underlying controls and only paint the areas where this control interacts directly with the background of the parent control. The exclude method here I can see is receiving the larger rectangle as its argument (as a Watch), but after the Exclude occurs, nothing about the clip's bounds changes.
Edit: Possible Resolution
It appears the Graphics.Clip region is managed by the Graphics object and is immutable. Replacing the exclusion snippet with the following yields all expected results:
'Prepare clip for parent draw'
Dim parentClip As System.Drawing.Region = New System.Drawing.Region(initialClip.GetRegionData)
For Each Control As Control In submarinedControls
parentClip.Exclude(Control.RelativeClientRectangle(Me))
Next
pevent.Graphics.Clip = parentClip
RelativeClientRectangle():
<Runtime.CompilerServices.Extension()>
Public Function RelativeClientRectangle(control As System.Windows.Forms.Control, toControl As System.Windows.Forms.Control) As System.Drawing.Rectangle
Return New System.Drawing.Rectangle(control.RelationTo(toControl), control.Size)
End Function
RelativeTo():
<Runtime.CompilerServices.Extension()>
Public Function RelationTo(control As System.Windows.Forms.Control, toControl As System.Windows.Forms.Control) As System.Drawing.Point
Return control.PointToScreen(New Point(0, 0)) - toControl.PointToScreen(New Point(0, 0))
End Function