Skip to content

Instantly share code, notes, and snippets.

@eriawan
Created July 21, 2017 11:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save eriawan/342085014607732b3e947b0248af23fd to your computer and use it in GitHub Desktop.
Save eriawan/342085014607732b3e947b0248af23fd to your computer and use it in GitHub Desktop.
Proposed BuildEventsPropPage implementation
' Copyright (c) Microsoft. All Rights Reserved. Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information.
Imports System.Drawing
Imports System.Windows.Forms
Imports EnvDTE
Imports Microsoft.VisualStudio.Editor
Imports Microsoft.VisualStudio.Shell.Interop
Imports VSLangProj80
Namespace Microsoft.VisualStudio.Editors.PropertyPages
Friend NotInheritable Class BuildEventsPropPage
Inherits PropPageUserControlBase
Implements IServiceProvider
Public Sub New()
MyBase.New()
'PERF: Set font before InitializeComponent so we don't cause unnecessary layouts (needs the site first)
_fontChangeWatcher = New Common.ShellUtil.FontChangeMonitor(Me, Me, True)
InitializeComponent()
'Opt out of page scaling since we're using AutoScaleMode
PageRequiresScaling = False
'Add any initialization after the InitializeComponent() call
AddChangeHandlers()
' Get the Text Editor's font setting from Font and Colors category and set it
SetupTextEditorFontSetting()
End Sub
Public Sub New(serviceProvider As IServiceProvider)
End Sub
Public Enum Tokens
OutDir = 0
ConfigurationName
ProjectName
TargetName
TargetPath
ProjectPath
ProjectFileName
TargetExt
TargetFileName
DevEnvDir
TargetDir
ProjectDir
SolutionFileName
SolutionPath
SolutionDir
SolutionName
PlatformName
ProjectExt
SolutionExt
Tokens_MAX
End Enum
Private Shared ReadOnly s_tokenNames() As String = {
"OutDir",
"ConfigurationName",
"ProjectName",
"TargetName",
"TargetPath",
"ProjectPath",
"ProjectFileName",
"TargetExt",
"TargetFileName",
"DevEnvDir",
"TargetDir",
"ProjectDir",
"SolutionFileName",
"SolutionPath",
"SolutionDir",
"SolutionName",
"PlatformName",
"ProjectExt",
"SolutionExt"
}
Private prevFontInfo As FontInfo
' Helper class to handle font change notifications...
Private _fontChangeWatcher As Common.ShellUtil.FontChangeMonitor
'Data shared by all pages hooked up to this project designer (available through GetService)
Private _configurationState As PropPageDesigner.ConfigurationState
'*** Project Property related data
Private _projectObject As Object 'Project's browse object
Private _DTEProject As Project 'Project's DTE object
Private _specialFiles As IVsProjectSpecialFiles
Private _serviceProvider As IServiceProvider
Private Sub SetupTextEditorFontSetting()
Dim Flags As __FCSTORAGEFLAGS = __FCSTORAGEFLAGS.FCSF_READONLY Or __FCSTORAGEFLAGS.FCSF_LOADDEFAULTS
Dim VsConstantResult As Integer
Dim FontAndColorStorageService = Shell.ServiceProvider.GlobalProvider.GetService(GetType(SVsFontAndColorStorage))
Dim FontColorStorage As IVsFontAndColorStorage = TryCast(FontAndColorStorageService, IVsFontAndColorStorage)
If FontColorStorage IsNot Nothing Then
VsConstantResult = FontColorStorage.OpenCategory(DefGuidList.guidTextEditorFontCategory, CType(Flags, System.UInt32))
If VsConstantResult = VSConstants.S_OK Then
Dim logFontw(1) As LOGFONTW
Dim TextEditorFont(1) As FontInfo
VsConstantResult = FontColorStorage.GetFont(logFontw, TextEditorFont)
If VsConstantResult = VSConstants.S_OK Then
' Try to set the font..
Dim FontDisplayed As Font
FontDisplayed = New Font(TextEditorFont(0).bstrFaceName, CType(TextEditorFont(0).wPointSize, Single))
txtPreBuildEventCommandLine.Font = FontDisplayed
txtPostBuildEventCommandLine.Font = FontDisplayed
End If
End If
FontColorStorage.CloseCategory()
End If
End Sub
Protected Overrides ReadOnly Property ControlData() As PropertyControlData()
Get
If m_ControlData Is Nothing Then
m_ControlData = New PropertyControlData() {
New PropertyControlData(VsProjPropId2.VBPROJPROPID_PreBuildEvent, "PreBuildEvent", txtPreBuildEventCommandLine, ControlDataFlags.None, New Control() {btnPreBuildBuilder, lblPreBuildEventCommandLine}),
New PropertyControlData(VsProjPropId2.VBPROJPROPID_PostBuildEvent, "PostBuildEvent", txtPostBuildEventCommandLine, ControlDataFlags.None, New Control() {btnPostBuildBuilder, lblPostBuildEventCommandLine}),
New PropertyControlData(VsProjPropId2.VBPROJPROPID_RunPostBuildEvent, "RunPostBuildEvent", cboRunPostBuildEvent, New Control() {lblRunPostBuildEvent})
}
End If
Return m_ControlData
End Get
End Property
Protected Overrides Function GetF1HelpKeyword() As String
If IsVBProject() Then
Return HelpKeywords.VBProjPropBuildEvents
Else
Return HelpKeywords.CSProjPropBuildEvents
End If
End Function
Private Sub PostBuildBuilderButton_Click(sender As Object, e As EventArgs) Handles btnPostBuildBuilder.Click
Dim CommandLineText As String
CommandLineText = txtPostBuildEventCommandLine.Text
LaunchEventBuilder(Me, AddressOf GetTokenValue, My.Resources.Designer.PPG_PostBuildCommandLineTitle, CommandLineText)
Dim oldCommandLine As String = txtPostBuildEventCommandLine.Text
txtPostBuildEventCommandLine.Text = CommandLineText
If oldCommandLine <> CommandLineText Then
SetDirty(txtPostBuildEventCommandLine, True)
End If
End Sub
Private Sub PreBuildBuilderButton_Click(sender As Object, e As EventArgs) Handles btnPreBuildBuilder.Click
Dim CommandLineText As String
CommandLineText = txtPreBuildEventCommandLine.Text
LaunchEventBuilder(Me, AddressOf GetTokenValue, My.Resources.Designer.PPG_PreBuildCommandLineTitle, CommandLineText)
Dim oldCommandLine As String = txtPreBuildEventCommandLine.Text
txtPreBuildEventCommandLine.Text = CommandLineText
If oldCommandLine <> CommandLineText Then
SetDirty(txtPreBuildEventCommandLine, True)
End If
End Sub
Friend Delegate Function GetTokenValueFunc(MacroName As String) As String
Private Function LaunchEventBuilder(Parent As BuildEventsPropPage, valueHelper As GetTokenValueFunc, WindowTitleText As String, ByRef CommandLine As String) As Boolean
Dim frm As New BuildEventCommandLineDialog
Dim Values() As String = Nothing
'// Initialize the title text
frm.SetFormTitleText(WindowTitleText)
'// Initialize the command line
frm.EventCommandLine = CommandLine
'// Set the page property
frm.Page = Parent
'// Set the Dte object for cmdline dialog
' VSWhidbey 163859 - help not able to retrieve DTE handle
frm.DTE = Parent.DTE
'// Initialize the token values
GetTokenValues(Values, valueHelper)
frm.SetTokensAndValues(s_tokenNames, Values)
'// Show the form
If (frm.ShowDialog(ServiceProvider) = System.Windows.Forms.DialogResult.OK) Then
CommandLine = frm.EventCommandLine
End If
Return True
End Function
Friend Shared Function GetTokenValues(ByRef Values() As String, valueHelper As GetTokenValueFunc) As Boolean
Dim i As Integer
Values = CType(Array.CreateInstance(GetType(String), Tokens.Tokens_MAX), String())
For i = 0 To Tokens.Tokens_MAX - 1
Values(i) = valueHelper(s_tokenNames(i))
Next
Return True
End Function
Private Function GetTokenValue(MacroName As String) As String
Dim MacroEval As IVsBuildMacroInfo
Dim MacroValue As String = Nothing
Dim Hier As IVsHierarchy = Nothing
Dim ItemId As UInteger
Dim ThisObj As Object = m_Objects(0)
If TypeOf ThisObj Is IVsBrowseObject Then
VSErrorHandler.ThrowOnFailure(CType(ThisObj, IVsBrowseObject).GetProjectItem(Hier, ItemId))
ElseIf TypeOf ThisObj Is IVsCfgBrowseObject Then
VSErrorHandler.ThrowOnFailure(CType(ThisObj, IVsCfgBrowseObject).GetProjectItem(Hier, ItemId))
End If
MacroEval = CType(Hier, IVsBuildMacroInfo)
VSErrorHandler.ThrowOnFailure(MacroEval.GetBuildMacroValue(MacroName, MacroValue))
Return MacroValue
End Function
Private Sub SetSite(serviceProvider As IServiceProvider)
_serviceProvider = serviceProvider
'Set the provider into the base tab control so it can get access to fonts and colors
MyBase.ServiceProvider = _serviceProvider
End Sub
Public Shadows Function GetService(serviceType As Type) As Object Implements IServiceProvider.GetService
'Dim Service As Object
'Dim FontAndColorStorageService = Shell.ServiceProvider.GlobalProvider.GetService(GetType(SVsFontAndColorStorage))
If serviceType Is GetType(BuildEventsPropPage) Then
'Allows the PropPageDesignerView to access the ApplicationDesignerView
Return ServiceProvider
Else
Return Shell.ServiceProvider.GlobalProvider.GetService(GetType(SVsFontAndColorStorage))
End If
'Service = _serviceProvider.GetService(serviceType)
'Return Service
End Function
End Class
End Namespace
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment