Visual Studio颜色属性编辑器中的自定义调色板 [英] Custom Color Palette in Visual Studio Color Property Editor
问题描述
在Visual Studio Designer中的属性窗口下,您可以使用以下方式选择 ForeColor
, BackColor
等颜色选择器。当您要选择颜色时,颜色选择器会带有自定义,网站,系统标签。
如果您选择自定义,则可以向选择器添加新的颜色,但是只有底部的2行是可更改的,并且更改不会在控件之间保留。因此,如果您向调色板添加颜色,则当您选择另一个控件并想要更改例如 BackColor
时,您先前添加的颜色就不存在了。
有没有一种方法可以创建一组自定义颜色并将其导入到设计师的颜色选择控件中?
注意:这个问题不是关于VS主题的问题,也不是关于颜色是否可以作为隐藏代码中的类实现的问题。我正在寻找一种定制设计师的方法。
可以帮助您在Visual Studio中选择颜色的编辑器是 ColorEditor
不会在不同控件之间保留自定义颜色。要解决该问题,您应该:
- 创建自定义
UITypeEditor
基于ColorEditor
- 在Visual Studio启动时注册类型为
Color
的编辑器
以下是详细的答案,包括我用来解决问题的代码。
创建CustomColorEditor
ColorEditor
使用私有的 ColorUI
类,以显示私有的 ColorPalette
控件。调色板使用颜色数组显示自定义颜色。
要创建 CustomColorEditor
我是从 ColorEditor
派生的,并使用反射,找到这些成员,并使用一些颜色的静态数组填充数组以在首次加载时显示。然后,在关闭编辑器之后,我从编辑器获取自定义颜色,并将其放入静态数组中,并在下次加载时使用此静态数组初始化颜色编辑器。这样,在我的 CustomColorEditor
的所有实例之间共享自定义颜色。
显示CustomColorEditor而不是默认的ColorEditor
要显示特定类型的所有属性的ui类型编辑器,应添加 编辑器
属性的类型。但是由于 Color
不是我的类型,我如何为它添加 Editor
属性?
TypeDescriptor.AddAttributes
帮助我为 Color $ c注册了编辑器$ c>类型。
我应该在哪里运行代码来注册属性?肯定是在Visual Studio运行时!
为此,我创建了一个Visual Studio Package项目,并将注册代码放在 Initialize
打包方法。我还添加了 ProvideAutoLoad
属性赋予包类,以使其在我打开解决方案时自动加载。
然后我安装了软件包。
然后我使用 gacutil.exe / i dll的路径将dll放入GAC
。代替GAC,还可以将Visual Studio中的dll放在 devenv.exe
附近,因为Visual stusio运行时将使用该dll显示所有颜色属性的自定义颜色编辑器。 / p>
结论
执行上述任务后,我打开了一个新的Visual Studio实例,在Windows窗体项目中,我看到了针对颜色显示的自定义颜色编辑器。我设置的初始颜色显示出来。而且颜色编辑器甚至在不同形式之间也保留了自定义颜色!
我在这里共享了代码。您可以使用构想和代码来增强编辑器。您可以提供自定义颜色以在开始时显示在编辑器中。您甚至可以在编辑器中添加另一个标签。这是我的代码:
颜色编辑器代码
class CustomColorEditor:ColorEditor
{
private static Color []颜色;
静态CustomColorEditor()
{
颜色=新颜色[] {
Color.Red,Color.Green,Color.Blue,Color.White,
颜色。白色,白色,白色,白色,
颜色,白色,白色,白色,白色,
颜色,白色,白色,白色, Color.White,
};
}
公共替代对象EditValue(ITypeDescriptorContext上下文,System.IServiceProvider提供程序,对象值)
{
var colorEditorObject = this;
类型colorUiType = typeof(ColorEditor).GetNestedType( ColorUI,BindingFlags.NonPublic);
var colorUiConstructor = colorUiType.GetConstructors()[0];
var colorUiField = typeof(ColorEditor).GetField( colorUI,BindingFlags.Instance | BindingFlags.NonPublic);
var colorUiObject = colorUiConstructor.Invoke(new [] {colorEditorObject});
colorUiField.SetValue(colorEditorObject,colorUiObject);
var palField = colorUiObject.GetType()。GetField( pal,BindingFlags.Instance | BindingFlags.NonPublic);
var palObject = palField.GetValue(colorUiObject);
var palCustomColorsField = palObject.GetType()。GetField( customColors,BindingFlags.Instance | BindingFlags.NonPublic);
palCustomColorsField.SetValue(palObject,Colors);
var selectedValue = base.EditValue(上下文,提供者,值);
Colors = palCustomColorsField.GetValue(palObject)as Color [];
返回selectedValue;
}
}
包装代码
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration(#110,#112, 1.0 ,IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)]
公共密封类VSPackage1Package:包
{
public VSPackage1Package(){}
受保护的重写void Initialize()
{
base.Initialize();
TypeDescriptor.AddAttributes(typeof(Color),new EditorAttribute(typeof(CustomColorEditor),typeof(UITypeEditor)));;
}
}
结果
这将是Visual Studio属性窗口中的结果。看看底部的红色
,绿色
,蓝色
我们添加的对话框:
< img src = https://i.stack.imgur.com/TJI3K.png alt =在此处输入图片描述>
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'.
If you select custom, then you are able to add a new color to the picker, but only the bottom 2 rows are changeable, and the changes don't persist across controls. So if you add a color to the palette, when you select another control and want to change for example BackColor
your previous added color is not there.
Is there a way to create and import a custom set of colors into the designer's color-picker control?
Note: This question isn't asking about VS themes, or if colors can be implemented as a class in the code-behind. I'm after a way to tailor the designer.
The editor that helps you to pick color in visual studio is ColorEditor
which doesn't persists custom colors across different controls. To solve the problem, you should:
- Create a custom
UITypeEditor
based onColorEditor
- Register the editor for type
Color
at visual studio startup
Here is a detailed answer including codes which I used to solve the problem.
Create CustomColorEditor
ColorEditor
uses a private ColorUI
class to show a private ColorPalette
control. The palette uses an array of colors to show custom colors.
To create CustomColorEditor
I derived from ColorEditor
and using reflection, found those members and filled the array using a static array of some colors to show at first load. Then after closing the editor, I get custom colors from the editor and put them in the static array and initialize the color editor using this static array at next load. This way custom colors are shared between all instances of my CustomColorEditor
.
Show CustomColorEditor instead of default ColorEditor
To show an ui type editor for all properties of a specific type, you should add an Editor
attribute to the type. But since Color
is not my type, how could I add Editor
attribute to it?
TypeDescriptor.AddAttributes
helped me to register the editor for Color
type.
Where should I run the code to register the attribute? Surely at visual studio run-time!
To do so, I created a Visual Studio Package project and put the registration code at Initialize
method of package. I also added ProvideAutoLoad
attribute to the package class to make it auto load when I open a solution.
Then I installed the package.
Then I put the dll in GAC using gacutil.exe /i "path to dll"
. Instead of GAC also can put the dll in Visual Studio near devenv.exe
because the visual stusio run-time will use it to show my custom color editor for all color properties.
Conclusion
After performing above tasks, I opened a new visual studio instance and in my Windows Forms project, I see my custom color editor shown for colors. The initial colors which I set displayed. Also the color editor persisted custom colors even between different forms!
I shared the codes here. You can use the idea and codes to enhance the editor. You can provide your custom colors to show in editor at start. You even can add another tab to the editor. Here is my codes:
Code for Color Editor
class CustomColorEditor : ColorEditor
{
private static Color[] Colors;
static CustomColorEditor()
{
Colors = new Color[]{
Color.Red, Color.Green, Color.Blue, Color.White,
Color.White, Color.White, Color.White, Color.White,
Color.White, Color.White, Color.White, Color.White,
Color.White, Color.White, Color.White, Color.White,
};
}
public override object EditValue(ITypeDescriptorContext context, System.IServiceProvider provider, object value)
{
var colorEditorObject = this;
Type colorUiType = typeof(ColorEditor).GetNestedType("ColorUI", BindingFlags.NonPublic);
var colorUiConstructor = colorUiType.GetConstructors()[0];
var colorUiField = typeof(ColorEditor).GetField("colorUI", BindingFlags.Instance | BindingFlags.NonPublic);
var colorUiObject = colorUiConstructor.Invoke(new[] { colorEditorObject });
colorUiField.SetValue(colorEditorObject, colorUiObject);
var palField = colorUiObject.GetType().GetField("pal", BindingFlags.Instance | BindingFlags.NonPublic);
var palObject = palField.GetValue(colorUiObject);
var palCustomColorsField = palObject.GetType().GetField("customColors", BindingFlags.Instance | BindingFlags.NonPublic);
palCustomColorsField.SetValue(palObject, Colors);
var selectedValue = base.EditValue(context, provider, value);
Colors = palCustomColorsField.GetValue(palObject) as Color[];
return selectedValue;
}
}
Code for Package
[PackageRegistration(UseManagedResourcesOnly = true)]
[InstalledProductRegistration("#110", "#112", "1.0", IconResourceID = 400)]
[Guid(GuidList.guidVSPackage1PkgString)]
[ProvideAutoLoad(Microsoft.VisualStudio.Shell.Interop.UIContextGuids80.SolutionExists)]
public sealed class VSPackage1Package : Package
{
public VSPackage1Package() { }
protected override void Initialize()
{
base.Initialize();
TypeDescriptor.AddAttributes(typeof(Color), new EditorAttribute(typeof(CustomColorEditor), typeof(UITypeEditor)));
}
}
Result
This would be the result in Visual Studio property window. Look at those Red
, Green
, Blue
at the bottom of dialog which we added:
这篇关于Visual Studio颜色属性编辑器中的自定义调色板的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!