Skip to content

Commit

Permalink
Cleanup Gdi32 Pen/Brush interop
Browse files Browse the repository at this point in the history
  • Loading branch information
hughbe committed Sep 19, 2019
1 parent 2a11660 commit aae3710
Show file tree
Hide file tree
Showing 26 changed files with 334 additions and 290 deletions.
24 changes: 24 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.BS.cs
@@ -0,0 +1,24 @@
// 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.

internal static partial class Interop
{
internal static partial class Gdi32
{
public enum BS : uint
{
SOLID = 0,
NULL = 1,
HOLLOW = 1,
HATCHED = 2,
PATTERN = 3,
INDEXED = 4,
DIBPATTERN = 5,
DIBPATTERNPT = 6,
PATTERN8X8 = 7,
DIBPATTERN8X8 = 8,
MONOPATTERN = 9,
}
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.CreateBrushIndirect.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateBrushIndirect(ref LOGBRUSH lb);
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.CreatePatternBrush.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreatePatternBrush(IntPtr hbm);
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.CreatePen.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public static extern IntPtr CreatePen(PS nStyle, int nWidth, int crColor);
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.CreateSolidBrush.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, SetLastError = true)]
public static extern IntPtr CreateSolidBrush(int crColor);
}
}
23 changes: 23 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.Ellipse.cs
@@ -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 Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true, SetLastError = true)]
public static extern BOOL Ellipse(IntPtr hdc, int left, int top, int right, int bottom);

public static BOOL Ellipse(IHandle hdc, int left, int top, int right, int bottom)
{
BOOL result = Ellipse(hdc.Handle, left, top, right, bottom);
GC.KeepAlive(hdc);
return result;
}
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.ExtCreatePen.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class Gdi32
{
[DllImport(Libraries.Gdi32, ExactSpelling = true)]
public unsafe static extern IntPtr ExtCreatePen(PS fnStyle, int dwWidth, ref LOGBRUSH lplb, uint dwStyleCount, uint* lpStyle);
}
}
18 changes: 18 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.LOGBRUSH.cs
@@ -0,0 +1,18 @@
// 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 Gdi32
{
public struct LOGBRUSH
{
public BS lbStyle;
public int lbColor;
public IntPtr lbHatch;
}
}
}
40 changes: 40 additions & 0 deletions src/Common/src/Interop/Gdi32/Interop.PS.cs
@@ -0,0 +1,40 @@
// 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 Gdi32
{
[Flags]
public enum PS : uint
{
SOLID = 0x00000000,
DASH = 0x00000001,
DOT = 0x00000002,
DASHDOT = 0x00000003,
DASHDOTDOT = 0x00000004,
NULL = 0x00000005,
INSIDEFRAME = 0x00000006,
USERSTYLE = 0x00000007,
ALTERNATE = 0x00000008,
STYLE_MASK = 0x0000000f,

ENDCAP_ROUND = 0x00000000,
ENDCAP_SQUARE = 0x00000100,
ENDCAP_FLAT = 0x00000200,
ENDCAP_MASK = 0x00000f00,

JOIN_ROUND = 0x00000000,
JOIN_BEVEL = 0x00001000,
JOIN_MITER = 0x00002000,
JOIN_MASK = 0x0000f000,

COSMETIC = 0x00000000,
GEOMETRIC = 0x00010000,
TYPE_MASK = 0x000f0000,
}
}
}
23 changes: 23 additions & 0 deletions src/Common/src/Interop/User32/Interop.FillRect.cs
@@ -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;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern int FillRect(IntPtr hDC, ref RECT lprc, IntPtr hbr);

public static int FillRect(HandleRef hDC, ref RECT lprc, HandleRef hbr)
{
int result = FillRect(hDC.Handle, ref lprc, hbr.Handle);
GC.KeepAlive(hDC.Wrapper);
GC.KeepAlive(hbr.Wrapper);
return result;
}
}
}
15 changes: 15 additions & 0 deletions src/Common/src/Interop/User32/Interop.GetSysColorBrush.cs
@@ -0,0 +1,15 @@
// 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;

internal static partial class Interop
{
internal static partial class User32
{
[DllImport(Libraries.User32, ExactSpelling = true)]
public static extern IntPtr GetSysColorBrush(int nIndex);
}
}
18 changes: 0 additions & 18 deletions src/Common/src/NativeMethods.cs
Expand Up @@ -64,7 +64,6 @@ public const int

public const int BCM_GETIDEALSIZE = 0x1601,
BI_RGB = 0,
BS_PATTERN = 3,
BDR_RAISEDOUTER = 0x0001,
BDR_SUNKENOUTER = 0x0002,
BDR_RAISEDINNER = 0x0004,
Expand Down Expand Up @@ -959,8 +958,6 @@ public static uint MAKELCID(uint lgid, uint sort)
PD_NOCURRENTPAGE = 0x00800000,
PD_EXCLUSIONFLAGS = 0x01000000,
PD_USELARGETEMPLATE = 0x10000000,
PS_SOLID = 0,
PS_DOT = 2,
PRF_CHECKVISIBLE = 0x00000001,
PRF_NONCLIENT = 0x00000002,
PRF_CLIENT = 0x00000004,
Expand Down Expand Up @@ -2182,14 +2179,6 @@ public class CHOOSECOLOR

public delegate IntPtr HookProc(int nCode, IntPtr wParam, IntPtr lParam);

[StructLayout(LayoutKind.Sequential)]
public struct LOGBRUSH
{
public int lbStyle;
public int lbColor;
public IntPtr lbHatch;
}

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
public unsafe struct LOGFONTW
{
Expand Down Expand Up @@ -3889,15 +3878,8 @@ public enum MONTCALENDAR_VIEW_MODE
public const int STAP_ALLOW_CONTROLS = (1 << 1);
public const int STAP_ALLOW_WEBCONTENT = (1 << 2);

public const int PS_NULL = 5;
public const int PS_INSIDEFRAME = 6;

public const int PS_GEOMETRIC = 0x00010000;
public const int PS_ENDCAP_SQUARE = 0x00000100;

public const int WS_EX_TRANSPARENT = 0x00000020;

public const int NULL_BRUSH = 5;
public const int MM_HIMETRIC = 3;

// Threading stuff
Expand Down
21 changes: 0 additions & 21 deletions src/Common/src/SafeNativeMethods.cs
Expand Up @@ -93,9 +93,6 @@ internal static class SafeNativeMethods
[DllImport(ExternDll.Oleaut32, EntryPoint = "OleCreateFontIndirect", ExactSpelling = true, PreserveSig = false)]
public static extern IFontDisp OleCreateIFontDispIndirect(NativeMethods.FONTDESC fd, ref Guid iid);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr CreateSolidBrush(int crColor);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
public static unsafe extern bool SetWindowExtEx(IntPtr hDC, int x, int y, Size *size);

Expand Down Expand Up @@ -245,18 +242,6 @@ public static IntPtr ImageList_Read(IStream pstm)
[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr /*HBITMAP*/ CreateBitmap(int nWidth, int nHeight, int nPlanes, int nBitsPerPixel, byte[] lpvBits);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr /*HBRUSH*/ CreatePatternBrush(HandleRef hbmp);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr CreateBrushIndirect(ref NativeMethods.LOGBRUSH lb);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr CreatePen(int nStyle, int nWidth, int crColor);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)]
public static extern IntPtr ExtCreatePen(int fnStyle, int dwWidth, ref NativeMethods.LOGBRUSH lplb, int dwStyleCount, int[] lpStyle);

[DllImport(ExternDll.Gdi32, ExactSpelling = true)]
public static unsafe extern bool SetViewportExtEx(IntPtr hDC, int x, int y, Size *size);

Expand All @@ -267,9 +252,6 @@ public static IntPtr ImageList_Read(IStream pstm)
[DllImport(ExternDll.User32, ExactSpelling = true)]
public static extern bool AdjustWindowRectExForDpi(ref RECT lpRect, int dwStyle, bool bMenu, int dwExStyle, uint dpi);

[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern IntPtr GetSysColorBrush(int nIndex);

[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool EnableWindow(HandleRef hWnd, bool enable);

Expand All @@ -285,9 +267,6 @@ public static IntPtr ImageList_Read(IStream pstm)
[DllImport(ExternDll.User32, ExactSpelling = true, CharSet = CharSet.Auto)]
public static extern bool ValidateRect(IntPtr hwnd, IntPtr prect);

[DllImport(ExternDll.User32, ExactSpelling = true)]
public static extern int FillRect(HandleRef hdc, ref RECT rect, HandleRef hbrush);

[DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)]
public static unsafe extern bool SetViewportOrgEx(IntPtr hdc, int x, int y, Point *lppt);

Expand Down
Expand Up @@ -52,11 +52,14 @@
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVIF.cs" Link="Interop\ComCtl32\Interop.TVIF.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVC.cs" Link="Interop\ComCtl32\Interop.TVC.cs" />
<Compile Include="..\..\Common\src\Interop\ComCtl32\Interop.TVIS.cs" Link="Interop\ComCtl32\Interop.TVIS.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreatePen.cs" Link="Interop\Gdi32\Interop.CreatePen.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreateRectRgn.cs" Link="Interop\Gdi32\Interop.CreateRectRgn.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.CreateSolidBrush.cs" Link="Interop\Gdi32\Interop.CreateSolidBrush.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.DeleteObject.cs" Link="Interop\Gdi32\Interop.DeleteObject.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.GetDeviceCaps.cs" Link="Interop\Gdi32\Interop.GetDeviceCaps.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.GetTextExtentPoint32.cs" Link="Interop\Gdi32\Interop.GetTextExtentPoint32.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.GetTextMetricsW.cs" Link="Interop\Gdi32\Interop.GetTextMetricsW.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.PS.cs" Link="Interop\Gdi32\Interop.PS.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.SelectObject.cs" Link="Interop\Gdi32\Interop.SelectObject.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.TEXTMETRICW.cs" Link="Interop\Gdi32\Interop.TEXTMETRICW.cs" />
<Compile Include="..\..\Common\src\Interop\Gdi32\Interop.TMPF.cs" Link="Interop\Gdi32\Interop.TMPF.cs" />
Expand Down
Expand Up @@ -28,10 +28,10 @@ internal static class DesignerUtils
//brush used to draw the resizeable selection borders around controls/components
private static HatchBrush s_selectionBorderBrush = new HatchBrush(HatchStyle.Percent50, SystemColors.ControlDarkDark, Color.Transparent);
//Pens and Brushes used via GDI to render our grabhandles
private static IntPtr s_grabHandleFillBrushPrimary = SafeNativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.Window));
private static IntPtr s_grabHandleFillBrush = SafeNativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.ControlText));
private static IntPtr s_grabHandlePenPrimary = SafeNativeMethods.CreatePen(NativeMethods.PS_SOLID, 1, ColorTranslator.ToWin32(SystemColors.ControlText));
private static IntPtr s_grabHandlePen = SafeNativeMethods.CreatePen(NativeMethods.PS_SOLID, 1, ColorTranslator.ToWin32(SystemColors.Window));
private static IntPtr s_grabHandleFillBrushPrimary = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.Window));
private static IntPtr s_grabHandleFillBrush = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.ControlText));
private static IntPtr s_grabHandlePenPrimary = Gdi32.CreatePen(Gdi32.PS.SOLID, 1, ColorTranslator.ToWin32(SystemColors.ControlText));
private static IntPtr s_grabHandlePen = Gdi32.CreatePen(Gdi32.PS.SOLID, 1, ColorTranslator.ToWin32(SystemColors.Window));

//The box-like image used as the user is dragging comps from the toolbox
private static Bitmap s_boxImage = null;
Expand Down Expand Up @@ -203,16 +203,16 @@ public static void SyncBrushes()
s_selectionBorderBrush = new HatchBrush(HatchStyle.Percent50, SystemColors.ControlDarkDark, Color.Transparent);

Gdi32.DeleteObject(s_grabHandleFillBrushPrimary);
s_grabHandleFillBrushPrimary = SafeNativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.Window));
s_grabHandleFillBrushPrimary = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.Window));

Gdi32.DeleteObject(s_grabHandleFillBrush);
s_grabHandleFillBrush = SafeNativeMethods.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.ControlText));
s_grabHandleFillBrush = Gdi32.CreateSolidBrush(ColorTranslator.ToWin32(SystemColors.ControlText));

Gdi32.DeleteObject(s_grabHandlePenPrimary);
s_grabHandlePenPrimary = SafeNativeMethods.CreatePen(NativeMethods.PS_SOLID, 1, ColorTranslator.ToWin32(SystemColors.ControlText));
s_grabHandlePenPrimary = Gdi32.CreatePen(Gdi32.PS.SOLID, 1, ColorTranslator.ToWin32(SystemColors.ControlText));

Gdi32.DeleteObject(s_grabHandlePen);
s_grabHandlePen = SafeNativeMethods.CreatePen(NativeMethods.PS_SOLID, 1, ColorTranslator.ToWin32(SystemColors.Window));
s_grabHandlePen = Gdi32.CreatePen(Gdi32.PS.SOLID, 1, ColorTranslator.ToWin32(SystemColors.Window));
}

/// <summary>
Expand Down
Expand Up @@ -95,13 +95,14 @@ private void PaintThemedButtonBackground(PaintEventArgs e, Rectangle bounds, boo
// to use it without enough bookkeeping to negate any performance gain of using GDI.
if (color.A == 255 && e.HDC != IntPtr.Zero)
{

if (DisplayInformation.BitsPerPixel > 8)
{
RECT r = new RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
var r = new RECT(bounds.X, bounds.Y, bounds.Right, bounds.Bottom);
// SysColorBrush does not have to be deleted.
SafeNativeMethods.FillRect(new HandleRef(e, e.HDC), ref r, new HandleRef(this,
isHighContrastHighlighted ? SafeNativeMethods.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF) : Control.BackColorBrush));
User32.FillRect(
new HandleRef(e, e.HDC),
ref r,
new HandleRef(this, isHighContrastHighlighted ? User32.GetSysColorBrush(ColorTranslator.ToOle(color) & 0xFF) : Control.BackColorBrush));
painted = true;
}
}
Expand Down

0 comments on commit aae3710

Please sign in to comment.