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

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

[原創]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()方法一樣)

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





主站蜘蛛池模板: 日日拍夜夜拍 | 日韩免费视频观看 | 啪啪精品 | 青青草综合 | 在线青青 | 午夜成年免费观看视频 | 日韩毛片大全免费高清 | 网站在线观看mv视频 | 日韩综合nv一区二区在线观看 | 亚洲宅男天堂a在线 | 日韩亚洲欧美在线 | 天堂网ww | 欧美一区日韩精品 | 日韩三级精品 | 亚洲天堂视频在线观看 | 天堂中文在线观看 | 偷窥自拍首页 | 深爱五月激情网 | 亚洲天堂毛片 | 日本道v高清免费 | 色手机在线 | 在线视频一二三区 | 色综合久久综合网欧美综合网 | 日本免费观看日本高清视频 | 欧美特一级| 亚洲天堂v | 日韩免费a级在线观看 | 欧美天堂久久 | 亚洲图片在线欧美专区图片 | 午夜影视在线视频观看免费 | 日本免费高清在线观看播放 | 婷婷综合激情网 | 四虎永久免费最新在线 | 日本国产一区 | 亚洲成在人线中文字幕 | 色婷婷视频在线 | 三级黄色片在线免费观看 | 在线观看视频h | 日日噜噜夜夜狠狠视频欧美人 | 性感美女视频黄.免费网站 性感保姆正片 | 亚洲一区二区三区播放在线 |