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

📄 changenamedlg.cpp

📁 windows下修改文件名的源代码
💻 CPP
字号:
// ChangeNameDlg.cpp : implementation file
//

#include "stdafx.h"
#include "ChangeName.h"
#include "ChangeNameDlg.h"
#include "PathDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CChangeNameDlg dialog

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

void CChangeNameDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CChangeNameDlg)
	DDX_Control(pDX, IDC_LIST1, m_List);
	DDX_Text(pDX, IDC_NEWSTR, m_NewStr);
	DDX_Text(pDX, IDC_OLDSTR, m_OldStr);
	DDX_Text(pDX, IDC_PATH, m_Path);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CChangeNameDlg, CDialog)
	//{{AFX_MSG_MAP(CChangeNameDlg)
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_BN_CLICKED(IDC_EXIT, OnExit)
	ON_BN_CLICKED(IDC_LOOK, OnLook)
	ON_BN_CLICKED(IDC_CHANGE, OnChange)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CChangeNameDlg message handlers

BOOL CChangeNameDlg::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
	char strPath[255];
	GetCurrentDirectory(255,strPath);
	prePath = strPath;
	int len = prePath.GetLength();
	if(len != 3)
		prePath.Insert(len,"\\");
	m_Path = prePath;
	UpdateData(false);
	m_List.Dir(0x0000,"*.*");
	int num = m_List.GetCount();
	char buf[255];
	wsprintf(buf,"文件换名[%d]",num);
	SetWindowText(buf);
	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 CChangeNameDlg::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 CChangeNameDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

void CChangeNameDlg::OnExit() 
{
	// TODO: Add your control notification handler code here
	OnOK();	
}

void CChangeNameDlg::OnLook() 
{
	// TODO: Add your control notification handler code here

	CString strYourCaption("打开文件夹");
	CString strYourTitle("当前路径:");
	CPathDialog dlg(strYourCaption, strYourTitle, prePath);
	if(dlg.DoModal()!=IDOK)	return ;

	CString fPath = dlg.GetPathName(); 
	int len = fPath.GetLength();
	if(len != 3)
		fPath.Insert(len,"\\");
	m_Path = fPath;
	prePath = fPath;
	UpdateData(false);

	SetCurrentDirectory(fPath);
	m_List.ResetContent();
	m_List.Dir(0x0000,"*.*");
	int num = m_List.GetCount();
	char buf[255];
	wsprintf(buf,"文件换名[%d]",num);
	SetWindowText(buf);
}

void CChangeNameDlg::OnChange() 
{
	// TODO: Add your control notification handler code here
	UpdateData(true);
	if(m_OldStr.GetLength == 0 || m_NewStr.GetLength() == 0)
	{
		MessageBox("替换的字符串不能为空!",NULL,MB_OK | MB_ICONSTOP);
		return ;
	}
	int i,num = m_List.GetCount();
	if(num <= 0)	return;
	CString pName;
	char OldName[255],NewName[255];
	for(i=0;i<num;i++)
	{
		m_List.GetText(i,pName);
		if(pName.Find(m_OldStr) == -1)	continue;
		strcpy(OldName,prePath);
		strcat(OldName,pName);
		pName.Replace(m_OldStr,m_NewStr);
		strcpy(NewName,prePath);
		strcpy(NewName,pName);
		rename(OldName,NewName);
	}
	m_List.ResetContent();
	m_List.Dir(0x0000,"*.*");
	MessageBox("替换操作已经完成!","完成",MB_OK | MB_ICONINFORMATION);
}

⌨️ 快捷键说明

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