真棒Visual Studio宏[封闭]
-
22-08-2019 - |
题
对一个小社会的讨论中,有哪些必要的Visual Studio宏使用?
我刚开始学习对他们,并希望听到一些你不能没有。
解决方案
我以前雇用许多宏在VS 2002/2003。一个例子是区域生成 - 我一直喜欢我的课被分为以下几个区域 - “私人会员”,“公共属性”,“公共方法”和“私有方法”。所以,我有一个宏映射到任何新的类文件创建这些区域的快捷键。
在VS 2005/2008重构支持(以及添加常用代码片段的设施),以及使用像DXCore和SlickEdit的加载项的让我不必再创建过多的宏工作。
其他提示
我添加按钮工具栏上的以下3个宏。每个将在任何文件中的当前选定的文本和google它(或MSDN-,或拼写检查 - 它)。弥补工具栏额外的样式,点了一个漂亮的图标。
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
您关闭解决方案后,显示的起始页(但保持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
,点击 这两者结合起来会导致你的起始页,当你打开一个解决方案隐藏自身。当您关闭解决方案,起始页回来。
我用以下的鲜为人知的捷径往往:
- Ctrl+酒:插入一个空白线上前线(和地方标有)
- Ctrl+Shift+酒:插入一个空白线以下的当前线(和地方标有)
- Ctrl+Shift+V:周期 剪贴板环
<强>大纲:折叠到定义,但扩大区域强>
您在围绕坚持地区的商店之一工作的所有的,所以,当你崩定义,你看不到任何代码?
你真正需要的是一个崩溃到定义,但是-扩大区域的宏,像这样的:
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,伟大的维克斯的工作,增加来菜单作为按钮或者作为关键的快捷方式。
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
组织using。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
我用杰夫的 FormatToHtml 宏如果我将要粘贴一个代码示例到博客文章或电子邮件。
我与双显示器工作,我发现沙龙的布局切换宏(从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
我映射CTRL-移-G到注册表格式生成GUID宏 - 这是一个用于编辑IDL
有用我目前正在与不同的编码标准两个不同的项目,一个使用标签的行开始和另一个使用空间。该宏将切换哪个标准之间使用基于该环境是当前活动的:
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)。
编辑:更新了链接