如何为一个类型为Color的属性设置默认值

mac2022-06-30  96

最近在研究GDI+的时候,用winform来写自定义控件遇到需要为控件的属性设置默认值,但这个属性的类型是System.Drawing.Color。本文只是总结一下各种设置的方法。

Example

[Description("设置颜色")] [DefaultValue(typeof(Color), "0, 0, 0")] public Color BaseColor { get; set; }

设置方法

用系统已定义的知名颜色(如:Color.Red) [DefaultValue(typeof(Color), "Red")]

使用16进制颜色值

[DefaultValue(typeof(Color), "0xFFFFC0")] 使用RGB颜色参数 [DefaultValue(typeof(Color),"248, 232, 192")]

注意:此方法在某些情况下可能默认颜色值会失败,具体可参看:How to set the default value of Colors in a custom control in Winforms?

References

http://jelle.druyts.net/2005/11/23/SettingADefaultValueForAColorProperty.aspxhttp://stackoverflow.com/questions/1522199/how-to-set-the-default-value-of-colors-in-a-custom-control-in-winforms

转载于:https://www.cnblogs.com/EasonWu/p/set-default-color-for-the-property-of-winform-control.html

最新回复(0)