Skip to content

Commit b428cde

Browse files
authored
Merge pull request #10544 from JeremyKuhne/sharedclipboard
Use shared clipboard code
2 parents b0f8705 + b33ea57 commit b428cde

File tree

108 files changed

+2926
-10266
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

108 files changed

+2926
-10266
lines changed

Microsoft.Dotnet.Wpf.sln

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -270,6 +270,7 @@ Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "eng", "eng", "{92968783-200
270270
eng\restore-toolset.ps1 = eng\restore-toolset.ps1
271271
Restore.cmd = Restore.cmd
272272
start-vs.cmd = start-vs.cmd
273+
eng\Version.Details.xml = eng\Version.Details.xml
273274
eng\Versions.props = eng\Versions.props
274275
EndProjectSection
275276
EndProject

eng/Version.Details.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,4 +194,4 @@
194194
<Sha>df0a52402aaf54eae5b1c0f848c8d3e7f788116f</Sha>
195195
</Dependency>
196196
</ToolsetDependencies>
197-
</Dependencies>
197+
</Dependencies>

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,4 @@
138138
<PropertyGroup>
139139
<MicrosoftDotNetWpfDncEngVersion>10.0.0-preview.4.25168.4</MicrosoftDotNetWpfDncEngVersion>
140140
</PropertyGroup>
141-
</Project>
141+
</Project>

src/Microsoft.DotNet.Wpf/src/Extensions/PresentationFramework-SystemDrawing/SystemDrawingExtension.cs

Lines changed: 41 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
// Licensed to the .NET Foundation under one or more agreements.
1+
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4+
#nullable enable
5+
46
// Description: Helper methods for code that uses types from System.Drawing.
57

68
using System;
@@ -15,61 +17,30 @@
1517
namespace MS.Internal
1618
{
1719
//FxCop can't tell that this class is instantiated via reflection, so suppress the FxCop warning.
18-
[SuppressMessage("Microsoft.Performance","CA1812:AvoidUninstantiatedInternalClasses")]
20+
[SuppressMessage("Microsoft.Performance", "CA1812:AvoidUninstantiatedInternalClasses")]
1921
internal class SystemDrawingExtension : SystemDrawingExtensionMethods
2022
{
21-
// return true if the data is a bitmap
22-
internal override bool IsBitmap(object data)
23-
{
24-
return data is Bitmap;
25-
}
23+
internal override bool IsBitmap(object? data) => data is Bitmap;
2624

27-
// return true if the data is an Image
28-
internal override bool IsImage(object data)
29-
{
30-
return data is Image;
31-
}
25+
internal override bool IsImage(object? data) => data is Image;
3226

33-
// return true if the data is a graphics metafile
34-
internal override bool IsMetafile(object data)
35-
{
36-
return data is Metafile;
37-
}
27+
internal override bool IsMetafile(object? data) => data is Metafile;
3828

39-
// return the handle from a metafile
40-
internal override IntPtr GetHandleFromMetafile(Object data)
29+
internal override nint GetHandleFromMetafile(object? data) => data switch
4130
{
42-
IntPtr hMetafile = IntPtr.Zero;
43-
Metafile metafile = data as Metafile;
31+
Metafile metafile => metafile.GetHenhmetafile(),
32+
_ => 0
33+
};
4434

45-
if (metafile != null)
46-
{
47-
// Get the Windows handle from the metafile object.
48-
hMetafile = metafile.GetHenhmetafile();
49-
}
35+
internal override object GetMetafileFromHemf(nint hMetafile) => new Metafile(hMetafile, deleteEmf: false);
5036

51-
return hMetafile;
52-
}
37+
internal override object? GetBitmap(object? data) => GetBitmapImpl(data);
5338

54-
// Get the metafile from the handle of the enhanced metafile.
55-
internal override Object GetMetafileFromHemf(IntPtr hMetafile)
39+
internal override nint GetHBitmap(object? data, out int width, out int height)
5640
{
57-
return new Metafile(hMetafile, false);
58-
}
41+
Bitmap? bitmapData = GetBitmapImpl(data);
5942

60-
// Get a bitmap from the given data (either BitmapSource or Bitmap)
61-
internal override object GetBitmap(object data)
62-
{
63-
return GetBitmapImpl(data);
64-
}
65-
66-
// Get a bitmap handle from the given data (either BitmapSource or Bitmap)
67-
// Also return its width and height.
68-
internal override IntPtr GetHBitmap(object data, out int width, out int height)
69-
{
70-
Bitmap bitmapData = GetBitmapImpl(data);
71-
72-
if (bitmapData == null)
43+
if (bitmapData is null)
7344
{
7445
width = height = 0;
7546
return IntPtr.Zero;
@@ -83,54 +54,48 @@ internal override IntPtr GetHBitmap(object data, out int width, out int height)
8354
return bitmapData.GetHbitmap();
8455
}
8556

86-
// Get a bitmap handle from a Bitmap
87-
internal override IntPtr GetHBitmapFromBitmap(object data)
88-
{
89-
Bitmap bitmap = data as Bitmap;
90-
return (bitmap != null) ? bitmap.GetHbitmap() : IntPtr.Zero;
91-
}
57+
internal override nint GetHBitmapFromBitmap(object? data) => data is Bitmap bitmap ? bitmap.GetHbitmap() : 0;
9258

93-
// Convert a metafile to HBitmap
94-
internal override IntPtr ConvertMetafileToHBitmap(IntPtr handle)
59+
internal override nint ConvertMetafileToHBitmap(nint handle)
9560
{
96-
Metafile metafile = new Metafile(handle, false);
61+
Metafile metafile = new(handle, deleteEmf: false);
9762

9863
// Initialize the bitmap size to render the metafile.
9964
int bitmapheight = metafile.Size.Height;
100-
int bitmapwidth = metafile.Size.Width;
65+
int bitmapwidth = metafile.Size.Width;
10166

10267
// We use System.Drawing to render metafile into the bitmap.
10368
Bitmap bmp = new Bitmap(bitmapwidth, bitmapheight);
10469
Graphics graphics = Graphics.FromImage(bmp);
105-
// graphics.FillRectangle(new System.Drawing.SolidBrush(System.Drawing.Color.White), 0, 0, bitmapwidth, bitmapheight);
10670
graphics.DrawImage(metafile, 0, 0, bitmapwidth, bitmapheight);
10771

10872
return bmp.GetHbitmap();
10973
}
11074

111-
// return a stream for the ExifUserComment in the given Gif
11275
internal override Stream GetCommentFromGifStream(Stream stream)
11376
{
114-
// Read the GIF header ...
115-
Bitmap img = new Bitmap(stream);
116-
// Read the comment as that is where the ISF is stored...
117-
// for reference the tag is PropertyTagExifUserComment [0x9286] or 37510 (int)
118-
PropertyItem piComment = img.GetPropertyItem(37510);
119-
return new MemoryStream(piComment.Value);
77+
// Read the GIF header
78+
Bitmap bitmap = new(stream);
79+
80+
// Read the comment as that is where the ISF is stored.
81+
// For reference the tag is PropertyTagExifUserComment [0x9286] or 37510 (int)
82+
PropertyItem? piComment = bitmap.GetPropertyItem(37510);
83+
84+
// Want to make this throw in the same way it used to for now. It would be good
85+
// to redo this to return null and handle the null case in the various callers.
86+
return new MemoryStream(piComment!.Value!);
12087
}
12188

122-
// write a metafile stream to the output stream in PNG format
12389
internal override void SaveMetafileToImageStream(MemoryStream metafileStream, Stream imageStream)
12490
{
125-
Metafile metafile = new Metafile(metafileStream);
91+
Metafile metafile = new(metafileStream);
12692
metafile.Save(imageStream, ImageFormat.Png);
12793
}
12894

12995
// Get a bitmap from the given data (either BitmapSource or Bitmap)
130-
private static Bitmap GetBitmapImpl(object data)
96+
private static Bitmap? GetBitmapImpl(object? data)
13197
{
132-
BitmapSource bitmapSource = data as BitmapSource;
133-
if (bitmapSource != null)
98+
if (data is BitmapSource bitmapSource)
13499
{
135100
// Convert BitmapSource to System.Drawing.Bitmap to get Win32 HBITMAP.
136101
BitmapEncoder bitmapEncoder;
@@ -151,23 +116,18 @@ private static Bitmap GetBitmapImpl(object data)
151116
}
152117
}
153118

154-
//returns bitmap snapshot of selected area
155-
//this code takes a BitmapImage and converts it to a Bitmap so it can be put on the clipboard
156119
internal override object GetBitmapFromBitmapSource(object source)
157120
{
158121
BitmapSource contentImage = (BitmapSource)source;
159122
int imageWidth = (int)contentImage.Width;
160123
int imageHeight = (int)contentImage.Height;
161124

162-
Bitmap bitmapFinal = new Bitmap(
163-
imageWidth,
164-
imageHeight,
165-
System.Drawing.Imaging.PixelFormat.Format32bppRgb);
125+
Bitmap bitmapFinal = new(imageWidth, imageHeight, PixelFormat.Format32bppRgb);
166126

167127
BitmapData bmData = bitmapFinal.LockBits(
168-
new Rectangle(0, 0, imageWidth, imageHeight),
169-
ImageLockMode.WriteOnly,
170-
System.Drawing.Imaging.PixelFormat.Format32bppRgb);
128+
new Rectangle(0, 0, imageWidth, imageHeight),
129+
ImageLockMode.WriteOnly,
130+
PixelFormat.Format32bppRgb);
171131

172132
FormatConvertedBitmap formatConverter = new FormatConvertedBitmap();
173133
formatConverter.BeginInit();
@@ -176,10 +136,10 @@ internal override object GetBitmapFromBitmapSource(object source)
176136
formatConverter.EndInit();
177137

178138
formatConverter.CopyPixels(
179-
new Int32Rect(0, 0, imageWidth, imageHeight),
180-
bmData.Scan0,
181-
bmData.Stride * (bmData.Height - 1) + (bmData.Width * 4),
182-
bmData.Stride);
139+
new Int32Rect(0, 0, imageWidth, imageHeight),
140+
bmData.Scan0,
141+
bmData.Stride * (bmData.Height - 1) + (bmData.Width * 4),
142+
bmData.Stride);
183143

184144
bitmapFinal.UnlockBits(bmData);
185145

src/Microsoft.DotNet.Wpf/src/PresentationCore/GlobalSuppressions.cs

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,3 +26,9 @@
2626
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataFormats.WaveAudio")]
2727
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataFormats.Xaml")]
2828
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataFormats.XamlPackage")]
29+
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataObject.CopyingEvent")]
30+
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataObject.PastingEvent")]
31+
[assembly: SuppressMessage("Style", "IDE1006:Naming Styles", Justification = "Public API", Scope = "member", Target = "~F:System.Windows.DataObject.SettingDataEvent")]
32+
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.Clipboard.IsCurrent(System.Windows.IDataObject)~System.Boolean")]
33+
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.DataObject.System#Runtime#InteropServices#ComTypes#IDataObject#EnumFormatEtc(System.Runtime.InteropServices.ComTypes.DATADIR)~System.Runtime.InteropServices.ComTypes.IEnumFORMATETC")]
34+
[assembly: SuppressMessage("Usage", "CA2201:Do not raise reserved exception types", Justification = "Compat", Scope = "member", Target = "~M:System.Windows.OleServicesContext.SetDispatcherThread")]

src/Microsoft.DotNet.Wpf/src/PresentationCore/GlobalUsings.cs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,17 @@
77

88
global using DataFormatsCore = System.Private.Windows.Ole.DataFormatsCore<
99
System.Windows.DataFormat>;
10+
#pragma warning disable IDE0005 // Using directive is unnecessary.
11+
global using DragDropHelper = System.Private.Windows.Ole.DragDropHelper<
12+
System.Windows.Ole.WpfOleServices,
13+
System.Windows.DataFormat>;
14+
#pragma warning restore IDE0005 // Using directive is unnecessary.
15+
global using ClipboardCore = System.Private.Windows.Ole.ClipboardCore<
16+
System.Windows.Ole.WpfOleServices>;
17+
global using Composition = System.Private.Windows.Ole.Composition<
18+
System.Windows.Ole.WpfOleServices,
19+
System.Windows.Nrbf.WpfNrbfSerializer,
20+
System.Windows.DataFormat>;
1021

1122
global using SR = MS.Internal.PresentationCore.SR;
1223

Lines changed: 42 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,107 +1,72 @@
11
// Licensed to the .NET Foundation under one or more agreements.
22
// The .NET Foundation licenses this file to you under the MIT license.
33

4-
//
5-
//
4+
#nullable enable
5+
66
// Description: Helper methods for code that uses types from System.Drawing.
7-
//
87

98
using System.IO;
9+
using Windows.Win32.Graphics.Gdi;
1010

1111
namespace MS.Internal
1212
{
1313
internal static class SystemDrawingHelper
1414
{
15-
// return true if the data is a bitmap
16-
internal static bool IsBitmap(object data)
17-
{
18-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing();
19-
return (extensions != null) ? extensions.IsBitmap(data) : false;
20-
}
15+
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsBitmap(object?)"/>
16+
internal static bool IsBitmap(object? data) =>
17+
AssemblyHelper.ExtensionsForSystemDrawing()?.IsBitmap(data) ?? false;
2118

22-
// return true if the data is an Image
23-
internal static bool IsImage(object data)
24-
{
25-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing();
26-
return (extensions != null) ? extensions.IsImage(data) : false;
27-
}
19+
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsImage(object?)"/>
20+
internal static bool IsImage(object? data) =>
21+
AssemblyHelper.ExtensionsForSystemDrawing()?.IsImage(data) ?? false;
2822

29-
// return true if the data is a graphics metafile
30-
internal static bool IsMetafile(object data)
31-
{
32-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing();
33-
return (extensions != null) ? extensions.IsMetafile(data) : false;
34-
}
23+
/// <inheritdoc cref="SystemDrawingExtensionMethods.IsMetafile(object?)"/>
24+
internal static bool IsMetafile(object? data) =>
25+
AssemblyHelper.ExtensionsForSystemDrawing()?.IsMetafile(data) ?? false;
3526

36-
// return the handle from a metafile
37-
internal static IntPtr GetHandleFromMetafile(Object data)
38-
{
39-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing();
40-
return (extensions != null) ? extensions.GetHandleFromMetafile(data) : IntPtr.Zero;
41-
}
27+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHandleFromMetafile(object?)"/>
28+
internal static HENHMETAFILE GetHandleFromMetafile(object? data) =>
29+
(HENHMETAFILE)(AssemblyHelper.ExtensionsForSystemDrawing()?.GetHandleFromMetafile(data) ?? 0);
4230

43-
// Get the metafile from the handle of the enhanced metafile.
44-
internal static Object GetMetafileFromHemf(IntPtr hMetafile)
45-
{
46-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
47-
return extensions?.GetMetafileFromHemf(hMetafile);
48-
}
31+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetMetafileFromHemf(nint)"/>
32+
internal static object? GetMetafileFromHemf(HENHMETAFILE hMetafile) =>
33+
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetMetafileFromHemf(hMetafile);
4934

50-
// Get a bitmap from the given data (either BitmapSource or Bitmap)
51-
internal static object GetBitmap(object data)
52-
{
53-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
54-
return extensions?.GetBitmap(data);
55-
}
35+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetBitmap(object?)"/>
36+
internal static object? GetBitmap(object? data) =>
37+
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetBitmap(data);
5638

57-
// Get a bitmap handle from the given data (either BitmapSource or Bitmap)
58-
// Also return its width and height.
59-
internal static IntPtr GetHBitmap(object data, out int width, out int height)
39+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHBitmap(object?, out int, out int)"/>
40+
internal static HBITMAP GetHBitmap(object? data, out int width, out int height)
6041
{
61-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
62-
if (extensions != null)
42+
var extensions = AssemblyHelper.ExtensionsForSystemDrawing(force: true);
43+
if (extensions is not null)
6344
{
64-
return extensions.GetHBitmap(data, out width, out height);
45+
return (HBITMAP)extensions.GetHBitmap(data, out width, out height);
6546
}
6647

6748
width = height = 0;
68-
return IntPtr.Zero;
49+
return HBITMAP.Null;
6950
}
7051

71-
// Get a bitmap handle from a Bitmap
72-
internal static IntPtr GetHBitmapFromBitmap(object data)
73-
{
74-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing();
75-
return (extensions != null) ? extensions.GetHBitmapFromBitmap(data) : IntPtr.Zero;
76-
}
52+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetHBitmapFromBitmap(object?)"/>
53+
internal static HBITMAP GetHBitmapFromBitmap(object? data) =>
54+
(HBITMAP)(AssemblyHelper.ExtensionsForSystemDrawing()?.GetHBitmapFromBitmap(data) ?? HBITMAP.Null);
7755

78-
// Convert a metafile to HBitmap
79-
internal static IntPtr ConvertMetafileToHBitmap(IntPtr handle)
80-
{
81-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
82-
return (extensions != null) ? extensions.ConvertMetafileToHBitmap(handle) : IntPtr.Zero;
83-
}
56+
/// <inheritdoc cref="SystemDrawingExtensionMethods.ConvertMetafileToHBitmap(nint)"/>
57+
internal static HBITMAP ConvertMetafileToHBitmap(HENHMETAFILE handle) =>
58+
(HBITMAP)(AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.ConvertMetafileToHBitmap(handle) ?? HBITMAP.Null);
8459

85-
// return a stream for the ExifUserComment in the given Gif
86-
internal static Stream GetCommentFromGifStream(Stream stream)
87-
{
88-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
89-
return extensions?.GetCommentFromGifStream(stream);
90-
}
60+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetCommentFromGifStream(Stream)"/>
61+
internal static Stream? GetCommentFromGifStream(Stream stream) =>
62+
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetCommentFromGifStream(stream);
9163

92-
// write a metafile stream to the output stream in PNG format
93-
internal static void SaveMetafileToImageStream(MemoryStream metafileStream, Stream imageStream)
94-
{
95-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
96-
extensions?.SaveMetafileToImageStream(metafileStream, imageStream);
97-
}
64+
/// <inheritdoc cref="SystemDrawingExtensionMethods.SaveMetafileToImageStream(MemoryStream, Stream)"/>
65+
internal static void SaveMetafileToImageStream(MemoryStream metafileStream, Stream imageStream) =>
66+
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.SaveMetafileToImageStream(metafileStream, imageStream);
9867

99-
//returns bitmap snapshot of selected area
100-
//this code takes a BitmapImage and converts it to a Bitmap so it can be put on the clipboard
101-
internal static object GetBitmapFromBitmapSource(object source)
102-
{
103-
SystemDrawingExtensionMethods extensions = AssemblyHelper.ExtensionsForSystemDrawing(force:true);
104-
return extensions?.GetBitmapFromBitmapSource(source);
105-
}
68+
/// <inheritdoc cref="SystemDrawingExtensionMethods.GetBitmapFromBitmapSource(object)"/>
69+
internal static object? GetBitmapFromBitmapSource(object source) =>
70+
AssemblyHelper.ExtensionsForSystemDrawing(force: true)?.GetBitmapFromBitmapSource(source);
10671
}
10772
}

0 commit comments

Comments
 (0)