需要在窗体增加imageList和listview控件,并把ListView控件的LargeImageList设置为imageList1
ListView控件显示图片的大小可以在imageList1控件中调整ImageSize属性,如果图片失真,可以设置imageList1控件的ColorDepth值为Depth32Bit.
窗体设计:
参考代码:
- using System;
- using System.Collections.Generic;
- using System.ComponentModel;
- using System.Data;
- using System.Drawing;
- using System.Linq;
- using System.Text;
- using System.Windows.Forms;
- using System.IO;
- using EXCEL = Microsoft.Office.Interop.Excel;
- using WORD = Microsoft.Office.Interop.Word;
- namespace ImageListView
- {
- public partial class OfficeTextEditor : Form
- {
- private LanguageWizard mParentFrm;
- public OfficeTextEditor(LanguageWizard parentFrm)
- {
- InitializeComponent();
- this.mParentFrm = parentFrm;
- fillListView_Word();
- fillListView_Excel();
- }
- private List mLViewWord_FileName = new List();
- private void fillListView_Word()
- {
- DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\Word");
-
- listViewWord.Items.Clear();
- this.imageListWord.Images.Clear();
- foreach (FileInfo d in dir.GetFiles())
- {
- if (d.Name.EndsWith("_E.doc"))
- {
- mLViewWord_FileName.Add(d.FullName);
- }
- }
- for (int i = 0; i mLViewWord_FileName.Count; i++)
- {
- this.imageListWord.Images.Add(System.Drawing.Image.FromFile(dir + @"\WordItem.png"));
- this.listViewWord.Items.Add(System.IO.Path.GetFileName(mLViewWord_FileName[i]), i);
- this.listViewWord.Items[i].ImageIndex = i;
- this.listViewWord.Items[i].Name = mLViewWord_FileName[i];
- }
- }
- private List mLViewExcel_FileName = new List();
- private void fillListView_Excel()
- {
- DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\Excels");
- listViewExcel.Items.Clear();
- this.imageListExcel.Images.Clear();
- foreach (FileInfo d in dir.GetFiles())
- {
- if (d.Name.EndsWith("_E.xls"))
- {
- mLViewExcel_FileName.Add(d.FullName);
- }
- }
- for (int i = 0; i mLViewExcel_FileName.Count; i++)
- {
- this.imageListExcel.Images.Add(System.Drawing.Image.FromFile(dir + @"\ExcelItem.png"));
- this.listViewExcel.Items.Add(System.IO.Path.GetFileName(mLViewExcel_FileName[i]), i);
- this.listViewExcel.Items[i].ImageIndex = i;
- this.listViewExcel.Items[i].Name = mLViewExcel_FileName[i];
- }
- }
- private void button1_Click(object sender, EventArgs e)
- {
- //this.mParentFrm.Show();
- this.Close();
- }
- private void listViewWord_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- ListViewHitTestInfo info = listViewWord.HitTest(e.X, e.Y);
- if (info.Item != null)
- {
- }
- }
- private void listViewExcel_MouseDoubleClick(object sender, MouseEventArgs e)
- {
- ListViewHitTestInfo info = listViewExcel.HitTest(e.X, e.Y);
- if (info.Item != null)
- {
- EXCEL.Application eApp = new EXCEL.Application();
- EXCEL.Workbook workBook = eApp.Workbooks.Open(info.Item.Name);
- EXCEL.Worksheet workSheet = eApp.ActiveSheet;
- try
- {
- eApp.Visible = true;
- }
- catch (Exception ex)
- {
- MessageBox.Show(ex.Message);
- }
- finally
- {
- /* 关掉任务管理器中的进程EXCEL/WORD的关键代码 */
- System.Runtime.InteropServices.Marshal.ReleaseComObject(eApp);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
- System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
- eApp = null;
- workBook = null;
- workSheet = null;
- GC.Collect();
- }
- }
- }
-
- }
- }
运行效果: