• 网络学院
  • 新手学堂
  • 操作系统
  • 网络技术
  • 软件应用
  • 办公软件
  • 编程技术
  • 网站架设
  • 数据库类
  • 平面设计
  • 多媒体类
  • 游戏资讯
  • 教学论文
  • 认证考试
设计带图标和自定义颜色的ListBox
广告位
  站点:
  • 首 页
  • 最新软件
  • 最新文章
  • 国内软件
  • 国外软件
  • 汉化软件
  • 源码下载
  • 字体下载
设计带图标和自定义颜色的ListBox
软件发布 设计带图标和自定义颜色的ListBox
网络软件 系统工具 应用软件 联络聊天 图形图像 多媒体类 行业软件 游戏娱乐 编程开发 安全相关 教育教学 数码软件
热门软件: QQ 瑞星 pplive e话通 木马克星 千千静听 office2000 五笔字根 Photoshop 视频分割
返回首页 | 文章首页 | 编程技术 | C#教程 | 设计带图标和自定义颜色的ListBox

设计带图标和自定义颜色的ListBox

 

添加时间: 2007-7-13 2:06:36  作者: C#教程  阅读次数:104   来源: http://www.d9soft.com

 

 

        在一个点对点文件传输的项目中,我需要显示文件传输的实时信息:传输的文件列表和当前传输的文件,当时我想到了用ListBox,但是但我用了ListBox后,我发现它不能改变控件中文本想的颜色,于是我就想扩展一下ListBox控件------ListBoxEx。


  我的目标是给空间加上图标,还要能时时改变控件文本颜色。于是从ListBox派生类

public class ListBoxEx : ListBox {…}

  为了操作方便我为ListBoxEx的每一项设计专门的类ListBoxExItem

public class ListBoxExItem {…}

  为了保持我这个控件与WinForm的标准控件的操作借口一致,我又重新设计了两个集合类:

public class ListBoxExItemCollection : IList, ICollection, IEnumerator {}
//这个类相对于标准ListBox中的ObjectCollection,这个类作为ListBoxEx中的Items属性的类型

public class SelectedListBoxExItemCollection : : IList, ICollection, IEnumerator{}
//这个类相对于标准ListBox中的SelectedObjectCollection,这个类作为ListBoxEx中的SelectedItems属性的类型

  下面看两个集合类的实现:

  ListBoxExItemCollection的实现:为了做到对集合(Items)的操作能够及时反映到ListBoxEx的控件中所以,此类只是对ListBox中Items(ObjectCollection类型)作了一层包装,就是把ListBox中Items属性的所有方法的只要是object类型的参数都转换成ListBoxExItem,比如:

public void Remove(ListBoxExItem item)
{
 this._Items.Remove(item); //_Items为ObjectCollection类型
}

public void Insert(int index, ListBoxExItem item)
{
 this._Items.Insert(index, item);
}

public int Add(ListBoxExItem item)
{
 return this._Items.Add(item);
}

  由上可知,ListBoxExItemCollection中有一个构造函数来传递ListBox中的Items对象

private ObjectCollection _Items;

public ListBoxExItemCollection(ObjectCollection baseItems)
{
 this._Items = baseItems;
}


  而SelectedListBoxExItemCollection类的实现也用同样的方法,只不过是对SelectedObjectCollection包装罢了。

  集合实现后,再来看ListBoxExItem的实现:

  为了使它支持图标和多种颜色添加如下成员

private int _ImageIndex;

public int ImageIndex
{
 get { return this._ImageIndex; }
 set { this._ImageIndex = value;}
}

private Color _ForeColor;

public Color ForeColor
{
 get{ return this._ForeColor;}
 set
 {
  this._ForeColor = value;
  this.Parent.Invalidate();
 }
}

  当然还有:

private string _Text;

public string Text
{
 get { return this._Text; }
 set { this._Text = value; }
}

  为了控件能正确显示此项的文本,还必须重写ToString()方法

public override string ToString()
{
 return this._Text;
}

  再看ListBoxEx的实现:

  为了使控件能够自我绘制,所以:DrawMode = DrawMode.OwnerDrawFixed;

  为了覆盖基类的Items等相关属性添加

private ListBoxExItemCollection _Items; //在构造函数中创建

  同时还需要重写属性Items:

new public ListBoxExItemCollection Items
{
 get
 {
  return this._Items;
 }
}

new public ListBoxExItem SelectedItem //强制转换为ListBoxExItem
{
 get{ return base.SelectedItem as ListBoxExItem;}
 set{ base.SelectedItem = value;}
}

new public SelectedListBoxExItemCollection SelectedItems //重新包装SelectedItems
{
 get
 {
  return new SelectedListBoxExItemCollection(base.SelectedItems);
 }
}

  为了支持图标,添加一个图像列表imagelist

private ImageList imageList;

public ImageList ImageList
{
 get { return this.imageList; }
 set
 {
  this.imageList = value;
  this.Invalidate();//图像列表改变后马上更新控件
 }
}

  而此控件的核心却在一个方法OnDrawItem,这个方法每当控件的项需要重绘时就被调用

protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs pe)
{
 pe.DrawBackground(); //画背景
 pe.DrawFocusRectangle(); //画边框
 Rectangle bounds = pe.Bounds;

 // Check whether the index is valid

if(pe.Index >= 0 && pe.Index < base.Items.Count)
{
 ListBoxExItem item = this.Items[pe.Index]; //取得需要绘制项的引用
 int iOffset = 0;

// If the image list is present and the image index is set, draw the image

 if(this.imageList != null)
 {
  if (item.ImageIndex > -1 && item.ImageIndex < this.imageList.Images.Count)
  {
    this.imageList.Draw(pe.Graphics, bounds.Left, bounds.Top, bounds.Height, bounds.Height, item.ImageIndex); //绘制图标
  }
  iOffset += bounds.Height;//this.imageList.ImageSize.Width;
 }

 // Draw item text

 pe.Graphics.DrawString(item.Text, pe.Font, new SolidBrush(item.ForeColor),bounds.Left + iOffset, bounds.Top); //根据项的颜色绘制文本

 }
 base.OnDrawItem(pe);
 }
}

  到此为止,ListBoxEx以完整的实现,并且支持可视化设计。

 

 

 

上下文章:

 

上一篇文章: C# Builder建一个ASP.NET应用程序 下一篇文章: 用C#设计在局域网发送短信的程序

相关文章:

  • Photoshop设计性感美腿高跟鞋插画
  • QQ非法关闭后恢复功能设计预览图
  • Oracle9i数据库设计指引全集
  • Oracle平台应用数据库系统的设计与开发[上]
  • oracle数据仓库设计指南

相关软件:

  • 若寒设计音乐小偷
  • 图标搜索大师 2.46
  • 宴席设计 V3.0
  • 闪亮FLASH联盟异术设计美化版
  • 闪亮FLASH异术设计美化版1206修改版
  • 数据库设计和部署软件BDB V3.0 专业版

 

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • ASP源码
  • PHP源码
  • Net源码
  • JSP 源码

编程技术分类导航

  • ASP & ASP.NET教程
  • PHP教程
  • JSP教程
  • C/C++教程
  • VB & VB.NET教程
  • VC教程
  • Delphi教程
  • BCB教程
  • VFP教程
  • PB教程
  • JAVA教程
  • XML教程
  • C#教程
  • CGI教程

本类经典文章推荐

  • ADO.NET的开发场景及传统ADO的处理
  • 利用Visual C#实现Window管道技术
  • C#取得汉字的拼音的首字母
  • 使用C#编写DES加密程序的framework
  • Visual C#编写3D游戏框架示例
  • 用C#和本地Windows API操纵系统菜...
  • 在C#程序设计中使用Win32类库
  • Visual C#中调用Windows服务初探
  • 如何在C#的WinForm中制作饼状图和...
  • C#中实现DataGrid双向排序

C#教程阅读排行

  • 如何在C#的WinForm中制作饼状图和...
  • 浅析C#中图形编程
  • 用C#和本地Windows API操纵系统菜...
  • 彻底剖析C# 2.0泛型类的创建和使用
  • Visual C#编写3D游戏框架示例
  • 使用C#编写DES加密程序的framework
  • Visual C# 2005实现控件中捕获按键
  • Visual C#中调用Windows服务初探
  • C#如何在Form中嵌入并且操作Excel...
  • C#中利用mediaplayer打造mp3播放器

编程技术阅读总排行

  • VB入门教程之一
  • 第二章 PowerBuilder 入门之创建新...
  • Java连接数据库实例
  • 第一章 什么是PowerBuilder
  • VC++之List Box/Check List Box控...
  • VC++ List Ctrl控件
  • VC++ Combo Box/Combo Box Ex控件
  • 学C++不得不看的一篇文章
  • VB入门教程之二
  • VC++之Button控件

广告位置

字母检索 A B C D E F G H I J K L M N O P Q R S T U V W X Y Z 回到顶部

关于我们 | 版权声明 | 免责条款 | 广告联系 | 软件发布 | 下载帮助 | 下载排行 | 网站地图 | 特别鸣谢 | 友情连接

copyright; 2005-2008 D9soft.com 第九软件网 版权所有