// LogFileRARDlg.cpp : implementation file
//
#include "stdafx.h"
#include "LogFileRAR.h"
#include "LogFileRARDlg.h"
#include "afxdialogex.h"
#include "FindPath.h"
#include "CZip.h"
using namespace std;
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
//回调函数,实现默认路径的指定
//static WCHAR* szdir;//[MAX_PATH];
static WCHAR szdir[MAX_PATH];
INT CALLBACK BrowseCallbackProc(HWND hwnd,UINT uMsg, LPARAM lp, LPARAM pData)
{
LPITEMIDLIST tmp = (LPITEMIDLIST)pData;
switch(uMsg)
{
case BFFM_INITIALIZED:
::SendMessageW(hwnd,BFFM_SETSELECTION,TRUE,(LPARAM)szdir);
break;
case BFFM_SELCHANGED:
::SendMessageW(hwnd,BFFM_SETSTATUSTEXT,0,(LPARAM)szdir);
break;
default:
break;
}
return 0;
}
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialogEx
{
public:
CAboutDlg();
// Dialog Data
enum { IDD = IDD_ABOUTBOX };
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
// Implementation
protected:
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialogEx(CAboutDlg::IDD)
{
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialogEx)
END_MESSAGE_MAP()
// CLogFileRARDlg dialog
CLogFileRARDlg::CLogFileRARDlg(CWnd* pParent /*=NULL*/)
: CDialogEx(CLogFileRARDlg::IDD, pParent)
{
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
m_strPath = _T("");
m_strFilePath = _T("");
}
void CLogFileRARDlg::DoDataExchange(CDataExchange* pDX)
{
CDialogEx::DoDataExchange(pDX);
}
BEGIN_MESSAGE_MAP(CLogFileRARDlg, CDialogEx)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_BTN_LOGFIND, &CLogFileRARDlg::OnBnClickedBtnLogfind)
ON_BN_CLICKED(IDCANCEL, &CLogFileRARDlg::OnBnClickedCancel)
ON_BN_CLICKED(IDC_BTN_SELPATH, &CLogFileRARDlg::OnBnClickedBtnSelpath)
ON_BN_CLICKED(IDC_BTN_SELFILE, &CLogFileRARDlg::OnBnClickedBtnSelfile)
ON_BN_CLICKED(IDC_BTN_OPENRARPATH, &CLogFileRARDlg::OnBnClickedBtnOpenrarpath)
END_MESSAGE_MAP()
// CLogFileRARDlg message handlers
BOOL CLogFileRARDlg::OnInitDialog()
{
CDialogEx::OnInitDialog();
// Add "About..." menu item to system menu.
// IDM_ABOUTBOX must be in the system command range.
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);
}
}
// 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
// ((CEdit*)GetDlgItem(IDC_EDIT_PATH))->SetReadOnly(TRUE);
((CEdit*)GetDlgItem(IDC_EDIT_FILEPATH))->SetReadOnly(TRUE);
//获取保存过的路径,如果是win7,从注册表中取值xp从系统ini文件中取值
if (CheckIsWin7())
{
CFindPath operREG;
m_strFilePath = operREG.GetKeyValue(HKEY_CLASSES_ROOT,_T("FileAndRARPath\\FilePath"),_T("FilePath"));
m_strPath = operREG.GetKeyValue(HKEY_CLASSES_ROOT,_T("FileAndRARPath\\RARPath"),_T("RARPath"));
}
else
{
wchar_t szFilePath[MAX_PATH];
RtlZeroMemory(szFilePath,MAX_PATH);
wchar_t szRARFilePath[MAX_PATH];
RtlZeroMemory(szRARFilePath,MAX_PATH);
GetPrivateProfileString(_T("LogPath"),_T("FilePath"),_T(""),szFilePath,MAX_PATH,_T("C:\\WINDOWS\\win.ini"));
m_strFilePath = szFilePath;
GetPrivateProfileString(_T("LogPath"),_T("FileRARPath"),_T(""),szRARFilePath,MAX_PATH,_T("C:\\WINDOWS\\win.ini"));
m_strPath = szRARFilePath;
}
GetDlgItem(IDC_EDIT_FILEPATH)->SetWindowText(m_strFilePath);
//添加Log.rar后缀
if (m_strPath != _T(""))
{
CString strPathTmp = _T("");
CString FileRARPath = _T("");
CString strR = m_strPath.Right(1);
if (strR == _T("\\"))
{
strPathTmp = m_strPath + _T("Log.rar");
}
else
{
strPathTmp = m_strPath + _T("\\Log.rar");
}
GetDlgItem(IDC_EDIT_PATH)->SetWindowText(strPathTmp);
}
return TRUE; // return TRUE unless you set the focus to a control
}
void CLogFileRARDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
else
{
CDialogEx::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 CLogFileRARDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // device context for painting
SendMessage(WM_ICONERASEBKGND, reinterpret_cast<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
{
CDialogEx::OnPaint();
}
}
// The system calls this function to obtain the cursor to display while the user drags
// the minimized window.
HCURSOR CLogFileRARDlg::OnQueryDragIcon()
{
return static_cast<HCURSOR>(m_hIcon);
}
/*****************************************************************************
函数名称:OnBnClickedBtnLogfind()
功能描述:开始处理日志文件
输入参数:
输出参数:
返 回 值:
*****************************************************************************/
void CLogFileRARDlg::OnBnClickedBtnLogfind()
{
// TODO: Add your control notification handler code here
//OCX注册路径和客户端安装路径分开,不在同一个地方,通过查找OCX键值可可用
/*
CString strPathFile = _T("");
CFindPath FindPath;
strPathFile = FindPath.FindPathByOcx(_T("IVS_OCXPlayerd.ocx"));
*/
GetDlgItem(IDC_EDIT_PATH)->GetWindowTextW(m_strPath);
m_strPath.TrimRight(_T("\\Log.rar"));
//先将路径写入到文件中,win7写入注册表,XP写入系统的ini文件中
if (CheckIsWin7())
{
CFindPath operREG;
if (operREG.CreateSubKey(HKEY_CLASSES_ROOT,NULL,_T("FileAndRARPath\\FilePath")))
{
operREG.AddKeyValue(HKEY_CLASSES_ROOT,_T("FileAndRARPath\\FilePath"),_T("FilePath"),REG_SZ,(LPBYTE)m_strFilePath.GetBuffer(),
m_strFilePath.GetLength());
}
if (operREG.CreateSubKey(HKEY_CLASSES_ROOT,NULL,_T("FileAndRARPath\\RARPath")))
{
operREG.AddKeyValue(HKEY_CLASSES_ROOT,_T("FileAndRARPath\\RARPath"),_T("RARPath"),REG_SZ,(LPBYTE)m_strPath.GetBuffer(),
m_strPath.GetLength());
}
}
else
{
if (m_strPath != _T(""))
{
WritePrivateProfileString(_T("LogPath"),_T("FileRARPath"),m_strPath.GetBuffer(),_T("C:\\WINDOWS\\win.ini"));
}
if (m_strFilePath != _T(""))
{
WritePrivateProfileString(_T("LogPath"),_T("FilePath"),m_strFilePath.GetBuffer(),_T("C:\\WINDOWS\\win.ini"));
}
}
//创建文件夹路径 先判断是否为根目录
int nPos = m_strPath.GetLength();
CString strRoot = m_strPath.Mid(nPos-2,1);
if ((m_strPath.Right(1) != _T(":")) && (strRoot != _T(":")))
{
//CreateDirectoryW(m_strPath.GetBuffer(), NULL);
//DWORD l = GetLastError();
if (FALSE == CreateDirectoryW(m_strPath.GetBuffer(), NULL))
{
m_strPath.TrimRight(_T("\\"));
BOOL bCreate = TRUE;
DWORD nError = 0;
CString strPathTemp = m_strPath.Left(m_strPath.Find(_T("\\")) + 1);
CString strLiftPath = m_strPath.Right(m_strPath.GetLength() - m_strPath.Find(_T("\\")) - 1);
while(1)
{
if (strLiftPath == _T(""))
{
break;
}
if (strLiftPath.Find(_T("\\")) < 0)
{
strPathTemp.TrimRight(_T("\\"));
strPathTemp = strPathTemp + _T("\\") + strLiftPath;
bCreate = CreateDirectoryW(strPathTemp.GetBuffer(), NULL);
nError = GetLastError();
if (FALSE == bCreate && nError != ERROR_ALREADY_EXISTS)
{
MessageBox(_T("文件夹路径创建失败!"));
return;
}
break;
}
CString str = strLiftPath.Left(strLiftPath.Find(_T("\\")));
if (strPathTemp.Right(1) == _T("\\"))
{
strPathTemp += str;
}
else
{
strPathTemp = strPathTemp + _T("\\") + str;
}
strLiftPath = strLiftPath.Right(strLiftPath.GetLength() - strLiftPath.Find(_T("\\")) - 1);
bCreate = CreateDirectoryW(strPathTemp.GetBuffer(), NULL);
nError = GetLastError();
if (FALSE == bCreate && nError != ERROR_ALREADY_EXISTS)
{
MessageBox(_T("文件夹路径创建失败!"));
return;
}
if (strLiftPath.Find(_T("\\")) < 0)
{
strPathTemp.TrimRight(_T("\\"));
strPathTemp = strPathTemp + _T("\\") + strLiftPath;
bCreate = CreateDirectoryW(strPathTemp.GetBuffer(), NULL);
nError = GetLastError();
if (FALSE == bCreate && nError != ERROR_ALREADY_EXISTS)
{
MessageBox(_T("文件夹路径创建失败!"));
return;
}
break;
}
}
}
}
if (MakeRARFile(m_strFilePath,m_strPath))
{
MessageBox(_T("日志文件压缩处理成功!"));
}
else
{
MessageBox(_T("日志文件压缩处理失败!"));
}
}
/*****************************************************************************
函数名称:GetSelectPath()
功能描述:获取用户选择的路径信息
输入参数:
输出参数:用户选择的路径
返 回 值: CString
*****************************************************************************/
CString CLogFileRARDlg::GetSelectPath(bool bOpenOrSave)
{
// BROWSEINFO stBrowseFinfo = {0};
// LPITEMIDLIST lpltemIDList = SHBrowseForFolder(&stBrowseFinfo);
wchar_t dir[MAX_PATH] = {0};
CString strPath = _T("");
BROWSEINFO bInfo;
ZeroMemory(&bInfo, sizeof(bInfo));
bInfo.hwndOwner = m_hWnd;
bInfo.pidlRoot = 0;
// bInfo.lpszTitle = _T("请选择文件夹: ");
if (bOpenOrSave)
{
bInfo.lpszTitle=_T("选择日志文件所在的路径");
bInfo.lParam = (LPARAM)(LPCTSTR)m_strFilePath;
memcpy(szdir,m_strFilePath.GetBuffer(),MAX_PATH);
}
else
{
bInfo.lpszTitle=_T("选择日志文件压缩后存放的路径");
bInfo.lParam = (LPARAM)(LPCTSTR)m_strPath;
memcpy(szdir,m_strPath.GetBuffer(),MAX_PATH);
}
bInfo.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT ;//| BIF_EDITBOX;
bInfo.lpfn = BrowseCallbackProc;
LPITEMIDLIST lpDlist; //用来保存返回信息的IDList
lpDlist = SHBrowseForFolderW(&bInfo) ; //显示选择对话框
if(lpDlist != NULL) //用户按了确定按钮
{
TCHAR chPath[255]; //用来存储路径的字符串
SHGetPathFromIDList(lpDlist, chPath);//把项目标识列表转化成字符串
strPath = chPath; //将TCHAR类型的字符串转换为CString类型的字符串
}
else
{
AfxMessageBox(_T("未选择文件夹"));
}
return strPath;
/
// BROWSEINFO bi;
// bi.hwndOwner=NULL;
// bi.pidlRoot=NULL;
// bi.pszDisplayName=dir;
//
// bi.lpfn = BrowseCallbackProc; //指定回调函数地址
//
// bi.ulFlags=BIF_RETURNONLYFSDIRS;
// if (bOpenOrSave)
// {
// bi.lpszTitle=_T("选择日志文件所在的路径");
// bi.pidlRoot = (LPCITEMIDLIST)m_strFilePath.GetBuffer();
// LPITEMIDLIST pNetHoodIDL;
// ::SHGetSpecialFolderLocation(NULL,CSIDL_HISTORY,&pNetHoodIDL); //根文件夹为历史文件夹
// bi.pidlRoot=pNetHoodIDL;
// }
// else
// {
// bi.lpszTitle=_T("选择日志文件压缩后存放的路径");
// }
//
// bi.ulFlags=0;
// bi.lpfn=NULL;
// bi.lParam=NULL;
// bi.iImage=NULL;
//
// SHGetPathFromIDListW(SHBrowseForFolder(&bi),dir);
//
// return dir;
}
/*****************************************************************************
函数名称:MakeRARFile()
功能描述:调用系统命令,将FilePath下的日志文件压缩到用户指定的路径下SelectPath
输入参数:日志文件所在路径FilePath,压缩到目标路径SelectPath
输出参数:用户选择的路径
返 回 值: true:成功。false:失败
*****************************************************************************/
bool CLogFileRARDlg::MakeRARFile(CString FilePath,CString SelectPath)
{
//先判断FilePath是否为空
if (FilePath == _T(""))
{
MessageBox(_T("请选择日志文件所在路径!"));
return false;
}
if (SelectPath == _T(""))
{
MessageBox(_T("请选择日志文件存放路径!"));
return false;
}
CString strCommand = _T("");
CString LogPath = FilePath;// + _T("\\Log ");
CString FileRARPath = _T("");
CString strR = SelectPath.Right(1);
if (strR == _T("\\"))
{
FileRARPath = SelectPath + _T("Log.rar ");
}
else
{
FileRARPath = SelectPath + _T("\\Log.rar ");
}
FileRARPath = _T("a ") + FileRARPath;
// int nLenth = FilePath.GetLength();
// char* OutBuf = new char[nLenth + 1];
// wchar_t* str = FilePath.GetBuffer();
// WChar2Char(str,-1,OutBuf,(nLenth+1)*2);
HINSTANCE hinstance = ShellExecuteW(NULL, _T("open"), _T("C:\\Program Files\\WinRAR\\WinRAR.exe"),
FileRARPath.GetBuffer(), LogPath.GetBuffer(), SW_SHOW);
int nResult = (int)hinstance;
if (nResult > 32)
{
return true;
}
else
{
return true;
}
}
void CLogFileRARDlg::OnBnClickedCancel()
{
// TODO: Add your control notification handler code here
CDialogEx::OnCancel();
}
/*****************************************************************************
函数名称:OnBnClickedBtnSelpath()
功能描述:设置日志文件存放的路径
输入参数:
输出参数:
返 回 值:
*****************************************************************************/
void CLogFileRARDlg::OnBnClickedBtnSelpath()
{
// TODO: Add your control notification handler code here
CString strPath = GetSelectPath(false);
if (strPath != _T(""))
{
m_strPath = strPath;
}
//添加Log.rar后缀
if (m_strPath != _T(""))
{
CString strPathTmp = _T("");
CString FileRARPath = _T("");
CString strR = m_strPath.Right(1);
if (strR == _T("\\"))
{
strPathTmp = m_strPath + _T("Log.rar");
}
else
{
strPathTmp = m_strPath + _T("\\Log.rar");
}
GetDlgItem(IDC_EDIT_PATH)->SetWindowText(strPathTmp);
}
}
/*****************************************************************************
函数名称:OnBnClickedBtnSelfile()
功能描述:选择日志文件所在的路径
输入参数:
输出参数:
返 回 值:
*****************************************************************************/
void CLogFileRARDlg::OnBnClickedBtnSelfile()
{
// TODO: Add your control notification handler code here
CString strFilePath = GetSelectPath(true);
if (strFilePath != _T(""))
{
m_strFilePath = strFilePath;
}
if (m_strFilePath != _T(""))
{
GetDlgItem(IDC_EDIT_FILEPATH)->SetWindowText(m_strFilePath);
}
}
/*****************************************************************************
函数名称:OnBnClickedBtnOpenrarpath()
功能描述:打开压缩后日志文件所在的路径
输入参数:
输出参数:
返 回 值:
*****************************************************************************/
void CLogFileRARDlg::OnBnClickedBtnOpenrarpath()
{
// TODO: Add your control notification handler code here
if (m_strPath != _T(""))
{
STARTUPINFO si = {sizeof(si)};
PROCESS_INFORMATION pi;
si.dwFlags = STARTF_USESHOWWINDOW;
si.wShowWindow = TRUE;
CString strCommandLine = _T("c:\\windows\\explorer.EXE ") + m_strPath;
//WCHAR sCommandLine[]={_T("c:\\windows\\explorer.EXE c:\\windows")};
BOOL ret = ::CreateProcessW(NULL, strCommandLine.GetBuffer(), NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi);
if (!ret)
{
MessageBox(_T("浏览文件失败!"));
}
}
else
{
MessageBox(_T("路径未设置!"));
}
}
/*****************************************************************************
函数名称:CheckIsWin7()
功能描述:判断当前的操作系统是否为win7版本
输入参数:
输出参数:BOOL,TRUE:是win7版本,FALSE:不是win7版本
返 回 值:BOOL
*****************************************************************************/
BOOL CLogFileRARDlg::CheckIsWin7()
{
OSVERSIONINFOEX stOsVersionInfo = {0};
stOsVersionInfo.dwOSVersionInfoSize = sizeof(stOsVersionInfo);
BOOL bResult = GetVersionEx((LPOSVERSIONINFO)&stOsVersionInfo);
if (!bResult)
{
//获得失败,默认是win7系统
return TRUE;
}
if ((stOsVersionInfo.dwMajorVersion >= 6) && (stOsVersionInfo.dwMinorVersion >= 1))
{
return TRUE;
}
return FALSE;
}