自己编写的C++个人光盘自助管理系统--课程设计
////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////// 个人光盘管理器V1.0 ////////////////////////////
///////////////////////////// 021104429~33编 ///////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////////////////////
// rtDlg.cpp : implementation file
//
#include "stdafx.h"
#include "rt.h"
#include "rtDlg.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
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);
//DDX_Text(pDX,IDC_EDIT2,m_strfile);
//DDX_control(pDX,IDC_LIST1,m_list);
//{{AFX_DATA_MAP(CAboutDlg)
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//ON_WM_PAINT()
//ON_WM_QUERYDRAGICON()
//{{AFX_MSG_MAP(CAboutDlg)
// No message handlers
//}}AFX_MSG_MAP
//ON_BN_CLICKED(IDC_BTN_SEARCH,OnBnClickedBtnSearch)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRtDlg dialog
CRtDlg::CRtDlg(CWnd* pParent /*=NULL*/)
: CDialog(CRtDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CRtDlg)
//m_path = _T("");
//m_strfile = _T("");
//}}AFX_DATA_INIT
// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}
void CRtDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CRtDlg)
DDX_Control(pDX, IDC_TREE1, m_tree);
DDX_Control(pDX, IDC_LIST1, m_list);
DDX_Control(pDX, IDC_COMBO1, m_Combox);
//DDX_Text(pDX, IDC_EDIT1, m_path);
DDX_Text(pDX, IDC_EDIT2, m_strfile);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CRtDlg, CDialog)
//{{AFX_MSG_MAP(CRtDlg)
ON_WM_SYSCOMMAND()
ON_WM_PAINT()
ON_WM_QUERYDRAGICON()
ON_BN_CLICKED(ID_EDIT_FIND, OnEditFind)
ON_NOTIFY(DTN_CLOSEUP, IDC_DATETIMEPICKER2, OnCloseupDatetimepicker2)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CRtDlg message handlers//消息处理程序
//对话框类初始化函数
BOOL CRtDlg::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);
}
}
//设置此对话框图标。当应用程序主窗口不是对话框时,框架将自动执行此操作
SetIcon(m_hIcon, TRUE); //设置大图标
SetIcon(m_hIcon, FALSE); //小图标
// TODO:在此处添加额外的初始化代码
CString path;
path="E:";
m_Root=path;
HTREEITEM TreeItem=m_tree.InsertItem(m_Root,TVI_ROOT,TVI_SORT); //显示树形目录
m_Combox.SetCurSel(3);
path+="\\*.*";
FindFile(path,TreeItem); //查找目录
DeleteListItem(); //删除LIST里的子文件夹
FindFileName(path); //查找文件名
m_tree.Expand(TreeItem,TVE_EXPAND);
m_iIndex=0;
for(int i=0;i<20;i++)
strName[i]=_T("");
m_Expand=TRUE;
return TRUE; //除非设置了控件的焦点,否则返回TRUE
}
void CRtDlg::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 CRtDlg::OnPaint()
{
if (IsIconic())
{
CPaintDC dc(this); // 用于绘制的设备上下文
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();
}
}
// 当用户拖动最小化窗口时系统调用此函数取得光标显示
HCURSOR CRtDlg::OnQueryDragIcon()
{
return (HCURSOR) m_hIcon;
}
//
void CRtDlg::FindFile(CString pszPath,HTREEITEM TreeItem)
{
CString str;
HANDLE hFind;
WIN32_FIND_DATA fd ;
hFind=::FindFirstFile((LPCTSTR)pszPath,&fd); //查找文件夹
if(hFind!=INVALID_HANDLE_VALUE)
{
do
{
if((fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
{
str=(LPCTSTR)&fd.cFileName; //显示文件
if((str!=_T(".")) && (str!=_T(".."))) //如果有子目录
{
m_tree.InsertItem(str,TreeItem,TVI_SORT);
}
}
}
while(::FindNextFile(hFind,&fd));
}
else //找不到光盘文件
{
m_tree.DeleteAllItems();
DeleteListItem();
MessageBox(“路径不对或设备没准备好|请检查!”
,"Error",MB_OK|MB_OKCANCEL|MB_ICONERROR);
}
FindClose(hFind);
}
//
void CRtDlg::FindFileName(CString pszPath)
{
CString str;
HANDLE hFind;
WIN32_FIND_DATA fd ;
hFind=::FindFirstFile((LPCTSTR)pszPath,&fd); //搜索
if(hFind!=INVALID_HANDLE_VALUE)
{
do
{
if(!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY ))
{
str=(LPCTSTR)&fd.cFileName;
m_list.AddString(str);
}
}
while(::FindNextFile(hFind,&fd));
}
FindClose(hFind);
for(int i=0;i<20;i++) //显示
strName[i]=_T("");
m_iIndex=0;
}
//
CString CRtDlg::GetParentName(HTREEITEM TreeItem)
{
HTREEITEM ParentTreeItem;
ParentTreeItem=m_tree.GetParentItem(TreeItem);
CString str=m_tree.GetItemText(ParentTreeItem); //使TREE显示文件名
return str;
}
//
void CRtDlg::DeleteListItem()
{
int m_Count=m_list.GetCount();
while(m_Count>=0)
{
m_list.SetCurSel(0);
m_Count=m_list.DeleteString(0);
}
}
//
void CRtDlg::OnSelchangeCombo1()
{
int iIndex=m_Combox.GetCurSel();
CString str;
m_Combox.GetLBText(iIndex,str);
if(str!=m_Root)
{
m_tree.DeleteAllItems();
m_Root=str;
str+="\\*.*";
HTREEITEM TreeItem=m_tree.InsertItem(m_Root,TVI_ROOT,TVI_SORT);
FindFile(str,TreeItem);
DeleteListItem();
FindFileName(str);
m_tree.Expand(TreeItem,TVE_EXPAND);
}
}
//
//为Tree Control控件添加LBN_SELCHANGED消息代码
void CRtDlg::OnSelchangedTree1(NMHDR* pNMHDR, LRESULT* pResult)
{
NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
CString str,Path,PathName;
PathName=_T("");
Path=_T("");
HTREEITEM ParentTreeItem;
HTREEITEM TreeItem=m_tree.GetSelectedItem();
if(!m_tree.Expand(TreeItem,TVE_EXPAND)) //如果子文件夹为空
{
m_Expand=TRUE;
}
else
m_Expand=FALSE;
ParentTreeItem=TreeItem;
str=m_tree.GetItemText(TreeItem);
while(str!=m_Root)
{
Path=GetParentName(ParentTreeItem);
ParentTreeItem=m_tree.GetParentItem(ParentTreeItem);
strName[m_iIndex]=Path;
if(Path==m_Root)
{
for(int i=m_iIndex;i>=0;i--)
PathName+=strName[i]+"\\";
PathName+=str;
PathName+="\\*.*";
DeleteListItem();
if(m_Expand)
FindFile(PathName,TreeItem);
FindFileName(PathName);
m_tree.Expand(TreeItem,TVE_EXPAND);
break;
}
m_iIndex++;
}
if(str==m_Root)
{
str=str+"\\*.*";
DeleteListItem();
FindFileName(str);
m_tree.Expand(TreeItem,TVE_EXPAND);
}
*pResult = 0;
}
//
void CRtDlg::OnEditFind()
{
// TODO: 在此处添加控件通知处理程序代码
UpdateData(TRUE);
if(!m_strfile.GetLength())
{
AfxMessageBox("请输入要查找的文件名和路径");
return;
}
WIN32_FIND_DATA fd;
m_list.ResetContent();//保存查找结果
HANDLE hd=FindFirstFile((LPCTSTR)m_strfile,&fd);//开始查找
if(hd==INVALID_HANDLE_VALUE)
return;
m_list.AddString(fd.cFileName);
while(FindNextFile(hd,&fd))
{
m_list.AddString(fd.cFileName);
};
FindClose(hd);//关闭查找
}
void CRtDlg::OnCloseupDatetimepicker2(NMHDR* pNMHDR, LRESULT* pResult)
{
// TODO: Add your control notification handler code here
*pResult = 0;
}
运行结果界面,例: ...........niu de 强劲来.....我那个C++哦..作孽来,什么都不懂
页:
[1]