frmCommonIconView.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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. namespace LYFZ.ComponentLibrary
  10. {
  11. public partial class frmCommonIconView : LYFZ.ComponentLibrary.BaseContentsFormMain
  12. {
  13. public frmCommonIconView()
  14. {
  15. InitializeComponent();
  16. this.Load += frmCommonIconView_Load;
  17. this.Shown += frmCommonIconView_Shown;
  18. this.listView1.ItemSelectionChanged += listView1_ItemSelectionChanged;
  19. this.btnSave.Click += btnSave_Click;
  20. }
  21. void btnSave_Click(object sender, EventArgs e)
  22. {
  23. this.DialogResult = System.Windows.Forms.DialogResult.OK;
  24. }
  25. void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  26. {
  27. if (e.Item != null && e.IsSelected)
  28. {
  29. this.picICO.Image = this.listView1.LargeImageList.Images[e.Item.ImageIndex];
  30. this.SelectIconPath = e.Item.Tag.ToString();
  31. }
  32. }
  33. string _SelectIconPath = "";
  34. /// <summary>
  35. /// 当前选择图标路径
  36. /// </summary>
  37. public string SelectIconPath
  38. {
  39. get { return _SelectIconPath; }
  40. set { _SelectIconPath = value; }
  41. }
  42. //创建一个委托,是为访问DataGridVie控件服务的。
  43. public delegate void UpdateControl();
  44. /// <summary>
  45. /// 显示相片集合
  46. /// </summary>
  47. static ImageList ICOImgeList = new ImageList();
  48. /// <summary>
  49. /// 系统图标目录路径
  50. /// </summary>
  51. static string sysTempHeadImagePath = LYFZ.WinAPI.CustomPublicMethod.GetFullDirectoryPath(LYFZ.WinAPI.CustomPublicMethod.BasePath) + "Resources\\快捷工具图标\\";
  52. public static string GetDefaultIconPath()
  53. {
  54. return sysTempHeadImagePath + "other\\question.png";
  55. }
  56. void frmCommonIconView_Shown(object sender, EventArgs e)
  57. {
  58. if (ICOImge != null)
  59. {
  60. this.picICO.Image = ICOImge;
  61. }
  62. if (System.IO.Directory.Exists(sysTempHeadImagePath))
  63. {
  64. string[] sysTempHeadImageFiles = System.IO.Directory.GetFiles(sysTempHeadImagePath, "*.png", System.IO.SearchOption.AllDirectories);
  65. if (ICOImgeList.Images.Count <= 0)
  66. {
  67. ICOImgeList.Images.Clear();
  68. ICOImgeList.ColorDepth = ColorDepth.Depth32Bit;
  69. ICOImgeList.ImageSize = new Size(64, 64);
  70. LYFZ.ComponentLibrary.FrmLoadHandling.LoadDoWorkMethod(delegate(object obj, BackgroundWorker backgroundWorker)
  71. {
  72. backgroundWorker.ReportProgress(0, "正在加载图标...");
  73. foreach (string imagePath in sysTempHeadImageFiles)
  74. {
  75. Image smallImage = GetNewBitmap(imagePath);
  76. ICOImgeList.Images.Add(imagePath, smallImage);
  77. }
  78. });
  79. }
  80. int count = 0;
  81. foreach (string imagePath in sysTempHeadImageFiles)
  82. {
  83. string PhotoName = "ICO_" + count.ToString().PadLeft(3, '0');
  84. ListViewItem lvi = new ListViewItem();
  85. lvi.Name = PhotoName;
  86. lvi.ImageIndex = 0;
  87. lvi.Text = PhotoName;
  88. lvi.Tag = imagePath;
  89. lvi.ToolTipText = lvi.Text;
  90. this.listView1.Items.Add(lvi);
  91. count++;
  92. }
  93. }
  94. this.listView1.LargeImageList = ICOImgeList;
  95. this.listView1.View = View.LargeIcon;
  96. this.listView1.Sorting = SortOrder.Ascending;
  97. this.listView1.Sort();
  98. RefreshShowPhotoImageList();
  99. }
  100. void frmCommonIconView_Load(object sender, EventArgs e)
  101. {
  102. }
  103. /// <summary>
  104. /// 刷新图片列表缩略图显示
  105. /// </summary>
  106. /// <param name=""></param>
  107. void RefreshShowPhotoImageList()
  108. {
  109. for (int i = 0; i < this.listView1.Items.Count; i++)
  110. {
  111. // if (this.listView1.Items[i].ImageIndex == 0)
  112. //{
  113. if (this.listView1.Items[i].Tag != null)
  114. {
  115. string lvpPhotoName = this.listView1.Items[i].Tag.ToString();
  116. if (this.listView1.LargeImageList.Images.ContainsKey(lvpPhotoName.ToLower()))
  117. {
  118. this.listView1.Items[i].ImageIndex = this.listView1.LargeImageList.Images.IndexOfKey(lvpPhotoName.ToLower());
  119. }
  120. if (!string.IsNullOrEmpty(this.SelectIconPath.Trim()))
  121. {
  122. if (this.listView1.LargeImageList.Images.ContainsKey(this.SelectIconPath.ToLower()))
  123. {
  124. this.picICO.Image = this.listView1.LargeImageList.Images[this.listView1.LargeImageList.Images.IndexOfKey(this.SelectIconPath.ToLower())];
  125. }
  126. }
  127. }
  128. // }
  129. }
  130. }
  131. /// <summary>
  132. /// 通过图片对像返回新图片但不占用文件
  133. /// </summary>
  134. /// <param name="_imgFilePath">image对象在返时被释放</param>
  135. /// <returns></returns>
  136. public static Bitmap GetNewBitmap(string _imgFilePath)
  137. {
  138. Image image = new Bitmap(_imgFilePath);
  139. try
  140. {
  141. return new Bitmap(image);
  142. }
  143. catch
  144. {
  145. return new Bitmap(1, 1);
  146. }
  147. finally
  148. {
  149. if (image != null)
  150. {
  151. image.Dispose();
  152. }
  153. }
  154. }
  155. System.Drawing.Image _ICOImge = null;
  156. /// <summary>
  157. /// 图标
  158. /// </summary>
  159. public System.Drawing.Image ICOImge
  160. {
  161. get { return _ICOImge; }
  162. set { _ICOImge = value; }
  163. }
  164. }
  165. }