1. 程式人生 > >C#中ListView和ImageList配合使用

C#中ListView和ImageList配合使用

http://www.programfan.com/club/post-218751-1.html
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        ListView lv = new ListView();
        ImageList il = new ImageList();

        public Form1()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            Controls.Add(lv);
            lv.Dock = DockStyle.Fill;
            lv.View = View.LargeIcon;
            lv.LargeImageList = il;

            if (System.IO.File.Exists(@"C:/Documents and Settings/Administrator/桌面/game.txt"))
            {
                face();
            }
            else
            {
                MessageBox.Show("file don't exists");
            }
        }

        private void face()
        {
           
            System.IO.StreamReader sr = new System.IO.StreamReader(@"C:/Documents and Settings/Administrator/桌面/game.txt");
            string sLine = "";
            ArrayList arrText = new ArrayList();

            while (sLine != null)
            {
                sLine = sr.ReadLine();
                if (sLine != null)
                    arrText.Add(sLine);
            }
            sr.Close();

            string[][] tmpArr = new string[arrText.Count][];

            for (int i = 1; i < arrText.Count; i++)
            {
                tmpArr[i] = new string[5];

                string[] tmpArrB = arrText[i].ToString().Split(',');
                tmpArr[i][0] = tmpArrB[0];  //序號
                tmpArr[i][1] = tmpArrB[1];  //圖片
                tmpArr[i][2] = tmpArrB[2];  //名稱
                tmpArr[i][3] = tmpArrB[3];  //路徑
                tmpArr[i][4] = tmpArrB[4];  //資訊

                il.ImageSize = new Size(24, 24);
                il.Images.Add(new Bitmap(tmpArr[i][1]));

                lv.Items.Add(tmpArr[i][2],i-1);
            }
        }
    }
}

// 圖片取自qq的face

// 讀取的文字檔案格式為

No.,gamePic,gameName,gamePath,gameInfo
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game
1,C:/Program Files/Tencent/QQ/Face2/0.gif,QQ,C:/Program Files/Tencent/QQ/Face2,QQ game