[原創]IssueVision 學習筆記(二)-----為控件添加自定義屬性與事件
發表時間:2024-06-22 來源:明輝站整理相關軟件相關文章人氣:
[摘要]我們先來看看IssueVision中一個用戶控件PaneCaption在可視化設計器中的屬性窗口. 再看一下在另一個用戶控件StaffPane中使用它時的屬性窗口: 大家會發現它多出來很多個屬性,這些屬性是原來繼承控件中沒有的屬性,如:InactiveTextColor,Inactiv...
我們先來看看IssueVision中一個用戶控件PaneCaption在可視化設計器中的屬性窗口.
再看一下在另一個用戶控件StaffPane中使用它時的屬性窗口:
大家會發現它多出來很多個屬性,這些屬性是原來繼承控件中沒有的屬性,如:InactiveTextColor,InactiveTextColor等等.它們是如何實現的呢?我們就來看一下這個用戶控件的代碼PaneCaption.cs吧.
namespace IssueVision
{
// Custom control that draws the caption for each pane. Contains an active
// state and draws the caption different for each state. Caption is drawn
// with a gradient fill and antialias font.
public class PaneCaption : UserControl
{
private class Consts
{
......
可以看到它是由 System.Windows.Forms.UserControl 繼承而來,圖一顯示了它的默認屬性.下面我們接著PaneCaption.cs代碼,看看是如何將屬性或事件顯示在可視化設計器中.
[DefaultValueAttribute(typeof(Color), "3, 55, 145")]
[DescriptionAttribute("Low color of the inactive gradient.")]
[CategoryAttribute("Appearance")]
public Color InactiveGradientLowColor
{
get
{
return m_colorInactiveLow;
}
set
{
if (value == Color.Empty)
{
value = Color.FromArgb(3, 55, 145);
}
m_colorInactiveLow = value;
CreateGradientBrushes(); //自定義方法,用于創建線形漸變筆刷
Invalidate(); //Control.Invalidate 方法,使控件的特定區域無效并向控件發送繪制消息
}
}
[CategoryAttribute("Appearance")]
[DescriptionAttribute("High color of the inactive gradient.")]
[DefaultValueAttribute(typeof(Color), "90, 135, 215")]
public Color InactiveGradientHighColor
{
get
{
return m_colorInactiveHigh;
}
set
{
if (value == Color.Empty)
{
value = Color.FromArgb(90, 135, 215);
}
m_colorInactiveHigh = value;
CreateGradientBrushes();
Invalidate();
}
}
[DescriptionAttribute("Text displayed in the caption.")]
[DefaultValueAttribute("")]
[CategoryAttribute("Appearance")]
public string Caption
{
get
{
return m_text;
}
set
{
m_text = value;
Invalidate();
}
}
其結果如下圖所示:
我們可以看到Views,Staff list背景都是使用這個自定義的PaneCaption產生漸變效果(由InactiveGradientHighColor和InactiveGradientLowColor屬性實現),文字Views和Staff list是由屬性Caption實現.
--------------------------------------------------------------------------------
代碼分析:
最重要的是 CategoryAttribute 類,它指定屬性或事件將顯示在可視化設計器中的哪個類別,具體類別如下表:
類別
說明
Action 關于可用操作的屬性。
Appearance 影響實體外觀的屬性。
Behavior 影響實體行為的屬性。
Data 關于數據的屬性。
Format 影響格式的屬性。
Layout 關于布局的屬性。
Default 沒有類別的屬性屬于默認類別。
有關更多信息,請參閱.Net Framework 1.1 SDK
我們看到舉例的這三個屬性CategoryAttribute屬性值都為CategoryAttribute("Appearance"),從圖二可以看出,這些屬性都顯示在"外觀"下.
DefaultValueAttribute 屬性顧名思義,就是此自定義屬性的默認值.
DescriptionAttribute 屬性則為此自定義屬性的描述.
關鍵部分已經設置完成,剩下的就是如何實現屬性的效果了,我以代碼說明:
protected override void OnPaint(PaintEventArgs e)
{
DrawCaption(e.Graphics);
base.OnPaint(e);
}
// draw the caption
private void DrawCaption(Graphics g)
{
// background
g.FillRectangle(this.BackBrush, this.DisplayRectangle);
if (m_antiAlias)
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;
// need a rectangle when want to use ellipsis
RectangleF bounds = new RectangleF(Consts.PosOffset,
0,
this.DisplayRectangle.Width - Consts.PosOffset,
this.DisplayRectangle.Height);
g.DrawString(m_text, this.Font, this.TextBrush, bounds, m_format);
}
使用Graphics.DrawString 繪制出Caption(即文字Views和Staff list),Graphics.FillRectangle 繪制漸變背景.注意此Graphics.FillRectangle 方法的第一個參數為筆刷,此筆刷就為上面自定義屬性代碼中CreateGradientBrushes() 方法所創建的筆刷.代碼如下:
private void CreateGradientBrushes()
{
// can only create brushes when have a width and height
if (Width > 0 && Height > 0)
{
if (m_brushActive != null)
{
m_brushActive.Dispose();
}
// 其中 m_colorActiveHigh 值就為自定義屬性InactiveGradientHighColor的值,m_colorActiveLow則為自定義屬性InactiveGradientLowColor的值.
m_brushActive = new LinearGradientBrush(DisplayRectangle, m_colorActiveHigh, m_colorActiveLow, LinearGradientMode.Vertical);
if (m_brushInactive != null)
{
m_brushInactive.Dispose();
}
m_brushInactive = new LinearGradientBrush(DisplayRectangle, m_colorInactiveHigh, m_colorInactiveLow, LinearGradientMode.Vertical);
}
}
// gradient brush for the background
private LinearGradientBrush BackBrush
{
get
{
if (m_active && m_allowActive)
return m_brushActive;
else
return m_brushInactive;
}
}
補充一點:根據GDI+要求,所有圖形的繪制都通過OnPaint()方法繪制,只用重載此方法,就可以重新繪制所需的圖形了.(此方法就如Java中的PaintComponet()方法一樣)
這次就寫這么多了,希望大家多多交流,這也只是我一個人的認識,還要多和大家交流才會有提高.