65.9K
CodeProject 正在变化。 阅读更多。
Home

MFC 项目从现有代码一键生成

2016 年 5 月 13 日

CPOL

3分钟阅读

viewsIcon

16887

downloadIcon

491

从现有代码创建 MFC 项目的替代方法

引言

MFC Visual C++ 项目编辑器中,文件->新建项目->从现有代码... 命令可用。如果您从未用过此命令,不妨试一下。 您将会经历很多阶段,而且需要满足各种条件。

背景

尽管如此,创建现有项目的副本并使用另一个名称的可能性证明是有用的。可以在已开发的功能的基础上开发新的特性和选项。

我开发了一个简单的代码,只需单击一下即可创建现有项目的副本并使用另一个名称。 与标准的 文件->新建项目->从现有代码... 命令不同的是,您可以在创建新项目后根据需要更改其属性。

Using the Code

我的项目 ProjRename 的基础是使用标准的 MFC 程序初始化的,用于创建基于对话框的应用程序。该过程非常简单,如下所示:

  • 选择包含要重命名的项目的路径
  • 指定要更改的项目名称(或名称的一部分)
  • 指定项目的新名称(或部分名称)以替换以前的 string
  • 然后按下 OK 按钮

项目重命名的递归过程

void CProjRenameDlg::RenameProject(CString tPath, CString oldName, CString newName)
{  //Recursive procedure to replace the oldName with the newName in the tPath directory 
	PushMyDir(); //remember current directory
_tchdir(tPath);
struct _tfinddata_t fInfo;

//Start the process of the handling all the directories and files in the tPath directory:
intptr_t handle = _tfindfirst(_T("*.*"), &fInfo );
if (handle == -1)
    return;
if(fInfo.name[0] != '.')
if( fInfo.attrib == _A_SUBDIR)
{
	_tgetcwd(str, MAX_PATH);
	CString dString = (CString)str + _T("\\") + (CString)fInfo.name;
	//if directory found inside the current strPath to call this procedure 
    //with the directory found:
	RenameProject(dString, oldName, newName);
	_tchdir(tPath);
}
else
	RenameFileMid(CString(fInfo.name), oldName, newName);

//Continue the process of the handling all the directories and files in the tPath directory:
while(!_tfindnext(handle, &fInfo))
if(fInfo.name[0] != '.')
if( fInfo.attrib == _A_SUBDIR)
{
_tgetcwd(str, MAX_PATH);
CString dString = (CString)str + _T("\\") + (CString)fInfo.name;
//if directory found inside the current strPath 
//to call this procedure with the directory found:
RenameProject(dString, oldName, newName);
_tchdir(tPath);
}
else //if fInfo is the File then replace the oldName 
     //with the newName in the File and in it's name if fit:
	RenameFileMid(fInfo.name, oldName, newName); 
_findclose(handle);

PopMyDir();//restore original directory

if (!m_bFileName) //if Replace in Files' Names CheckBox Control 
				  //not selected do not change Files' and Paths' names  
    return;

_tchdir(tPath);
CString fName = tPath;
int nn = tPath.ReverseFind('\\');
if (nn >= 0)
  fName = tPath.Mid(nn + 1);
//To found if the name of the current path is affected with the replacement;
CString sName = ReplaceToChange(fName, oldName, newName, m_bIgnoreCase, m_bWholeWord);
if (sName != fName)
{
	PushMyDir();
	_tchdir(_T("..\\"));
	_trename(fName, sName);//Rename current directory name with the new one;
	PopMyDir();

}//if (sName != fName)
}

应用程序演示控件

对话框菜单和一些特殊的 控件 用于演示项目名称的重命名

  • 菜单 文件->打开 - 选择包含要重命名的项目的路径
  • 菜单 文件->任何最近的文件 - 选择以前使用此程序更改过的路径
  • 要替换的字符串控件 - 要更改的项目名称(或名称的一部分)
  • 替换为字符串控件 - 用于替换以前的 string 的项目的新名称(或名称的一部分)
  • 在文件名中替换控件 - 如果选中,则将以前的 string 替换为新的字符串
  • 区分大小写控件 - 如果选中,则只有当受影响的 string 中大小写与指定的模板相同时,才会将以前的 string 替换为新的字符串
  • 仅限完整单词控件 - 如果选中,则只有当受影响的 string 中的整个单词与指定的模板相同时,才会将以前的 string 替换为新的字符串
  • 扩展名框控件 - 显示文件扩展名,这些文件将要替换这些文件夹中以前的 string
  • 确定按钮控件 - 在选定的路径中执行项目重命名

关注点

上面提供的技术看起来有点像处理项目的野蛮方式。 无论如何,它正在工作,我相信一些开发人员应该对此感兴趣。

该项目是为 MSVS-2015 pro 开发的。 尽管如此,它对 MSVS-2010 也有效。

在本文的开发过程中,该项目本身已被重命名多次。

致谢

项目中的最近文件处理借鉴了 CodeProject 的一篇优秀文章,在基于 MFC 对话框的应用程序中添加最近文件列表,作者是 PPresedo

历史

  • 2016 年 8 月 7 日:初始版本
© . All rights reserved.