Skip to content

Commit

Permalink
Cleanup GetDCEx interop (#2309)
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe authored and RussKie committed Nov 7, 2019
1 parent 5badfc0 commit a76dac3
Show file tree
Hide file tree
Showing 9 changed files with 64 additions and 26 deletions.
28 changes: 28 additions & 0 deletions src/Common/src/Interop/User32/Interop.DCX.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;

internal static partial class Interop
{
internal static partial class User32
{
[Flags]
public enum DCX : uint
{
WINDOW = 0x00000001,
CACHE = 0x00000002,
NORESETATTRS = 0x00000004,
CLIPCHILDREN = 0x00000008,
CLIPSIBLINGS = 0x00000010,
PARENTCLIP = 0x00000020,
EXCLUDERGN = 0x00000040,
INTERSECTRGN = 0x00000080,
EXCLUDEUPDATE = 0x00000100,
INTERSECTUPDATE = 0x00000200,
LOCKWINDOWUPDATE = 0x00000400,
VALIDATE = 0x00200000,
}
}
}
23 changes: 23 additions & 0 deletions src/Common/src/Interop/User32/Interop.GetDCEx.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.

using System;
using System.Runtime.InteropServices;
using System.Windows.Forms;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern IntPtr GetDCEx(IntPtr hWnd, IntPtr hrgnClip, DCX flags);

public static IntPtr GetDCEx(IHandle hWnd, IntPtr hrgnClip, DCX flags)
{
IntPtr result = GetDCEx(hWnd.Handle, hrgnClip, flags);
GC.KeepAlive(hWnd);
return result;
}
}
}
4 changes: 0 additions & 4 deletions src/Common/src/NativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -140,10 +140,6 @@ public const int
DISP_E_MEMBERNOTFOUND = unchecked((int)0x80020003),
DISP_E_PARAMNOTFOUND = unchecked((int)0x80020004),
DIB_RGB_COLORS = 0,
DCX_WINDOW = 0x00000001,
DCX_CACHE = 0x00000002,
DCX_LOCKWINDOWUPDATE = 0x00000400,
DCX_INTERSECTRGN = 0x00000080,
DI_NORMAL = 0x0003,
DLGC_WANTARROWS = 0x0001,
DLGC_WANTTAB = 0x0002,
Expand Down
4 changes: 0 additions & 4 deletions src/Common/src/UnsafeNativeMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -197,10 +197,6 @@ public static StringBuilder GetModuleFileNameLongPath(HandleRef hModule)
[DllImport(ExternDll.User32, ExactSpelling = true)]
public static extern IntPtr ChildWindowFromPointEx(IntPtr hwndParent, Point pt, int uFlags);


[DllImport(ExternDll.User32, ExactSpelling = true)]
public static extern IntPtr GetDCEx(HandleRef hWnd, HandleRef hrgnClip, int flags);

[DllImport(ExternDll.Kernel32, CharSet = CharSet.Auto)]
public static extern uint GetShortPathName(string lpszLongPath, StringBuilder lpszShortPath, uint cchBuffer);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1991,7 +1991,7 @@ public static void DrawReversibleFrame(Rectangle rectangle, Color backColor, Fra
graphicsColor = Color.Black;
}

IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(null, UnsafeNativeMethods.GetDesktopWindow()), NativeMethods.NullHandleRef, NativeMethods.DCX_WINDOW | NativeMethods.DCX_LOCKWINDOWUPDATE | NativeMethods.DCX_CACHE);
IntPtr dc = User32.GetDCEx(UnsafeNativeMethods.GetDesktopWindow(), IntPtr.Zero, User32.DCX.WINDOW | User32.DCX.LOCKWINDOWUPDATE | User32.DCX.CACHE);
IntPtr pen;

switch (style)
Expand Down Expand Up @@ -2032,7 +2032,7 @@ public static unsafe void DrawReversibleLine(Point start, Point end, Color backC
{
Gdi32.R2 rop2 = (Gdi32.R2)GetColorRop(backColor, (int)Gdi32.R2.NOTXORPEN, (int)Gdi32.R2.XORPEN);

IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(null, UnsafeNativeMethods.GetDesktopWindow()), NativeMethods.NullHandleRef, NativeMethods.DCX_WINDOW | NativeMethods.DCX_LOCKWINDOWUPDATE | NativeMethods.DCX_CACHE);
IntPtr dc = User32.GetDCEx(UnsafeNativeMethods.GetDesktopWindow(), IntPtr.Zero, User32.DCX.WINDOW | User32.DCX.LOCKWINDOWUPDATE | User32.DCX.CACHE);
IntPtr pen = Gdi32.CreatePen(Gdi32.PS.SOLID, 1, ColorTranslator.ToWin32(backColor));

Gdi32.R2 prevRop2 = Gdi32.SetROP2(dc, rop2);
Expand Down Expand Up @@ -2222,7 +2222,7 @@ public static void FillReversibleRectangle(Rectangle rectangle, Color backColor)
0x5a0049); // RasterOp.BRUSH.XorWith(RasterOp.TARGET));
Gdi32.R2 rop2 = Gdi32.R2.NOT;

IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(null, UnsafeNativeMethods.GetDesktopWindow()), NativeMethods.NullHandleRef, NativeMethods.DCX_WINDOW | NativeMethods.DCX_LOCKWINDOWUPDATE | NativeMethods.DCX_CACHE);
IntPtr dc = User32.GetDCEx(UnsafeNativeMethods.GetDesktopWindow(), IntPtr.Zero, User32.DCX.WINDOW | User32.DCX.LOCKWINDOWUPDATE | User32.DCX.CACHE);
IntPtr brush = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(backColor));

Gdi32.R2 prevRop2 = Gdi32.SetROP2(dc, rop2);
Expand Down
5 changes: 2 additions & 3 deletions src/System.Windows.Forms/src/System/Windows/Forms/DataGrid.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6402,14 +6402,13 @@ private void DrawRowSplitBar(MouseEventArgs e)
/// </summary>
private void DrawSplitBar(Rectangle r)
{
IntPtr parentHandle = Handle;
IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(this, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
IntPtr dc = User32.GetDCEx(this, IntPtr.Zero, User32.DCX.CACHE | User32.DCX.LOCKWINDOWUPDATE);
IntPtr halftone = ControlPaint.CreateHalftoneHBRUSH();
IntPtr saveBrush = Gdi32.SelectObject(dc, halftone);
SafeNativeMethods.PatBlt(new HandleRef(this, dc), r.X, r.Y, r.Width, r.Height, NativeMethods.PATINVERT);
Gdi32.SelectObject(dc, saveBrush);
Gdi32.DeleteObject(halftone);
User32.ReleaseDC(new HandleRef(this, parentHandle), dc);
User32.ReleaseDC(new HandleRef(this, Handle), dc);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5679,8 +5679,7 @@ private void DrawShadowRect(Rectangle r)
{
const byte DATAGRIDVIEW_shadowEdgeThickness = 3;

IntPtr parentHandle = Handle;
IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(this, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
IntPtr dc = User32.GetDCEx(this, IntPtr.Zero, User32.DCX.CACHE | User32.DCX.LOCKWINDOWUPDATE);
IntPtr halftone = ControlPaint.CreateHalftoneHBRUSH();
IntPtr saveBrush = Gdi32.SelectObject(dc, halftone);

Expand All @@ -5691,7 +5690,7 @@ private void DrawShadowRect(Rectangle r)

Gdi32.SelectObject(dc, saveBrush);
Gdi32.DeleteObject(halftone);
User32.ReleaseDC(new HandleRef(this, parentHandle), dc);
User32.ReleaseDC(new HandleRef(this, Handle), dc);
}

/// <summary>
Expand All @@ -5700,14 +5699,13 @@ private void DrawShadowRect(Rectangle r)
/// </summary>
private void DrawSplitBar(Rectangle r)
{
IntPtr parentHandle = Handle;
IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(this, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
IntPtr dc = User32.GetDCEx(this, IntPtr.Zero, User32.DCX.CACHE | User32.DCX.LOCKWINDOWUPDATE);
IntPtr halftone = ControlPaint.CreateHalftoneHBRUSH();
IntPtr saveBrush = Gdi32.SelectObject(dc, halftone);
SafeNativeMethods.PatBlt(new HandleRef(this, dc), r.X, r.Y, r.Width, r.Height, NativeMethods.PATINVERT);
Gdi32.SelectObject(dc, saveBrush);
Gdi32.DeleteObject(halftone);
User32.ReleaseDC(new HandleRef(this, parentHandle), dc);
User32.ReleaseDC(new HandleRef(this, Handle), dc);
}

private void EditingControls_CommonMouseEventHandler(object sender, MouseEventArgs e, DataGridViewMouseEvent dgvme)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1676,14 +1676,13 @@ private void DrawFocus(Graphics g, Rectangle r)
private void DrawSplitHelper(int splitSize)
{
Rectangle r = CalcSplitLine(splitSize, 3);
IntPtr parentHandle = Handle;
IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(this, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
IntPtr dc = User32.GetDCEx(this, IntPtr.Zero, User32.DCX.CACHE | User32.DCX.LOCKWINDOWUPDATE);
IntPtr halftone = ControlPaint.CreateHalftoneHBRUSH();
IntPtr saveBrush = Gdi32.SelectObject(dc, halftone);
SafeNativeMethods.PatBlt(new HandleRef(this, dc), r.X, r.Y, r.Width, r.Height, NativeMethods.PATINVERT);
Gdi32.SelectObject(dc, saveBrush);
Gdi32.DeleteObject(halftone);
User32.ReleaseDC(new HandleRef(this, parentHandle), dc);
User32.ReleaseDC(new HandleRef(this, Handle), dc);
}

/// <summary>
Expand Down
5 changes: 2 additions & 3 deletions src/System.Windows.Forms/src/System/Windows/Forms/Splitter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -733,14 +733,13 @@ private void DrawSplitHelper(int splitSize)
}

Rectangle r = CalcSplitLine(splitSize, 3);
IntPtr parentHandle = ParentInternal.Handle;
IntPtr dc = UnsafeNativeMethods.GetDCEx(new HandleRef(ParentInternal, parentHandle), NativeMethods.NullHandleRef, NativeMethods.DCX_CACHE | NativeMethods.DCX_LOCKWINDOWUPDATE);
IntPtr dc = User32.GetDCEx(ParentInternal, IntPtr.Zero, User32.DCX.CACHE | User32.DCX.LOCKWINDOWUPDATE);
IntPtr halftone = ControlPaint.CreateHalftoneHBRUSH();
IntPtr saveBrush = Gdi32.SelectObject(dc, halftone);
SafeNativeMethods.PatBlt(new HandleRef(ParentInternal, dc), r.X, r.Y, r.Width, r.Height, NativeMethods.PATINVERT);
Gdi32.SelectObject(dc, saveBrush);
Gdi32.DeleteObject(halftone);
User32.ReleaseDC(new HandleRef(ParentInternal, parentHandle), dc);
User32.ReleaseDC(new HandleRef(ParentInternal, ParentInternal.Handle), dc);
}

/// <summary>
Expand Down

0 comments on commit a76dac3

Please sign in to comment.