📄 8dlg.cpp
字号:
// 8Dlg.cpp : implementation file
//
#include "stdafx.h"
#include "8.h"
#include "8Dlg.h"
#include <math.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
bool flag=true;
bool compare=true;
bool insert=true;
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy8Dlg dialog
CMy8Dlg::CMy8Dlg(CWnd* pParent /*=NULL*/)
: CDialog(CMy8Dlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CMy8Dlg)
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDI_ICON1);
byte=0;
total_amount=0;
sPath="";
}
void CMy8Dlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CMy8Dlg)
DDX_Control(pDX, IDC_STATIC_4, m_4);
DDX_Control(pDX, IDC_STATIC_3, m_3);
DDX_Control(pDX, IDC_STATIC_2, m_2);
DDX_Control(pDX, IDC_STATIC_1, m_1);
DDX_Control(pDX, IDC_TREE1, m_tree);
DDX_Control(pDX, IDC_LIST2, m_caller_list);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CMy8Dlg, CDialog)
//{{AFX_MSG_MAP(CMy8Dlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(IDC_ADD, OnAdd)
ON_NOTIFY(TVN_SELCHANGED, IDC_TREE1, OnSelchangedTree1)
ON_BN_CLICKED(IDC_CLEAR, OnClear)
ON_WM_ERASEBKGND()
ON_NOTIFY(LVN_COLUMNCLICK, IDC_LIST2, OnColumnclickList2)
ON_NOTIFY(NM_DBLCLK, IDC_LIST2, OnDblclkList2)
ON_NOTIFY(TVN_ITEMEXPANDED, IDC_TREE1, OnItemexpandedTree1)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMy8Dlg message handlers
int nSubItem;
BOOL CMy8Dlg::OnInitDialog()
{
CDialog::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)
{
CString strAboutMenu;
strAboutMenu.LoadString(IDS_ABOUTBOX);
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, TRUE); // Set small icon
// TODO: Add extra initialization here
ModifyStyle(0,WS_MINIMIZEBOX);
char *szColumn[]={"路径","大小"};
int widths[]={250,98,50};
LV_COLUMN lvc;
lvc.mask=LVCF_FMT|LVCF_WIDTH|LVCF_TEXT|LVCF_SUBITEM;
lvc.fmt=LVCFMT_LEFT;
for(int i=0;i<2;i++)
{//插入各列
lvc.pszText=szColumn[i];
lvc.cx=widths[i];
lvc.iSubItem=i;
m_caller_list.InsertColumn(i,&lvc);
}
m_caller_list.SetExtendedStyle(m_caller_list.GetExtendedStyle()|LVS_EX_GRIDLINES|
LVS_EX_FULLROWSELECT);
DWORD dwStyle = GetWindowLong(m_tree.m_hWnd,GWL_STYLE);
dwStyle |= TVS_HASBUTTONS | TVS_HASLINES | TVS_LINESATROOT;
SetWindowLong(m_tree.m_hWnd,GWL_STYLE,dwStyle);
m_hRoot = m_tree.InsertItem("我的电脑");
GetLogicalDrives(m_hRoot);
GetDriveDir(m_hRoot);
//m_tree.Expand(m_hRoot,TVE_EXPAND);
CFont *font=new CFont;
//font->CreatePointFont(5,"宋体");
font->CreateFont(16,0,0,0,FW_DONTCARE,FALSE,FALSE,0,ANSI_CHARSET,OUT_DEFAULT_PRECIS,
CLIP_DEFAULT_PRECIS,DEFAULT_QUALITY,DEFAULT_PITCH|FF_SWISS,"楷体");
m_1.SetFont(font);
m_2.SetFont(font);
m_3.SetFont(font);
m_4.SetFont(font);
m_myToolTip.Create(this);
m_myToolTip.Activate(TRUE);
m_myToolTip.AddTool(GetDlgItem(IDC_LIST2),"双击路径详细查看文件夹");
m_myToolTip.AddTool(GetDlgItem(IDC_TREE1),"选择待统计文件夹");
return TRUE; // return TRUE unless you set the focus to a control
}
void CMy8Dlg::OnSysCommand(UINT nID, LPARAM lParam)
{
if ((nID & 0xFFF0) == IDM_ABOUTBOX)
{
CAboutDlg dlgAbout;
dlgAbout.DoModal();
}
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 CMy8Dlg::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 CMy8Dlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
void CMy8Dlg::OnAdd()
{
// TODO: Add your control notification handler code here
double none_file=0;
double total_file=0;
static int index=0;
CString str_total_space;
CString str_free_space;
CString str_used_space;
CFileFind ff;
BOOL bFound;
if(sPath=="")
return;
bFound=ff.FindFile(sPath+"\\*.*");
while(bFound)
{
bFound=ff.FindNextFile();
CString sFilePath=ff.GetFilePath();
if(ff.IsDirectory())
{
if(!ff.IsDots())
{
m_caller_list.InsertItem(0,sFilePath);
ListFolder(sFilePath);
total_file+=byte;
WeightCount(byte);
byte=0;
}
}
else
none_file+=ff.GetLength();
}
total_file+=none_file;
m_caller_list.SetItemText(0,0,"非目录文件");
WeightCount(none_file);
insert=false;
GetDlgItem(IDC_EDIT1)->SetWindowText(sPath.Left(3));
GetDiskFreeSpaceEx(sPath.Left(3),&uliUserFree,&uliTotal,&uliRealFree);
str_free_space=WeightCount((double)(LONGLONG)uliUserFree.QuadPart);
GetDlgItem(IDC_EDIT2)->SetWindowText(str_free_space);
str_total_space=WeightCount((double)(LONGLONG)uliTotal.QuadPart);
GetDlgItem(IDC_EDIT3)->SetWindowText(str_total_space);
str_used_space=WeightCount((double)(LONGLONG)uliTotal.QuadPart-
(double)(LONGLONG)uliUserFree.QuadPart);
GetDlgItem(IDC_EDIT4)->SetWindowText(str_used_space);
ff.Close();
}
void CMy8Dlg::ListFolder(CString sPath)
{
CFileFind ff;
BOOL bFound;
bFound=ff.FindFile(sPath+"\\*.*");
while(bFound)
{
bFound=ff.FindNextFile();
CString sFilePath=ff.GetFilePath();
if(ff.IsDirectory())
{
if(!ff.IsDots())
ListFolder(sFilePath);
}
else
{
CString str;
char temp[300];
byte+=ff.GetLength();
_gcvt(byte,10,temp);
str=temp;
WeightCount(byte);
m_caller_list.UpdateWindow();
}
}
ff.Close();
}
CString CMy8Dlg::WeightCount(double dtemp)
{
CString str;
char temp[300];
_gcvt(dtemp,10,temp);
str=temp;
if(dtemp==0)
str+="0B";
else
if(0<dtemp&&dtemp<=1024*0.8)
str+="B";
else
if(1024*0.8<dtemp&&dtemp<=(1024*1024*0.8))
{
_gcvt((int((dtemp/1024*100.0+0.05))/100.0),10,temp);
str=temp;
str+="KB";
}
else
if((1024*1024*0.8)<dtemp&&dtemp<=(1024*1024*1024*0.8))
{
_gcvt((int((dtemp/(1024*1024)*100.0+0.05))/100.0),10,temp);
str=temp;
str+="MB";
}
else
{
_gcvt((int((dtemp/(1024*1024*1024)*100.0+0.05))/100.0),10,temp);
str=temp;
str+="GB";
}
if(insert)
m_caller_list.SetItemText(0,1,str);
return str;
}
void CMy8Dlg::GetLogicalDrives(HTREEITEM hParent)
{
int szAllDriveStrings = GetLogicalDriveStrings(0,NULL);
char *pDriveStrings = new char[szAllDriveStrings];//MSDN
GetLogicalDriveStrings(szAllDriveStrings,pDriveStrings);//MSDN
int szDriveString = strlen(pDriveStrings);
while(szDriveString > 0)
{
m_tree.InsertItem(pDriveStrings,hParent);
pDriveStrings += szDriveString + 1;
szDriveString = strlen(pDriveStrings);
}
}
void CMy8Dlg::GetDriveDir(HTREEITEM hParent)
{
HTREEITEM hChild = m_tree.GetChildItem(hParent);
while(hChild)
{
CString strText = m_tree.GetItemText(hChild);
if(strText.Right(1) != "\\")
strText += _T("\\");
strText += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strText);
while(bContinue)
{
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hChild);
}
GetDriveDir(hChild);
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
}
void CMy8Dlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
else
sPath= GetFullPath(item.hItem);
}
CString CMy8Dlg::GetFullPath(HTREEITEM hCurrent)
{
CString strTemp;
CString strReturn = "";
while(hCurrent != m_hRoot)
{
strTemp = m_tree.GetItemText(hCurrent);
if(strTemp.Right(1) != "\\")
strTemp += "\\";
strReturn = strTemp + strReturn;
hCurrent = m_tree.GetParentItem(hCurrent);
}
return strReturn;
}
void CMy8Dlg::OnClear()
{
// TODO: Add your control notification handler code here
byte=0;
total_amount=0;
sPath="";
m_caller_list.DeleteAllItems();
insert=true;
GetDlgItem(IDC_EDIT1)->SetWindowText("");
GetDlgItem(IDC_EDIT2)->SetWindowText("");
GetDlgItem(IDC_EDIT3)->SetWindowText("");
GetDlgItem(IDC_EDIT4)->SetWindowText("");
}
BOOL CMy8Dlg::OnEraseBkgnd(CDC* pDC)
{
// TODO: Add your message handler code here and/or call default
CRect rect;
GetClientRect(&rect);//得到窗体的大小
CDC dcMem;
dcMem.CreateCompatibleDC(pDC);
CBitmap bmpBackground;
bmpBackground.LoadBitmap(IDB_BITMAP1);//加载背景图片
BITMAP bitMap;
bmpBackground.GetBitmap(&bitMap);
CBitmap *pbmpOld=dcMem.SelectObject(&bmpBackground);
pDC->StretchBlt(0,0,rect.Width(),rect.Height(),&dcMem,0,0,
bitMap.bmWidth, bitMap.bmHeight,SRCCOPY);//该函数给对话框贴上位图
return true;
//return CDialog::OnEraseBkgnd(pDC);
}
int MyMax(double i,double j)
{
if(i<j)
return -1;
else
if(i==j)
return 0;
else
return 1;
}
int MyMin(double i,double j)
{
if(i>j)
return -1;
else
if(i==j)
return 0;
else
return 1;
}
void ResetStr(CString str,double &temp)
{
int index=0;
if(str.Right(2)=="KB")
index=0;
else
if(str.Right(2)=="MB")
index=1;
else
if(str.Right(2)=="GB")
index=2;
else
index=-1;
temp=atof(str.Left(str.GetLength()-2));
temp*=pow(1024,index);
}
static int CALLBACK ListCompare(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
{
double temp1;
double temp2;
CListCtrl* pListCtrl=(CListCtrl*)lParamSort;
CString strItem1=pListCtrl->GetItemText(lParam1,1);
CString strItem2=pListCtrl->GetItemText(lParam2,1);
ResetStr(strItem1,temp1);
ResetStr(strItem2,temp2);
if(compare)
return MyMax(temp1,temp2);
else
return MyMin(temp1,temp2);
}
void CMy8Dlg::OnColumnclickList2(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_LISTVIEW* pNMListView = (NM_LISTVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
int i=m_caller_list.GetItemCount();
while(i--)
m_caller_list.SetItemData(i,i);
//LPNMITEMACTIVATE temp=(LPNMITEMACTIVATE)pNMHDR; /////////??????
if(!flag)
{
SetHeaderBitmap(1,IDB_BITMAP3,NULL);
flag=true;
compare=false;
m_caller_list.SortItems(ListCompare,(DWORD)&m_caller_list);
}
else
{
SetHeaderBitmap(1,IDB_BITMAP2,NULL);
flag=false;
compare=true;
m_caller_list.SortItems(ListCompare,(DWORD)&m_caller_list);
}
*pResult = 0;
}
void CMy8Dlg::SetHeaderBitmap(int nCol, int nBitmap, DWORD dwRemove)
{
CHeaderCtrl* pHeader=(CHeaderCtrl*)m_caller_list.GetDlgItem(0);
if(pHeader==NULL)
return ;
HD_ITEM hdi;
hdi.mask=HDI_FORMAT;
pHeader->GetItem(nCol,&hdi);
hdi.mask=HDI_BITMAP|HDI_FORMAT;
hdi.fmt|=HDF_BITMAP;
hdi.hbm =(HBITMAP)::LoadImage(AfxGetInstanceHandle(),MAKEINTRESOURCE(nBitmap),
IMAGE_BITMAP,0,0,LR_LOADMAP3DCOLORS);
if(dwRemove)
hdi.fmt&= ~dwRemove;
pHeader->SetItem(nCol,&hdi);
}
void CMy8Dlg::OnDblclkList2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
CString path="";
int row=0;
LPNMITEMACTIVATE temp=(LPNMITEMACTIVATE)pNMHDR;
row=temp->iItem;
path=m_caller_list.GetItemText(row,0);
if(path!="")
ShellExecute(m_hWnd, NULL, path, NULL, NULL, SW_SHOWNORMAL);
*pResult = 0;
}
BOOL CMy8Dlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
m_myToolTip.RelayEvent(pMsg);
return CDialog::PreTranslateMessage(pMsg);
}
void CMy8Dlg::OnItemexpandedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
// TODO: Add your control notification handler code here
TVITEM item = pNMTreeView->itemNew;
if(item.hItem == m_hRoot)
return;
HTREEITEM hChild = m_tree.GetChildItem(item.hItem);
while(hChild)
{
AddSubDir(hChild);
hChild = m_tree.GetNextItem(hChild,TVGN_NEXT);
}
*pResult = 0;
}
void CMy8Dlg::AddSubDir(HTREEITEM hParent)
{
CString strPath = GetFullPath(hParent);
if(strPath.Right(1) != "\\")
strPath += "\\";
strPath += "*.*";
CFileFind file;
BOOL bContinue = file.FindFile(strPath);
while(bContinue)
{
bContinue = file.FindNextFile();
if(file.IsDirectory() && !file.IsDots())
m_tree.InsertItem(file.GetFileName(),hParent);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -