问题描述
我正在用C#编写Visual Studio扩展,希望它可以根据一天中的时间更改颜色主题(日落之后将应用深色主题-日出时将根据需要应用蓝色/浅色主题,具体取决于用户的偏好).
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).
我可以使用ShellSettingsManager
对象公开的WriteableSettingsStore
更改颜色主题.当我执行以下代码时,主题将在重新启动Visual Studio之后发生变化.
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);
我希望自动更新主题-我尝试使用User32 API的UpdateWindow
和RedrawWindow
函数,但是不会重新加载窗口.
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.
问题是-更改注册表中的CurrentTheme属性后,如何重绘" Visual Studio?
So the question is - How do I "redraw" Visual Studio after changing the CurrentTheme property in the registry?
推荐答案
ShellSettingsManager
使您只能在Windows注册表中访问和修改Visual Studio设置.在重新启动之前,Visual Studio将不会对您进行的任何更改,因为VS仅在启动时才从注册表中读取设置.所以这是错误的方法.
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.
要更改设置并应用它们而无需重新启动,则必须使用 DTE2.Properties ,如此处中所述.以下代码段显示了可以在环境/常规"页面(可以在其中更改主题)中以编程方式更改的所有设置:
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);
}
在VS2013中,默认情况下,此代码将产生以下输出:
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
所有这些设置都可以更改,VS会立即应用更改.问题在于,没有可用于更改主题的属性.这就是为什么我认为无法完成的原因.
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.
这篇关于更改“颜色主题";在Visual Studio扩展中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!