⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 deletecertainfiledlg.cpp

📁 Visual.C++程序设计技巧与实例--配套光盘 第6章 文件和系统操作 本章共11个实例: 1. FolderCopy文件夹的选择和拷贝 2. DeleteCertainFile删除指定路
💻 CPP
字号:
// DeleteCertainFileDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DeleteCertainFile.h"
#include "DeleteCertainFileDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDeleteCertainFileDlg dialog

CDeleteCertainFileDlg::CDeleteCertainFileDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDeleteCertainFileDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDeleteCertainFileDlg)
	m_StrDirEdit = _T("");
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDeleteCertainFileDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDeleteCertainFileDlg)
	DDX_Control(pDX, IDC_COMBO_FILEEXTENSION, m_FileExtenCombo);
	DDX_Text(pDX, IDC_DIRECTORYEDIT, m_StrDirEdit);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDeleteCertainFileDlg, CDialog)
	//{{AFX_MSG_MAP(CDeleteCertainFileDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_GETDIR, OnGetdir)
	ON_BN_CLICKED(IDC_DELETEFILE, OnDeletefile)
	ON_CBN_EDITCHANGE(IDC_COMBO_FILEEXTENSION, OnEditchangeComboFileextension)
	ON_CBN_SELCHANGE(IDC_COMBO_FILEEXTENSION, OnSelchangeComboFileextension)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeleteCertainFileDlg message handlers

BOOL CDeleteCertainFileDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// 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
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

// 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 CDeleteCertainFileDlg::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 CDeleteCertainFileDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CDeleteCertainFileDlg::OnGetdir() 
{
	//得到所选路径
	m_path=GetDirectoryPath();
	m_StrDirEdit=m_path;
	//将变量的值更新到控件
	UpdateData(FALSE);
    //显示路径下的文件
	DisplayFileList(m_path);
	// TODO: Add your control notification handler code here
	
}
CString CDeleteCertainFileDlg::GetDirectoryPath()
{
	//定义文件夹的路径
    CString strPath ="";
	//定义指向IMalloc的指针
    LPMALLOC  pMalloc;  
    if (::SHGetMalloc(&pMalloc) != NOERROR)
        return strPath;
    char szDisplayName[_MAX_PATH];
    char szBuffer[_MAX_PATH];

    //定义BROWSEINFO结构browseInfo
    BROWSEINFO browseInfo;

    //browseInfo结构赋值
    browseInfo.hwndOwner = this->m_hWnd;
    browseInfo.pidlRoot = NULL;
    browseInfo.pszDisplayName = szDisplayName;
    browseInfo.lpszTitle = "Select a path";  // Dialog title
    browseInfo.ulFlags = BIF_RETURNFSANCESTORS|BIF_RETURNONLYFSDIRS;
    browseInfo.lpfn = NULL;      
    browseInfo.lParam = 0;      

    LPITEMIDLIST lpItemIDList;
    if ((lpItemIDList = ::SHBrowseForFolder(&browseInfo))
        != NULL)
    {
		      //得到用户选择的路径
           if (::SHGetPathFromIDList(lpItemIDList, szBuffer))
           {
            // 这时szBuffer包含了用户选择的路径.
            if (szBuffer[0] == '\0')
            {
                AfxMessageBox("不能得到所选路径!",
                    MB_ICONSTOP|MB_OK);
                return strPath;
            }
			  //返回所选的文件架
            strPath = szBuffer;
            return strPath;
        }
        else
        {
    			AfxMessageBox("不能得到所选的文件夹!",
                MB_ICONSTOP|MB_OK);
			//返回的路径为空
            return strPath; // strPath is empty
        }
        pMalloc->Free(lpItemIDList);
        pMalloc->Release();
    }
    return strPath;
}

void CDeleteCertainFileDlg::OnDeletefile() 
{
	if(m_path!="" && m_fileexten!="")
	{
		CString str,strdel;
		
		//str="C:\\My Documents\\*.txt"; //待删除的文件在C:\\My Documents下
		//待删除的文件的后缀名为buaa
		
		char string[80];
		strcpy( string, m_path);
		strcat( string, "\\*" );
		strcat( string, m_fileexten );
		//strcat( string, m_FileExtenCombo.GetLBText(m_FileExtenCombo.GetCurSel() );
		
		CFileFind filefind;
		if(filefind.FindFile(string,0)!=0)
			//if(filefind.FindFile(str,0)!=0)
		{
			while(filefind.FindNextFile()!=0)
			{
				strdel=filefind.GetFilePath();
				CFile::Remove(strdel);
			}
			strdel=filefind.GetFilePath();
			CFile::Remove(strdel);
			AfxMessageBox("文件删除完成!");
		}
		
	}
	else
	{
		AfxMessageBox("目录为空或者文件扩展名为空,不能删除!");
	}

	//刷新文件列表
	DisplayFileList(m_path);
	
}

void CDeleteCertainFileDlg::OnEditchangeComboFileextension() 
{
	//UpdateData(FALSE);
	// TODO: Add your control notification handler code here
	
}

void CDeleteCertainFileDlg::OnSelchangeComboFileextension() 
{
	//UpdateData(FALSE);
	m_FileExtenCombo.GetLBText(m_FileExtenCombo.GetCurSel(),m_fileexten);
	// TODO: Add your control notification handler code here
	
}

//此函数用于在ListBox中显示某路径下的文件
void CDeleteCertainFileDlg::DisplayFileList(CString strdir) 
{
	TCHAR szPath [MAX_PATH];
	strcpy(szPath,strdir);
	//DlgDirList函数用于向ListBox中加入某路径下的文件
	int nReslt = DlgDirList(
		szPath,//路径
		IDC_FILELIST,//ListBox的资源ID
		0,//静态控件ID.此控件显示的文本是当前选择的驱动器和路径,
		//如果不用显示这些驱动器和路径,置为0
		DDL_READWRITE|DDL_READONLY|DDL_HIDDEN|
		DDL_SYSTEM|DDL_ARCHIVE);
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -