public static void DrawDragRectangles(Rectangle[] newRects, int indent) { if (newRects.Length > 0) { // Create the first region IntPtr newRegion = CreateRectangleRegion(newRects[0], indent); for (int index = 1; index < newRects.Length; index++) { // Create the extra region IntPtr extraRegion = CreateRectangleRegion(newRects[index], indent); // Remove the intersection of the existing and extra regions Gdi32.CombineRgn(newRegion, newRegion, extraRegion, (int)Win32.CombineFlags.RGN_XOR); // Remove unwanted intermediate objects Gdi32.DeleteObject(extraRegion); } // Get hold of the DC for the desktop IntPtr hDC = User32.GetDC(IntPtr.Zero); // Define the area we are allowed to draw into Gdi32.SelectClipRgn(hDC, newRegion); Win32.RECT rectBox = new Win32.RECT(); // Get the smallest rectangle that encloses region Gdi32.GetClipBox(hDC, ref rectBox); IntPtr brushHandler = GetHalfToneBrush(); // Select brush into the device context IntPtr oldHandle = Gdi32.SelectObject(hDC, brushHandler); // Blit to screen using provided pattern brush and invert with existing screen contents Gdi32.PatBlt(hDC, rectBox.left, rectBox.top, rectBox.right - rectBox.left, rectBox.bottom - rectBox.top, (uint)RasterOperations.PATINVERT); // Put old handle back again Gdi32.SelectObject(hDC, oldHandle); // Reset the clipping region Gdi32.SelectClipRgn(hDC, IntPtr.Zero); // Remove unwanted region object Gdi32.DeleteObject(newRegion); // Must remember to release the HDC resource! User32.ReleaseDC(IntPtr.Zero, hDC); } }
public static void DrawDragOutline(Region region) { if (region == null) { return; } // Get hold of the DC for the desktop IntPtr hDC = User32.GetDC(IntPtr.Zero); // Define the area we are allowed to draw into IntPtr hRegion = region.GetHrgn(Graphics.FromHdc(hDC)); Gdi32.SelectClipRgn(hDC, hRegion); Win32.RECT rectBox = new Win32.RECT(); // Get the smallest rectangle that encloses region Gdi32.GetClipBox(hDC, ref rectBox); IntPtr brushHandler = GetHalfToneBrush(); // Select brush into the device context IntPtr oldHandle = Gdi32.SelectObject(hDC, brushHandler); // Blit to screen using provided pattern brush and invert with existing screen contents Gdi32.PatBlt(hDC, rectBox.left, rectBox.top, rectBox.right - rectBox.left, rectBox.bottom - rectBox.top, (uint)Win32.RasterOperations.PATINVERT); // Put old handle back again Gdi32.SelectObject(hDC, oldHandle); // Reset the clipping region Gdi32.SelectClipRgn(hDC, IntPtr.Zero); Gdi32.DeleteObject(hRegion); // Must remember to release the HDC resource! User32.ReleaseDC(IntPtr.Zero, hDC); }
public void CreateWithGraphicsBasedOnImageAppliesRequestedParameters() { using Bitmap b = new Bitmap(10, 10); using Graphics g = Graphics.FromImage(b); Rectangle clipRectangle = new Rectangle(1, 1, 5, 5); using Region r = new Region(clipRectangle); g.Clip = r; Matrix transform = new Matrix(); transform.Translate(1.0f, 2.0f); g.Transform = transform; // Just the translation transform using (var scope = new DeviceContextHdcScope(g, ApplyGraphicsProperties.TranslateTransform)) { Gdi32.GetViewportOrgEx(scope, out Point origin); Assert.Equal(new Point(1, 2), origin); RECT clipRect = default; RegionType regionType = Gdi32.GetClipBox(scope, ref clipRect); Assert.Equal(RegionType.SIMPLEREGION, regionType); Assert.Equal(new Rectangle(-1, -2, 10, 10), (Rectangle)clipRect); Assert.Equal(g, scope.DeviceContext); } // Just the clipping using (var scope = new DeviceContextHdcScope(g, ApplyGraphicsProperties.Clipping)) { Gdi32.GetViewportOrgEx(scope, out Point origin); Assert.Equal(new Point(0, 0), origin); RECT clipRect = default; RegionType regionType = Gdi32.GetClipBox(scope, ref clipRect); Assert.Equal(RegionType.SIMPLEREGION, regionType); Assert.Equal(clipRectangle, (Rectangle)clipRect); Assert.Equal(g, scope.DeviceContext); } // Both using (var scope = new DeviceContextHdcScope(g, ApplyGraphicsProperties.All)) { Gdi32.GetViewportOrgEx(scope, out Point origin); Assert.Equal(new Point(1, 2), origin); RECT clipRect = default; RegionType regionType = Gdi32.GetClipBox(scope, ref clipRect); Assert.Equal(RegionType.SIMPLEREGION, regionType); Assert.Equal(new Rectangle(0, -1, 5, 5), (Rectangle)clipRect); Assert.Equal(g, scope.DeviceContext); } // Nothing using (var scope = new DeviceContextHdcScope(g, ApplyGraphicsProperties.None)) { Gdi32.GetViewportOrgEx(scope, out Point origin); Assert.Equal(new Point(0, 0), origin); RECT clipRect = default; RegionType regionType = Gdi32.GetClipBox(scope, ref clipRect); Assert.Equal(RegionType.SIMPLEREGION, regionType); Assert.Equal(new Rectangle(0, 0, 10, 10), (Rectangle)clipRect); Assert.Equal(g, scope.DeviceContext); } // Validating we've unlocked the graphics object g.DrawLine(Pens.DarkBlue, default, default);