-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSFormMain.cpp
496 lines (457 loc) · 15.2 KB
/
SFormMain.cpp
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
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "SFormMain.h"
#include "Clipbrd.hpp"
//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma link "trayicon"
#pragma resource "*.dfm"
TFormMain *FormMain;
//---------------------------------------------------------------------------
__fastcall TFormMain::TFormMain(TComponent* Owner)
: TForm(Owner)
{
AnsiString exePath=ExtractFileDir(Application->ExeName);
dataPath = exePath+"\\history.bin";
pClipHistory = new ClipHistory(10240);
pClipHistory->LoadFromFile(dataPath);
lastContent = pClipHistory->getHeadContent();
}
//---------------------------------------------------------------------------
void TFormMain::Log(int index, AnsiString str)
{
if(index>=statusBar->Panels->Count)
{
return;
}
statusBar->Panels->Items[index]->Text = str;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::tmrClipWatcherTimer(TObject *Sender)
{
try
{
AnsiString content = Clipboard()->AsText;
if(content != lastContent)
{
lastContent = content;
Log(SB_CLIP_DATA, content);
pClipHistory->Add(content);
ReloadClipHistoryList();
}
}
catch(...)
{}
try
{
HWND currentForeground = GetForegroundWindow();
if(currentForeground == this->Handle || currentForeground == edtSearch->Handle || currentForeground == lvClipHistory->Handle
|| currentForeground == mmoPreview->Handle || currentForeground == mmoTime->Handle || currentForeground == statusBar->Handle)
{
return;
}
// 获取前置窗口进程名和标题
lastWindowExeName = GetProcessNameByHandle(currentForeground);
char chs[1000];
GetWindowText(currentForeground, chs, 1000);
lastWindowTitle = chs;
Log(SB_CLIP_WIN, lastWindowExeName + " => " + lastWindowTitle);
lastForeground = currentForeground;
}catch(...) {
}
}
AnsiString TFormMain::GetProcessNameByHandle(HWND nlHandle)
{
//得到该进程的进程id
DWORD ldwProID;
GetWindowThreadProcessId(nlHandle,&ldwProID);
if(0==ldwProID)
return "";
HANDLE handle = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
if(handle==(HANDLE)-1)
{
return "";
}
PROCESSENTRY32 procinfo;
procinfo.dwSize = sizeof(PROCESSENTRY32);
BOOL more=::Process32First(handle,&procinfo);
while(more)
{
if(procinfo.th32ProcessID==ldwProID)
{
AnsiString loStrRet=procinfo.szExeFile;
CloseHandle(handle);
return loStrRet;
}
more=Process32Next(handle,&procinfo);
}
CloseHandle(handle);
return "";
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormShow(TObject *Sender)
{
edtSearch->SetFocus();
ReloadClipHistoryList();
// 注册热键
//enter
RegisterHotKey(this->Handle, HOTKEY_RETURN, NULL, VK_RETURN);
//up
RegisterHotKey(this->Handle, HOTKEY_UP, NULL, VK_UP);
//down
RegisterHotKey(this->Handle, HOTKEY_DOWN, NULL, VK_DOWN);
//esc
RegisterHotKey(this->Handle, HOTKEY_ESC, NULL, VK_ESCAPE);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormHide(TObject *Sender)
{
// 注销热键
UnregisterHotKey(Handle, HOTKEY_RETURN);
UnregisterHotKey(Handle, HOTKEY_UP);
UnregisterHotKey(Handle, HOTKEY_DOWN);
UnregisterHotKey(Handle, HOTKEY_ESC);
}
//---------------------------------------------------------------------------
void TFormMain::ReloadClipHistoryList()
{
if(!this->Visible)
{
return;
}
list<ClipHistoryItem*> result = pClipHistory->Search(edtSearch->Text, 500);
//Log(SB_CLIP_DATA, IntToStr(result.size()));
lvClipHistory->Clear();
list<ClipHistoryItem*>::iterator it;
for(it=result.begin();it!=result.end();it++)
{
// Application->HandleMessage();
ClipHistoryItem* pItem = *it;
TListItem* pListItem = lvClipHistory->Items->Add();
pListItem->Caption = "";
pListItem->SubItems->Add(pItem->content.Length() > 100 ? pItem->content.SubString(1, 100) : pItem->content);
pListItem->Data = (void *)pItem;
}
if(lvClipHistory->Items->Count > 0)
{
lvClipHistory->Items->Item[0]->Selected = True;
}
}
//---------------------------------------------------------------------------
AnsiString TFormMain::FormatTime(long time)
{
struct tm *temp_time = localtime(&time);
AnsiString s = IntToStr(temp_time->tm_year + 1900);
s += "-";
if(temp_time->tm_mon + 1 < 10)
{
s += "0";
}
s += IntToStr(temp_time->tm_mon + 1);
s += "-";
s += (temp_time->tm_mday<10?"0":"");
s += IntToStr(temp_time->tm_mday);
s += " ";
s += (temp_time->tm_hour<10?"0":"");
s += IntToStr(temp_time->tm_hour);
s += ":";
s += (temp_time->tm_min<10?"0":"");
s += IntToStr(temp_time->tm_min);
s += ":";
s += (temp_time->tm_sec<10?"0":"");
s += IntToStr(temp_time->tm_sec);
return s;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::lvClipHistorySelectItem(TObject *Sender,
TListItem *Item, bool Selected)
{
if(Selected)
{
for(int i=0;i<lvClipHistory->Items->Count;i++)
{
lvClipHistory->Items->Item[i]->Caption = IntToStr(i);
}
Item->Caption = "->";
ClipHistoryItem* pItem = (ClipHistoryItem*)(Item->Data);
mmoTime->Text = FormatTime(pItem->time);
mmoPreview->Text = pItem->content.Length() > 200 ? (pItem->content.SubString(1, 200) + "...") : pItem->content;
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::edtSearchChange(TObject *Sender)
{
ReloadClipHistoryList();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCloseQuery(TObject *Sender, bool &CanClose)
{
this->Hide();
CanClose = false;
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::trayRestore(TObject *Sender)
{
this->Show();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N1Click(TObject *Sender)
{
this->Show();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N3Click(TObject *Sender)
{
pClipHistory->SaveToFile(dataPath);
Application->Terminate();
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::tmrSaveTimer(TObject *Sender)
{
pClipHistory->SaveToFile(dataPath);
}
//---------------------------------------------------------------------------
void TFormMain::OnSpecialKey(WORD Key)
{
if(Key != VK_UP && Key != VK_DOWN && Key != VK_RETURN && Key != VK_ESCAPE && Key != VK_DELETE)
{
edtSearch->SetFocus();
return;
}
if(Key == VK_ESCAPE)
{
this->Hide();
}
if(lvClipHistory->Items->Count == 0)
{
return;
}
if(lvClipHistory->Selected == NULL)
{
lvClipHistory->Items->Item[0]->Selected = True;
}
if(Key == VK_UP)
{
SetSelectedInClipboardHistoryList(lvClipHistory->Selected ? (lvClipHistory->Selected->Index-1) : 0);
}
else if(Key == VK_DOWN)
{
SetSelectedInClipboardHistoryList(lvClipHistory->Selected ? (lvClipHistory->Selected->Index+1) : 0);
}
else if(Key == VK_RETURN)
{
PasteAndHide();
}
else if(Key == VK_DELETE)
{
N4Click(NULL);
}
}
void TFormMain::OnCharKey(WORD Key, TShiftState Shift)
{
// TODO
}
void TFormMain::PasteAndHide()
{
if(!lvClipHistory->Selected)
{
return;
}
ClipHistoryItem* pItem = (ClipHistoryItem*)(lvClipHistory->Selected->Data);
Clipboard()->AsText = pItem->content;
this->Hide();
if(lastForeground != NULL)
{
SetForegroundWindow(lastForeground);
if(lastWindowExeName == "mintty.exe" || lastWindowExeName == "Xshell.exe")
{
// 模拟Shift+Ins
INPUT input[4];
memset(input, 0, sizeof(input));
//设置模拟键盘输入
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_SHIFT;
input[1].ki.wVk = input[3].ki.wVk = VK_INSERT;
// 释放按键,这非常重要
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4,input,sizeof(INPUT));
}
else
{
// 模拟Ctrl+V
INPUT input[4];
memset(input, 0, sizeof(input));
//设置模拟键盘输入
input[0].type = input[1].type = input[2].type = input[3].type = INPUT_KEYBOARD;
input[0].ki.wVk = input[2].ki.wVk = VK_CONTROL;
input[1].ki.wVk = input[3].ki.wVk = 86;
// 释放按键,这非常重要
input[2].ki.dwFlags = input[3].ki.dwFlags = KEYEVENTF_KEYUP;
SendInput(4,input,sizeof(INPUT));
}
}
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::edtSearchKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
OnSpecialKey(Key);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::mmoPreviewKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
OnSpecialKey(Key);
OnCharKey(Key, Shift);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::lvClipHistoryKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
OnSpecialKey(Key);
OnCharKey(Key, Shift);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormKeyDown(TObject *Sender, WORD &Key,
TShiftState Shift)
{
OnSpecialKey(Key);
OnCharKey(Key, Shift);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormCreate(TObject *Sender)
{
// alt+shift+v
RegisterHotKey(this->Handle, HOTKEY_CTRL_ALT_V, MOD_ALT|MOD_SHIFT, 0x56);
AutoRunCheck(true);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::FormDestroy(TObject *Sender)
{
UnregisterHotKey(Handle, HOTKEY_CTRL_ALT_V);
UnregisterHotKey(Handle, HOTKEY_RETURN);
UnregisterHotKey(Handle, HOTKEY_UP);
UnregisterHotKey(Handle, HOTKEY_DOWN);
UnregisterHotKey(Handle, HOTKEY_ESC);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::WMHotKey(TMessage &Msg)
{
if(Msg.WParam == 0x1000)
{
// 当前鼠标位置
POINT pt;
GetCursorPos(&pt);
//获取可用桌面大小
RECT rect;
SystemParametersInfo(SPI_GETWORKAREA, 0, &rect, 0);
//ShowMessage(IntToStr(rect.left)+"=>"+IntToStr(rect.right)+", "+IntToStr(rect.top)+"=>"+IntToStr(rect.bottom));
if(pt.x + this->Width > rect.right)
{
pt.x = rect.right - this->Width;
}
if(pt.y + this->Height > rect.bottom)
{
pt.y = rect.bottom - this->Height;
}
this->Left = pt.x;
this->Top = pt.y;
this->Hide();
this->Show();
}
else if(Msg.WParam == HOTKEY_RETURN)
{
if(this->Visible)
{
PasteAndHide();
}
}
else if(Msg.WParam == HOTKEY_UP)
{
if(this->Visible)
{
SetSelectedInClipboardHistoryList(lvClipHistory->Selected ? (lvClipHistory->Selected->Index-1) : 0);
}
}
else if(Msg.WParam == HOTKEY_DOWN)
{
if(this->Visible)
{
SetSelectedInClipboardHistoryList(lvClipHistory->Selected ? (lvClipHistory->Selected->Index+1) : 0);
}
}
else if(Msg.WParam == HOTKEY_ESC)
{
this->Close();
}
}
//---------------------------------------------------------------------------
void TFormMain::SetSelectedInClipboardHistoryList(int index)
{
if(index < 0 || index >= lvClipHistory->Items->Count)
{
return;
}
lvClipHistory->Items->Item[index]->Selected = True;
ListView_EnsureVisible(lvClipHistory->Handle, index, true);
ListView_SetItemState(lvClipHistory->Handle, index, LVIS_SELECTED, LVIS_SELECTED);
::SetFocus(lvClipHistory->Handle);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::N4Click(TObject *Sender)
{
if(lvClipHistory->Selected == NULL)
{
return;
}
int idx = lvClipHistory->Selected->Index;
pClipHistory->Remove(lvClipHistory->Selected->SubItems->Strings[0]);
lvClipHistory->Selected->Delete();
if(idx >= lvClipHistory->Items->Count)
{
idx = lvClipHistory->Items->Count - 1;
}
SetSelectedInClipboardHistoryList(idx);
}
//---------------------------------------------------------------------------
void __fastcall TFormMain::lvClipHistoryDblClick(TObject *Sender)
{
PasteAndHide();
}
//---------------------------------------------------------------------------
int __fastcall TFormMain::AutoRunCheck(bool value)
{
int ret = 0;
TRegistry * Reg = new TRegistry;
AnsiString keyval="\""+Application->ExeName+"\"";
AnsiString key1="Software\\Microsoft\\Windows\\CurrentVersion\\Run\\";
Reg->RootKey=HKEY_CURRENT_USER;
if( !Reg->OpenKey(key1,false))
{
//Show msg
}
else
{
if(value)
{
AnsiString curvalstr = Reg->ReadString("CLIP.EXE");
if(curvalstr != keyval)
{
Reg->WriteString("CLIP.EXE",keyval);
}
Reg->CloseKey();
ret = 1;
}
else
{
if(Reg->DeleteValue("CLIP.EXE"))
{
ret = 1;
}
}
}
delete Reg;
return ret;
}
//---------------------------------------------------------------------------