使用宏自动化 Visual Studio 中的项目设置






4.50/5 (5投票s)
一组宏,有助于自动化项目设置。
下载 ADVANCEDNIBUMACROS.zip - 1.4 KB
引言
在使用 Visual Studio 6 时,我总是需要修复客户的项目设置相关问题。主要问题通常发生在您有多个项目配置时。手动打开每个项目的设置并修复问题是一项艰巨的任务。
例如:在所有项目和每个项目的配置中输入 UNICODE 和 _UNICODE 是件很麻烦的事,将警告设置更改为四级,为发布版本启用调试信息。为工作区中的所有项目启用 RTTI 等等。
有了项目设置宏,您就无需执行这些操作,只需单击鼠标即可完成。 :)
背景
那么,我便产生了记录宏来自动化项目设置相关问题的想法,但这遭到了惨败,当然您知道为什么。;) 项目设置是通过 VS 提供的 UI 完成的,宏也只工作到那个点。当您单击该宏时,只会弹出项目设置对话框。
因此,在深入研究 MSDN 后,我发现我们可以通过使用 Developer Studio 对象、VBScript 宏和 Developer Studio 插件来自动化 Visual C++ 开发环境中的任务。
Developer studio 插件对象
我不会详细介绍 Developer studio 插件,因为它本身就是一个很大的主题,但我想告诉您,您也可以为此目的创建一个插件。
现在转向 VBScript Developer studio 插件对象。
以下是 Developer studio 对象层次结构(来自 MSDN):
Application Application (Application is its own parent) Projects Project Configurations Configuration Configurations (circular) Documents Document Window (for "Generic" window types) TextDocument TextSelection TextWindow Window (for "Text" window types) TextEditor Windows Debugger Breakpoints Breakpoint
最顶层的对象是 Application Object。对于插件,这将是 IApplication 接口。如果您对 VBScript 有所了解,现在您可能已经有了一些想法。请原谅我使用 VB。
例如
要访问应用程序中的所有项目,您将使用
' This will give a project collection
Set ProjectColls = Application.Projects
'To access individual projects in the above project Collection we will use
ProjectColls.Project.
查看上面的对象层次结构,您将理解正在发生的事情。
现在的问题是如何获取项目中的所有配置。正如您所猜到的,这很简单……
' Get all configurations for a given project
Set ProjectConfigs = ProjectColls.Project.Configurations
很容易从配置集合中获取每个配置。
' Loop through each configuration
For each Config in ProjectConfigs
' Do something with this configuration
MsgBox Config.Name
Next
这是一个简单的 RebuildAll 宏……
Sub ReBuildAll
' Get all projects in the workspace
Set ProjectsCols = Application.Projects
For Each Proj in ProjectsCols
if Proj.Type = "Build" then
Set ConfigCols = Proj.Configurations
for each Config in ConfigCols
RebuildAll Config
Next
End if
Next
End Sub
应用程序事件
另一个有趣的方面是事件。应用程序对象可以处理的事件列表如下……
-
BeforeApplicationShutDown
-
NewDocument
-
BeforeBuildStart
-
NewWorkspace
-
BeforeDocumentClose
-
WindowActivate
-
BuildFinish
-
WindowDeactivate
-
DocumentOpen
-
WorkspaceClose
-
DocumentSave
-
WorkspaceOpen
下面是一个事件处理程序的示例
Sub Application_BeforeBuildStart()
' Insert code to handle the event here
MsgBox "Go ahead and build, let your project compile with zero errors. ;P"
End sub
您看到“Application”这个词是否加粗了?您应该在应用程序事件处理程序前加上“Application”前缀。
再举一个例子……
Sub Application_BuildFinish( nNumErrors, nNumWarnings )
' Display a message box if there are any errors or warnings.
If nNumErrors <> 0 Or nNumWarnings <> 0 Then
MsgBox "You have " & nNumErrors & " errors and " & nNumWarnings & " warnings"
End If
End sub
要获取项目中所有断点的句柄,请执行此操作……(来自 MSDN)
Dim myBreakpoint For Each myBreakpoint in Debugger.Breakpoints ' Access myBreakpoint here. ' For example: MsgBox myBreakpoint.PassCount Next
还有其他与断点相关的函数……
AddBreakpointAtLine
RemoveBreakpointAtLine
还有一些其他对象您可能会感兴趣。我发现比较好的是:
调试器
TextEditor
TextDocument
TextSelection
不过说实话,我没有时间深入研究它们。
更改项目设置
现在让我们回到本文的目的,即如何通过 VB 脚本使用 Developer Studio 对象来更改项目设置。
我已经解释了我们如何做到这一点,所以这里我将只分享代码……
Sub ProjectSettingsMacro()
' Get all projects in the workspace
Set ProjectsCols = Application.Projects
for each Proj in ProjectsCols
if Proj.Type = "Build" then
Set ConfigCols = Proj.Configurations
for each Config in ConfigCols
' Do release specific configuration
if InStr( Config.Name, "Release" ) > 0 then
' Set optimization to minimize size
Config.AddToolSettings "cl.exe", "/O1"
' Generate PDB file
Config.AddToolSettings "cl.exe", "/Zi"
' Enable Multithreaded DLL
Config.AddToolSettings "cl.exe", "/MD"
elseif InStr( Config.Name, "Debug" ) > 0 then
' Enable Multithreaded Dll debug option
Config.AddToolSettings "cl.exe", "/MDd"
end if
'Enable unicode settings
Config.AddToolSettings "cl.exe", "/DUNICODE /D_UNICODE"
' Remove precompiled header file support
Config.RemoveToolSettings "cl.exe", "/Yu""stdafx.h"""
' Set warning level to 4 and treat any warnings as error
Config.AddToolSettings "cl.exe", "/W4 /WX"
' Remove browse file information
Config.RemoveToolSettings "cl.exe", "/Fr /FR"
' Enable no default library option
Config.AddToolSettings "link.exe", "/NODEFAULTLIB"
' Enable debug information
Config.AddToolSettings "link.exe", "/DEBUG"
' Disable profiling
Config.RemoveToolSettings "link.exe", "/profile"
Next ' For
end if ' Fi
Next ' For
End Sub
如何……
让我们看看将此集成到我们的 Developer Studio 中的过程……
-
将提供的宏文件下载到此目录……
-
%YourMSDEVDirectory%/Common\MSDev98\Macros
-
转到 Tools->Customize->选择宏文件。
4 现在转到 Tools->Macro->单击宏文件组合框… 选择
已下载的文件。
现在选择任何宏,然后单击“运行”按钮。
要测试此宏,请启动一个新项目并运行宏(ProjectSettingsMacro)。打开项目设置对话框以查看结果。
创建新宏的步骤……
打开 MSDEV
现在在同一个宏对话框中,在“宏名称”字段中键入新宏的名称。然后单击“编辑”按钮。
只需将上面的代码粘贴在那里。现在,完成此操作后,您还可以为您的宏分配键盘快捷键。在同一个对话框中,单击“KeyStrokes”。指定一个新的快捷键。这样,每当您按下此快捷键时,宏就会运行。
现在创建一个新项目,然后按下快捷键看看它是否有效。这为我节省了很多时间。
关注点
Developer studio 对象功能强大。插件也很强大,但宏的强大之处在于,与它们完成的任务相比,它们非常简单。
结论
还有一些我可以在本文中添加的其他对象。使用上述关键字搜索 MSDN,您会发现一些非常有趣且强大的工具。
当然,本文没有什么极客的东西,只是使用了微软提供的一些工具。
希望这对您有所帮助。 :)
注意:我没有在 VS7 和 VS8 上测试过这些想法,但这些想法应该是一样的。我只在 VS6 上测试过,并且效果非常好。
历史
创建于 2007/4/11
修改于 2007/4/18 -- 添加了一些演示图片。