中国最大的web开发资源网站及技术社区,
我们先来看看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()方法一样)
 这次就写这么多了,希望大家多多交流,这也只是我一个人的认识,还要多和大家交流才会有提高.