Skip to content

Instantly share code, notes, and snippets.

@FransBouma
Created January 21, 2016 16:33
Show Gist options
  • Save FransBouma/60ec21b909743973d5a2 to your computer and use it in GitHub Desktop.
Save FransBouma/60ec21b909743973d5a2 to your computer and use it in GitHub Desktop.
How to get the theme ID inside a vs.net extension without using references to vsnet2012 dlls (so code can be added to 2010 vsix too)
var themeService = this.ActiveServiceProvider.GetService(new Guid("0D915B59-2ED7-472A-9DE8-9161737EA1C5"));
if(themeService == null)
{
// no theming, no problem!
return;
}
var currentThemeProperty = themeService.GetType().GetProperty("CurrentTheme");
if(currentThemeProperty == null)
{
// something's wrong. No theming for you
return;
}
var currentTheme = currentThemeProperty.GetValue(themeService, null);
if(currentTheme == null)
{
// user has no theme. We can't help further
return;
}
var themeIdProperty = currentTheme.GetType().GetProperty("ThemeId");
if(themeIdProperty == null)
{
return;
}
var themeIdGuid = themeIdProperty.GetValue(currentTheme, null);
if(themeIdGuid == null)
{
return;
}
// use id here. IDs of common themes:
//Blue: {a4d6a176-b948-4b29-8c66-53c97a1ed7d0}
//Dark: {1ded0138-47ce-435e-84ef-9ec1f439b749}
//Light: {de3dbbcd-f642-433c-8353-8f1df4370aba}
@Layoric
Copy link

Layoric commented Feb 12, 2016

Thanks for documenting this stuff! Is ActiveServiceProvider your own wrapper to look up services via Guid? Not something I've seen else where, but looks handy! Struggling with VS being able to use anything in the Microsoft.Internal.VisualStudio.Shell.Interop namespace. Can see which DLL to get the type from but still complains missing reference (eg IVsColorThemeService), ActiveServiceProvider + reflection looks like a good way to get around the issue.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment