Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Changing "Color theme" within a Visual Studio Extension

I'm writing a Visual Studio extension in C# that I hope will change the color theme depending on the time of day (After sunset the dark theme will be applied - at sunrise either the blue/light theme will be applied depending on the users preference).

I'm able to change the color theme using the WriteableSettingsStore exposed by a ShellSettingsManager object. When I execute the following code, the theme changes after restarting Visual Studio.

var settingsManager = new ShellSettingsManager(this);
var writeableUserStore = settingsManager.GetWritableSettingsStore(SettingsScope.UserSettings);

writeableUserStore.SetString("General", "CurrentTheme", GuidList.guidDarkTheme);

What I'd prefer is to have the theme update automatically - I've tried making use of the UpdateWindow and RedrawWindow functions of the User32 API, but the window doesn't reload.

So the question is - How do I "redraw" Visual Studio after changing the CurrentTheme property in the registry?

like image 714
Cameron Wilby Avatar asked Jul 13 '15 07:07

Cameron Wilby


People also ask

How do you add a custom color in Visual Studio?

Within the Visual Studio Designer, under the properties window you are able to select the ForeColor , BackColor etc using color picker. When you want to pick a color, a color picker comes up with the tabs 'Custom, Web, System'.

Can you edit a theme in Vscode?

You can customize your active Visual Studio Code color theme with the workbench. colorCustomizations user setting. Note: If you want to use an existing color theme, see Color Themes where you'll learn how to set the active color theme through the Preferences: Color Theme dropdown (Ctrl+K Ctrl+T).

How do I use custom themes in Visual Studio?

The easiest way to install a Visual Studio theme is to navigate over to the Extensions option in the menu bar and select Manage Extensions. The next step is to select Visual Studio Marketplace under Online on the left panel. Once done, search for the theme you want and select it in the search results.


1 Answers

ShellSettingsManager enables you to access and modify Visual Studio settings but only in the Windows registry. Any changes you make will not be picked up by Visual Studio until it is restarted because VS reads settings from the registry only when it starts. So this is the wrong approach.

To both change settings and apply them without requiring a restart, you will have to use DTE2.Properties as discussed in here. The following code snippet shows all the settings that can be changed programmatically from the Environment/General page (this is where you can change the theme):

EnvDTE.Properties generalProps = dte2Obj.Properties["Environment", "General"];
for (int i = 1; i <= generalProps.Count; ++i)
{
    System.Diagnostics.Debug.WriteLine(
        generalProps.Item(i).Name + ": " + generalProps.Item(i).Value);
}

By default in VS2013, this code will produce the following output:

AnimationSpeed: 5
RichClientExperienceOptions: 65535
WindowMenuContainsNItems: 10
CloseButtonActiveTabOnly: True
UseTitleCaseOnMenu: False
AutoAdjustExperience: True
Animations: True
AutohidePinActiveTabOnly: False
ShowStatusBar: True
MRUListContainsNItems: 10

All of these settings can be changed and VS will immediately apply the changes. The problem is that there is no property that enables you to change the theme. That's why I think it cannot be done.

like image 141
Hadi Brais Avatar answered Sep 20 '22 16:09

Hadi Brais