[原創]IssueVision 學習筆記(二)-----為控制項添加自訂屬性和事件

來源:互聯網
上載者:User
筆記|控制項|原創 我們先來看看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()方法一樣)

這次就寫這麼多了,希望大家多多交流,這也只是我一個人的認識,還要多和大家交流才會有提高.




聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.