標籤:winform style blog http io color ar 使用 for
1.建立一個Windows表單應用程式
2.添加檔案夾及圖片資源
檔案夾圖片(folder.png),檔案圖片(file.png)
3.在設計模式下添加控制項
即:1個ListView(lvwData)、2個ImageList(imgLarge,imgSmall),1個ContextMenuStrip(cmsStyle),如下所示:
4.設定各控制項詳細屬性
2個ImageList(imgLarge,imgSmall)添加Images、設定ImageSize(imgLarge--32*32, imgSmall--16*16),如下:
ContextMenuStrip(cmsStyle)添加Items,如下:
ListView(lvwData)要添加Items、Columns,綁定ContextMenuStrip(cmsStyle)、LargeImageList及SmallImageList
介面設計完成後,鎖定所有控制項,這是個好習慣~~
5.為ContextMenuStrip(cmsStyle)的每個子項添加事件處理邏輯
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 10 namespace Controls 11 { 12 public partial class MainForm : Form 13 { 14 /// <summary> 15 /// 預設建構函式 16 /// </summary> 17 public MainForm() 18 { 19 InitializeComponent(); 20 21 this.大表徵圖ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click); 22 this.詳細資料ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click); 23 this.平鋪ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click); 24 this.小表徵圖ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click); 25 this.列表ToolStripMenuItem.Click += new System.EventHandler(this.ToolStripMenuItem_Click); 26 } 27 28 #region 滑鼠右鍵勾選事件處理 29 /// <summary> 30 /// 設定右鍵菜單勾選項,設定ListView列表顯示樣式 31 /// </summary> 32 /// <param name="sender"></param> 33 /// <param name="e"></param> 34 private void ToolStripMenuItem_Click(object sender, EventArgs e) 35 { 36 ToolStripMenuItem tsMenumItem = sender as ToolStripMenuItem; 37 if (tsMenumItem.Checked) 38 { 39 return;//已經選中則返回 40 } 41 else 42 { 43 //清除勾選的右鍵功能表項目 44 ClearCheckState(cmsStyle); 45 //勾選滑鼠選中的右鍵功能表項目 46 tsMenumItem.Checked = true; 47 } 48 //設定ListView列表顯示樣式 49 SetDisplayStyle(tsMenumItem.Text); 50 } 51 52 /// <summary> 53 /// 清除勾選的右鍵功能表項目 54 /// </summary> 55 /// <param name="cms">右鍵菜單</param> 56 private void ClearCheckState(ContextMenuStrip cms) 57 { 58 ToolStripMenuItem tsMenumItemTemp; 59 for (int i = 0; i < cms.Items.Count; i++) 60 { 61 if (!(cms.Items[i] is ToolStripMenuItem)) 62 { 63 continue; 64 } 65 tsMenumItemTemp = cms.Items[i] as ToolStripMenuItem; 66 if (tsMenumItemTemp.Checked) 67 { 68 tsMenumItemTemp.Checked = false; 69 } 70 } 71 } 72 73 /// <summary> 74 /// 設定ListView列表顯示樣式 75 /// </summary> 76 /// <param name="name"></param> 77 private void SetDisplayStyle(string name) 78 { 79 foreach (ListViewItem lvi in listData.Items) 80 { 81 if (name == "詳細資料") 82 { 83 lvi.ImageIndex = 1; 84 } 85 else 86 { 87 lvi.ImageIndex = 0; 88 } 89 } 90 switch (name) 91 { 92 case "大表徵圖": 93 listData.View = View.LargeIcon; 94 break; 95 case "詳細資料": 96 listData.View = View.Details; 97 break; 98 case "小表徵圖": 99 listData.View = View.SmallIcon;100 break;101 case "列表":102 listData.View = View.List;103 break;104 case "平鋪":105 listData.View = View.Tile;106 break;107 }108 }109 #endregion110 }111 }
6.運行效果如下所示:
C#winform中ListView及ContextMenuStrip的使用