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

创建带图标的ListBoox

 

添加时间: 2007-7-13 1:57:58  作者: C#教程  阅读次数:54   来源: http://www.d9soft.com

 

 

       

下面的代码实现了带图标的ListBoox的功能,可以直接拷贝即可运行。运行结果如下:

 

using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.<a href="http://dev.21tx.com/os/windows/" target="_blank">Windows</a>.Forms;
using System.Data;

namespace ListBoxWithIcon
{
  /// <summary>
  /// Form1 的摘要说明。
  /// </summary>
  public class Form1 : System.Windows.Forms.Form
  {
    private GListBox lb;
    /// <summary>
    /// 必需的设计器变量。
    /// </summary>
    private System.ComponentModel.Container components = null;

    public Form1()
    {
      //
      // Windows 窗体设计器支持所必需的
      //
      InitializeComponent();

      //
      // TODO: 在 InitializeComponent 调用后添加任何构造函数代码
      //
    }

    /// <summary>
    /// 清理所有正在使用的资源。
    /// </summary>
    protected override void Dispose( bool disposing )
    {
      if( disposing )
      {
        if (components != null)
        {
          components.Dispose();
        }
      }
      base.Dispose( disposing );
    }

    #region Windows 窗体设计器生成的代码
    /// <summary>
    /// 设计器支持所需的方法 - 不要使用代码编辑器修改
    /// 此方法的内容。
    /// </summary>
    private void InitializeComponent()
    {
      ImageList imageList = new ImageList();
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon.ico"));
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon4.ico"));
      imageList.Images.Add(Image.FromFile(@"D:\我的文件\myrice\favicon5.ico"));

      this.lb = new GListBox();;
      this.SuspendLayout();
      //
      // lb
      //
      this.lb.ItemHeight = 16;
      this.lb.Location = new System.Drawing.Point(10, 10);
      this.lb.Name = "listBox1";
      this.lb.Size = new System.Drawing.Size(200, 60);
      this.lb.TabIndex = 0;

      lb.ImageList = imageList;
      lb.Items.Add( new GListBoxItem("http://<a href="http://dev.21tx.com/dotnet/xml/" target="_blank">XML</a>.sz.luohuedu<a href="http://dev.21tx.com/dotnet/" target="_blank">.net</a>/",0));
      lb.Items.Add( new GListBoxItem("http://lucky.myrice.com/",1));
      lb.Items.Add( new GListBoxItem("【孟宪会之精彩世界】",2));

      //
      // Form1
      //
      this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
      this.ClientSize = new System.Drawing.Size(292, 160);
      this.Controls.Add(this.lb);
      this.Text = "带图标的ListBox测试";
      this.Load += new System.EventHandler(this.Form1_Load);
      this.ResumeLayout(false);

    }
    #endregion

    /// <summary>
    /// 应用程序的主入口点。
    /// </summary>
    [STAThread]
    static void Main()
    {
      Application.Run(new Form1());
    }

    private void Form1_Load(object sender, System.EventArgs e)
    {
    }

    // GListBoxItem 类
    public class GListBoxItem
    {
      private string _myText;
      private int _myImageIndex;
      // 属性
      public string Text
      {
        get {return _myText;}
        set {_myText = value;}
      }
      public int ImageIndex
      {
        get {return _myImageIndex;}
        set {_myImageIndex = value;}
      }
      //构造函数
      public GListBoxItem(string text, int index)
      {
        _myText = text;
        _myImageIndex = index;
      }
      public GListBoxItem(string text): this(text,-1){}
      public GListBoxItem(): this(""){}
      public override string ToString()
      {
        return _myText;
      }
    }

    // GListBox 类
    public class GListBox : ListBox
    {
      private ImageList _myImageList;
      public ImageList ImageList
      {
        get {return _myImageList;}
        set {_myImageList = value;}
      }
      public GListBox()
      {
        this.DrawMode = DrawMode.OwnerDrawFixed;
      }
      protected override void OnDrawItem(System.Windows.Forms.DrawItemEventArgs e)
      {
        e.DrawBackground();
        e.DrawFocusRectangle();
        GListBoxItem item;
        Rectangle bounds = e.Bounds;
        Size imageSize = _myImageList.ImageSize;
        try
        {
          item = (GListBoxItem) Items[e.Index];
          if (item.ImageIndex != -1)
          {
            ImageList.Draw(e.Graphics, bounds.Left,bounds.Top,item.ImageIndex);
            e.Graphics.DrawString(item.Text, e.Font, new SolidBrush(e.ForeColor),
              bounds.Left+imageSize.Width, bounds.Top);
          }
          else
          {
            e.Graphics.DrawString(item.Text, e.Font,new SolidBrush(e.ForeColor),
              bounds.Left, bounds.Top);
          }
        }
        catch
        {
          if (e.Index != -1)
          {
            e.Graphics.DrawString(Items[e.Index].ToString(),e.Font,
              new SolidBrush(e.ForeColor) ,bounds.Left, bounds.Top);
          }
          else
          {
            e.Graphics.DrawString(Text,e.Font,new SolidBrush(e.ForeColor),
              bounds.Left, bounds.Top);
          }
        }
        base.OnDrawItem(e);
      }
    }
  }
}

 

 

 

 

上下文章:

 

上一篇文章: 用C#Builder编写屏幕保护程序 下一篇文章: C#中改变显示器的分辨率

相关文章:

  • ORACLE公司传奇创建史
  • 创建隐藏IP地址的最后一项的函数
  • Oracle集成技术及创建工具
  • 使用配置文件创建口令管理策略
  • Oracle中的用户创建和权限的分配

相关软件:

  • 图标搜索大师 2.46
  • 零度文件图标提取器 V1.1
  • 图标转换器 V1.0
  • 虚拟DOS引导系统创建工具 V6.2
  • 桌面图标排列助手 V1.2
  • Ei图标 V2.3

 

 

快速导航

  • 网络学院
  • 精品汇聚
  • 字体下载
  • 教程下载
  • 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 第九软件网 版权所有