📄 filemanagedlg.cpp
字号:
// FileManageDlg.cpp : 实现文件
//
#include "stdafx.h"
#include "VipClient.h"
#include "FileManageDlg.h"
#include ".\filemanagedlg.h"
#include "VipClient.h"
// CFileManageDlg 对话框
IMPLEMENT_DYNAMIC(CFileManageDlg, CDialog)
CFileManageDlg::CFileManageDlg(CWnd* pParent /*=NULL*/)
: CDialog(CFileManageDlg::IDD, pParent)
{
m_bIsClose = false;
m_hThread = NULL;
}
CFileManageDlg::~CFileManageDlg()
{
}
void CFileManageDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
DDX_Control(pDX, IDC_LIST_FILE, m_ListFile);
DDX_Control(pDX, IDC_COMBO_FILEPATH, m_ComBoxFilePath);
}
BEGIN_MESSAGE_MAP(CFileManageDlg, CDialog)
ON_BN_CLICKED(IDOK, OnBnClickedOk)
ON_WM_SIZE()
ON_NOTIFY(NM_DBLCLK, IDC_LIST_FILE, OnNMDblclkListFile)
ON_COMMAND(ID_FILE_SHOW, OnFileShow)
ON_COMMAND(ID_FILE_HIDE, OnFileHide)
ON_COMMAND(ID_FILE_MOVE, OnFileMove)
ON_COMMAND(ID_FILE_DEL, OnFileDel)
ON_COMMAND(ID_FILE_DOWN, OnFileDown)
ON_NOTIFY(LVN_ENDLABELEDIT, IDC_LIST_FILE, OnLvnEndlabeleditListFile)
ON_NOTIFY(NM_RCLICK, IDC_LIST_FILE, OnNMRclickListFile)
ON_WM_DROPFILES()
ON_NOTIFY(HDN_ITEMCLICK, 0, OnHdnItemclickListFile)
ON_COMMAND(ID_FILE_RUNAT, OnFileRunat)
END_MESSAGE_MAP()
// CFileManageDlg 消息处理程序
DWORD WINAPI ThreadDoWork(LPVOID lp)
{
CFileManageDlg* pThis = (CFileManageDlg*)lp;
pThis->__OnDoWork();
return 0;
}
void CFileManageDlg::Layout()
{
if(m_ListFile.GetSafeHwnd() == NULL)
return ;
CRect rcWnd; GetClientRect(&rcWnd);
CRect rcFilePath;
m_ComBoxFilePath.GetClientRect(&rcFilePath);
rcFilePath.right = rcWnd.Width() - 120;
m_ComBoxFilePath.MoveWindow(&rcFilePath);
CRect rcList; rcList = rcWnd;
rcList.top = rcFilePath.Height();
// rcList.right -= 120;
rcList.bottom = rcWnd.Height() - 25;
m_ListFile.MoveWindow(&rcList);
CRect rcButtonOK; GetDlgItem(IDOK)->GetClientRect(&rcButtonOK);
GetDlgItem(IDOK)->MoveWindow(rcWnd.right - 100, 0, rcButtonOK.Width(), rcButtonOK.Height());
CRect rcStatus; rcStatus = rcWnd;
rcStatus.top = rcStatus.bottom - 25;
m_wndStatusBar.MoveWindow(&rcStatus);
}
void CFileManageDlg::__InitWorkVec()
{
m_DownFileVec.clear();
m_UpFileVec.clear();
m_DownDirVec.clear();
m_UpDirVec.clear();
}
void CFileManageDlg::__OnDoWork()
{
m_ListFile.EnableWindow(FALSE);
GetDlgItem(IDOK)->EnableWindow(FALSE);
for(DWORD i = 0; i < m_DownFileVec.size(); i ++)
{
m_hServer.DowndFile(m_DownFileVec[i].strP, m_DownFileVec[i].strR);
if(m_bIsClose)
return ;
}
for(DWORD i = 0; i < m_UpFileVec.size(); i ++)
{
m_hServer.UpFile(m_UpFileVec[i].strP, m_UpFileVec[i].strR);
if(m_bIsClose)
return ;
}
for(DWORD i = 0; i < m_DownDirVec.size(); i ++)
{
m_hServer.DowndFileDir(m_DownDirVec[i].strP, m_DownDirVec[i].strR);
if(m_bIsClose)
return ;
}
for(DWORD i = 0; i < m_UpDirVec.size(); i ++)
{
m_hServer.UpFileDir(m_UpDirVec[i].strP, m_UpDirVec[i].strR);
if(m_bIsClose)
return ;
}
m_ListFile.EnableWindow();
GetDlgItem(IDOK)->EnableWindow();
__OnGetFileInfo(GetCurrentPath());
}
void CFileManageDlg::OnBnClickedOk()
{
// TODO: 在此添加控件通知处理程序代码
CString strPath;
m_ComBoxFilePath.GetWindowText(strPath);
if( strPath == GetCurrentPath() )
return ;
if(strPath.GetLength() != 0)
__OnGetFileInfo(strPath);
else
__OnGetRoot();
//__OnGetRoot();
}
void CFileManageDlg::OnSize(UINT nType, int cx, int cy)
{
CDialog::OnSize(nType, cx, cy);
// TODO: 在此处添加消息处理程序代码
Layout();
}
BOOL CFileManageDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: 在此添加额外的初始化
m_hServer.SetCallBack(this);
m_ListFile.InsertColumn(0, L"名称", LVCFMT_LEFT, 300);
m_ListFile.InsertColumn(1, L"大小", LVCFMT_LEFT, 100);
m_ListFile.InsertColumn(2, L"修改时间", LVCFMT_LEFT, 200);
WCHAR szIp[100] =L"";
DWORD dwPort = m_hServer.GetPeer(szIp);
CString strS; strS.Format(L"%s:%d", szIp, dwPort);
strS += L"文件管理 文件上传支持鼠标拖拽";
SetWindowText(strS);
m_ListFile.DragAcceptFiles();
m_wndStatusBar.Create(WS_CHILD | WS_BORDER | WS_VISIBLE ,CRect(1, 1, 1, 1),this, 0x154);
// DragAcceptFiles();
CRect rc;
GetWindowRect(&rc);
rc.right = rc.left + 800;
rc.bottom = rc.top + 600;
MoveWindow(&rc);
Layout();
__InitImgList();
__OnGetRoot();
return TRUE; // return TRUE unless you set the focus to a control
// 异常: OCX 属性页应返回 FALSE
}
void CFileManageDlg::OnDownFileIng(LPCTSTR szPath, LPCTSTR szRemote, DWORD dwBig, DWORD dwLen, DWORD dwSTime)
{
CString strText, strP, strR;
DWORD dwEndTime = GetTickCount();
DWORD dwTime = dwEndTime - dwSTime;
if(dwTime == 0)
return ;
DWORD dwDown = dwLen / dwTime * 1000 ;
DWORD dwRTime = (dwBig - dwLen) / (dwLen / dwTime) / 1000;
strText.Format(_T("下载文件:%s 文件大小:%s 已经下载:%s 速度:%s/秒 剩余%s "), _GetNameByPath(szRemote), __MakeFileSizeString(dwBig), __MakeFileSizeString(dwLen), __MakeFileSizeString(dwDown), __MakeTimeString(dwRTime));
m_wndStatusBar.SetWindowText(strText);
}
void CFileManageDlg::OnUpFileIng(LPCTSTR szPath, LPCTSTR szRemote, DWORD dwBig, DWORD dwLen, DWORD dwSTime)
{
CString strText, strP, strR;
DWORD dwEndTime = GetTickCount();
DWORD dwTime = dwEndTime - dwSTime;
if(dwTime == 0)
return ;
DWORD dwDown = dwLen / dwTime * 1000 ;
DWORD dwRTime = (dwBig - dwLen) / (dwLen / dwTime) / 1000;
strText.Format(_T("上传文件:%s 文件大小:%s 已经上传:%s 速度:%s/秒 剩余%s "), _GetNameByPath(szRemote), __MakeFileSizeString(dwBig), __MakeFileSizeString(dwLen), __MakeFileSizeString(dwDown), __MakeTimeString(dwRTime));
m_wndStatusBar.SetWindowText(strText);
}
DWORD CFileManageDlg::_GetFileImg(LPCTSTR sz)
{
CString strType;
CString strT = sz;/*GetCurrentPath();*/
TCHAR sPath[MAX_PATH] = {0};
lstrcpy(sPath, strT);
if(_tcsrchr(sPath, _T('.')) != NULL)
strType = _tcsrchr(sPath, _T('.'));
else
strType = sPath;
if(strType[0] != _T('.'))
strType = sz;
if( strType.CompareNoCase(L".exe") == 0 )
return _GetFileImg(L"__exe");
if( strType.CompareNoCase(L".sys") == 0 )
strType = L".dll";
for (UINT i = 0; i < m_strFileType.size(); i++)
{
CString strT = m_strFileType[i];
if(strType.CompareNoCase(m_strFileType[i]) ==0)
return i;
}
HICON hIcon = __FindFileType(strType);
if ( hIcon )
{
_AddImgFileType(hIcon, strType);
return _GetFileImg(sz);
}
return 0;
}
HICON CFileManageDlg::__FindFileType(LPCTSTR szType)
{
CString strRet;
LPCTSTR szKey = szType;
if(lstrlen(szKey) == 0)
return NULL;
HKEY HKEYFileType;
long ret;
CString strFileType = szKey;
ret = RegOpenKeyEx(HKEY_CLASSES_ROOT, szKey, 0, KEY_READ, &HKEYFileType);
if(ret != ERROR_SUCCESS)
{
RegCloseKey(HKEYFileType);
return NULL;
}
DWORD dwType = REG_SZ;
TCHAR szVel[256] = {0};
DWORD dwLen = 256;
RegQueryValueEx(HKEYFileType, NULL, NULL, &dwType, (BYTE*)szVel, &dwLen);
RegCloseKey(HKEYFileType);
strRet = szVel;
if(strRet.GetLength() == 0)
return NULL;
{{
CString strFileType ; strFileType.Format(_T("%s\\DefaultIcon"), strRet);
ret = RegOpenKeyEx(HKEY_CLASSES_ROOT, strFileType, 0, KEY_READ, &HKEYFileType);
if(ret != ERROR_SUCCESS)
{
RegCloseKey(HKEYFileType);
return NULL;
}
DWORD dwType = REG_SZ;
TCHAR szVel[256] = {0};
DWORD dwLen = 256;
RegQueryValueEx(HKEYFileType, NULL, NULL, &dwType, (BYTE*)szVel, &dwLen);
RegCloseKey(HKEYFileType);
CString strIcoPath = szVel;
CString strFile;
int nFind = strIcoPath.Find(_T(","));
DWORD dwIndex = 0;
if(nFind == -1)
{
strFile = strIcoPath;
}
else
{
strFile = strIcoPath.Left(nFind);
CString strIndex = strIcoPath.Mid(nFind + 1);
LPCTSTR szIndex = strIndex.GetString() ;
for(DWORD i = 0; i < (DWORD)strIndex.GetLength(); i++)
{
if(_tstoi(szIndex) != 0)
{
dwIndex = _tstoi(szIndex);
break;
}
szIndex ++;
}
}
return ExtractIcon(NULL, strFile, dwIndex );
}}
return NULL;
}
void CFileManageDlg::_AddImgFileType(HICON hIcon, LPCTSTR strFile)
{
m_FileImgList.Add(hIcon);
m_strFileType.push_back(strFile);
}
void CFileManageDlg::_AddImgFileType(UINT dwImgFile, LPCTSTR strFile)
{
HICON hIcon =theApp.LoadIcon(dwImgFile);
_AddImgFileType(hIcon, strFile);
}
void CFileManageDlg::__InitImgList()
{
m_FileImgList.Create(16, 16, TRUE | ILC_COLOR32 , 1000, 0);
_AddImgFileType(IDI_ICON_NOTHING, L"__Nothing");
_AddImgFileType(IDI_ICON_FLOPPY, L"__floppy");
_AddImgFileType(IDI_ICON_EXE, L"__exe");
_AddImgFileType(IDI_ICON_DISK, L"__disk");
_AddImgFileType(IDI_ICON_DIR, L"__dir");
_AddImgFileType(IDI_ICON_CD, L"__cd");
m_ListFile.SetImageList(&m_FileImgList, LVSIL_SMALL);
}
bool CFileManageDlg::IsRoot()
{
CString strText = m_ListFile.GetItemText(0, 0);
if( strText[0] == _T('.'))
return false;
return true;
}
void CFileManageDlg::__OnGetRoot()
{
m_ListFile.DeleteAllItems();
CServerFileManageHandle::File_Root_Vector vectorRoot;
bool bIsOK = m_hServer.GetRoot(&vectorRoot);
if(bIsOK)
{
DWORD dwCount;
for(DWORD i = 0 ; i < vectorRoot.size(); i++)
{
DWORD dwType;
dwCount = m_ListFile.GetItemCount();
if(DRIVE_CDROM == vectorRoot[i].dwType)
dwType = _GetFileImg(L"__cd");
else if(DRIVE_FIXED == vectorRoot[i].dwType)
dwType = _GetFileImg(L"__disk");
else
dwType = _GetFileImg(L"__floppy");
int nRet = m_ListFile.InsertItem(dwCount, vectorRoot[i].szName, dwType);
CString strDeskLen; strDeskLen.Format(L"%4.1f/%4.1fG", (float) vectorRoot[i].ullcanusebyte / 1024 / 1024 / 1024 , (float)vectorRoot[i].ulltotalspace / 1024 /1024/ 1024);
m_ListFile.SetItemText(nRet, 1, strDeskLen);
}
}
}
CString CFileManageDlg::__MakeTimeString(DWORD dwTime)
{
CString strRet;
if ( dwTime < 60)
strRet.Format(_T("%d秒"), dwTime);
else if (dwTime < 60 * 60)
strRet.Format(_T("%d分钟"), dwTime / 60);
else if (dwTime < 60 * 60 * 24)
strRet.Format(_T("%d小时"), dwTime / 60 / 60);
else
strRet.Format(_T("%d天"), dwTime / 60 / 60 / 24);
return strRet;
}
CString CFileManageDlg::__MakeFileSizeString(DWORD dwSize)
{
CString strRet;
if(dwSize < 1024)
strRet.Format(L"%d字节", dwSize);
else if (dwSize > 1024 && dwSize < 1024 * 1024)
strRet.Format(L"%4.2f KB", (float)dwSize / 1024);
else if(dwSize > 1024 * 1024 && dwSize < 1024 * 1024 * 1024)
strRet.Format(L"%4.2f MB", (float)dwSize / 1024 / 1024);
else
strRet.Format(L"%4.2f GB", (float)dwSize / 1024 / 1024 / 1024);
return strRet;
}
CString CFileManageDlg::__MakeFileTimeString(FILETIME& fileTime)
{
SYSTEMTIME systime;
FileTimeToSystemTime(&fileTime, &systime);
CString strTime;
strTime.Format(_T("%d-%d-%d %d:%d:%02d"), systime.wYear, systime.wMonth, systime.wDay, systime.wHour, systime.wMinute, systime.wSecond);
return strTime;
}
void CFileManageDlg::__OnGetFileInfo(LPCTSTR szPath)
{
m_ListFile.DeleteAllItems();
CServerFileManageHandle::File_Info_Vector vectorFile;
bool bIsOK = m_hServer.GetFileInfo(szPath, &vectorFile);
m_FileInfo.clear();
if(bIsOK)
{
DWORD dwCount;
int nRetAdd;
DWORD dwVecCount;
CString strText;
for(DWORD i = 0 ; i < vectorFile.size(); i++)
{
if(vectorFile[i].cFileName[0] == _T('.') )
continue;
dwCount = m_ListFile.GetItemCount();
if(!vectorFile[i].bIsDir)
nRetAdd = m_ListFile.InsertItem(dwCount, vectorFile[i].cFileName, _GetFileImg(vectorFile[i].cFileName));
else
nRetAdd = m_ListFile.InsertItem(dwCount, vectorFile[i].cFileName, _GetFileImg(L"__dir"));
if(!vectorFile[i].bIsDir)
m_ListFile.SetItemText(nRetAdd, 1, __MakeFileSizeString(vectorFile[i].dwFileSize ));
m_ListFile.SetItemText(nRetAdd, 2, __MakeFileTimeString(vectorFile[i].ftLastWriteTime ));
m_FileInfo.push_back(vectorFile[i]);
dwVecCount = m_FileInfo.size() - 1;
m_ListFile.SetItemData(nRetAdd, dwVecCount);
}
}
m_ListFile.InsertItem(0, L"..向上", _GetFileImg(L"__dir"));
m_ComBoxFilePath.SetWindowText(szPath);
SetCurrentPath(szPath);
}
CString CFileManageDlg::GetCurrentUpPath(LPCTSTR str)
{
CString strT = str;/*GetCurrentPath();*/
TCHAR sPath[MAX_PATH] = {0};
lstrcpy(sPath, strT);
if(sPath[lstrlen(sPath) -1] == _T('\\'))
sPath[lstrlen(sPath) -1] = 0;
*(_tcsrchr(sPath, _T('\\')) +1) = 0;
strT = sPath;
return strT;
}
CString CFileManageDlg::GetCurrentPath()
{
return m_strCurrentPath;
}
void CFileManageDlg::SetCurrentPath(LPCTSTR sz)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -