首页  :: 新随笔  :: 联系 :: 管理

WinForm控件开发总结(十)-----为属性设置默认值

Posted on 2011-04-01 22:03  子卿  阅读(244)  评论(0)    收藏  举报
  本系列的前面几篇文章讲解了如何来定义属性以及更有效的编辑属性,接下来我要讲一下控件属性的默认值。如果我们希望自己开发的控件更易于被其它开发者使用,那么提供默认值是非常值得的。
  如果你为属性设定了默认值,那么当开发者修改了属性的值,这个值在Property Explorer中将会以粗体显示。VS为属性提供一个上下文菜单,允许程序员使用控件把值重置为默认值。当VS进行控件的串行化时,他会判断那些值不是默认值,只有不是默认值的属性才会被串行化,所以为属性提供默认值时可以大大减少串行化的属性数目,提高效率。
  那么VS怎么知道我们的属性值不是默认值了呢?我们需要一种机制来通知VS默认值。实现这种机制有两种方法:
对于简单类型的属性,比如Int32Boolean等等这些Primitive类型,你可以在属性的声明前设置一个DefaultValueAttribute,在Attribute的构造函数里传入默认值。
  对于复杂的类型,比如FontColor,你不能够直接将这些类型的值传递给Attibute的构造函数。相反你应该提供Reset<PropertyName> ShouldSerialize<PropertyName>方法,比如ResetBackgroundColor(),ShouldSerializeBackgroundColor()VS能够根据方法的名称来识别这种方法,比如Reset<PropertyName>方法把重置为默认值,ShouldSerialize<PropertyName>方法检查属性是否是默认值。过去我们把它称之为魔术命名法,应该说是一种不好的编程习惯,可是现在微软依然使用这种机制。我还是以前面几篇文章使用的例子代码。
1 using System;
2  using System.Collections.Generic;
3 using System.Text;
4 using System.Windows.Forms;
5 using System.ComponentModel;
6 using System.Drawing;
7
8 namespace CustomControlSample
9 {
10 public class FirstControl : Control
11 {
12
13 private String _displayText=”Hello World!”;
14 private Color _textColor=Color.Red;
15
16 public FirstControl()
17 {
18
19 }
20
21 // ContentAlignment is an enumeration defined in the System.Drawing
22 // namespace that specifies the alignment of content on a drawing
23 // surface.
24 private ContentAlignment alignmentValue = ContentAlignment.MiddleLeft;
25
26 [
27 Category("Alignment"),
28 Description("Specifies the alignment of text.")
29 ]
30 public ContentAlignment TextAlignment
31 {
32
33 get
34 {
35 return alignmentValue;
36 }
37 set
38 {
39 alignmentValue = value;
40
41 // The Invalidate method invokes the OnPaint method described
42 // in step 3.
43 Invalidate();
44 }
45 }
46
47
48 [Browsable(true)]
49 [DefaultValue(“Hello World”)]
50 public String DisplayText
51 {
52 get
53 {
54 return _displayText;
55 }
56 set
57 {
58 _displayText =value;
59 Invalidate();
60 }
61 }
62
63 [Browsable(true)]
64 public Color TextColor
65 {
66 get
67 {
68 return _textColor;
69 }
70 set
71 {
72 _textColor=value;
73 Invalidate();
74 }
75 }
76
77 public void ResetTextColor()
78 {
79 TextColor=Color.Red;
80 }
81
82 public bool ShouldSerializeTextColor()
83 {
84 return TextColor!=Color.Red;
85 }
86
87 protected override void OnPaint(PaintEventArgs e)
88 {
89 base.OnPaint(e);
90 StringFormat style = new StringFormat();
91 style.Alignment = StringAlignment.Near;
92 switch (alignmentValue)
93 {
94 case ContentAlignment.MiddleLeft:
95 style.Alignment = StringAlignment.Near;
96 break;
97 case ContentAlignment.MiddleRight:
98 style.Alignment = StringAlignment.Far;
99 break;
100 case ContentAlignment.MiddleCenter:
101 style.Alignment = StringAlignment.Center;
102 break;
103 }
104
105 // Call the DrawString method of the System.Drawing class to write
106 // text. Text and ClientRectangle are properties inherited from
107 // Control.
108 e.Graphics.DrawString(
109 DisplayText,
110 Font,
111 new SolidBrush(TextColor),
112 ClientRectangle, style);
113
114 }
115 }
116 }

 在上面的代码中,我增加了两个属性,一个是DisplayText,这是一个简单属性,我们只需要在它的声明前添加一个DefaultValue Attribute就可以了。另外一个是TextColor属性,这个复杂类型的属性,所以我们提供了ResetTextColorShouldSerializeTextColor来实现默认值。
默认值的实现就讲完了,但是有一点不要忽视了,你设定了默认值,就应该相应的初始化这些属性,比如我们例子中的代码:

private String _displayText=”Hello World!”;
private Color _textColor=Color.Red;