From 851adba131f5dfa2539155ee12c2c82825c01856 Mon Sep 17 00:00:00 2001 From: Hugh Bellamy Date: Fri, 1 Nov 2019 23:43:13 +0000 Subject: [PATCH] Cleanup GDI interop in System.Drawing.Common (dotnet/corefx#39727) * Cleanup GDI object Interop * Cleanup GDI region interop * Cleanup GDI font interop * Cleanup GDI DC interop * Address PR feedback Commit migrated from https://github.com/dotnet/corefx/commit/f807df691a3007cea9c7aa169f18be4cd9540758 --- .../Interop/Windows/Gdi32/Interop.CombineRgn.cs | 32 ++++++ .../Windows/Gdi32/Interop.CreateCompatibleDC.cs | 15 +++ .../src/Interop/Windows/Gdi32/Interop.CreateDC.cs | 15 +++ .../Windows/Gdi32/Interop.CreateFontIndirect.cs | 15 +++ .../src/Interop/Windows/Gdi32/Interop.CreateIC.cs | 15 +++ .../Interop/Windows/Gdi32/Interop.CreateRectRgn.cs | 15 +++ .../src/Interop/Windows/Gdi32/Interop.DeleteDC.cs | 22 ++++ .../Interop/Windows/Gdi32/Interop.DeleteObject.cs | 22 ++++ .../Interop/Windows/Gdi32/Interop.GetClipRgn.cs | 31 ++++++ .../Windows/Gdi32/Interop.GetCurrentObject.cs | 22 ++++ .../Interop/Windows/Gdi32/Interop.GetDeviceCaps.cs | 44 ++++++++ .../Interop/Windows/Gdi32/Interop.GetObjectType.cs | 15 +++ .../src/Interop/Windows/Gdi32/Interop.GetRgnBox.cs | 22 ++++ .../Windows/Gdi32/Interop.GetStockObject.cs | 20 ++++ .../Interop/Windows/Gdi32/Interop.ObjectType.cs | 27 +++++ .../Windows/Gdi32/Interop.OffsetViewportOrgEx.cs | 23 ++++ .../src/Interop/Windows/Gdi32/Interop.RECT.cs | 23 ++++ .../Interop/Windows/Gdi32/Interop.RegionType.cs} | 15 ++- .../src/Interop/Windows/Gdi32/Interop.RestoreDC.cs | 22 ++++ .../src/Interop/Windows/Gdi32/Interop.SaveDC.cs | 22 ++++ .../Interop/Windows/Gdi32/Interop.SelectClipRgn.cs | 30 ++++++ .../src/Interop/Windows/User32/Interop.GetDC.cs | 22 ++++ .../src/Interop/Windows/User32/Interop.LOGFONT.cs | 56 ++++++++++ .../Windows/User32/Interop.NONCLIENTMETRICS.cs | 33 ++++++ .../Interop/Windows/User32/Interop.ReleaseDC.cs | 30 ++++++ .../Windows/User32/Interop.SystemParametersInfo.cs | 21 ++++ .../Interop/Windows/User32/Interop.WindowFromDC.cs | 22 ++++ .../src/PinvokeAnalyzerExceptionList.analyzerdata | 18 +--- .../src/System.Drawing.Common.csproj | 87 ++++++++++++++- .../Drawing/BufferedGraphicsContext.Windows.cs | 19 ++-- .../src/System/Drawing/Font.Unix.cs | 4 +- .../src/System/Drawing/Font.Windows.cs | 16 +-- .../src/System/Drawing/Font.cs | 8 +- .../src/System/Drawing/Gdiplus.cs | 119 +-------------------- .../src/System/Drawing/GdiplusNative.Unix.cs | 4 +- .../src/System/Drawing/GdiplusNative.Windows.cs | 2 +- .../src/System/Drawing/GdiplusNative.cs | 2 +- .../src/System/Drawing/Graphics.Windows.cs | 2 +- .../src/System/Drawing/Icon.Windows.cs | 40 +++++-- .../src/System/Drawing/NativeMethods.cs | 41 +------ .../src/System/Drawing/NativeStructs.Unix.cs | 20 ---- .../Drawing/Printing/DefaultPrintController.cs | 11 +- .../Drawing/Printing/PageSettings.Windows.cs | 28 ++--- .../Printing/PreviewPrintController.Windows.cs | 8 +- .../Drawing/Printing/PrintPreviewGraphics.cs | 8 +- .../Drawing/Printing/PrinterSettings.Windows.cs | 27 ++--- .../src/System/Drawing/Region.Windows.cs | 3 +- .../src/System/Drawing/ScreenDC.cs | 14 +-- .../src/System/Drawing/SystemFonts.Windows.cs | 24 ++--- .../src/System/Drawing/UnsafeNativeMethods.cs | 24 ----- .../System.Drawing.Common/src/misc/DpiHelper.cs | 8 +- .../src/misc/GDI/DeviceContext.cs | 48 ++++----- .../src/misc/GDI/NativeMethods.cs | 63 ----------- .../src/misc/GDI/SafeNativeMethods.cs | 18 ---- .../src/misc/GDI/UnsafeNativeMethods.cs | 116 -------------------- .../src/misc/GDI/WindowsRegion.cs | 12 +-- 56 files changed, 875 insertions(+), 570 deletions(-) create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CombineRgn.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateCompatibleDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateFontIndirect.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateIC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateRectRgn.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteObject.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetClipRgn.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetCurrentObject.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetDeviceCaps.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetObjectType.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetRgnBox.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetStockObject.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.ObjectType.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.OffsetViewportOrgEx.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RECT.cs rename src/libraries/{System.Drawing.Common/src/misc/GDI/WindowsRegionCombineMode.cs => Common/src/Interop/Windows/Gdi32/Interop.RegionType.cs} (53%) create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RestoreDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SaveDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SelectClipRgn.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.GetDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.LOGFONT.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.NONCLIENTMETRICS.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.ReleaseDC.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.SystemParametersInfo.cs create mode 100644 src/libraries/Common/src/Interop/Windows/User32/Interop.WindowFromDC.cs delete mode 100644 src/libraries/System.Drawing.Common/src/misc/GDI/NativeMethods.cs delete mode 100644 src/libraries/System.Drawing.Common/src/misc/GDI/SafeNativeMethods.cs delete mode 100644 src/libraries/System.Drawing.Common/src/misc/GDI/UnsafeNativeMethods.cs diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CombineRgn.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CombineRgn.cs new file mode 100644 index 0000000..5b01456 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CombineRgn.cs @@ -0,0 +1,32 @@ +// 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.Diagnostics; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + public enum CombineMode : int + { + RGN_AND = 1, + RGN_XOR = 3, + RGN_DIFF = 4, + } + + [DllImport(Libraries.Gdi32, SetLastError = true, ExactSpelling = true)] + public static extern RegionType CombineRgn(IntPtr hrgnDst, IntPtr hrgnSrc1, IntPtr hrgnSrc2, CombineMode iMode); + + public static RegionType CombineRgn(HandleRef hrgnDst, HandleRef hrgnSrc1, HandleRef hrgnSrc2, CombineMode iMode) + { + RegionType result = CombineRgn(hrgnDst.Handle, hrgnSrc1.Handle, hrgnSrc2.Handle, iMode); + GC.KeepAlive(hrgnDst.Wrapper); + GC.KeepAlive(hrgnSrc1.Wrapper); + GC.KeepAlive(hrgnSrc2.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateCompatibleDC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateCompatibleDC.cs new file mode 100644 index 0000000..424471d --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateCompatibleDC.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 CreateCompatibleDC(IntPtr hdc); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateDC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateDC.cs new file mode 100644 index 0000000..209f436 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateDC.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, CharSet = CharSet.Unicode)] + public static extern IntPtr CreateDCW(string pwszDriver, string pwszDevice, string pszPort, IntPtr pdm); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateFontIndirect.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateFontIndirect.cs new file mode 100644 index 0000000..03890f8 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateFontIndirect.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, CharSet = CharSet.Unicode)] + public static extern IntPtr CreateFontIndirectW(ref User32.LOGFONT lplf); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateIC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateIC.cs new file mode 100644 index 0000000..f4176dd --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateIC.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, CharSet = CharSet.Unicode)] + public static extern IntPtr CreateICW(string pszDriver, string pszDevice, string pszPort, IntPtr pdm); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateRectRgn.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateRectRgn.cs new file mode 100644 index 0000000..f318043 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.CreateRectRgn.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 CreateRectRgn(int x1, int y1, int x2, int y2); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteDC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteDC.cs new file mode 100644 index 0000000..89adffc --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteDC.cs @@ -0,0 +1,22 @@ +// 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 bool DeleteDC(IntPtr hdc); + + public static bool DeleteDC(HandleRef hdc) + { + bool result = DeleteDC(hdc.Handle); + GC.KeepAlive(hdc.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteObject.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteObject.cs new file mode 100644 index 0000000..0b36993 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.DeleteObject.cs @@ -0,0 +1,22 @@ +// 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 bool DeleteObject(IntPtr ho); + + public static bool DeleteObject(HandleRef ho) + { + bool result = DeleteObject(ho.Handle); + GC.KeepAlive(ho.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetClipRgn.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetClipRgn.cs new file mode 100644 index 0000000..7923411 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetClipRgn.cs @@ -0,0 +1,31 @@ +// 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.Diagnostics; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + [DllImport(Libraries.Gdi32, ExactSpelling = true)] + public static extern int GetClipRgn(IntPtr hdc, IntPtr hrgn); + + public static int GetClipRgn(HandleRef hdc, IntPtr hrgn) + { + int result = GetClipRgn(hdc.Handle, hrgn); + GC.KeepAlive(hdc.Wrapper); + return result; + } + + public static int GetClipRgn(HandleRef hdc, HandleRef hrgn) + { + int result = GetClipRgn(hdc.Handle, hrgn.Handle); + GC.KeepAlive(hdc.Wrapper); + GC.KeepAlive(hrgn.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetCurrentObject.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetCurrentObject.cs new file mode 100644 index 0000000..025c836 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetCurrentObject.cs @@ -0,0 +1,22 @@ +// 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 GetCurrentObject(IntPtr hdc, ObjectType type); + + public static IntPtr GetCurrentObject(HandleRef hdc, ObjectType type) + { + IntPtr result = GetCurrentObject(hdc.Handle, type); + GC.KeepAlive(hdc.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetDeviceCaps.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetDeviceCaps.cs new file mode 100644 index 0000000..5184735 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetDeviceCaps.cs @@ -0,0 +1,44 @@ +// 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.Diagnostics; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + public enum DeviceCapability : int + { + TECHNOLOGY = 2, + VERTRES = 10, + HORZRES = 8, + BITSPIXEL = 12, + PLANES = 14, + LOGPIXELSX = 88, + LOGPIXELSY = 90, + PHYSICALWIDTH = 110, + PHYSICALHEIGHT = 111, + PHYSICALOFFSETX = 112, + PHYSICALOFFSETY = 113 + } + + public static class DeviceTechnology + { + public const int DT_PLOTTER = 0; + public const int DT_RASPRINTER = 2; + } + + [DllImport(Libraries.Gdi32, ExactSpelling = true)] + public static extern int GetDeviceCaps(IntPtr hdc, DeviceCapability index); + + public static int GetDeviceCaps(HandleRef hdc, DeviceCapability index) + { + int caps = GetDeviceCaps(hdc.Handle, index); + GC.KeepAlive(hdc.Wrapper); + return caps; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetObjectType.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetObjectType.cs new file mode 100644 index 0000000..6511173 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetObjectType.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 ObjectType GetObjectType(IntPtr h); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetRgnBox.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetRgnBox.cs new file mode 100644 index 0000000..aef9f59 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetRgnBox.cs @@ -0,0 +1,22 @@ +// 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 RegionType GetRgnBox(IntPtr hrgn, ref RECT lprc); + + public static RegionType GetRgnBox(HandleRef hrgn, ref RECT lprc) + { + RegionType result = GetRgnBox(hrgn.Handle, ref lprc); + GC.KeepAlive(hrgn.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetStockObject.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetStockObject.cs new file mode 100644 index 0000000..50b0c52 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.GetStockObject.cs @@ -0,0 +1,20 @@ +// 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 + { + public enum StockObject : int + { + DEFAULT_GUI_FONT = 17 + } + + [DllImport(Libraries.Gdi32, ExactSpelling = true)] + public static extern IntPtr GetStockObject(StockObject i); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.ObjectType.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.ObjectType.cs new file mode 100644 index 0000000..e890ba7 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.ObjectType.cs @@ -0,0 +1,27 @@ +// 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 ObjectType : int + { + OBJ_PEN = 1, + OBJ_BRUSH = 2, + OBJ_DC = 3, + OBJ_METADC = 4, + OBJ_PAL = 5, + OBJ_FONT = 6, + OBJ_BITMAP = 7, + OBJ_REGION = 8, + OBJ_METAFILE = 9, + OBJ_MEMDC = 10, + OBJ_EXTPEN = 11, + OBJ_ENHMETADC = 12, + OBJ_ENHMETAFILE = 13, + OBJ_COLORSPACE = 14 + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.OffsetViewportOrgEx.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.OffsetViewportOrgEx.cs new file mode 100644 index 0000000..57f304b --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.OffsetViewportOrgEx.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.Drawing; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + [DllImport(Libraries.Gdi32, ExactSpelling = true)] + public static extern bool OffsetViewportOrgEx(IntPtr hdc, int x, int y, ref Point lppt); + + public static bool OffsetViewportOrgEx(HandleRef hdc, int x, int y, ref Point lppt) + { + bool result = OffsetViewportOrgEx(hdc.Handle, x, y, ref lppt); + GC.KeepAlive(hdc.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RECT.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RECT.cs new file mode 100644 index 0000000..058d264 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RECT.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.Drawing; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class Gdi32 + { + [StructLayout(LayoutKind.Sequential)] + public struct RECT + { + public int left; + public int top; + public int right; + public int bottom; + + public Size Size => new Size(right - left, bottom - top); + } + } +} diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegionCombineMode.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RegionType.cs similarity index 53% rename from src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegionCombineMode.cs rename to src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RegionType.cs index 2bac763..eff32ac 100644 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegionCombineMode.cs +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RegionType.cs @@ -2,16 +2,13 @@ // The .NET Foundation licenses this file to you under the MIT license. // See the LICENSE file in the project root for more information. -namespace System.Drawing.Internal +internal static partial class Interop { - internal enum RegionCombineMode + internal enum RegionType : int { - AND = 1, - OR = 2, - XOR = 3, - DIFF = 4, - COPY = 5, - MIN = AND, - MAX = COPY + ERROR = 0, + NULLREGION = 1, + SIMPLEREGION = 2, + COMPLEXREGION = 3, } } diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RestoreDC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RestoreDC.cs new file mode 100644 index 0000000..6461e85 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.RestoreDC.cs @@ -0,0 +1,22 @@ +// 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 bool RestoreDC(IntPtr hdc, int nSavedDC); + + public static bool RestoreDC(HandleRef hdc, int nSavedDC) + { + bool result = RestoreDC(hdc.Handle, nSavedDC); + GC.KeepAlive(hdc.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SaveDC.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SaveDC.cs new file mode 100644 index 0000000..ca27682 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SaveDC.cs @@ -0,0 +1,22 @@ +// 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 int SaveDC(IntPtr hdc); + + public static int SaveDC(HandleRef hdc) + { + int result = SaveDC(hdc.Handle); + GC.KeepAlive(hdc.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SelectClipRgn.cs b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SelectClipRgn.cs new file mode 100644 index 0000000..280514f --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/Gdi32/Interop.SelectClipRgn.cs @@ -0,0 +1,30 @@ +// 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, SetLastError = true, ExactSpelling = true)] + public static extern RegionType SelectClipRgn(IntPtr hdc, IntPtr hrgn); + + public static RegionType SelectClipRgn(HandleRef hdc, IntPtr hrgn) + { + RegionType result = SelectClipRgn(hdc.Handle, hrgn); + GC.KeepAlive(hdc.Wrapper); + return result; + } + + public static RegionType SelectClipRgn(HandleRef hdc, HandleRef hrgn) + { + RegionType result = SelectClipRgn(hdc.Handle, hrgn.Handle); + GC.KeepAlive(hdc.Wrapper); + GC.KeepAlive(hrgn.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.GetDC.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.GetDC.cs new file mode 100644 index 0000000..19c63e4 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.GetDC.cs @@ -0,0 +1,22 @@ +// 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 GetDC(IntPtr hWnd); + + public static IntPtr GetDC(HandleRef hWnd) + { + IntPtr dc = GetDC(hWnd.Handle); + GC.KeepAlive(hWnd.Wrapper); + return dc; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.LOGFONT.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.LOGFONT.cs new file mode 100644 index 0000000..2b26ce6 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.LOGFONT.cs @@ -0,0 +1,56 @@ +// 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 + { + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public unsafe struct LOGFONT + { + private const int LF_FACESIZE = 32; + + public int lfHeight; + public int lfWidth; + public int lfEscapement; + public int lfOrientation; + public int lfWeight; + public byte lfItalic; + public byte lfUnderline; + public byte lfStrikeOut; + public byte lfCharSet; + public byte lfOutPrecision; + public byte lfClipPrecision; + public byte lfQuality; + public byte lfPitchAndFamily; + private fixed char _lfFaceName[LF_FACESIZE]; + public Span lfFaceName + { + get { fixed (char* c = _lfFaceName) { return new Span(c, LF_FACESIZE); } } + } + + public override string ToString() + { + return + "lfHeight=" + lfHeight + ", " + + "lfWidth=" + lfWidth + ", " + + "lfEscapement=" + lfEscapement + ", " + + "lfOrientation=" + lfOrientation + ", " + + "lfWeight=" + lfWeight + ", " + + "lfItalic=" + lfItalic + ", " + + "lfUnderline=" + lfUnderline + ", " + + "lfStrikeOut=" + lfStrikeOut + ", " + + "lfCharSet=" + lfCharSet + ", " + + "lfOutPrecision=" + lfOutPrecision + ", " + + "lfClipPrecision=" + lfClipPrecision + ", " + + "lfQuality=" + lfQuality + ", " + + "lfPitchAndFamily=" + lfPitchAndFamily + ", " + + "lfFaceName=" + lfFaceName.ToString(); + } + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.NONCLIENTMETRICS.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.NONCLIENTMETRICS.cs new file mode 100644 index 0000000..88872ca --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.NONCLIENTMETRICS.cs @@ -0,0 +1,33 @@ +// 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.Drawing; +using System.Runtime.InteropServices; + +internal static partial class Interop +{ + internal static partial class User32 + { + [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] + public struct NONCLIENTMETRICS + { + public uint cbSize; + public int iBorderWidth; + public int iScrollWidth; + public int iScrollHeight; + public int iCaptionWidth; + public int iCaptionHeight; + public LOGFONT lfCaptionFont; + public int iSmCaptionWidth; + public int iSmCaptionHeight; + public LOGFONT lfSmCaptionFont; + public int iMenuWidth; + public int iMenuHeight; + public LOGFONT lfMenuFont; + public LOGFONT lfStatusFont; + public LOGFONT lfMessageFont; + public int iPaddedBorderWidth; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.ReleaseDC.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.ReleaseDC.cs new file mode 100644 index 0000000..383c09e --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.ReleaseDC.cs @@ -0,0 +1,30 @@ +// 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 ReleaseDC(IntPtr hWnd, IntPtr hDC); + + public static int ReleaseDC(HandleRef hWnd, IntPtr hDC) + { + int result = ReleaseDC(hWnd.Handle, hDC); + GC.KeepAlive(hWnd.Wrapper); + return result; + } + + public static int ReleaseDC(HandleRef hWnd, HandleRef hDC) + { + int result = ReleaseDC(hWnd.Handle, hDC.Handle); + GC.KeepAlive(hWnd.Wrapper); + GC.KeepAlive(hDC.Wrapper); + return result; + } + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.SystemParametersInfo.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.SystemParametersInfo.cs new file mode 100644 index 0000000..510d106 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.SystemParametersInfo.cs @@ -0,0 +1,21 @@ +// 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 partial class Interop +{ + internal partial class User32 + { + public enum SystemParametersAction : uint + { + SPI_GETICONTITLELOGFONT = 0x1F, + SPI_GETNONCLIENTMETRICS = 0x29 + } + + [DllImport(Libraries.User32, ExactSpelling = true, CharSet = CharSet.Unicode)] + public static extern unsafe bool SystemParametersInfoW(SystemParametersAction uiAction, uint uiParam, void* pvParam, uint fWinIni); + } +} diff --git a/src/libraries/Common/src/Interop/Windows/User32/Interop.WindowFromDC.cs b/src/libraries/Common/src/Interop/Windows/User32/Interop.WindowFromDC.cs new file mode 100644 index 0000000..0a68a03 --- /dev/null +++ b/src/libraries/Common/src/Interop/Windows/User32/Interop.WindowFromDC.cs @@ -0,0 +1,22 @@ +// 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 WindowFromDC(IntPtr hDC); + + public static IntPtr WindowFromDC(HandleRef hDC) + { + IntPtr result = WindowFromDC(hDC.Handle); + GC.KeepAlive(hDC.Wrapper); + return result; + } + } +} diff --git a/src/libraries/System.Drawing.Common/src/PinvokeAnalyzerExceptionList.analyzerdata b/src/libraries/System.Drawing.Common/src/PinvokeAnalyzerExceptionList.analyzerdata index 4147d12..dc4eb46 100644 --- a/src/libraries/System.Drawing.Common/src/PinvokeAnalyzerExceptionList.analyzerdata +++ b/src/libraries/System.Drawing.Common/src/PinvokeAnalyzerExceptionList.analyzerdata @@ -6,28 +6,22 @@ gdi32.dll!BitBlt gdi32.dll!CombineRgn gdi32.dll!CreateCompatibleBitmap gdi32.dll!CreateCompatibleDC -gdi32.dll!CreateCompatibleDC -gdi32.dll!CreateDC +gdi32.dll!CreateDCW gdi32.dll!CreateDIBSection -gdi32.dll!CreateFontIndirect -gdi32.dll!CreateIC -gdi32.dll!CreateRectRgn +gdi32.dll!CreateFontIndirectW +gdi32.dll!CreateICW gdi32.dll!CreateRectRgn gdi32.dll!DeleteDC -gdi32.dll!DeleteDC -gdi32.dll!DeleteObject gdi32.dll!DeleteObject gdi32.dll!EndDoc gdi32.dll!EndPage gdi32.dll!ExtEscape gdi32.dll!ExtEscape gdi32.dll!GetClipRgn -gdi32.dll!GetClipRgn gdi32.dll!GetCurrentObject gdi32.dll!GetDeviceCaps gdi32.dll!GetDIBits gdi32.dll!GetObject -gdi32.dll!GetObject gdi32.dll!GetObjectType gdi32.dll!GetPaletteEntries gdi32.dll!GetRgnBox @@ -38,7 +32,6 @@ gdi32.dll!ResetDC gdi32.dll!RestoreDC gdi32.dll!SaveDC gdi32.dll!SelectClipRgn -gdi32.dll!SelectClipRgn gdi32.dll!SelectObject gdi32.dll!StartDoc gdi32.dll!StartPage @@ -55,11 +48,8 @@ user32.dll!GetSysColor user32.dll!GetSystemMetrics user32.dll!LoadIcon user32.dll!ReleaseDC -user32.dll!ReleaseDC -user32.dll!SystemParametersInfo -user32.dll!SystemParametersInfo +user32.dll!SystemParametersInfoW user32.dll!WindowFromDC winspool.drv!DeviceCapabilities winspool.drv!DocumentProperties -winspool.drv!DocumentProperties winspool.drv!EnumPrinters diff --git a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj index 405bf05..dd66c80 100644 --- a/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj +++ b/src/libraries/System.Drawing.Common/src/System.Drawing.Common.csproj @@ -112,7 +112,6 @@ - @@ -150,6 +149,9 @@ + + Common\Interop\Windows\User32\Interop.LOGFONT.cs + Common\Interop\Windows\Gdi32\Interop.RasterOp.cs @@ -203,6 +205,7 @@ + @@ -214,18 +217,77 @@ - - - - Common\CoreLib\System\LocalAppContextSwitches.Common.cs Common\Interop\Windows\Interop.Libraries.cs + + Common\Interop\Windows\Gdi32\Interop.CombineRgn.cs + + + Common\Interop\Windows\Gdi32\Interop.CreateCompatibleDC.cs + + + Common\Interop\Windows\Gdi32\Interop.CreateDC.cs + + + Common\Interop\Windows\Gdi32\Interop.CreateIC.cs + + + Common\Interop\Windows\Gdi32\Interop.CreateFontIndirect.cs + + + Common\Interop\Windows\Gdi32\Interop.CreateRectRgn.cs + + + Common\Interop\Windows\Gdi32\Interop.DeleteDC.cs + + + Common\Interop\Windows\Gdi32\Interop.DeleteObject.cs + + + Common\Interop\Windows\Gdi32\Interop.GetClipRgn.cs + + + Common\Interop\Windows\Gdi32\Interop.GetCurrentObject.cs + + + Common\Interop\Windows\Gdi32\Interop.GetDeviceCaps.cs + + + Common\Interop\Windows\Gdi32\Interop.GetObjectType.cs + + + Common\Interop\Windows\Gdi32\Interop.GetRgnBox.cs + + + Common\Interop\Windows\Gdi32\Interop.GetStockObject.cs + + + Common\Interop\Windows\Gdi32\Interop.ObjectType.cs + + + Common\Interop\Windows\Gdi32\Interop.OffsetViewportOrgEx.cs + + + Common\Interop\Windows\Gdi32\Interop.RECT.cs + + + Common\Interop\Windows\Gdi32\Interop.RegionType.cs + + + Common\Interop\Windows\Gdi32\Interop.RestoreDC.cs + + + Common\Interop\Windows\Gdi32\Interop.SaveDC.cs + + + Common\Interop\Windows\Gdi32\Interop.SelectClipRgn.cs + Common\Interop\Windows\Kernel32\Interop.FreeLibrary.cs @@ -262,6 +324,21 @@ Common\Interop\Windows\Ole32\Interop.STGM.cs + + Common\Interop\Windows\User32\Interop.GetDC.cs + + + Common\Interop\Windows\User32\Interop.ReleaseDC.cs + + + Common\Interop\Windows\User32\Interop.SystemParametersInfo.cs + + + Common\Interop\Windows\User32\Interop.NONCLIENTMETRICS.cs + + + Common\Interop\Windows\User32\Interop.WindowFromDC.cs + Common\Interop\Windows\Interop.HRESULT.cs diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs index 4c5dbf1..106865f 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/BufferedGraphicsContext.Windows.cs @@ -140,7 +140,7 @@ namespace System.Drawing { if (hbm != IntPtr.Zero) { - SafeNativeMethods.DeleteObject(new HandleRef(null, hbm)); + Interop.Gdi32.DeleteObject(hbm); hbm = IntPtr.Zero; } } @@ -209,7 +209,7 @@ namespace System.Drawing _busy = BufferBusyDisposing; DisposeDC(); _busy = BufferBusyPainting; - _compatDC = UnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, src)); + _compatDC = Interop.Gdi32.CreateCompatibleDC(src); // Recreate the bitmap if necessary. if (width > _bufferSize.Width || height > _bufferSize.Height) @@ -262,14 +262,13 @@ namespace System.Drawing var pbmi = new NativeMethods.BITMAPINFO_FLAT(); // Validate hdc. - int objType = UnsafeNativeMethods.GetObjectType(new HandleRef(null, hdc)); - + Interop.Gdi32.ObjectType objType = Interop.Gdi32.GetObjectType(hdc); switch (objType) { - case NativeMethods.OBJ_DC: - case NativeMethods.OBJ_METADC: - case NativeMethods.OBJ_MEMDC: - case NativeMethods.OBJ_ENHMETADC: + case Interop.Gdi32.ObjectType.OBJ_DC: + case Interop.Gdi32.ObjectType.OBJ_METADC: + case Interop.Gdi32.ObjectType.OBJ_MEMDC: + case Interop.Gdi32.ObjectType.OBJ_ENHMETADC: break; default: throw new ArgumentException(SR.DCTypeInvalid); @@ -333,7 +332,7 @@ namespace System.Drawing if (_compatDC != IntPtr.Zero) { - UnsafeNativeMethods.DeleteDC(new HandleRef(this, _compatDC)); + Interop.Gdi32.DeleteDC(new HandleRef(this, _compatDC)); _compatDC = IntPtr.Zero; } } @@ -347,7 +346,7 @@ namespace System.Drawing { Debug.Assert(_oldBitmap == IntPtr.Zero); - SafeNativeMethods.DeleteObject(new HandleRef(this, _dib)); + Interop.Gdi32.DeleteObject(new HandleRef(this, _dib)); _dib = IntPtr.Zero; } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Unix.cs index a29f99d..000c1bc 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Unix.cs @@ -162,7 +162,7 @@ namespace System.Drawing IntPtr newObject; FontStyle newStyle = FontStyle.Regular; float newSize; - SafeNativeMethods.LOGFONT lf = new SafeNativeMethods.LOGFONT(); + Interop.User32.LOGFONT lf = new Interop.User32.LOGFONT(); // Sanity. Should we throw an exception? if (hfont == IntPtr.Zero) @@ -322,7 +322,7 @@ namespace System.Drawing public static Font FromLogFont(object lf, IntPtr hdc) { IntPtr newObject; - SafeNativeMethods.LOGFONT o = (SafeNativeMethods.LOGFONT)lf; + Interop.User32.LOGFONT o = (Interop.User32.LOGFONT)lf; int status = Gdip.GdipCreateFontFromLogfont(hdc, ref o, out newObject); Gdip.CheckStatus(status); return new Font(newObject, "Microsoft Sans Serif", FontStyle.Regular, 10); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs index d52f7fd..1d25e3c 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.Windows.cs @@ -226,7 +226,7 @@ namespace System.Drawing /// public static Font FromHfont(IntPtr hfont) { - var logFont = new SafeNativeMethods.LOGFONT(); + var logFont = new Interop.User32.LOGFONT(); SafeNativeMethods.GetObject(new HandleRef(null, hfont), ref logFont); using (ScreenDC dc = ScreenDC.Create()) @@ -248,7 +248,7 @@ namespace System.Drawing } } - internal static Font FromLogFont(ref SafeNativeMethods.LOGFONT logFont) + internal static Font FromLogFont(ref Interop.User32.LOGFONT logFont) { using (ScreenDC dc = ScreenDC.Create()) { @@ -256,7 +256,7 @@ namespace System.Drawing } } - internal static Font FromLogFontInternal(ref SafeNativeMethods.LOGFONT logFont, IntPtr hdc) + internal static Font FromLogFontInternal(ref Interop.User32.LOGFONT logFont, IntPtr hdc) { int status = Gdip.GdipCreateFontFromLogfontW(hdc, ref logFont, out IntPtr font); @@ -293,14 +293,14 @@ namespace System.Drawing throw new ArgumentNullException(nameof(lf)); } - if (lf is SafeNativeMethods.LOGFONT logFont) + if (lf is Interop.User32.LOGFONT logFont) { // A boxed LOGFONT, just use it to create the font return FromLogFontInternal(ref logFont, hdc); } Type type = lf.GetType(); - int nativeSize = sizeof(SafeNativeMethods.LOGFONT); + int nativeSize = sizeof(Interop.User32.LOGFONT); if (Marshal.SizeOf(type) != nativeSize) { // If we don't actually have an object that is LOGFONT in size, trying to pass @@ -309,7 +309,7 @@ namespace System.Drawing } // Now that we know the marshalled size is the same as LOGFONT, copy in the data - logFont = new SafeNativeMethods.LOGFONT(); + logFont = new Interop.User32.LOGFONT(); Marshal.StructureToPtr(lf, new IntPtr(&logFont), fDeleteOld: false); @@ -388,8 +388,8 @@ namespace System.Drawing using (ScreenDC dc = ScreenDC.Create()) using (Graphics graphics = Graphics.FromHdcInternal(dc)) { - SafeNativeMethods.LOGFONT lf = ToLogFontInternal(graphics); - IntPtr handle = IntUnsafeNativeMethods.CreateFontIndirect(ref lf); + Interop.User32.LOGFONT lf = ToLogFontInternal(graphics); + IntPtr handle = Interop.Gdi32.CreateFontIndirectW(ref lf); if (handle == IntPtr.Zero) { throw new Win32Exception(); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs index e22e8b2..ccf9442 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Font.cs @@ -280,7 +280,7 @@ namespace System.Drawing } Type type = logFont.GetType(); - int nativeSize = sizeof(SafeNativeMethods.LOGFONT); + int nativeSize = sizeof(Interop.User32.LOGFONT); if (Marshal.SizeOf(type) != nativeSize) { // If we don't actually have an object that is LOGFONT in size, trying to pass @@ -288,7 +288,7 @@ namespace System.Drawing throw new ArgumentException(); } - SafeNativeMethods.LOGFONT nativeLogFont = ToLogFontInternal(graphics); + Interop.User32.LOGFONT nativeLogFont = ToLogFontInternal(graphics); // PtrToStructure requires that the passed in object not be a value type. if (!type.IsValueType) @@ -303,14 +303,14 @@ namespace System.Drawing } } - private unsafe SafeNativeMethods.LOGFONT ToLogFontInternal(Graphics graphics) + private unsafe Interop.User32.LOGFONT ToLogFontInternal(Graphics graphics) { if (graphics == null) { throw new ArgumentNullException(nameof(graphics)); } - SafeNativeMethods.LOGFONT logFont = new SafeNativeMethods.LOGFONT(); + Interop.User32.LOGFONT logFont = new Interop.User32.LOGFONT(); Gdip.CheckStatus(Gdip.GdipGetLogFontW( new HandleRef(this, NativeFont), new HandleRef(graphics, graphics.NativeGraphics), ref logFont)); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs index 61b6cd6..1cac0ec 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Gdiplus.cs @@ -169,9 +169,6 @@ namespace System.Drawing GMEM_ZEROINIT = 0x0040, DM_IN_BUFFER = 8, DM_OUT_BUFFER = 2, - DT_PLOTTER = 0, - DT_RASPRINTER = 2, - TECHNOLOGY = 2, DC_PAPERS = 2, DC_PAPERSIZE = 3, DC_BINS = 6, @@ -197,16 +194,6 @@ namespace System.Drawing IDI_WARNING = 32515, IDI_ERROR = 32513, IDI_INFORMATION = 32516, - PLANES = 14, - BITSPIXEL = 12, - LOGPIXELSX = 88, - LOGPIXELSY = 90, - PHYSICALWIDTH = 110, - PHYSICALHEIGHT = 111, - PHYSICALOFFSETX = 112, - PHYSICALOFFSETY = 113, - VERTRES = 10, - HORZRES = 8, DM_ORIENTATION = 0x00000001, DM_PAPERSIZE = 0x00000002, DM_PAPERLENGTH = 0x00000004, @@ -425,15 +412,6 @@ namespace System.Drawing [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] public static extern IntPtr /*HDC*/ ResetDC(HandleRef hDC, HandleRef /*DEVMODE*/ lpDevMode); - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetClipRgn(HandleRef hDC, HandleRef hRgn); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int SelectClipRgn(HandleRef hDC, HandleRef hRgn); - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Auto)] public static extern int AddFontResourceEx(string lpszFilename, int fl, IntPtr pdv); @@ -442,45 +420,6 @@ namespace System.Drawing return AddFontResourceEx(fileName, /*FR_PRIVATE*/ 0x10, IntPtr.Zero); } - internal static IntPtr SaveClipRgn(IntPtr hDC) - { - IntPtr hTempRgn = CreateRectRgn(0, 0, 0, 0); - IntPtr hSaveRgn = IntPtr.Zero; - try - { - int result = GetClipRgn(new HandleRef(null, hDC), new HandleRef(null, hTempRgn)); - if (result > 0) - { - hSaveRgn = hTempRgn; - hTempRgn = IntPtr.Zero; - } - } - finally - { - if (hTempRgn != IntPtr.Zero) - { - DeleteObject(new HandleRef(null, hTempRgn)); - } - } - - return hSaveRgn; - } - - internal static void RestoreClipRgn(IntPtr hDC, IntPtr hRgn) - { - try - { - SelectClipRgn(new HandleRef(null, hDC), new HandleRef(null, hRgn)); - } - finally - { - if (hRgn != IntPtr.Zero) - { - DeleteObject(new HandleRef(null, hRgn)); - } - } - } - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern int ExtEscape(HandleRef hDC, int nEscape, int cbInput, ref int inData, int cbOutput, [Out] out int outData); @@ -639,50 +578,6 @@ namespace System.Drawing public int biClrImportant; } - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - public unsafe struct LOGFONT - { - private const int LF_FACESIZE = 32; - - public int lfHeight; - public int lfWidth; - public int lfEscapement; - public int lfOrientation; - public int lfWeight; - public byte lfItalic; - public byte lfUnderline; - public byte lfStrikeOut; - public byte lfCharSet; - public byte lfOutPrecision; - public byte lfClipPrecision; - public byte lfQuality; - public byte lfPitchAndFamily; - private fixed char _lfFaceName[LF_FACESIZE]; - public Span lfFaceName - { - get { fixed (char* c = _lfFaceName) { return new Span(c, LF_FACESIZE); } } - } - - public override string ToString() - { - return - "lfHeight=" + lfHeight + ", " + - "lfWidth=" + lfWidth + ", " + - "lfEscapement=" + lfEscapement + ", " + - "lfOrientation=" + lfOrientation + ", " + - "lfWeight=" + lfWeight + ", " + - "lfItalic=" + lfItalic + ", " + - "lfUnderline=" + lfUnderline + ", " + - "lfStrikeOut=" + lfStrikeOut + ", " + - "lfCharSet=" + lfCharSet + ", " + - "lfOutPrecision=" + lfOutPrecision + ", " + - "lfClipPrecision=" + lfClipPrecision + ", " + - "lfQuality=" + lfQuality + ", " + - "lfPitchAndFamily=" + lfPitchAndFamily + ", " + - "lfFaceName=" + lfFaceName.ToString(); - } - } - // https://devblogs.microsoft.com/oldnewthing/20101018-00/?p=12513 // https://devblogs.microsoft.com/oldnewthing/20120720-00/?p=7083 @@ -796,14 +691,6 @@ namespace System.Drawing } } - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "DeleteObject", CharSet = CharSet.Auto)] - internal static extern int IntDeleteObject(HandleRef hObject); - - public static int DeleteObject(HandleRef hObject) - { - return IntDeleteObject(hObject); - } - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] public static extern IntPtr SelectObject(HandleRef hdc, HandleRef obj); @@ -834,10 +721,10 @@ namespace System.Drawing public static extern int GetObject(HandleRef hObject, int nSize, ref BITMAP bm); [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern int GetObject(HandleRef hObject, int nSize, ref LOGFONT lf); + public static extern int GetObject(HandleRef hObject, int nSize, ref Interop.User32.LOGFONT lf); - public static unsafe int GetObject(HandleRef hObject, ref LOGFONT lp) - => GetObject(hObject, sizeof(LOGFONT), ref lp); + public static unsafe int GetObject(HandleRef hObject, ref Interop.User32.LOGFONT lp) + => GetObject(hObject, sizeof(Interop.User32.LOGFONT), ref lp); [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] public static extern bool GetIconInfo(HandleRef hIcon, ref ICONINFO info); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs index 5640a2d..e48425f 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Unix.cs @@ -340,10 +340,10 @@ namespace System.Drawing internal static extern int GdipIsOutlineVisiblePathPointI(IntPtr path, int x, int y, IntPtr pen, IntPtr graphics, out bool result); [DllImport(LibraryName, ExactSpelling = true)] - internal static extern int GdipCreateFontFromLogfont(IntPtr hdc, ref LOGFONT lf, out IntPtr ptr); + internal static extern int GdipCreateFontFromLogfont(IntPtr hdc, ref Interop.User32.LOGFONT lf, out IntPtr ptr); [DllImport(LibraryName, ExactSpelling = true)] - internal static extern int GdipCreateFontFromHfont(IntPtr hdc, out IntPtr font, ref LOGFONT lf); + internal static extern int GdipCreateFontFromHfont(IntPtr hdc, out IntPtr font, ref Interop.User32.LOGFONT lf); [DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)] internal static extern int GdipGetMetafileHeaderFromFile(string filename, IntPtr header); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Windows.cs index db9ef1a..e6054f2 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.Windows.cs @@ -375,7 +375,7 @@ namespace System.Drawing internal static extern int GdipComment(HandleRef graphics, int sizeData, byte[] data); [DllImport(LibraryName, ExactSpelling = true, CharSet = CharSet.Unicode)] - internal static extern int GdipCreateFontFromLogfontW(IntPtr hdc, ref LOGFONT lf, out IntPtr font); + internal static extern int GdipCreateFontFromLogfontW(IntPtr hdc, ref Interop.User32.LOGFONT lf, out IntPtr font); [DllImport(LibraryName, ExactSpelling = true)] internal static extern int GdipCreateBitmapFromStream(Interop.Ole32.IStream stream, out IntPtr bitmap); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs index 6bc6cbe..1862a48 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/GdiplusNative.cs @@ -527,7 +527,7 @@ namespace System.Drawing internal static extern int GdipGetFontUnit(HandleRef font, out GraphicsUnit unit); [DllImport(LibraryName, ExactSpelling = true)] - internal static extern int GdipGetLogFontW(HandleRef font, HandleRef graphics, ref LOGFONT lf); + internal static extern int GdipGetLogFontW(HandleRef font, HandleRef graphics, ref Interop.User32.LOGFONT lf); [DllImport(LibraryName, ExactSpelling = true)] internal static extern int GdipCreatePen1(int argb, float width, int unit, out IntPtr pen); diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs index 64ea178..e4b1712 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Graphics.Windows.cs @@ -924,7 +924,7 @@ namespace System.Drawing { if (s_halftonePalette != IntPtr.Zero) { - SafeNativeMethods.IntDeleteObject(new HandleRef(null, s_halftonePalette)); + Interop.Gdi32.DeleteObject(s_halftonePalette); s_halftonePalette = IntPtr.Zero; } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs index caaf047..32c87d9 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Icon.Windows.cs @@ -227,7 +227,7 @@ namespace System.Drawing new HandleRef(null, info.hbmColor), sizeof(SafeNativeMethods.BITMAP), ref bitmap); - SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmColor)); + Interop.Gdi32.DeleteObject(info.hbmColor); _iconSize = new Size((int)bitmap.bmWidth, (int)bitmap.bmHeight); } else if (info.hbmMask != IntPtr.Zero) @@ -241,7 +241,7 @@ namespace System.Drawing if (info.hbmMask != IntPtr.Zero) { - SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmMask)); + Interop.Gdi32.DeleteObject(info.hbmMask); } } @@ -358,7 +358,7 @@ namespace System.Drawing // The ROP is SRCCOPY, so we can be simple here and take // advantage of clipping regions. Drawing the cursor // is merely a matter of offsetting and clipping. - IntPtr hSaveRgn = SafeNativeMethods.SaveClipRgn(dc); + IntPtr hSaveRgn = SaveClipRgn(dc); try { SafeNativeMethods.IntersectClipRect(new HandleRef(this, dc), targetX, targetY, targetX + clipWidth, targetY + clipHeight); @@ -374,10 +374,30 @@ namespace System.Drawing } finally { - SafeNativeMethods.RestoreClipRgn(dc, hSaveRgn); + RestoreClipRgn(dc, hSaveRgn); } } + private static IntPtr SaveClipRgn(IntPtr hDC) + { + IntPtr hTempRgn = Interop.Gdi32.CreateRectRgn(0, 0, 0, 0); + IntPtr hSaveRgn = IntPtr.Zero; + + int result = Interop.Gdi32.GetClipRgn(hDC, hTempRgn); + if (result > 0) + { + hSaveRgn = hTempRgn; + hTempRgn = IntPtr.Zero; + } + + return hSaveRgn; + } + + private static void RestoreClipRgn(IntPtr hDC, IntPtr hRgn) + { + Interop.Gdi32.SelectClipRgn(new HandleRef(null, hDC), new HandleRef(null, hRgn)); + } + internal void Draw(Graphics graphics, int x, int y) { Size size = Size; @@ -450,10 +470,10 @@ namespace System.Drawing if (s_bitDepth == 0) { - IntPtr dc = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef); - s_bitDepth = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dc), SafeNativeMethods.BITSPIXEL); - s_bitDepth *= UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, dc), SafeNativeMethods.PLANES); - UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, dc)); + IntPtr dc = Interop.User32.GetDC(IntPtr.Zero); + s_bitDepth = Interop.Gdi32.GetDeviceCaps(dc, Interop.Gdi32.DeviceCapability.BITSPIXEL); + s_bitDepth *= Interop.Gdi32.GetDeviceCaps(dc, Interop.Gdi32.DeviceCapability.PLANES); + Interop.User32.ReleaseDC(IntPtr.Zero, dc); // If the bitdepth is 8, make it 4 because windows does not // choose a 256 color icon if the display is running in 256 color mode @@ -792,11 +812,11 @@ namespace System.Drawing { if (info.hbmColor != IntPtr.Zero) { - SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmColor)); + Interop.Gdi32.DeleteObject(info.hbmColor); } if (info.hbmMask != IntPtr.Zero) { - SafeNativeMethods.IntDeleteObject(new HandleRef(null, info.hbmMask)); + Interop.Gdi32.DeleteObject(info.hbmMask); } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs index 505a0d2..5b12a5b 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeMethods.cs @@ -14,20 +14,10 @@ namespace System.Drawing public const int MAX_PATH = 260; internal const int SM_REMOTESESSION = 0x1000; - internal const int OBJ_DC = 3, - OBJ_METADC = 4, - OBJ_MEMDC = 10, - OBJ_ENHMETADC = 12, - DIB_RGB_COLORS = 0, - BI_BITFIELDS = 3, - BI_RGB = 0, - BITMAPINFO_MAX_COLORSIZE = 256, - DEFAULT_GUI_FONT = 17; - - internal const uint SPI_GETICONTITLELOGFONT = 0x001F; - - // Gets metrics associated with the nonclient area of nonminimized windows - internal const uint SPI_GETNONCLIENTMETRICS = 41; + internal const int DIB_RGB_COLORS = 0; + internal const int BI_BITFIELDS = 3; + internal const int BI_RGB = 0; + internal const int BITMAPINFO_MAX_COLORSIZE = 256; [StructLayout(LayoutKind.Sequential)] internal struct BITMAPINFO_FLAT @@ -81,28 +71,5 @@ namespace System.Drawing public byte rgbRed; public byte rgbReserved; } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal struct NONCLIENTMETRICS - { - public uint cbSize; - public int iBorderWidth; - public int iScrollWidth; - public int iScrollHeight; - public int iCaptionWidth; - public int iCaptionHeight; - public SafeNativeMethods.LOGFONT lfCaptionFont; - public int iSmCaptionWidth; - public int iSmCaptionHeight; - public SafeNativeMethods.LOGFONT lfSmCaptionFont; - public int iMenuWidth; - public int iMenuHeight; - public SafeNativeMethods.LOGFONT lfMenuFont; - public SafeNativeMethods.LOGFONT lfStatusFont; - public SafeNativeMethods.LOGFONT lfMessageFont; - - // This is supported on Windows vista and later - public int iPaddedBorderWidth; - } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeStructs.Unix.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeStructs.Unix.cs index f3535bf..5b677c5 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/NativeStructs.Unix.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/NativeStructs.Unix.cs @@ -34,26 +34,6 @@ using System.Runtime.InteropServices; namespace System.Drawing { - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)] - internal struct LOGFONT - { - internal int lfHeight; - internal uint lfWidth; - internal uint lfEscapement; - internal uint lfOrientation; - internal uint lfWeight; - internal byte lfItalic; - internal byte lfUnderline; - internal byte lfStrikeOut; - internal byte lfCharSet; - internal byte lfOutPrecision; - internal byte lfClipPrecision; - internal byte lfQuality; - internal byte lfPitchAndFamily; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - internal string lfFaceName; - } - [StructLayout(LayoutKind.Sequential)] internal struct GdipImageCodecInfo { diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs index 56c7898..151fcb5 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/DefaultPrintController.cs @@ -74,9 +74,6 @@ namespace System.Drawing.Printing Interop.Kernel32.GlobalUnlock(new HandleRef(this, _modeHandle)); } - // int horizontalResolution = Windows.GetDeviceCaps(dc.Hdc, SafeNativeMethods.HORZRES); - // int verticalResolution = Windows.GetDeviceCaps(dc.Hdc, SafeNativeMethods.VERTRES); - _graphics = Graphics.FromHdcInternal(_dc.Hdc); if (_graphics != null && document.OriginAtMargins) @@ -84,10 +81,10 @@ namespace System.Drawing.Printing // Adjust the origin of the graphics object to be at the // user-specified margin location // - int dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSX); - int dpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSY); - int hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX); - int hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY); + int dpiX = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSX); + int dpiY = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSY); + int hardMarginX_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX); + int hardMarginY_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY); float hardMarginX = hardMarginX_DU * 100 / dpiX; float hardMarginY = hardMarginY_DU * 100 / dpiY; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs index 598e42d..e46f67a 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PageSettings.Windows.cs @@ -80,8 +80,8 @@ namespace System.Drawing.Printing try { - int dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSX); - int hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX); + int dpiX = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSX); + int hardMarginX_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX); hardMarginX = hardMarginX_DU * 100 / dpiX; } finally @@ -105,8 +105,8 @@ namespace System.Drawing.Printing try { - int dpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSY); - int hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY); + int dpiY = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSY); + int hardMarginY_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY); hardMarginY = hardMarginY_DU * 100 / dpiY; } finally @@ -192,25 +192,25 @@ namespace System.Drawing.Printing try { - int dpiX = UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.LOGPIXELSX); - int dpiY = UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.LOGPIXELSY); + int dpiX = Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.LOGPIXELSX); + int dpiY = Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.LOGPIXELSY); if (!Landscape) { // // Need to convert the printable area to 100th of an inch from the device units - printableArea.X = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.PHYSICALOFFSETX) * 100 / dpiX; - printableArea.Y = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.PHYSICALOFFSETY) * 100 / dpiY; - printableArea.Width = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.HORZRES) * 100 / dpiX; - printableArea.Height = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.VERTRES) * 100 / dpiY; + printableArea.X = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX) * 100 / dpiX; + printableArea.Y = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY) * 100 / dpiY; + printableArea.Width = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.HORZRES) * 100 / dpiX; + printableArea.Height = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.VERTRES) * 100 / dpiY; } else { // // Need to convert the printable area to 100th of an inch from the device units - printableArea.Y = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.PHYSICALOFFSETX) * 100 / dpiX; - printableArea.X = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.PHYSICALOFFSETY) * 100 / dpiY; - printableArea.Height = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.HORZRES) * 100 / dpiX; - printableArea.Width = (float)UnsafeNativeMethods.GetDeviceCaps(hdc, SafeNativeMethods.VERTRES) * 100 / dpiY; + printableArea.Y = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX) * 100 / dpiX; + printableArea.X = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY) * 100 / dpiY; + printableArea.Height = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.HORZRES) * 100 / dpiX; + printableArea.Width = (float)Interop.Gdi32.GetDeviceCaps(hdc, Interop.Gdi32.DeviceCapability.VERTRES) * 100 / dpiY; } } finally diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PreviewPrintController.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PreviewPrintController.Windows.cs index aa63b8e..6a99e82 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PreviewPrintController.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PreviewPrintController.Windows.cs @@ -73,10 +73,10 @@ namespace System.Drawing.Printing { // Adjust the origin of the graphics object to be at the // user-specified margin location - int dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSX); - int dpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.LOGPIXELSY); - int hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX); - int hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY); + int dpiX = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSX); + int dpiY = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSY); + int hardMarginX_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX); + int hardMarginY_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(_dc, _dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY); float hardMarginX = hardMarginX_DU * 100 / dpiX; float hardMarginY = hardMarginY_DU * 100 / dpiY; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintPreviewGraphics.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintPreviewGraphics.cs index 3bb940b..b0f4e59 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintPreviewGraphics.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrintPreviewGraphics.cs @@ -39,10 +39,10 @@ namespace System.Drawing { // Adjust the origin of the graphics object to be at the user-specified margin location // Note: Graphics.FromHdc internally calls SaveDC(hdc), we can still use the saved hdc to get the resolution. - int dpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSX); - int dpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.LOGPIXELSY); - int hardMarginX_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETX); - int hardMarginY_DU = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), SafeNativeMethods.PHYSICALOFFSETY); + int dpiX = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSX); + int dpiY = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.LOGPIXELSY); + int hardMarginX_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETX); + int hardMarginY_DU = Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), Interop.Gdi32.DeviceCapability.PHYSICALOFFSETY); float hardMarginX = hardMarginX_DU * 100 / dpiX; float hardMarginY = hardMarginY_DU * 100 / dpiY; diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs index 540e00b..a123443 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Printing/PrinterSettings.Windows.cs @@ -235,7 +235,7 @@ namespace System.Drawing.Printing { get { - return GetDeviceCaps(SafeNativeMethods.TECHNOLOGY, SafeNativeMethods.DT_RASPRINTER) == SafeNativeMethods.DT_PLOTTER; + return GetDeviceCaps(Interop.Gdi32.DeviceCapability.TECHNOLOGY, Interop.Gdi32.DeviceTechnology.DT_RASPRINTER) == Interop.Gdi32.DeviceTechnology.DT_PLOTTER; } } @@ -523,7 +523,7 @@ namespace System.Drawing.Printing { get { - return GetDeviceCaps(SafeNativeMethods.BITSPIXEL, 1) > 1; + return GetDeviceCaps(Interop.Gdi32.DeviceCapability.BITSPIXEL, 1) > 1; } } @@ -574,7 +574,7 @@ namespace System.Drawing.Printing internal DeviceContext CreateDeviceContext(IntPtr hdevmode) { IntPtr modePointer = Interop.Kernel32.GlobalLock(hdevmode); - DeviceContext dc = DeviceContext.CreateDC(DriverName, PrinterNameInternal, (string)null, new HandleRef(null, modePointer)); + DeviceContext dc = DeviceContext.CreateDC(DriverName, PrinterNameInternal, (string)null, modePointer); Interop.Kernel32.GlobalUnlock(hdevmode); return dc; } @@ -603,7 +603,7 @@ namespace System.Drawing.Printing internal DeviceContext CreateInformationContext(IntPtr hdevmode) { IntPtr modePointer = Interop.Kernel32.GlobalLock(hdevmode); - DeviceContext dc = DeviceContext.CreateIC(DriverName, PrinterNameInternal, (string)null, new HandleRef(null, modePointer)); + DeviceContext dc = DeviceContext.CreateIC(DriverName, PrinterNameInternal, (string)null, modePointer); Interop.Kernel32.GlobalUnlock(hdevmode); return dc; } @@ -830,25 +830,12 @@ namespace System.Drawing.Printing } } - private int GetDeviceCaps(int capability, int defaultValue) + private int GetDeviceCaps(Interop.Gdi32.DeviceCapability capability, int defaultValue) { - DeviceContext dc = CreateInformationContext(DefaultPageSettings); - int result = defaultValue; - - try + using (DeviceContext dc = CreateInformationContext(DefaultPageSettings)) { - result = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(dc, dc.Hdc), capability); + return Interop.Gdi32.GetDeviceCaps(new HandleRef(dc, dc.Hdc), capability); } - catch (InvalidPrinterException) - { - // do nothing, will return defaultValue. - } - finally - { - dc.Dispose(); - } - - return result; } /// diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/Region.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/Region.Windows.cs index 81b6699..31ccdb2 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/Region.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/Region.Windows.cs @@ -17,8 +17,7 @@ namespace System.Drawing throw new ArgumentNullException(nameof(regionHandle)); } - // On Windows HRGN are (old) GDI objects. Deskop .NET does not check the return code of IntDeleteObject - SafeNativeMethods.IntDeleteObject(new HandleRef(this, regionHandle)); + Interop.Gdi32.DeleteObject(new HandleRef(this, regionHandle)); } } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/ScreenDC.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/ScreenDC.cs index 45e8873..cff0eb7 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/ScreenDC.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/ScreenDC.cs @@ -13,19 +13,13 @@ namespace System.Drawing { private IntPtr _handle; - public static ScreenDC Create() + public static ScreenDC Create() => new ScreenDC { - return new ScreenDC - { - _handle = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef) - }; - } + _handle = Interop.User32.GetDC(IntPtr.Zero) + }; public static implicit operator IntPtr(ScreenDC screenDC) => screenDC._handle; - public void Dispose() - { - UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, _handle)); - } + public void Dispose() => Interop.User32.ReleaseDC(IntPtr.Zero, _handle); } } diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs index 7a6e7a5..2c099e3 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/SystemFonts.Windows.cs @@ -10,12 +10,12 @@ namespace System.Drawing { public static partial class SystemFonts { - private static unsafe bool GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics) + private static unsafe bool GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics) { - metrics = new NativeMethods.NONCLIENTMETRICS { cbSize = (uint)sizeof(NativeMethods.NONCLIENTMETRICS) }; + metrics = new Interop.User32.NONCLIENTMETRICS { cbSize = (uint)sizeof(Interop.User32.NONCLIENTMETRICS) }; fixed (void* m = &metrics) { - return UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETNONCLIENTMETRICS, metrics.cbSize, m, 0); + return Interop.User32.SystemParametersInfoW(Interop.User32.SystemParametersAction.SPI_GETNONCLIENTMETRICS, metrics.cbSize, m, 0); } } @@ -25,7 +25,7 @@ namespace System.Drawing { Font captionFont = null; - if (GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics)) + if (GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics)) { captionFont = GetFontFromData(metrics.lfCaptionFont); captionFont.SetSystemFontName(nameof(CaptionFont)); @@ -41,7 +41,7 @@ namespace System.Drawing { Font smcaptionFont = null; - if (GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics)) + if (GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics)) { smcaptionFont = GetFontFromData(metrics.lfSmCaptionFont); smcaptionFont.SetSystemFontName(nameof(SmallCaptionFont)); @@ -57,7 +57,7 @@ namespace System.Drawing { Font menuFont = null; - if (GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics)) + if (GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics)) { menuFont = GetFontFromData(metrics.lfMenuFont); menuFont.SetSystemFontName(nameof(MenuFont)); @@ -73,7 +73,7 @@ namespace System.Drawing { Font statusFont = null; - if (GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics)) + if (GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics)) { statusFont = GetFontFromData(metrics.lfStatusFont); statusFont.SetSystemFontName(nameof(StatusFont)); @@ -89,7 +89,7 @@ namespace System.Drawing { Font messageBoxFont = null; - if (GetNonClientMetrics(out NativeMethods.NONCLIENTMETRICS metrics)) + if (GetNonClientMetrics(out Interop.User32.NONCLIENTMETRICS metrics)) { messageBoxFont = GetFontFromData(metrics.lfMessageFont); messageBoxFont.SetSystemFontName(nameof(MessageBoxFont)); @@ -117,8 +117,8 @@ namespace System.Drawing { Font iconTitleFont = null; - var itfont = new SafeNativeMethods.LOGFONT(); - if (UnsafeNativeMethods.SystemParametersInfo(NativeMethods.SPI_GETICONTITLELOGFONT, (uint)sizeof(SafeNativeMethods.LOGFONT), &itfont, 0)) + var itfont = new Interop.User32.LOGFONT(); + if (Interop.User32.SystemParametersInfoW(Interop.User32.SystemParametersAction.SPI_GETICONTITLELOGFONT, (uint)sizeof(Interop.User32.LOGFONT), &itfont, 0)) { iconTitleFont = GetFontFromData(itfont); iconTitleFont.SetSystemFontName(nameof(IconTitleFont)); @@ -147,7 +147,7 @@ namespace System.Drawing // First try DEFAULT_GUI. if (defaultFont == null) { - IntPtr handle = UnsafeNativeMethods.GetStockObject(NativeMethods.DEFAULT_GUI_FONT); + IntPtr handle = Interop.Gdi32.GetStockObject(Interop.Gdi32.StockObject.DEFAULT_GUI_FONT); try { using (Font fontInWorldUnits = Font.FromHfont(handle)) @@ -236,7 +236,7 @@ namespace System.Drawing return new Font(font.FontFamily, font.SizeInPoints, font.Style, GraphicsUnit.Point, font.GdiCharSet, font.GdiVerticalFont); } - private static Font GetFontFromData(SafeNativeMethods.LOGFONT logFont) + private static Font GetFontFromData(Interop.User32.LOGFONT logFont) { Font font = null; try diff --git a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs b/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs index 388cd87..d66459c 100644 --- a/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs +++ b/src/libraries/System.Drawing.Common/src/System/Drawing/UnsafeNativeMethods.cs @@ -11,34 +11,10 @@ namespace System.Drawing [DllImport(ExternDll.Kernel32, SetLastError = true, ExactSpelling = true, EntryPoint = "RtlMoveMemory")] public static extern void CopyMemory(HandleRef destData, HandleRef srcData, int size); - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr GetDC(HandleRef hWnd); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern bool DeleteDC(HandleRef hDC); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern int ReleaseDC(HandleRef hWnd, HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateCompatibleDC(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr GetStockObject(int nIndex); - [DllImport(ExternDll.Kernel32, SetLastError = true)] public static extern int GetSystemDefaultLCID(); [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] public static extern int GetSystemMetrics(int nIndex); - - [DllImport(ExternDll.User32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern unsafe bool SystemParametersInfo(uint uiAction, uint uiParam, void* pvParam, uint fWinIni); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetDeviceCaps(HandleRef hDC, int nIndex); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, CharSet = CharSet.Auto)] - public static extern int GetObjectType(HandleRef hObject); } } diff --git a/src/libraries/System.Drawing.Common/src/misc/DpiHelper.cs b/src/libraries/System.Drawing.Common/src/misc/DpiHelper.cs index 25dcfa2..b6f8e54 100644 --- a/src/libraries/System.Drawing.Common/src/misc/DpiHelper.cs +++ b/src/libraries/System.Drawing.Common/src/misc/DpiHelper.cs @@ -37,13 +37,13 @@ namespace System.Windows.Forms return; } - IntPtr hDC = UnsafeNativeMethods.GetDC(NativeMethods.NullHandleRef); + IntPtr hDC = Interop.User32.GetDC(IntPtr.Zero); if (hDC != IntPtr.Zero) { - s_deviceDpiX = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSX); - s_deviceDpiY = UnsafeNativeMethods.GetDeviceCaps(new HandleRef(null, hDC), SafeNativeMethods.LOGPIXELSY); + s_deviceDpiX = Interop.Gdi32.GetDeviceCaps(hDC, Interop.Gdi32.DeviceCapability.LOGPIXELSX); + s_deviceDpiY = Interop.Gdi32.GetDeviceCaps(hDC, Interop.Gdi32.DeviceCapability.LOGPIXELSY); - UnsafeNativeMethods.ReleaseDC(NativeMethods.NullHandleRef, new HandleRef(null, hDC)); + Interop.User32.ReleaseDC(IntPtr.Zero, hDC); } s_isInitialized = true; diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/DeviceContext.cs b/src/libraries/System.Drawing.Common/src/misc/GDI/DeviceContext.cs index 31c10e7..a15e1c2 100644 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/DeviceContext.cs +++ b/src/libraries/System.Drawing.Common/src/misc/GDI/DeviceContext.cs @@ -124,10 +124,10 @@ namespace System.Drawing.Internal private void CacheInitialState() { Debug.Assert(_hDC != IntPtr.Zero, "Cannot get initial state without a valid HDC"); - _hCurrentPen = _hInitialPen = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, _hDC), IntNativeMethods.OBJ_PEN); - _hCurrentBrush = _hInitialBrush = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, _hDC), IntNativeMethods.OBJ_BRUSH); - _hCurrentBmp = _hInitialBmp = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, _hDC), IntNativeMethods.OBJ_BITMAP); - _hCurrentFont = _hInitialFont = IntUnsafeNativeMethods.GetCurrentObject(new HandleRef(this, _hDC), IntNativeMethods.OBJ_FONT); + _hCurrentPen = _hInitialPen = Interop.Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Interop.Gdi32.ObjectType.OBJ_PEN); + _hCurrentBrush = _hInitialBrush = Interop.Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Interop.Gdi32.ObjectType.OBJ_BRUSH); + _hCurrentBmp = _hInitialBmp = Interop.Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Interop.Gdi32.ObjectType.OBJ_BITMAP); + _hCurrentFont = _hInitialFont = Interop.Gdi32.GetCurrentObject(new HandleRef(this, _hDC), Interop.Gdi32.ObjectType.OBJ_FONT); } @@ -161,34 +161,31 @@ namespace System.Drawing.Internal if (dcType == DeviceContextType.Display) { - _hWnd = IntUnsafeNativeMethods.WindowFromDC(new HandleRef(this, _hDC)); + _hWnd = Interop.User32.WindowFromDC(new HandleRef(this, _hDC)); } #if TRACK_HDC - Debug.WriteLine( DbgUtil.StackTraceToStr( string.Format("DeviceContext( hDC=0x{0:X8}, Type={1} )", unchecked((int) hDC), dcType) )); + Debug.WriteLine(DbgUtil.StackTraceToStr($"DeviceContext(hDC=0x{(int)hDC:X8}, Type={dcType})")); #endif } - - /// /// CreateDC creates a DeviceContext object wrapping an hdc created with the Win32 CreateDC function. /// - public static DeviceContext CreateDC(string driverName, string deviceName, string fileName, HandleRef devMode) + public static DeviceContext CreateDC(string driverName, string deviceName, string fileName, IntPtr devMode) { // Note: All input params can be null but not at the same time. See MSDN for information. - - IntPtr hdc = IntUnsafeNativeMethods.CreateDC(driverName, deviceName, fileName, devMode); + IntPtr hdc = Interop.Gdi32.CreateDCW(driverName, deviceName, fileName, devMode); return new DeviceContext(hdc, DeviceContextType.NamedDevice); } /// /// CreateIC creates a DeviceContext object wrapping an hdc created with the Win32 CreateIC function. /// - public static DeviceContext CreateIC(string driverName, string deviceName, string fileName, HandleRef devMode) + public static DeviceContext CreateIC(string driverName, string deviceName, string fileName, IntPtr devMode) { // Note: All input params can be null but not at the same time. See MSDN for information. - IntPtr hdc = IntUnsafeNativeMethods.CreateIC(driverName, deviceName, fileName, devMode); + IntPtr hdc = Interop.Gdi32.CreateICW(driverName, deviceName, fileName, devMode); return new DeviceContext(hdc, DeviceContextType.Information); } @@ -201,8 +198,7 @@ namespace System.Drawing.Internal // Win2K+: (See CreateCompatibleDC in the MSDN). // In this case the thread that calls CreateCompatibleDC owns the HDC that is created. When this thread is destroyed, // the HDC is no longer valid. - - IntPtr compatibleDc = IntUnsafeNativeMethods.CreateCompatibleDC(new HandleRef(null, hdc)); + IntPtr compatibleDc = Interop.Gdi32.CreateCompatibleDC(hdc); return new DeviceContext(compatibleDc, DeviceContextType.Memory); } @@ -248,11 +244,11 @@ namespace System.Drawing.Internal break; case DeviceContextType.Information: case DeviceContextType.NamedDevice: - IntUnsafeNativeMethods.DeleteDC(new HandleRef(this, _hDC)); + Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; case DeviceContextType.Memory: - IntUnsafeNativeMethods.DeleteDC(new HandleRef(this, _hDC)); + Interop.Gdi32.DeleteDC(new HandleRef(this, _hDC)); _hDC = IntPtr.Zero; break; // case DeviceContextType.Metafile: - not yet supported. @@ -279,7 +275,7 @@ namespace System.Drawing.Internal // Note: for common DCs, GetDC assigns default attributes to the DC each time it is retrieved. // For example, the default font is System. - _hDC = UnsafeNativeMethods.GetDC(new HandleRef(this, _hWnd)); + _hDC = Interop.User32.GetDC(new HandleRef(this, _hWnd)); #if TRACK_HDC Debug.WriteLine( DbgUtil.StackTraceToStr( string.Format("hdc[0x{0:x8}]=DC.GetHdc(hWnd=0x{1:x8})", unchecked((int) _hDC), unchecked((int) _hWnd)))); #endif @@ -299,7 +295,7 @@ namespace System.Drawing.Internal #if TRACK_HDC int retVal = #endif - UnsafeNativeMethods.ReleaseDC(new HandleRef(this, _hWnd), new HandleRef(this, _hDC)); + Interop.User32.ReleaseDC(new HandleRef(this, _hWnd), new HandleRef(this, _hDC)); // Note: retVal == 0 means it was not released but doesn't necessarily means an error; class or private DCs are never released. #if TRACK_HDC Debug.WriteLine( DbgUtil.StackTraceToStr( string.Format("[ret={0}]=DC.ReleaseDC(hDc=0x{1:x8}, hWnd=0x{2:x8})", retVal, unchecked((int) _hDC), unchecked((int) _hWnd)))); @@ -325,7 +321,7 @@ namespace System.Drawing.Internal bool result = #endif // Note: Don't use the Hdc property here, it would force handle creation. - IntUnsafeNativeMethods.RestoreDC(new HandleRef(this, _hDC), -1); + Interop.Gdi32.RestoreDC(new HandleRef(this, _hDC), -1); #if TRACK_HDC // Note: Winforms may call this method during app exit at which point the DC may have been finalized already causing this assert to popup. Debug.WriteLine( DbgUtil.StackTraceToStr( string.Format("ret[0]=DC.RestoreHdc(hDc=0x{1:x8}, state={2})", result, unchecked((int) _hDC), restoreState) )); @@ -361,7 +357,7 @@ namespace System.Drawing.Internal public int SaveHdc() { HandleRef hdc = new HandleRef(this, _hDC); - int state = IntUnsafeNativeMethods.SaveDC(hdc); + int state = Interop.Gdi32.SaveDC(hdc); if (_contextStack == null) { @@ -395,7 +391,7 @@ namespace System.Drawing.Internal HandleRef hdc = new HandleRef(this, _hDC); HandleRef hRegion = new HandleRef(region, region.HRegion); - IntUnsafeNativeMethods.SelectClipRgn(hdc, hRegion); + Interop.Gdi32.SelectClipRgn(hdc, hRegion); } /// @@ -412,13 +408,13 @@ namespace System.Drawing.Internal WindowsRegion clip = new WindowsRegion(0, 0, 0, 0); try { - int result = IntUnsafeNativeMethods.GetClipRgn(new HandleRef(this, _hDC), new HandleRef(clip, clip.HRegion)); + int result = Interop.Gdi32.GetClipRgn(new HandleRef(this, _hDC), new HandleRef(clip, clip.HRegion)); // If the function succeeds and there is a clipping region for the given device context, the return value is 1. if (result == 1) { Debug.Assert(clip.HRegion != IntPtr.Zero); - wr.CombineRegion(clip, wr, RegionCombineMode.AND); //1 = AND (or Intersect) + wr.CombineRegion(clip, wr, Interop.Gdi32.CombineMode.RGN_AND); } SetClip(wr); @@ -435,8 +431,8 @@ namespace System.Drawing.Internal /// public void TranslateTransform(int dx, int dy) { - IntNativeMethods.POINT orgn = new IntNativeMethods.POINT(); - IntUnsafeNativeMethods.OffsetViewportOrgEx(new HandleRef(this, _hDC), dx, dy, orgn); + var origin = new Point(); + Interop.Gdi32.OffsetViewportOrgEx(new HandleRef(this, _hDC), dx, dy, ref origin); } /// diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/NativeMethods.cs b/src/libraries/System.Drawing.Common/src/misc/GDI/NativeMethods.cs deleted file mode 100644 index 254d12d..0000000 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/NativeMethods.cs +++ /dev/null @@ -1,63 +0,0 @@ -// 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.Diagnostics.CodeAnalysis; -using System.Runtime.InteropServices; - -namespace System.Drawing.Internal -{ - internal partial class IntNativeMethods - { - public const int OBJ_PEN = 1; - public const int OBJ_BRUSH = 2; - public const int OBJ_FONT = 6; - public const int OBJ_BITMAP = 7; - - public enum RegionFlags - { - ERROR = 0, - NULLREGION = 1, - SIMPLEREGION = 2, - COMPLEXREGION = 3, - } - - [StructLayout(LayoutKind.Sequential)] - public struct RECT - { - public int left; - public int top; - public int right; - public int bottom; - - public Size Size => new Size(right - left, bottom - top); - } - - [StructLayout(LayoutKind.Sequential)] - public class POINT - { - public int x; - public int y; - } - - [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] - public class LOGFONT - { - public int lfHeight; - public int lfWidth; - public int lfEscapement; - public int lfOrientation; - public int lfWeight; - public byte lfItalic; - public byte lfUnderline; - public byte lfStrikeOut; - public byte lfCharSet; - public byte lfOutPrecision; - public byte lfClipPrecision; - public byte lfQuality; - public byte lfPitchAndFamily; - [MarshalAs(UnmanagedType.ByValTStr, SizeConst = 32)] - public string lfFaceName; - } - } -} diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/SafeNativeMethods.cs b/src/libraries/System.Drawing.Common/src/misc/GDI/SafeNativeMethods.cs deleted file mode 100644 index b449658..0000000 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/SafeNativeMethods.cs +++ /dev/null @@ -1,18 +0,0 @@ -// 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.Runtime.InteropServices; - -namespace System.Drawing.Internal -{ - /// - /// This is an extract of the System.Drawing IntNativeMethods in the CommonUI tree. - /// This is done to be able to compile the GDI code in both assemblies System.Drawing and System.Windows.Forms. - /// - internal static partial class IntSafeNativeMethods - { - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateRectRgn(int x1, int y1, int x2, int y2); - } -} diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/UnsafeNativeMethods.cs b/src/libraries/System.Drawing.Common/src/misc/GDI/UnsafeNativeMethods.cs deleted file mode 100644 index cbf33ef..0000000 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/UnsafeNativeMethods.cs +++ /dev/null @@ -1,116 +0,0 @@ -// 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.Diagnostics; -using System.Runtime.InteropServices; - -namespace System.Drawing.Internal -{ - internal static partial class IntUnsafeNativeMethods - { - /// - /// NOTE: DeleteDC is to be used to delete the hdc created from CreateCompatibleDC ONLY. All other hdcs should - /// be deleted with DeleteHDC. - /// - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern bool DeleteDC(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern IntPtr CreateDC(string lpszDriverName, string lpszDeviceName, string lpszOutput, HandleRef /*DEVMODE*/ lpInitData); - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern IntPtr CreateIC(string lpszDriverName, string lpszDeviceName, string lpszOutput, HandleRef /*DEVMODE*/ lpInitData); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr CreateCompatibleDC(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern int SaveDC(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern bool RestoreDC(HandleRef hDC, int nSavedDC); - - [DllImport(ExternDll.User32, SetLastError = true, ExactSpelling = true)] - public static extern IntPtr WindowFromDC(HandleRef hDC); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "OffsetViewportOrgEx", CharSet = CharSet.Auto)] - public static extern bool IntOffsetViewportOrgEx(HandleRef hDC, int nXOffset, int nYOffset, [In, Out] IntNativeMethods.POINT point); - - public static bool OffsetViewportOrgEx(HandleRef hDC, int nXOffset, int nYOffset, [In, Out] IntNativeMethods.POINT point) - { - bool retVal = IntOffsetViewportOrgEx(hDC, nXOffset, nYOffset, point); - DbgUtil.AssertWin32(retVal, "OffsetViewportOrgEx([hdc=0x{0:X8}], dx=[{1}], dy=[{2}], [out pPoint]) failed.", hDC.Handle, nXOffset, nYOffset); - return retVal; - } - - // Region. - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "CombineRgn", CharSet = CharSet.Auto)] - public static extern IntNativeMethods.RegionFlags IntCombineRgn(HandleRef hRgnDest, HandleRef hRgnSrc1, HandleRef hRgnSrc2, RegionCombineMode combineMode); - - public static IntNativeMethods.RegionFlags CombineRgn(HandleRef hRgnDest, HandleRef hRgnSrc1, HandleRef hRgnSrc2, RegionCombineMode combineMode) - { - Debug.Assert(hRgnDest.Wrapper != null && hRgnDest.Handle != IntPtr.Zero, "Destination region is invalid"); - Debug.Assert(hRgnSrc1.Wrapper != null && hRgnSrc1.Handle != IntPtr.Zero, "Source region 1 is invalid"); - Debug.Assert(hRgnSrc2.Wrapper != null && hRgnSrc2.Handle != IntPtr.Zero, "Source region 2 is invalid"); - - if (hRgnDest.Wrapper == null || hRgnSrc1.Wrapper == null || hRgnSrc2.Wrapper == null) - { - return IntNativeMethods.RegionFlags.ERROR; - } - - // Note: CombineRgn can return Error when no regions are combined, this is not an error condition. - return IntCombineRgn(hRgnDest, hRgnSrc1, hRgnSrc2, combineMode); - } - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "GetClipRgn", CharSet = CharSet.Auto)] - public static extern int IntGetClipRgn(HandleRef hDC, HandleRef hRgn); - - public static int GetClipRgn(HandleRef hDC, HandleRef hRgn) - { - int retVal = IntGetClipRgn(hDC, hRgn); - DbgUtil.AssertWin32(retVal != -1, "IntGetClipRgn([hdc=0x{0:X8}], [hRgn]) failed.", hDC.Handle); - return retVal; - } - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "SelectClipRgn", CharSet = CharSet.Auto)] - public static extern IntNativeMethods.RegionFlags IntSelectClipRgn(HandleRef hDC, HandleRef hRgn); - - public static IntNativeMethods.RegionFlags SelectClipRgn(HandleRef hDC, HandleRef hRgn) - { - IntNativeMethods.RegionFlags result = IntSelectClipRgn(hDC, hRgn); - DbgUtil.AssertWin32(result != IntNativeMethods.RegionFlags.ERROR, "SelectClipRgn([hdc=0x{0:X8}], [hRegion=0x{1:X8}]) failed.", hDC.Handle, hRgn.Handle); - return result; - } - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "GetRgnBox", CharSet = CharSet.Auto)] - public static extern IntNativeMethods.RegionFlags IntGetRgnBox(HandleRef hRgn, [In, Out] ref IntNativeMethods.RECT clipRect); - - public static IntNativeMethods.RegionFlags GetRgnBox(HandleRef hRgn, [In, Out] ref IntNativeMethods.RECT clipRect) - { - IntNativeMethods.RegionFlags result = IntGetRgnBox(hRgn, ref clipRect); - DbgUtil.AssertWin32(result != IntNativeMethods.RegionFlags.ERROR, "GetRgnBox([hRegion=0x{0:X8}], [out rect]) failed.", hRgn.Handle); - return result; - } - - // Font. - - [DllImport(ExternDll.Gdi32, SetLastError = true, CharSet = CharSet.Unicode)] - public static extern IntPtr CreateFontIndirect(ref SafeNativeMethods.LOGFONT lf); - - // Common. - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true)] - public static extern bool DeleteObject(HandleRef hObject); - - [DllImport(ExternDll.Gdi32, SetLastError = true, ExactSpelling = true, EntryPoint = "GetCurrentObject", CharSet = CharSet.Auto)] - public static extern IntPtr IntGetCurrentObject(HandleRef hDC, int uObjectType); - - public static IntPtr GetCurrentObject(HandleRef hDC, int uObjectType) - { - IntPtr hGdiObj = IntGetCurrentObject(hDC, uObjectType); - // If the selected object is a region the return value is HGI_ERROR on failure. - DbgUtil.AssertWin32(hGdiObj != IntPtr.Zero, "GetObject(hdc=[0x{0:X8}], type=[{1}]) failed.", hDC, uObjectType); - return hGdiObj; - } - } -} diff --git a/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegion.cs b/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegion.cs index b58ef48..7133753 100644 --- a/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegion.cs +++ b/src/libraries/System.Drawing.Common/src/misc/GDI/WindowsRegion.cs @@ -90,15 +90,15 @@ namespace System.Drawing.Internal /// Combines region1 & region2 into this region. The regions cannot be null. The three regions need not be /// distinct. For example, the sourceRgn1 can equal this region. /// - public IntNativeMethods.RegionFlags CombineRegion(WindowsRegion region1, WindowsRegion region2, RegionCombineMode mode) + public Interop.RegionType CombineRegion(WindowsRegion region1, WindowsRegion region2, Interop.Gdi32.CombineMode mode) { - return IntUnsafeNativeMethods.CombineRgn(new HandleRef(this, HRegion), new HandleRef(region1, region1.HRegion), new HandleRef(region2, region2.HRegion), mode); + return Interop.Gdi32.CombineRgn(new HandleRef(this, HRegion), new HandleRef(region1, region1.HRegion), new HandleRef(region2, region2.HRegion), mode); } private void CreateRegion(Rectangle rect) { Debug.Assert(_nativeHandle == IntPtr.Zero, "nativeHandle should be null, we're leaking handle"); - _nativeHandle = IntSafeNativeMethods.CreateRectRgn(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); + _nativeHandle = Interop.Gdi32.CreateRectRgn(rect.X, rect.Y, rect.X + rect.Width, rect.Y + rect.Height); _ownHandle = true; } @@ -112,7 +112,7 @@ namespace System.Drawing.Internal if (_ownHandle) { - IntUnsafeNativeMethods.DeleteObject(new HandleRef(this, _nativeHandle)); + Interop.Gdi32.DeleteObject(new HandleRef(this, _nativeHandle)); } _nativeHandle = IntPtr.Zero; @@ -143,8 +143,8 @@ namespace System.Drawing.Internal return new Rectangle(-int.MaxValue, -int.MaxValue, int.MaxValue, int.MaxValue); } - var rect = new IntNativeMethods.RECT(); - IntUnsafeNativeMethods.GetRgnBox(new HandleRef(this, _nativeHandle), ref rect); + var rect = new Interop.Gdi32.RECT(); + Interop.Gdi32.GetRgnBox(new HandleRef(this, _nativeHandle), ref rect); return new Rectangle(new Point(rect.left, rect.top), rect.Size); } } -- 2.7.4