-
Notifications
You must be signed in to change notification settings - Fork 16
/
TestForm.cs
284 lines (241 loc) · 8.7 KB
/
TestForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
using FluentDragDrop;
using FluentDragDrop.Effects;
using FluentTransitions;
using System;
using System.Drawing;
using System.Drawing.Imaging;
using System.Linq;
using System.Windows.Forms;
namespace FluentDragDropExample
{
public partial class TestForm : Form
{
private bool _listCompatibilityTraditionalMouseDown;
public TestForm()
{
InitializeComponent();
}
private void picNoPreview_MouseDown(object sender, MouseEventArgs e)
{
var pic = (PictureBox)sender;
pic.InitializeDragAndDrop()
.Copy()
.Immediately()
.WithData(pic.Image)
.WithoutPreview()
.To(PreviewBoxes, (target, data) => target.Image = data);
}
#region ImmediateUsage
private void picControlPreviewBehindCursor_MouseDown(object sender, MouseEventArgs e)
{
var pic = (PictureBox)sender;
pic.InitializeDragAndDrop()
.Copy()
.Immediately()
.WithData(pic.Image)
.WithPreview().BehindCursor()
.To(PreviewBoxes, (target, data) => target.Image = data);
// Copy(), Move() or Link() to define allowed effects
// Immediately() or OnMouseMove() for deferred start on mouse move
// WithData() to pass any object you like
// WithPreview() to define your preview and how it should behave
// BehindCursor() or RelativeToCursor() to define the preview placement
// To() to define target controls and how the dragged data should be used on drop
}
#endregion
private void picCustomPreviewWindowsExplorerStyle_MouseDown(object sender, MouseEventArgs e)
{
var pic = (PictureBox)sender;
// Image: Custom
// Drag style: Like Windows Explorer
pic.InitializeDragAndDrop()
.Copy()
.OnMouseMove()
.WithData(() => pic.Image)
.WithPreview(() => Grayscale((Bitmap)pic.Image)).LikeWindowsExplorer()
.To(PreviewBoxes, (target, data) => target.Image = data);
}
private void picUpdatingPreviewRelativeToCursor_MouseDown(object sender, MouseEventArgs e)
{
var pic = (PictureBox)sender;
pic.InitializeDragAndDrop()
.Copy()
.Immediately()
.WithData(pic.Image)
.WithPreview((img, _) => new UpdatablePreview(img, Control.MousePosition)).RelativeToCursor()
.To(PreviewBoxes, (target, data) => target.Image = data);
}
#region DelayedUsage
private void CountryList_MouseDown(object sender, MouseEventArgs e)
{
var source = (ListView)sender;
var target = source.Equals(listLeft) ? listRight : listLeft;
source.InitializeDragAndDrop()
.Move()
.OnMouseMove()
.If(() => source.SelectedIndices.Count > 0)
.WithData(() => source.SelectedItems.OfType<ListViewItem>().ToArray())
.WithPreview((_, data) => RenderPreview(data)).BehindCursor()
.To(target, MoveItems);
}
#endregion
private void linkCompatibilityBrowser_MouseDown(object sender, MouseEventArgs e)
{
linkCompatibilityBrowser.InitializeDragAndDrop()
.Link()
.Immediately()
.WithData("https://twitter.com/waescher");
}
private void listCompatibilityFluent_MouseDown(object sender, MouseEventArgs e)
{
listCompatibilityFluent.InitializeDragAndDrop()
.Copy()
.OnMouseMove()
.If(() => listCompatibilityFluent.SelectedItems.OfType<ListViewItem>().ToArray().Any())
.WithData(() => listCompatibilityFluent.SelectedItems.OfType<ListViewItem>().ToArray())
.WithPreview((_, data) => RenderPreview(data)).BehindCursor();
//.To(listCompatibilityTarget, CopyItems) -> doubles items if used, because listCompatibilityTarget handles the drop input already
}
private void listCompatibilityTraditional_MouseDown(object sender, MouseEventArgs e)
{
_listCompatibilityTraditionalMouseDown = true;
}
private void listCompatibilityTraditional_MouseUp(object sender, MouseEventArgs e)
{
_listCompatibilityTraditionalMouseDown = false;
}
private void listCompatibilityTraditional_MouseMove(object sender, MouseEventArgs e)
{
if (!_listCompatibilityTraditionalMouseDown)
return;
_listCompatibilityTraditionalMouseDown = false;
if (listCompatibilityTraditional.SelectedItems.Count == 0)
return;
var newItems = listCompatibilityTraditional.SelectedItems.OfType<ListViewItem>().ToArray();
listCompatibilityTraditional.DoDragDrop(newItems, DragDropEffects.Copy | DragDropEffects.Move);
}
private void listCompatibilityTarget_DragEnter(object sender, DragEventArgs e)
{
e.Effect = DragDropEffects.Copy;
}
private void listCompatibilityTarget_DragDrop(object sender, DragEventArgs e)
{
var items = e.Data.GetData(typeof(ListViewItem[])) as ListViewItem[];
if (items != null)
{
var newItems = items.Select(i => new ListViewItem { Text = i.Text, ImageIndex = i.ImageIndex }).ToArray();
listCompatibilityTarget.Items.AddRange(newItems);
}
}
private void picDragOverHook_MouseDown(object sender, MouseEventArgs e)
{
picDragOverHook.InitializeDragAndDrop()
.Copy()
.OnMouseMove()
.WithData(() => picDragOverHook.Image);
}
private void picDragOverHookGiveFeedback_MouseDown(object sender, MouseEventArgs e)
{
picDragOverHookGiveFeedback.InitializeDragAndDrop()
.Copy()
.OnMouseMove()
.WithData(() => picDragOverHookGiveFeedback.Image)
.WithoutMouseHooks();
}
private void MoveItems(ListView targetListView, ListViewItem[] draggedItems)
{
var newItems = draggedItems.Select(i => new ListViewItem { Text = i.Text, ImageIndex = i.ImageIndex }).ToArray();
targetListView.Items.AddRange(newItems);
var sourceListView = draggedItems[0].ListView;
foreach (var item in draggedItems)
sourceListView.Items.Remove(item);
}
private Bitmap Grayscale(Bitmap image)
{
var result = new Bitmap(image.Width, image.Height);
using (var graphics = Graphics.FromImage(result))
{
var colorMatrix = new ColorMatrix(new float[][]
{
new float[] {.3f, .3f, .3f, 0, 0},
new float[] {.59f, .59f, .59f, 0, 0},
new float[] {.11f, .11f, .11f, 0, 0},
new float[] {0, 0, 0, 1, 0},
new float[] {0, 0, 0, 0, 1}
});
using (var attributes = new ImageAttributes())
{
attributes.SetColorMatrix(colorMatrix);
graphics.DrawImage(image, new Rectangle(0, 0, result.Width, result.Height), 0, 0, image.Width, image.Height, GraphicsUnit.Pixel, attributes);
}
}
return result;
}
private Bitmap RenderPreview(ListViewItem[] items)
{
var itemHeight = 23;
var image = new Bitmap(120, itemHeight * items.Length);
var borderBounds = new Rectangle(Point.Empty, image.Size);
borderBounds.Width--;
borderBounds.Height--;
using (var graphics = Graphics.FromImage(image))
{
graphics.Clear(Color.White);
graphics.DrawRectangle(Pens.Black, borderBounds);
using (var format = new StringFormat())
{
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
format.FormatFlags = StringFormatFlags.NoWrap;
format.Trimming = StringTrimming.EllipsisCharacter;
for (int i = 0; i < items.Length; i++)
{
var itemY = itemHeight * i;
var itemImage = imlSmall.Images[items[i].ImageIndex];
var imagePadding = (itemHeight - itemImage.Height) / 2;
graphics.DrawImage(itemImage, new Point(imagePadding, itemY + imagePadding));
var textX = itemImage.Width + imagePadding;
var textBounds = new Rectangle(textX, itemY, image.Width - textX, itemHeight);
graphics.DrawString(items[i].Text, listLeft.Font, Brushes.Black, textBounds, format);
}
}
}
return image;
}
private PictureBox[] PreviewBoxes => new[] { picNoPreview, picControlPreviewBehindCursor, picCustomPreviewWindowsExplorerStyle, picUpdatingPreviewRelativeToCursor, picEmpty1, picEmpty2, picEmpty3, picEmpty4 };
private void picReturnOnCancel_MouseDown(object sender, MouseEventArgs e)
{
picReturnOnCancel
.InitializeDragAndDrop()
.Move()
.Immediately()
.WithData("")
.WithPreview().RelativeToCursor()
.FadeInOnStart()
.ReturnToStartOnCancel()
.WithDropEffects(new MorphToTargetEffect(), new FlashSourceControlEffect())
.To(picEffectsTarget, null);
}
internal class FlashSourceControlEffect : IEffect
{
public void Start(IEffect.Arguments arguments)
{
if (arguments?.TargetControl is null)
throw new ArgumentNullException($"Target control cannot be null for this {nameof(FlashSourceControlEffect)}");
Transition
.With(arguments.TargetControl, nameof(arguments.TargetControl.BackColor), Color.LightSeaGreen)
.Bounce(TimeSpan.FromSeconds(1.2));
}
}
private void picTransparency1_MouseDown(object sender, MouseEventArgs e)
{
var pic = (PictureBox)sender;
pic.InitializeDragAndDrop()
.Move()
.Immediately()
.WithData(pic.Image)
.WithPreview(() => (Bitmap)pic.Image).RelativeToCursor()
.To(picTransparencyDrop, (target, image) => target.Image = image);
}
}
}