1
1
// Licensed to the .NET Foundation under one or more agreements.
2
2
// The .NET Foundation licenses this file to you under the MIT license.
3
3
4
- using System . Runtime . Serialization . Formatters . Binary ;
4
+ using System . Collections ;
5
+ using System . Collections . ObjectModel ;
5
6
using System . ComponentModel ;
6
7
using System . ComponentModel . Design ;
7
8
using System . ComponentModel . Design . Serialization ;
8
- using System . Windows . Forms . Design . Behavior ;
9
9
using System . Drawing ;
10
10
using System . Drawing . Design ;
11
- using System . Collections ;
11
+ using System . Runtime . Serialization . Formatters . Binary ;
12
+ using System . Windows . Forms . Design . Behavior ;
12
13
13
14
namespace System . Windows . Forms . Design ;
14
15
@@ -1382,6 +1383,7 @@ protected void OnMenuCenterSelection(object? sender, EventArgs e)
1382
1383
}
1383
1384
}
1384
1385
1386
+ private ICollection _toBeCopiedComponents ;
1385
1387
/// <summary>
1386
1388
/// Called when the copy menu item is selected.
1387
1389
/// </summary>
@@ -1397,29 +1399,8 @@ protected void OnMenuCopy(object? sender, EventArgs e)
1397
1399
{
1398
1400
Cursor . Current = Cursors . WaitCursor ;
1399
1401
1400
- ICollection selectedComponents = GetCopySelection ( ) ;
1401
-
1402
- selectedComponents = PrependComponentNames ( selectedComponents ) ;
1403
-
1404
- IDesignerSerializationService ? ds = GetService < IDesignerSerializationService > ( ) ;
1405
- Debug . Assert ( ds is not null , "No designer serialization service -- we cannot copy to clipboard" ) ;
1406
- if ( ds is not null )
1407
- {
1408
- object serializationData = ds . Serialize ( selectedComponents ) ;
1409
- using MemoryStream stream = new ( ) ;
1410
- #pragma warning disable SYSLIB0011 // Type or member is obsolete
1411
- new BinaryFormatter ( ) . Serialize ( stream , serializationData ) ;
1412
- #pragma warning restore SYSLIB0011
1413
- stream . Seek ( 0 , SeekOrigin . Begin ) ;
1414
- byte [ ] bytes = stream . GetBuffer ( ) ;
1415
- IDataObject dataObj = new DataObject ( CF_DESIGNER , bytes ) ;
1416
- if ( ! ExecuteSafely ( ( ) => Clipboard . SetDataObject ( dataObj ) , throwOnException : false ) )
1417
- {
1418
- _uiService ? . ShowError ( SR . ClipboardError ) ;
1419
- }
1420
- }
1421
-
1422
- UpdateClipboardItems ( null , null ) ;
1402
+ _toBeCopiedComponents = SelectionService ! . GetSelectedComponents ( ) ;
1403
+ _commandSet . First ( command => command . CommandID == StandardCommands . Paste ) ? . UpdateStatus ( ) ;
1423
1404
}
1424
1405
finally
1425
1406
{
@@ -1843,52 +1824,31 @@ protected void OnMenuPaste(object? sender, EventArgs e)
1843
1824
return ;
1844
1825
}
1845
1826
1846
- bool clipboardOperationSuccessful = ExecuteSafely ( Clipboard . GetDataObject , false , out IDataObject ? dataObj ) ;
1847
-
1848
- if ( clipboardOperationSuccessful )
1827
+ if ( _toBeCopiedComponents is not null && _toBeCopiedComponents . Count != 0 )
1849
1828
{
1850
- ICollection ? components = null ;
1829
+ List < IComponent > ? components = null ;
1851
1830
bool createdItems = false ;
1852
1831
1853
1832
// Get the current number of controls in the Component Tray in the target
1854
1833
ComponentTray ? tray = GetService < ComponentTray > ( ) ;
1855
1834
int numberOfOriginalTrayControls = tray is not null ? tray . Controls . Count : 0 ;
1856
1835
1857
- // We understand two things: CF_DESIGNER, and toolbox items.
1858
- object ? data = dataObj ? . GetData ( CF_DESIGNER ) ;
1859
-
1860
1836
using DesignerTransaction trans = host . CreateTransaction ( SR . CommandSetPaste ) ;
1861
- if ( data is byte [ ] bytes )
1862
- {
1863
- MemoryStream s = new ( bytes ) ;
1864
1837
1865
- // CF_DESIGNER was put on the clipboard by us using the designer serialization service.
1866
- if ( TryGetService ( out IDesignerSerializationService ? ds ) )
1867
- {
1868
- s . Seek ( 0 , SeekOrigin . Begin ) ;
1869
- #pragma warning disable SYSLIB0011 // Type or member is obsolete
1870
- #pragma warning disable CA2300 // Do not use insecure deserializer BinaryFormatter
1871
- object serializationData = new BinaryFormatter ( ) . Deserialize ( s ) ; // CodeQL[SM03722, SM04191] : The operation is essential for the design experience when users are running their own designers they have created. This cannot be achieved without BinaryFormatter
1872
- #pragma warning restore CA2300
1873
- #pragma warning restore SYSLIB0011
1874
- using ( ScaleHelper . EnterDpiAwarenessScope ( DPI_AWARENESS_CONTEXT . DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ) )
1875
- {
1876
- components = ds . Deserialize ( serializationData ) ;
1877
- }
1878
- }
1879
- }
1880
- else if ( TryGetService ( out IToolboxService ? ts ) && ts . IsSupported ( dataObj , host ) )
1838
+ components = DesignerUtils . CopyDragObjects ( new ReadOnlyCollection < IComponent > ( [ .. _toBeCopiedComponents . Cast < IComponent > ( ) ] ) , GetService < IDesignerHost > ( ) ) ;
1839
+
1840
+ if ( _toBeCopiedComponents . Count == components . Count )
1881
1841
{
1882
- // Now check for a toolbox item.
1883
- ToolboxItem ? ti = ts . DeserializeToolboxItem ( dataObj , host ) ;
1884
- if ( ti is not null )
1842
+ int i = 0 ;
1843
+ foreach ( IComponent component in _toBeCopiedComponents )
1885
1844
{
1886
- using ( ScaleHelper . EnterDpiAwarenessScope ( DPI_AWARENESS_CONTEXT . DPI_AWARENESS_CONTEXT_SYSTEM_AWARE ) )
1845
+ if ( component is Control original && components [ i ] is Control duplicate )
1887
1846
{
1888
- components = ti . CreateComponents ( host ) ;
1847
+ duplicate . Parent = original . Parent ;
1848
+ duplicate . Location = new Point ( original . Location . X + 15 , original . Location . Y + 15 ) ;
1889
1849
}
1890
1850
1891
- createdItems = true ;
1851
+ i ++ ;
1892
1852
}
1893
1853
}
1894
1854
@@ -1897,7 +1857,7 @@ protected void OnMenuPaste(object? sender, EventArgs e)
1897
1857
if ( components is not null && components . Count > 0 )
1898
1858
{
1899
1859
// Make copy of Items in Array..
1900
- object [ ] allComponents = new object [ components . Count ] ;
1860
+ IComponent [ ] allComponents = new IComponent [ components . Count ] ;
1901
1861
components . CopyTo ( allComponents , 0 ) ;
1902
1862
1903
1863
List < IComponent > selectComps = [ ] ;
@@ -2170,12 +2130,9 @@ protected void OnMenuPaste(object? sender, EventArgs e)
2170
2130
}
2171
2131
2172
2132
trans . Commit ( ) ;
2133
+ SelectionService ? . SetSelectedComponents ( components ) ;
2173
2134
}
2174
2135
}
2175
- else
2176
- {
2177
- _uiService ? . ShowError ( SR . ClipboardError ) ;
2178
- }
2179
2136
}
2180
2137
finally
2181
2138
{
@@ -3091,28 +3048,7 @@ protected void OnStatusPaste(object? sender, EventArgs e)
3091
3048
}
3092
3049
}
3093
3050
3094
- // Not being inherited. Now look at the contents of the data
3095
- bool clipboardOperationSuccessful = ExecuteSafely ( Clipboard . GetDataObject , false , out IDataObject ? dataObj ) ;
3096
-
3097
- bool enable = false ;
3098
-
3099
- if ( clipboardOperationSuccessful && dataObj is not null )
3100
- {
3101
- if ( dataObj . GetDataPresent ( CF_DESIGNER ) )
3102
- {
3103
- enable = true ;
3104
- }
3105
- else
3106
- {
3107
- // Not ours, check to see if the toolbox service understands this
3108
- if ( TryGetService ( out IToolboxService ? ts ) )
3109
- {
3110
- enable = host is not null ? ts . IsSupported ( dataObj , host ) : ts . IsToolboxItem ( dataObj ) ;
3111
- }
3112
- }
3113
- }
3114
-
3115
- cmd . Enabled = enable ;
3051
+ cmd . Enabled = _toBeCopiedComponents is not null ;
3116
3052
}
3117
3053
3118
3054
private void OnStatusPrimarySelection ( object ? sender , EventArgs e )
0 commit comments