Skip to content

Commit 398f78b

Browse files
lonitraRussKie
authored andcommitted
Convert BitBlt to cswin32 PInvoke (#7446)
* Convert BitBlt to cswin32 PInvoke - Add HDC conversion to scopes - Remove unused BeginPath - NativeMethods.txt
1 parent b8f7d6b commit 398f78b

File tree

12 files changed

+54
-58
lines changed

12 files changed

+54
-58
lines changed

src/System.Windows.Forms.Design/src/System/Windows/Forms/Design/DesignerUtils.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,9 @@
1212
using System.Drawing.Imaging;
1313
using System.Globalization;
1414
using System.Windows.Forms.Design.Behavior;
15+
using Windows.Win32;
1516
using static Interop;
17+
using Gdi = Windows.Win32.Graphics.Gdi;
1618

1719
namespace System.Windows.Forms.Design
1820
{
@@ -418,7 +420,7 @@ public static void GenerateSnapShotWithBitBlt(Control control, ref Image image)
418420
using var destDC = new DeviceContextHdcScope(gDest, applyGraphicsState: false);
419421

420422
// Perform our bitblit operation to push the image into the dest bitmap
421-
Gdi32.BitBlt(
423+
PInvoke.BitBlt(
422424
destDC,
423425
0,
424426
0,
@@ -427,7 +429,7 @@ public static void GenerateSnapShotWithBitBlt(Control control, ref Image image)
427429
controlDC,
428430
0,
429431
0,
430-
Gdi32.ROP.SRCCOPY);
432+
Gdi.ROP_CODE.SRCCOPY);
431433
}
432434

433435
/// <summary>

src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BeginPath.cs

Lines changed: 0 additions & 14 deletions
This file was deleted.

src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.CreateDcScope.cs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Gdi = Windows.Win32.Graphics.Gdi;
6+
57
internal static partial class Interop
68
{
79
internal static partial class Gdi32
@@ -45,9 +47,10 @@ public CreateDcScope(
4547
: CreateDC(lpszDriverName, lpszDeviceName, lpszOutput, lpInitData);
4648
}
4749

48-
public static implicit operator HDC(in CreateDcScope dcScope) => dcScope.HDC;
49-
public static implicit operator HGDIOBJ(in CreateDcScope dcScope) => dcScope.HDC;
50-
public static implicit operator nint(in CreateDcScope dcScope) => dcScope.HDC.Handle;
50+
public static implicit operator HDC(in CreateDcScope scope) => scope.HDC;
51+
public static implicit operator HGDIOBJ(in CreateDcScope scope) => scope.HDC;
52+
public static implicit operator nint(in CreateDcScope scope) => scope.HDC.Handle;
53+
public static implicit operator Gdi.HDC(in CreateDcScope scope) => scope.HDC;
5154

5255
public bool IsNull => HDC.IsNull;
5356

src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.HDC.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Gdi = Windows.Win32.Graphics.Gdi;
6+
57
internal static partial class Interop
68
{
79
internal static partial class Gdi32
@@ -17,6 +19,8 @@ internal static partial class Gdi32
1719
public static implicit operator nint(HDC hdc) => hdc.Handle;
1820
public static explicit operator HDC(nint hdc) => new(hdc);
1921
public static implicit operator HGDIOBJ(HDC hdc) => new(hdc.Handle);
22+
public static implicit operator HDC(Gdi.HDC hdc) => new(hdc.Value);
23+
public static implicit operator Gdi.HDC(HDC hdc) => new(hdc.Handle);
2024

2125
public static bool operator ==(HDC value1, HDC value2) => value1.Handle == value2.Handle;
2226
public static bool operator !=(HDC value1, HDC value2) => value1.Handle != value2.Handle;

src/System.Windows.Forms.Primitives/src/Interop/User32/Interop.GetDcScope.cs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using Gdi = Windows.Win32.Graphics.Gdi;
6+
57
internal static partial class Interop
68
{
79
internal static partial class User32
@@ -51,8 +53,9 @@ public GetDcScope(IntPtr hwnd, IntPtr hrgnClip, DCX flags)
5153

5254
public bool IsNull => HDC.IsNull;
5355

54-
public static implicit operator IntPtr(in GetDcScope dcScope) => dcScope.HDC.Handle;
55-
public static implicit operator Gdi32.HDC(in GetDcScope dcScope) => dcScope.HDC;
56+
public static implicit operator IntPtr(in GetDcScope scope) => scope.HDC.Handle;
57+
public static implicit operator Gdi32.HDC(in GetDcScope scope) => scope.HDC;
58+
public static implicit operator Gdi.HDC(in GetDcScope scope) => scope.HDC;
5659

5760
public void Dispose()
5861
{
Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,14 @@
1-
GetCurrentProcess
1+
BitBlt
2+
DuplicateHandle
3+
GetCurrentActCtx
4+
GetCurrentProcess
5+
GetCurrentProcessId
26
GetCurrentThread
37
GetCurrentThreadId
4-
DuplicateHandle
8+
GetExitCodeThread
9+
GetThreadLocale
510
GlobalAlloc
11+
GlobalFree
612
GlobalLock
7-
GlobalUnlock
8-
GetCurrentProcessId
913
GlobalReAlloc
10-
GlobalFree
11-
GetExitCodeThread
12-
GetThreadLocale
13-
GetCurrentActCtx
14+
GlobalUnlock

src/System.Windows.Forms.Primitives/src/System/Windows/Forms/DeviceContextHdcScope.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
using System.Diagnostics;
77
using System.Drawing;
88
using static Interop;
9+
using Gdi = Windows.Win32.Graphics.Gdi;
910

1011
namespace System.Windows.Forms
1112
{
@@ -211,6 +212,7 @@ public unsafe DeviceContextHdcScope(
211212
}
212213

213214
public static implicit operator Gdi32.HDC(in DeviceContextHdcScope scope) => scope.HDC;
215+
public static implicit operator Gdi.HDC(in DeviceContextHdcScope scope) => scope.HDC;
214216
public static implicit operator nint(in DeviceContextHdcScope scope) => scope.HDC.Handle;
215217

216218
[Conditional("DEBUG")]

src/System.Windows.Forms.Primitives/src/Interop/Gdi32/Interop.BitBlt.cs renamed to src/System.Windows.Forms.Primitives/src/Windows/Win32/PInvoke.BitBlt.cs

Lines changed: 6 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,13 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5-
using System.Runtime.InteropServices;
5+
using Windows.Win32.Foundation;
6+
using Windows.Win32.Graphics.Gdi;
67

7-
internal static partial class Interop
8+
namespace Windows.Win32
89
{
9-
internal static partial class Gdi32
10+
internal static partial class PInvoke
1011
{
11-
[LibraryImport(Libraries.Gdi32, SetLastError = true)]
12-
public static partial BOOL BitBlt(
13-
HDC hdc,
14-
int x,
15-
int y,
16-
int cx,
17-
int cy,
18-
HDC hdcSrc,
19-
int x1,
20-
int y1,
21-
ROP rop);
22-
2312
public static BOOL BitBlt(
2413
IHandle hdc,
2514
int x,
@@ -29,7 +18,7 @@ public static BOOL BitBlt(
2918
HDC hdcSrc,
3019
int x1,
3120
int y1,
32-
ROP rop)
21+
ROP_CODE rop)
3322
{
3423
BOOL result = BitBlt(
3524
(HDC)hdc.Handle,
@@ -54,7 +43,7 @@ public static BOOL BitBlt(
5443
IHandle hdcSrc,
5544
int x1,
5645
int y1,
57-
ROP rop)
46+
ROP_CODE rop)
5847
{
5948
BOOL result = BitBlt(
6049
hdc,

src/System.Windows.Forms/src/System/Windows/Forms/Control.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
using Foundation = Windows.Win32.Foundation;
2222
using static Interop;
2323
using Encoding = System.Text.Encoding;
24+
using Gdi = Windows.Win32.Graphics.Gdi;
2425
using IComDataObject = System.Runtime.InteropServices.ComTypes.IDataObject;
2526

2627
namespace System.Windows.Forms
@@ -5460,7 +5461,7 @@ public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
54605461
// Now BLT the result to the destination bitmap.
54615462
using Graphics destGraphics = Graphics.FromImage(bitmap);
54625463
using var desthDC = new DeviceContextHdcScope(destGraphics, applyGraphicsState: false);
5463-
Gdi32.BitBlt(
5464+
PInvoke.BitBlt(
54645465
desthDC,
54655466
targetBounds.X,
54665467
targetBounds.Y,
@@ -5469,7 +5470,7 @@ public void DrawToBitmap(Bitmap bitmap, Rectangle targetBounds)
54695470
hDc,
54705471
0,
54715472
0,
5472-
Gdi32.ROP.SRCCOPY);
5473+
Gdi.ROP_CODE.SRCCOPY);
54735474
}
54745475

54755476
/// <summary>

src/System.Windows.Forms/src/System/Windows/Forms/ControlPaint.cs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,9 @@
99
using System.Drawing.Drawing2D;
1010
using System.Drawing.Imaging;
1111
using System.Runtime.InteropServices;
12+
using Windows.Win32;
1213
using static Interop;
14+
using Gdi = Windows.Win32.Graphics.Gdi;
1315

1416
namespace System.Windows.Forms
1517
{
@@ -332,12 +334,12 @@ public static IntPtr CreateHBitmapColorMask(Bitmap bitmap, IntPtr monochromeMask
332334

333335
Gdi32.SetBkColor(targetDC, 0x00ffffff); // white
334336
Gdi32.SetTextColor(targetDC, 0x00000000); // black
335-
Gdi32.BitBlt(
337+
PInvoke.BitBlt(
336338
targetDC,
337339
0, 0, size.Width, size.Height,
338340
sourceDC,
339341
0, 0,
340-
(Gdi32.ROP)0x220326); // RasterOp.SOURCE.Invert().AndWith(RasterOp.TARGET).GetRop());
342+
(Gdi.ROP_CODE)0x220326); // RasterOp.SOURCE.Invert().AndWith(RasterOp.TARGET).GetRop());
341343

342344
return (IntPtr)colorMask;
343345
}

0 commit comments

Comments
 (0)