六月婷婷综合激情-六月婷婷综合-六月婷婷在线观看-六月婷婷在线-亚洲黄色在线网站-亚洲黄色在线观看网站

明輝手游網中心:是一個免費提供流行視頻軟件教程、在線學習分享的學習平臺!

[原創]IssueVision 學習筆記(二)-----為控件添加自定義屬性與事件

[摘要]我們先來看看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()方法一樣)

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





主站蜘蛛池模板: 色噜噜狠狠色综合日日 | 欧洲性大片xxxxx久久久 | 在线亚洲精品国产波多野结衣 | 伊人婷婷在线 | 亚洲欧美一级视频 | 日本成片免费高清 | 亚洲成人黄色片 | 伊人五月在线 | 日本欧美高清 | 亚洲精品免费在线观看 | 日韩中文字幕在线看 | 色天使久久综合给合久久97色 | 天天干天天干天天操 | 欧洲成品大片在线播放 | 亚洲va中文字幕 | 四虎影视免费观看免费观看 | 日日摸人人拍人人澡 | 亚欧免费视频一区二区三区 | 色综合久久久久久久久久久 | 欧美亚洲国产精品久久第一页 | 色一情一乱一伦一视频免费看 | 天天影视色香欲综合免费 | 日韩综合在线视频 | 图片一区 | 日本www色视频 | 四虎国产永久在线精品免费观看 | 四虎永久在线精品免费影视 | 天天干天天舔 | 天天做天天爱夜夜爽 | 色狠台湾色综合网站 | 亚洲欧洲视频在线 | 欧美在线播放视频 | 日韩高清在线高清免费 | 色yeye在线观视频 | 午夜一级视频 | 天天色天天干天天射 | 青娱乐国产在线 | 亚洲性大片 | 午夜在线影视 | 欧美特黄一级大片 | 亚洲婷婷在线 |