-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathForm2.cs
More file actions
315 lines (277 loc) · 11.7 KB
/
Form2.cs
File metadata and controls
315 lines (277 loc) · 11.7 KB
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace CleanCache
{
public partial class Form2 : Form
{
private string Tipo;
Form1 form;
List<Control> Controles = new List<Control>();
List<Control> SelectedFiles = new List<Control>();
public Form2(Form1 _form, string _tipo)
{
InitializeComponent();
Tipo = _tipo;
form = _form;
Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, Width, Height, 20, 20));
foreach (Control control in Controls)
{
if (control.Tag != null && int.TryParse(control.Tag.ToString(), out int tag)) control.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control.Width, control.Height, tag, tag));
foreach (Control control2 in control.Controls)
{
if (control2.Tag != null && int.TryParse(control2.Tag.ToString(), out tag)) control2.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control2.Width, control2.Height, tag, tag));
foreach (Control control3 in control2.Controls)
{
if (control3.Tag != null && int.TryParse(control3.Tag.ToString(), out tag)) control3.Region = Region.FromHrgn(CreateRoundRectRgn(0, 0, control3.Width, control3.Height, tag, tag));
}
}
}
}
[DllImport("Gdi32.dll", EntryPoint = "CreateRoundRectRgn")]
private static extern IntPtr CreateRoundRectRgn(int nLeftRect, int nTopRect, int nRightRect, int nBottomRect, int nWidthEllipse, int nHeightEllipse);
private void Form2_Load(object sender, EventArgs e)
{
LoadFiles();
}
private async Task LoadFiles()
{
try
{
maintitle.Text = Tipo;
await Task.Delay(250);
int id = 0;
int x = 0;
int y = 0;
foreach (var file in form.DuplicatedFiles[Tipo].Keys)
{
Image preview = null;
if (Tipo == "Imagens")
using (var stream = new FileStream(form.DuplicatedFiles[Tipo][file][0], FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
preview = Image.FromStream(stream).Clone() as Image;
var pic = new PictureBox
{
Name = "picfile" + id,
Parent = listpanel,
Image = Tipo.Equals("Documentos") ? Properties.Resources.paper : Tipo.Equals("Imagens") ? preview ?? Properties.Resources.image__1_ : Tipo.Equals("Vídeos") ? Properties.Resources.video_player : Properties.Resources.folder,
SizeMode = PictureBoxSizeMode.Zoom,
Size = new Size(55, 55),
Location = new Point(37 + (105 * x), 21 + (110 * y) + listpanel.AutoScrollPosition.Y),
Tag = file,
Cursor = Cursors.Hand,
};
pic.Click += FilesClick;
pic.DoubleClick += FilesDoubleClick;
var lbl = new Label
{
Name = "namefile" + id,
Parent = listpanel,
AutoSize = true,
MinimumSize = new Size(85, 17),
MaximumSize = new Size(85, 40),
Location = new Point(24 + (105 * x), 78 + (110 * y) + listpanel.AutoScrollPosition.Y),
ForeColor = Color.DimGray,
Font = new Font("Arial Narrow", 8.25f, FontStyle.Bold),
TextAlign = ContentAlignment.TopCenter,
UseCompatibleTextRendering = true,
Text = Path.GetFileName(form.DuplicatedFiles[Tipo][file][0]),
Tag = file,
Cursor = Cursors.Hand,
};
lbl.Click += FilesClick;
lbl.DoubleClick += FilesDoubleClick;
var check = new CheckBox
{
Name = "checkfile" + id,
Parent = pic,
Tag = file,
AutoSize = true,
BackColor = Color.White,
ForeColor = Color.Indigo,
FlatStyle = FlatStyle.Flat,
Location = new Point(0, 0),
};
check.CheckedChanged += CheckedDoublesFile;
Controles.Add(pic);
Controles.Add(lbl);
Controles.Add(check);
id++;
y += x == 5 ? 1 : 0;
x = x < 5 ? x + 1 : 0;
await Task.Delay(60);
}
}
catch { }
}
private void SelectAllFiles(object sender, EventArgs e)
{
CheckBox check = (CheckBox)sender;
bool state = check.Checked;
foreach (var control in listpanel.Controls)
if (control is PictureBox pic)
if (pic.Controls.Cast<Control>().ToList()[0] is CheckBox _check)
_check.Checked = state;
}
private void CheckedDoublesFile(object sender, EventArgs e)
{
CheckBox check = (CheckBox)sender;
if (check.Checked)
SelectedFiles.Add(check);
else
if (SelectedFiles.Contains(check))
SelectedFiles.Remove(check);
if (SelectedFiles.Count > 0)
{
selectedchecks.Visible = true;
selectedchecks.Text = SelectedFiles.Count + " Selecionado(s)";
cleanallbtn.Visible = true;
selectedchecks.CheckedChanged -= SelectAllFiles;
if (SelectedFiles.Count == form.DuplicatedFiles[Tipo].Keys.Count)
selectedchecks.Checked = true;
else
selectedchecks.Checked = false;
selectedchecks.CheckedChanged += SelectAllFiles;
}
else
{
selectedchecks.Visible = false;
cleanallbtn.Visible = false;
}
}
private void listpanel_Click(object sender, EventArgs e)
{
foreach (var control in listpanel.Controls)
{
if (control is PictureBox pic && pic.BackColor == Color.Thistle)
pic.BackColor = Color.Transparent;
else if (control is Label lbl && lbl.BackColor == Color.Thistle)
lbl.BackColor = Color.Transparent;
}
}
private void FilesClick(object sender, EventArgs e)
{
foreach (var control in listpanel.Controls)
{
if (control is PictureBox _pic && _pic.BackColor == Color.Thistle)
_pic.BackColor = Color.Transparent;
else if (control is Label lbl && lbl.BackColor == Color.Thistle)
lbl.BackColor = Color.Transparent;
}
if (sender is PictureBox pic)
{
pic.BackColor = Color.Thistle;
foreach (var control in listpanel.Controls)
{
if (control is Label lbl && lbl.Name.Equals("namefile" + pic.Name.Replace("picfile", string.Empty)))
lbl.BackColor = Color.Thistle;
}
}
else if (sender is Label lbl)
{
lbl.BackColor = Color.Thistle;
foreach (var control in listpanel.Controls)
{
if (control is PictureBox _pic && _pic.Name.Equals("picfile" + lbl.Name.Replace("namefile", string.Empty)))
_pic.BackColor = Color.Thistle;
}
}
}
private void FilesDoubleClick(object sender, EventArgs e)
{
string tag = null;
if (sender is PictureBox pic)
tag = pic.Tag.ToString();
else if (sender is Label lbl)
tag = lbl.Tag.ToString();
var _form = new Form3(form, Tipo, tag);
_form.FormClosing += Form3Closing;
_form.ShowDialog();
}
private void Form3Closing(object sender, FormClosingEventArgs e)
{
UpdateList();
}
private void ButtonsClick(object sender, EventArgs e)
{
Button btn = (Button)sender;
switch (btn.Name.ToLower())
{
case "cleanallbtn":
opcwindow.Visible = !opcwindow.Visible;
break;
case "deleteclonesbtn":
ClearFiles();
opcwindow.Visible = false;
break;
case "deleteallbtn":
ClearFiles(true);
opcwindow.Visible = false;
break;
case "closebtn":
Close();
break;
}
}
private void UpdateList()
{
selectedchecks.Checked = false;
selectedchecks.Visible = false;
opcwindow.Visible = false;
foreach (var control in Controles.ToList())
{
if (control is PictureBox pic)
listpanel.Controls.Remove(pic);
if (control is Label lbl && lbl.Name != "maintitle")
listpanel.Controls.Remove(lbl);
}
Controles.Clear();
LoadFiles();
}
private async Task ClearFiles(bool all = false)
{
int deletedcount = 0;
foreach (var control in listpanel.Controls)
if (control is PictureBox pic)
if (pic.Controls.Cast<Control>().ToList()[0] is CheckBox _check && _check.Checked)
{
try
{
var files = form.DuplicatedFiles[Tipo][pic.Tag.ToString()];
if (!all) files.Remove(files[0]);
files.ForEach(x => File.Delete(x));
deletedcount += files.Count;
form.DuplicatedFiles[Tipo].Remove(pic.Tag.ToString());
}
catch { }
}
UpdateList();
MessageBox.Show($"{deletedcount} arquivos foram deletados!");
}
private bool Moving = false;
private int posX = 0;
private int posY = 0;
private void panel1_MouseMove(object sender, MouseEventArgs e)
{
if (!Moving) return;
this.SetDesktopLocation((MousePosition.X - posX), (MousePosition.Y - posY));
}
private void panel1_MouseUp(object sender, MouseEventArgs e)
{
Moving = false;
}
private void panel1_MouseDown(object sender, MouseEventArgs e)
{
if (MousePosition.X <= this.Location.X || MousePosition.X >= (this.Location.X + this.Size.Width)) return;
if (MousePosition.Y <= this.Location.Y || MousePosition.Y > (this.Location.Y + 30)) return;
posX = MousePosition.X - this.Location.X;
posY = MousePosition.Y - this.Location.Y;
Moving = true;
}
}
}