惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

WordPress大学
WordPress大学
Microsoft Security Blog
Microsoft Security Blog
Security Archives - TechRepublic
Security Archives - TechRepublic
V
Visual Studio Blog
宝玉的分享
宝玉的分享
IT之家
IT之家
人人都是产品经理
人人都是产品经理
T
The Blog of Author Tim Ferriss
I
InfoQ
B
Blog RSS Feed
T
Threatpost
博客园_首页
M
MIT News - Artificial intelligence
Spread Privacy
Spread Privacy
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Know Your Adversary
Know Your Adversary
U
Unit 42
Engineering at Meta
Engineering at Meta
C
Cyber Attacks, Cyber Crime and Cyber Security
月光博客
月光博客
Scott Helme
Scott Helme
T
Tor Project blog
有赞技术团队
有赞技术团队
AWS News Blog
AWS News Blog
让小产品的独立变现更简单 - ezindie.com
让小产品的独立变现更简单 - ezindie.com
Last Week in AI
Last Week in AI
S
Schneier on Security
Vercel News
Vercel News
博客园 - Franky
C
Cybersecurity and Infrastructure Security Agency CISA
L
LINUX DO - 热门话题
NISL@THU
NISL@THU
L
LangChain Blog
爱范儿
爱范儿
Google DeepMind News
Google DeepMind News
The GitHub Blog
The GitHub Blog
雷峰网
雷峰网
Latest news
Latest news
C
CXSECURITY Database RSS Feed - CXSecurity.com
Hugging Face - Blog
Hugging Face - Blog
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
www.infosecurity-magazine.com
www.infosecurity-magazine.com
G
GRAHAM CLULEY
S
Security Affairs
A
About on SuperTechFans
K
KPMG report finds enterprise disconnect between AI and its ROI | CIO
大猫的无限游戏
大猫的无限游戏
W
WeLiveSecurity
Cisco Talos Blog
Cisco Talos Blog
罗磊的独立博客

博客园 - YellowWee(端木柒)

Founders at Work: Stories of Startups' Early Days 创业初期的故事 生成自己站点的SiteMap 开通新博客,欢迎大家访问:http://www.yellowwee.com.cn [转载]Fix Your Site With the Right DOCTYPE Java Top Books 自定义DataContext类 [转载]Encrypting Configuration Information in ASP.NET 2.0 Applications 安装 Sql Server Query Visualizer - YellowWee(端木柒) 生活的五项调整 C# 中的扩展方法---Extension methods in C# 《架构师杂志》评述:Scott Guthrie 转自MSDN WPF/Every CTP 发布 使用虚拟机安装vista RTM 配置.net 3.0开发环境 如何在TableAdapter中使用Data Access Application Block的疑问?? 今年的 Jolt 大奖 胡汉三 归来 IssueVision 学习笔记(三)-----设计模式之OBSERVER(观察者)模式 IssueVision 学习笔记(一)-----使用SoapHeader传递Web Serivices自定义的身份验证数据
IssueVision 学习笔记(二)-----为控件添加自定义属性和事件
YellowWee(端木柒) · 2004-06-07 · via 博客园 - YellowWee(端木柒)

    我们先来看看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产生渐变效果(由InactiveGradientHighColorInactiveGradientLowColor属性实现),文字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()方法一样)

  这次就写这么多了,希望大家多多交流,这也只是我一个人的认识,还要多和大家交流才会有提高.

CopyRight © YellowWee 2004. All Right Reserved.