-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
430 lines (401 loc) · 15.2 KB
/
MainForm.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
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
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
using IClipboard.Base;
using IClipboard.Properties;
using IClipboard.Win32;
using MaterialSkin.Controls;
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Net;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace IClipboard
{
public partial class MainForm : MaterialForm
{
string[] args = null;
public MainForm(string[] args)
{
InitializeComponent();
Context.CLIPBOARD_COUNT = Settings.Default.Count;
NewViewer();
this.args = args;
new Thread(() => {
update();
}).Start();
}
private IntPtr mNextClipBoardViewerHWnd;
public void NewViewer()
{
mNextClipBoardViewerHWnd = Win32API.SetClipboardViewer(this.Handle);
}
string LastString = string.Empty;
Image LastImage = null;
protected override void WndProc(ref Message m)
{
string Time = DateTime.Now.ToString("yy/MM/dd HH:mm");
switch (m.Msg)
{
case (int)WinMsg.WM_DRAWCLIPBOARD:
{
if (ICopy == 1) return;
Win32API.SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
//显示剪贴板中的文本信息
if (Clipboard.ContainsText())
{
string text= Clipboard.GetText();
if (text == LastString||string.IsNullOrEmpty(text)) return;
LastString = text;
string t = "";
if (text.Length > 35)
{
t = text.Substring(0,35);
}
else
{
t = text;
}
ListViewItem item = new ListViewItem(new string[3] { t, "文本", Time });
item.Tag = text;
item.ToolTipText = "文本";
list.Items.Add(item);
}
//显示剪贴板中的图片信息
if (Clipboard.ContainsImage())
{
Image img= Clipboard.GetImage();
if (img == LastImage) return;
LastImage = img;
string txt= img.Width + "*" + img.Height+"["+ ImageType.GetType(img)+"]";
ListViewItem item = new ListViewItem(new string[3] { "图片文件("+ txt + ")", "图片", Time });
item.Tag = img;
item.ToolTipText = "图片";
list.Items.Add(item);
}
if (Clipboard.ContainsFileDropList())
{
StringCollection ss=Clipboard.GetFileDropList();
foreach (string text in ss)
{
string t = "";
if (text.Length > 35)
{
t = text.Substring(0, 35);
}
else
{
t = text;
}
ListViewItem item = new ListViewItem(new string[3] { t, "文件路径", Time });
item.ToolTipText = "路径";
item.Tag = text;
list.Items.Add(item);
}
}
if (list.Items.Count > Context.CLIPBOARD_COUNT)
{
int num = list.Items.Count - Context.CLIPBOARD_COUNT;
for (int i = 0; i < num; i++)
{
list.Items.RemoveAt(0);
}
}
break;
}
case (int)WinMsg.WM_CHANGECBCHAIN:
{
if (m.WParam == (IntPtr)mNextClipBoardViewerHWnd)
{
mNextClipBoardViewerHWnd = m.LParam;
}
else
{
Win32API.SendMessage(mNextClipBoardViewerHWnd, m.Msg, m.WParam.ToInt32(), m.LParam.ToInt32());
}
break;
}
case (int)WinMsg.WM_HOTKEY:
{
switch (m.WParam.ToInt32())
{
case Space: //热键ID
icontab_DoubleClick(null, null);
break;
default:
break;
}
break;
}
case (int)WinMsg.WM_CREATE: //窗口消息-创建
RegKey(Handle, Space, KeyModifiers.Alt, Keys.C);
break;
case (int)WinMsg.WM_DESTROY: //窗口消息-销毁
UnRegKey(Handle, Space); //销毁热键
break;
}
base.WndProc(ref m);
}
private const int Space = 13682; //热键ID
private void ClipboardMenuStrip_Opening(object sender, CancelEventArgs e)
{
tscopycontent.Enabled = false;
tslookimage.Enabled = false;
tsopenpath.Enabled = false;
tssaveimage.Enabled = false;
if (list.SelectedItems != null&& list.SelectedItems.Count>0)
{
selectItem = list.SelectedItems[0];
string type = selectItem.ToolTipText;
switch (type)
{
case "文本":
tscopycontent.Enabled = true;
break;
case "图片":
tscopycontent.Enabled = true;
tssaveimage.Enabled = true;
tslookimage.Enabled = true;
break;
case "路径":
tscopycontent.Enabled = true;
tsopenpath.Enabled = true;
break;
default:
break;
}
}
}
public static int ICopy = 0;
ListViewItem selectItem = null;
private void ClipboardMenuStrip_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
{
string type = e.ClickedItem.Text;
if(type== "清除列表")
{
selectItem = null;
list.Items.Clear();
Clipboard.Clear();
return;
}
if (selectItem == null) return;
Image img = null;
if (selectItem.ToolTipText == "图片")
{
img = (Image)selectItem.Tag;
}
switch (type)
{
case "打开路径":
System.Diagnostics.Process.Start("explorer.exe", selectItem.Tag.ToString());
break;
case "复制内容":
ICopy = 1;
if(selectItem.ToolTipText== "图片")
{
Clipboard.SetImage(img);
}
else
{
Clipboard.SetText(selectItem.Tag.ToString());
}
break;
case "预览图片":
new ImageForm(img).ShowDialog();
break;
case "图片另存为":
SaveFileDialog save = new SaveFileDialog();
save.Filter = "图片文件|*.JPG|*.PNG|*.GIF|*.TIFF|*.BMP|*.ICO|";
save.FileName = "IC" + DateTime.Now.ToFileTime().ToString() + "." + ImageType.GetType(img);
if (save.ShowDialog() == DialogResult.OK)
{
string path = save.FileName;
img.Save(save.FileName);
}
break;
default:
break;
}
ICopy = 0;
}
private void list_DoubleClick(object sender, EventArgs e)
{
if (list.SelectedItems != null && list.SelectedItems.Count > 0)
{
ListViewItem selectlist = list.SelectedItems[0];
string type = selectlist.ToolTipText;
switch (type)
{
case "文本":
ICopy = 1;
Clipboard.SetText(selectlist.Tag.ToString());
break;
case "图片":
Image im = (Image)selectlist.Tag;
new ImageForm(im).ShowDialog();
break;
case "路径":
System.Diagnostics.Process.Start("explorer.exe", selectlist.Tag.ToString());
break;
default:
break;
}
ICopy = 0;
}
}
bool AutoExit = false;
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
if (!AutoExit)
{
DialogResult r = DialogForm.ShowMsg("关闭后剪贴板会清空,是否关闭软件?", "提示", MessageFormIcon.Doubt, MessageFormButtons.YesNo);
if (r != DialogResult.Yes)
{
e.Cancel = true;
}
}
}
/// <summary>
/// 注册热键
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="hotKey_id">热键ID</param>
/// <param name="keyModifiers">组合键</param>
/// <param name="key">热键</param>
public static void RegKey(IntPtr hwnd, int hotKey_id, KeyModifiers keyModifiers, Keys key)
{
try
{
if (!Win32API.RegisterHotKey(hwnd, hotKey_id, keyModifiers, key))
{
if (Marshal.GetLastWin32Error() == 1409) {
MessageBox.Show("热键被占用 !");
}
else
{
MessageBox.Show("注册热键失败!");
}
}
}
catch (Exception) { }
}
/// <summary>
/// 注销热键
/// </summary>
/// <param name="hwnd">窗口句柄</param>
/// <param name="hotKey_id">热键ID</param>
public static void UnRegKey(IntPtr hwnd, int hotKey_id)
{
//注销Id号为hotKey_id的热键设定
Win32API.UnregisterHotKey(hwnd, hotKey_id);
}
private void icontab_DoubleClick(object sender, EventArgs e)
{
if (this.WindowState!= FormWindowState.Normal)
{
this.Show();
this.WindowState = FormWindowState.Normal;
}
else
{
this.Hide();
this.WindowState = FormWindowState.Minimized;
}
}
private void tsExit_Click(object sender, EventArgs e)
{
this.Close();
}
private void tsshow_Click(object sender, EventArgs e)
{
icontab_DoubleClick(null,null);
}
private void MainForm_Resize(object sender, EventArgs e)
{
if(this.WindowState== FormWindowState.Minimized)
{
this.Hide();
}
}
private void lblmenu_MouseEnter(object sender, EventArgs e)
{
Label l = (Label)sender;
l.BackColor = Color.FromArgb(61, 71, 76);
}
private void lblmenu_MouseLeave(object sender, EventArgs e)
{
Label l = (Label)sender;
l.BackColor = Color.Transparent;
}
private void lblmenu_Click(object sender, EventArgs e)
{
new SettingsForm().ShowDialog();
}
private void MainForm_Load(object sender, EventArgs e)
{
if(args!=null&& args.Length > 0)
{
this.WindowState = FormWindowState.Minimized;
this.Hide();
}
}
public void update()
{
string Edition = GetVersion("IClipboard");
Version curVersion = new Version(AssemblyVersion);//本地资源版本
Version lowVersion = new Version(Edition);//服务器资源版本
if (lowVersion > curVersion)
{
this.Invoke((EventHandler)delegate
{
DialogResult r = DialogForm.ShowMsg("检测到新版本,是否下载?", "提示", MessageFormIcon.Doubt, MessageFormButtons.YesNo);
if (r == DialogResult.Yes)
{
string url = "http://itiit.cn/file/IClipboard.exe";
try
{
System.Diagnostics.Process.Start(url);
}
catch (Exception)
{
System.Diagnostics.Process.Start("iexplore.exe", url);
}
AutoExit = true;
Application.Exit();
}
});
}
}
public string AssemblyVersion
{
get
{
return Assembly.GetExecutingAssembly().GetName().Version.ToString();
}
}
public string GetVersion(string AppName)
{
try
{
string postData = "AppName=" + AppName;
byte[] bytes = Encoding.UTF8.GetBytes(postData);
WebClient client = new WebClient();
client.Headers.Add("Content-Type", "application/x-www-form-urlencoded");
client.Headers.Add("ContentLength", postData.Length.ToString());
Encoding enc = Encoding.GetEncoding("UTF-8");
byte[] responseData = client.UploadData("http://itiit.cn/Tools/Update", "POST", bytes);
return enc.GetString(responseData).Replace("\"", "");
}
catch (Exception)
{
return "1.0.0.0";
}
}
}
}