Chinaunix首页 | 论坛 | 博客
  • 博客访问: 12896533
  • 博文数量: 1293
  • 博客积分: 13501
  • 博客等级: 上将
  • 技术积分: 17974
  • 用 户 组: 普通用户
  • 注册时间: 2011-03-08 18:11
文章分类

全部博文(1293)

文章存档

2019年(1)

2018年(1)

2016年(118)

2015年(257)

2014年(128)

2013年(222)

2012年(229)

2011年(337)

分类: C#/.net

2016-06-15 19:40:06

需要在窗体增加imageList和listview控件,并把ListView控件的LargeImageList设置为imageList1

ListView控件显示图片的大小可以在imageList1控件中调整ImageSize属性,如果图片失真,可以设置imageList1控件的ColorDepth值为Depth32Bit.

窗体设计:

image

参考代码:


  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Linq;
  7. using System.Text;
  8. using System.Windows.Forms;
  9. using System.IO;
  10. using EXCEL = Microsoft.Office.Interop.Excel;
  11. using WORD = Microsoft.Office.Interop.Word;
  12. namespace ImageListView
  13. {
  14.     public partial class OfficeTextEditor : Form
  15.     {
  16.         private LanguageWizard mParentFrm;
  17.         public OfficeTextEditor(LanguageWizard parentFrm)
  18.         {
  19.             InitializeComponent();
  20.             this.mParentFrm = parentFrm;
  21.             fillListView_Word();
  22.             fillListView_Excel();
  23.         }
  24.         private List mLViewWord_FileName = new List();
  25.         private void fillListView_Word()
  26.         {
  27.             DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\Word");
  28.           
  29.             listViewWord.Items.Clear();
  30.             this.imageListWord.Images.Clear();
  31.             foreach (FileInfo d in dir.GetFiles())
  32.             {
  33.                 if (d.Name.EndsWith("_E.doc"))
  34.                 {
  35.                     mLViewWord_FileName.Add(d.FullName);
  36.                 }
  37.             }
  38.             for (int i = 0; i < mLViewWord_FileName.Count; i++)
  39.             {
  40.                 this.imageListWord.Images.Add(System.Drawing.Image.FromFile(dir + @"\WordItem.png"));
  41.                 this.listViewWord.Items.Add(System.IO.Path.GetFileName(mLViewWord_FileName[i]), i);
  42.                 this.listViewWord.Items[i].ImageIndex = i;
  43.                 this.listViewWord.Items[i].Name = mLViewWord_FileName[i];
  44.             }
  45.         }
  46.         private List mLViewExcel_FileName = new List();
  47.         private void fillListView_Excel()
  48.         {
  49.             DirectoryInfo dir = new DirectoryInfo(Application.StartupPath + @"\Excels");
  50.             listViewExcel.Items.Clear();
  51.             this.imageListExcel.Images.Clear();
  52.             foreach (FileInfo d in dir.GetFiles())
  53.             {
  54.                 if (d.Name.EndsWith("_E.xls"))
  55.                 {
  56.                     mLViewExcel_FileName.Add(d.FullName);
  57.                 }
  58.             }
  59.             for (int i = 0; i < mLViewExcel_FileName.Count; i++)
  60.             {
  61.                 this.imageListExcel.Images.Add(System.Drawing.Image.FromFile(dir + @"\ExcelItem.png"));
  62.                 this.listViewExcel.Items.Add(System.IO.Path.GetFileName(mLViewExcel_FileName[i]), i);
  63.                 this.listViewExcel.Items[i].ImageIndex = i;
  64.                 this.listViewExcel.Items[i].Name = mLViewExcel_FileName[i];
  65.             }
  66.         }
  67.         private void button1_Click(object sender, EventArgs e)
  68.         {
  69.             //this.mParentFrm.Show();
  70.             this.Close();
  71.         }
  72.         private void listViewWord_MouseDoubleClick(object sender, MouseEventArgs e)
  73.         {
  74.             ListViewHitTestInfo info = listViewWord.HitTest(e.X, e.Y);
  75.             if (info.Item != null)
  76.             {
  77.             }
  78.         }
  79.         private void listViewExcel_MouseDoubleClick(object sender, MouseEventArgs e)
  80.         {
  81.             ListViewHitTestInfo info = listViewExcel.HitTest(e.X, e.Y);
  82.             if (info.Item != null)
  83.             {
  84.                 EXCEL.Application eApp = new EXCEL.Application();
  85.                 EXCEL.Workbook workBook = eApp.Workbooks.Open(info.Item.Name);
  86.                 EXCEL.Worksheet workSheet = eApp.ActiveSheet;
  87.                 try
  88.                 {
  89.                     eApp.Visible = true;
  90.                 }
  91.                 catch (Exception ex)
  92.                 {
  93.                     MessageBox.Show(ex.Message);
  94.                 }
  95.                 finally
  96.                 {
  97.                     /* 关掉任务管理器中的进程EXCEL/WORD的关键代码 */
  98.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(eApp);
  99.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(workBook);
  100.                     System.Runtime.InteropServices.Marshal.ReleaseComObject(workSheet);
  101.                     eApp = null;
  102.                     workBook = null;
  103.                     workSheet = null;
  104.                     GC.Collect();
  105.                 }
  106.             }
  107.         }

  108.     }
  109. }


运行效果:

image


参考网址:

http://www.cnblogs.com/hfzsjz/p/3929131.html

阅读(4161) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~