对于小型社区讨论,您使用哪些基本的Visual Studio宏?
我刚刚开始了解它们,并想听听你们中有些人的生活。
对于小型社区讨论,您使用哪些基本的Visual Studio宏?
我刚刚开始了解它们,并想听听你们中有些人的生活。
My Documents\Visual Studio 2012\Addins
。在那里,对于您的项目whatever
,将whatever.dll
add以及whatever.AddIn
来自主项目目录的文件放入其中。
Answers:
我曾经在VS 2002/2003中使用过很多宏。一个示例就是区域创建-我一直希望将我的班级划分为以下区域-“私有成员”,“公共属性”,“公共方法”和“私有方法”。因此,我有一个映射到快捷键的宏,该快捷键在任何新的类文件中创建这些区域。
VS 2005/2008中的重构支持(以及添加通用代码段的功能)以及诸如DXCore和SlickEdit之类的Addins的使用使我可以工作,而不必再创建太多的宏。
我在工具栏上添加了以下3个宏的按钮。每个文件都会将当前选定的文本保存在任何文件中,并将其谷歌搜索(或MSDN-it或拼写检查-it)。为工具栏组成一个漂亮的图标,以获取更多样式点。
Private Const BROWSER_PATH As String = "C:\Program Files\Mozilla Firefox\firefox.exe"
Sub SearchGoogle()
Dim cmd As String
cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
Sub SearchMSDN()
Dim cmd As String
cmd = String.Format("{0} http://www.google.com/search?hl-en&q={1}+site%3Amsdn.microsoft.com", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
Sub SpellCheck()
Dim cmd As String
cmd = String.Format("{0} http://www.spellcheck.net/cgi-bin/spell.exe?action=CHECKWORD&string={1}", BROWSER_PATH, DTE.ActiveDocument.Selection.Text)
Shell(cmd, AppWinStyle.NormalFocus)
End Sub
在“输出”窗口中显示构建持续时间
将此代码放在EnvironmentEvents模块中。这会将持续时间直接写入到构建窗口中,以执行解决方案上的任何操作(构建,重建,清理,部署)。
您可以更改IsBuild函数以指定要查看此信息的操作。
Dim buildStart As Date
Private Function IsBuild(ByVal scope As EnvDTE.vsBuildScope, ByVal action As EnvDTE.vsBuildAction) As Boolean
Return scope = vsBuildScope.vsBuildScopeSolution
End Function
Private Sub BuildEvents_OnBuildBegin(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildBegin
If (IsBuild(Scope, Action)) Then
buildStart = Date.Now
End If
End Sub
Private Sub BuildEvents_OnBuildDone(ByVal Scope As EnvDTE.vsBuildScope, ByVal Action As EnvDTE.vsBuildAction) Handles BuildEvents.OnBuildDone
If (IsBuild(Scope, Action)) Then
Dim buildTime = Date.Now - buildStart
WriteToBuildWindow(String.Format("Build time: {0}", buildTime.ToString))
End If
End Sub
Private Sub WriteToBuildWindow(ByVal message As String)
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim ow As OutputWindow = CType(win.Object, OutputWindow)
For Each owPane As OutputWindowPane In ow.OutputWindowPanes
If (owPane.Name.Equals("Build")) Then
owPane.OutputString(message)
Exit For
End If
Next
End Sub
ow.OutputWindowPanes.Item("Build").OutputString(message)
代替For Each
。
关闭解决方案后显示起始页(但保持Visual Studio打开)
将此代码放在您的EnvironmentEvents模块中:
Private Sub SolutionEvents_AfterClosing() Handles SolutionEvents.AfterClosing
DTE.ExecuteCommand("View.StartPage")
End Sub
打开解决方案后隐藏起始页
将此代码放在您的EnvironmentEvents模块中:
Private Sub SolutionEvents_Opened() Handles SolutionEvents.Opened
Dim startPageGuid As String = "{387CB18D-6153-4156-9257-9AC3F9207BBE}"
Dim startPage As EnvDTE.Window = DTE.Windows.Item(startPageGuid)
If startPage IsNot Nothing Then startPage.Close()
End Sub
当您打开解决方案时,这两个因素会使您的“起始页”隐藏起来。关闭解决方案后,“起始页”将返回。
我经常使用以下鲜为人知的快捷方式:
概述:折叠到定义,但扩大区域
您是否在其中一家坚持围绕所有地区的商店中工作?,以便当您折叠到定义时看不到任何代码?
您真正需要的是一个折叠到定义但扩展区域的宏,如下所示:
Sub CollapseToDefinitionsButExpandAllRegions()
DTE.ExecuteCommand("Edit.CollapsetoDefinitions")
DTE.SuppressUI = True
Dim objSelection As TextSelection = DTE.ActiveDocument.Selection
objSelection.StartOfDocument()
Do While objSelection.FindText("#region",
vsFindOptions.vsFindOptionsMatchInHiddenText)
Loop
objSelection.StartOfDocument()
DTE.SuppressUI = False
End Sub
将其放在常规宏模块中,将其分配给热键,然后代码将返回。
(除非...如果您与将区域放入方法中的某些真正邪恶的人一起工作,那么不幸的是,这将扩展这些方法。如果有人知道一种编写方法来避免这种情况,请随时进行编辑。)
插入GUID,非常适合WiX工作,将其添加为菜单中的按钮或快捷键。
Sub InsertGuid()
Dim objTextSelection As TextSelection
objTextSelection = CType(DTE.ActiveDocument.Selection(), EnvDTE.TextSelection)
objTextSelection.Text = System.Guid.NewGuid.ToString.ToUpper(New System.Globalization.CultureInfo("en", False))
End Sub
组织解决方案中所有.cs文件的使用-原始作者: djpark。
Sub OrganizeSolution()
Dim sol As Solution = DTE.Solution
For i As Integer = 1 To sol.Projects.Count
OrganizeProject(sol.Projects.Item(i))
Next
End Sub
Private Sub OrganizeProject(ByVal proj As Project)
For i As Integer = 1 To proj.ProjectItems.Count
OrganizeProjectItem(proj.ProjectItems.Item(i))
Next
End Sub
Private Sub OrganizeProjectItem(ByVal projectItem As ProjectItem)
Dim fileIsOpen As Boolean = False
If projectItem.Kind = Constants.vsProjectItemKindPhysicalFile Then
'If this is a c# file
If projectItem.Name.LastIndexOf(".cs") = projectItem.Name.Length - 3 Then
'Set flag to true if file is already open
fileIsOpen = projectItem.IsOpen
Dim window As Window = projectItem.Open(Constants.vsViewKindCode)
window.Activate()
projectItem.Document.DTE.ExecuteCommand("Edit.RemoveAndSort")
'Only close the file if it was not already open
If Not fileIsOpen Then
window.Close(vsSaveChanges.vsSaveChangesYes)
End If
End If
End If
'Be sure to apply RemoveAndSort on all of the ProjectItems.
If Not projectItem.ProjectItems Is Nothing Then
For i As Integer = 1 To projectItem.ProjectItems.Count
OrganizeProjectItem(projectItem.ProjectItems.Item(i))
Next
End If
'Apply RemoveAndSort on a SubProject if it exists.
If Not projectItem.SubProject Is Nothing Then
OrganizeProject(projectItem.SubProject)
End If
End Sub
折叠“解决方案”面板的所有节点,这对大型项目特别有用:
Public Module CollapseAllNodes
Sub RunCollapseAllNodes()
Dim UIHSolutionExplorer As UIHierarchy
UIHSolutionExplorer = DTE.Windows.Item(Constants.vsext_wk_SProjectWindow).Object()
' Check if there is any open solution
If (UIHSolutionExplorer.UIHierarchyItems.Count = 0) Then
Return
End If
' Get the top node (the name of the solution)
Dim UIHSolutionRootNode As UIHierarchyItem
UIHSolutionRootNode = UIHSolutionExplorer.UIHierarchyItems.Item(1)
CloseRecursif(UIHSolutionRootNode)
' Select the solution node, or else when you click
' on the solution windows scrollbar, it will synchronize the open document
' with the tree and pop out the corresponding node which is probably not
' what you want.
UIHSolutionRootNode.Select(vsUISelectionType.vsUISelectionTypeSelect)
End Sub
Function CloseRecursif(ByRef element)
For Each UIHChild In element.UIHierarchyItems()
CloseRecursif(UIHChild)
If (UIHChild.UIHierarchyItems.Expanded = True) Then
UIHChild.UIHierarchyItems.Expanded = False
End If
Next
End Function
End Module
如果要将代码示例粘贴到博客文章或电子邮件中,则使用Jeff的FormatToHtml宏。
我使用双显示器,发现Sharon的布局切换宏(从1台显示器更改为2台显示器)非常有价值。如果您需要在键入一些代码的同时引用网页或其他程序,请按Ctrl-Alt-1切换到Visual Studio窗口的一个监视器布局。完成后,请按Ctrl-Alt-2切换到两个监视器布局,并恢复所有窗口。太棒了!
http://www.invisible-city.com/sharon/2008/06/workstation-hack-visual-studio-on-2.html
它本身不是一个宏,但很有用:
Public Sub WriteToOutputWindow(ByVal pane as String, ByVal Msg As String)
Dim owPane As OutputWindowPane
Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
Dim ow As OutputWindow = win.Object
Try
owPane = ow.OutputWindowPanes.Item(pane)
Catch
owPane = ow.OutputWindowPanes.Add(pane)
End Try
If Not owPane Is Nothing Then
owPane.Activate()
owPane.OutputString(Msg & vbCrLf)
End If
End Sub
我目前正在从事两个具有不同编码标准的项目,一个项目使用制表符作为行首,另一个项目使用空格。此宏将根据当前活动的环境在使用哪种标准之间进行切换:
Public Sub ToggleTabs()
If DTE.ActiveDocument.Language = "CSharp" Then
Dim currentSetting As Boolean = DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value
DTE.Properties("TextEditor", "CSharp").Item("InsertTabs").Value = Not currentSetting
End If
If DTE.ActiveDocument.Language = "SQL" Then
Dim currentSQLSetting As Boolean = DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value
DTE.Properties("TextEditor", "SQL").Item("InsertTabs").Value = Not currentSQLSetting
End If
If DTE.ActiveDocument.Language = "HTML" Then
Dim currentHTMLSetting As Boolean = DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value
DTE.Properties("TextEditor", "HTML").Item("InsertTabs").Value = Not currentHTMLSetting
End If
If DTE.ActiveDocument.Language = "JScript" Then
Dim currentJScriptSetting As Boolean = DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value
DTE.Properties("TextEditor", "JScript").Item("InsertTabs").Value = Not currentJScriptSetting
End If
End Sub
我不能不提这个问题而已。它甚至有一个视频,显示如何安装和使用它。该宏仅允许您在解决方案资源管理器中创建嵌套文件(例如resources.resx)。
编辑:更新了链接