C# Window form 自定義控件的結(jié)構(gòu)和設(shè)計(jì)(三)
一、前面介紹了如何來創(chuàng)建第一個(gè)自定義的控件,以及一個(gè)測試程序。下面我們來看下如何在自定義控件中添加屬性。
C#和其他.NET語言支持屬性作為語言的第一類成員。把屬性作為語言的基礎(chǔ)屬性有兩點(diǎn)主要的有點(diǎn):
①利用屬性使放射返回一個(gè)類的屬性更加簡單。
②編寫代碼時(shí),我們可以取得或者設(shè)置屬性,就像取得或者設(shè)置一個(gè)類的成員變量一樣。
接下來,我們來創(chuàng)建一個(gè)實(shí)現(xiàn)許多屬性的自定義控件。
和之前的程序一樣修改基類為System.Windows.Forms.Control。
第一步在類中添加屬性值,如下所示:
public enum TextDirection
{
VerticalText,
HorizonalText
};
// 字段名稱
要輸出的文本
private string displayText;
// 文本被輸出的次數(shù)
private int displayCount;
// 文本被顯示的顏色
private Color textColor;
// 用來顯示文本的字體
private Font textFont;
// 文本顯示方向
private TextDirection textDirection;
// 文本顯示位置
private Point startDisplayPoint;
// 屬性實(shí)現(xiàn)
public string DisplayText
{
get { return displayText; }
set { displayText = value; Invalidate(); }
}
public int DisplayCount
{
get { return displayCount; }
set { displayCount = value; Invalidate(); }
}
public Color TextColor
{
get { return textColor; }
set { textColor = value; Invalidate(); }
}
public Font TextFont
{
get { return textFont; }
set { textFont = value; Invalidate(); }
}
public TextDirection TextDirect
{
get { return textDirection; }
set { textDirection = value; Invalidate(); }
}
public Point StartDisplayPoint
{
get { return startDisplayPoint; }
set { startDisplayPoint = value; Invalidate(); }
}
第二步然后添加一個(gè)控件Paint事件,代碼如下:
Graphics g = e.Graphics;
g.FillRectangle(Brushes.White, ClientRectangle);
PointF point = StartDisplayPoint;
Brush brush = new SolidBrush(textColor);
StringFormat sf = new StringFormat();
if (TextFont == null)
TextFont = new Font("Times New Roman", 12);
if (TextDirect == TextDirection.VerticalText)
sf.FormatFlags = StringFormatFlags.DirectionVertical;
for (int nCount = 0; nCount < displayCount; nCount++)
{
g.DrawString(displayText, TextFont, brush, point.X, point.Y, sf);
if (TextDirect == TextDirection.VerticalText)
point.X += TextFont.GetHeight();
else
point.Y += TextFont.GetHeight();
}
運(yùn)行程序,生成一個(gè)CustomControlWithProperties.dll。
接下來我們,我們生成一個(gè)測試程序,如下圖:
在窗體編輯器中可以看到我們剛才生成的控件。拖放到窗體中,在右邊的控件屬性窗口中,我們看到了我們剛才自定義的控件屬性值。
二、屬性的默認(rèn)值
在上面自定義控件代碼中,所有的屬性都是空白的。但是在很多情況下,我們需要把屬性值設(shè)置為有意義的值。我們需要注意兩個(gè)問題:
①確定屬性是在控件代碼中初始化的。
②確定VS編譯器了解默認(rèn)值。
在剛才的程序中,我們添加默認(rèn)的屬性值。代碼如下:
private string displayText = "GoodBye,World";
// 文本被輸出的次數(shù)
private int displayCount = 5;
// 文本被顯示的顏色
private Color textColor = Color.Lime;
// 用來顯示文本的字體
private Font textFont = new Font("Times New Roman", 12);
// 文本顯示方向
private TextDirection textDirection = TextDirection.VerticalText;
// 文本顯示位置
private Point startDisplayPoint = new Point(6, 6);
我們需要將默認(rèn)值顯示到VS的屬性窗口中,有兩種方式:
①在屬性的聲明前設(shè)置一個(gè)屬性。
在DisplayText,DisplayCount,TextDirect屬性中設(shè)置如下屬性:
// 屬性的實(shí)現(xiàn)
[DefaultValue("Hello,World")]
public string DisplayText
{
get { return displayText; }
set { displayText = value; Invalidate(); }
}
[DefaultValue(3)]
public int DisplayCount
{
get { return displayCount; }
set { displayCount = value; Invalidate(); }
}
[DefaultValue(TextDirection.HorizonalText)]
public TextDirection TextDirect
{
get { return textDirection; }
set { textDirection = value; Invalidate(); }
}
說明:當(dāng)我們的屬性值屬于其值可以作為屬性中一個(gè)參數(shù)的類型(一個(gè)字符串,一個(gè)數(shù)字,或者一個(gè)枚舉)列出時(shí),這種方式是很好的。
②我們使用一種基于Reset和ShouldSerialize的方法。使用這種方式,我們可以將屬性重置為默認(rèn)值。并將給定屬性和默認(rèn)值比較。更具體的說:Reset負(fù)責(zé)重置為默認(rèn)屬性。ShouldSerialize檢查屬性是否具有默認(rèn)值。
public void ResetTextColor()
{
TextColor = Color.Red;
}
public bool ShouldSerializeTextColor()
{
return TextColor != Color.Red;
}
public void ResetTextFont()
{
TextFont = new Font("Times New Roman",12);
}
public bool ShouldSerializeTextFont()
{
return !TextFont.Equals(new Font("Times New Roman", 12));
}
public void ResetStartDisplayPoint()
{
StartDisplayPoint = new Point(6,6);
}
public bool ShouldSerializeStartDisplayPoint()
{
return StartDisplayPoint != new Point(6, 6);
}
運(yùn)行自定義控件程序,然后再測試程序中再次打開控件屬性,可以看到右鍵菜單項(xiàng)中多了一個(gè)重置的功能。
運(yùn)行自定義控件程序,然后再測試程序中再次打開控件屬性,可以看到右鍵菜單項(xiàng)中多了一個(gè)重置的功能。文章來源:http://www.zghlxwxcb.cn/news/detail-852055.html
好了,自定義控件添加屬性的功能就介紹到這里了。歡迎大家一起交流。文章來源地址http://www.zghlxwxcb.cn/news/detail-852055.html
到了這里,關(guān)于C# Window form 自定義控件的結(jié)構(gòu)和設(shè)計(jì)(三)的文章就介紹完了。如果您還想了解更多內(nèi)容,請(qǐng)?jiān)谟疑辖撬阉鱐OY模板網(wǎng)以前的文章或繼續(xù)瀏覽下面的相關(guān)文章,希望大家以后多多支持TOY模板網(wǎng)!