frmMain.cs
上传用户:jiupin
上传日期:2022-04-21
资源大小:1525k
文件大小:37k
源码类别:

ListView/ListBox

开发平台:

C#

  1. using System;
  2. using System.Collections.Generic;
  3. using System.ComponentModel;
  4. using System.Data;
  5. using System.Drawing;
  6. using System.Text;
  7. using System.Windows.Forms;
  8. using System.Diagnostics;//
  9. using System.Drawing.Drawing2D;//
  10. using System.Threading;//
  11. using System.Runtime.InteropServices;//
  12. using System.Xml;//
  13. using System.IO;//
  14. namespace WinListViewQQ
  15. {
  16.     public partial class frmMain : Form
  17.     {
  18.         /// <summary>
  19.         /// 组标志参数
  20.         /// </summary>
  21.         private int  btnFlag =0;
  22.         ListView listView1 = new ListView();
  23.         PerformanceCounter p = new PerformanceCounter();
  24.         public frmMain()
  25.         {
  26.             InitializeComponent();
  27.           
  28.             p.CounterName =@"% Processor Time"; //@"% Committed Bytes In Use";
  29.             p.CategoryName = "Processor";//"Memory";
  30.             p.InstanceName = "_Total";
  31.  
  32.             
  33.         }
  34.        
  35.         private void Form1_Load(object sender, EventArgs e)
  36.         {
  37.               InitialFrmMain();//调方法初始化主窗体
  38.               //
  39.               InitialListView();//调方法初始化ListView
  40.              //
  41.               btnOne_Click(sender, e);
  42.        
  43.               string fileName = Application.StartupPath + "\config.xml";
  44.             
  45.               if (!File.Exists(fileName))
  46.               {
  47.                  clsXMl.CreateXmlConfigFile();//如果不存在配置文件,则创建
  48.                  ReadXmlConfigFile(fileName);
  49.                  fileName = null;
  50.                  GC.Collect();
  51.               }
  52.               else 
  53.               {
  54.                   ReadXmlConfigFile(fileName);//如果存在配置文件,则读取
  55.                   fileName = null;
  56.                   GC.Collect();
  57.               }   
  58.            
  59.         }
  60.         /// <summary>
  61.         /// 初始化主窗体相关
  62.         /// </summary>
  63.         private void InitialFrmMain()
  64.         {
  65.             contextMenuStripTaskbar.AutoClose = true;
  66.             contextMenuStripListView.AutoClose = true;
  67.             this.ContextMenuStrip = contextMenuStripTaskbar;
  68.             //图片存放的相对路径         
  69.             string strImagePath = Application.StartupPath.Substring(0 ) + "\images";//**************************************************
  70.             //设置窗体图标
  71.            this.Icon = System.Drawing.Icon.ExtractAssociatedIcon(strImagePath + "\ico.ico");
  72.             //初始化ImageList图片集
  73.            // for (int i = 0; i < 1; i++)
  74.             //{
  75.                 imageListSmallBlue.Images.Add("0", Image.FromFile(strImagePath + "\smallBlue.bmp"));//
  76.            // }
  77.             for(int j=0;j<1;j++)
  78.             {
  79.                 imageListLargeBlue.Images.Add(j.ToString(), Image.FromFile(strImagePath + "\largeBlue.bmp"));
  80.             }
  81.             for (int j = 0; j < 1; j++)
  82.             {
  83.                 imageListLargeRed.Images.Add(j.ToString(), Image.FromFile(strImagePath + "\largeRed.bmp"));
  84.             }
  85.             for (int j = 0; j <1; j++)
  86.             {
  87.                 imageListSmallRed.Images.Add(j.ToString(), Image.FromFile(strImagePath + "\smallRed.bmp"));
  88.             }
  89.             //设置菜单图标
  90.             toolStripButtonShowDesktopIcon.Image = Image.FromFile(strImagePath + "\desktop.png");
  91.             toolStripButtonExit.Image=Image.FromFile(strImagePath+"\exit.ico");
  92.             //设置右键菜单图标
  93.             ToolStripMenuItemShowDeskTopIco.Image = Image.FromFile(strImagePath + "\desktop.png");
  94.             ToolStripMenuItemShowFrmMain.Image= Image.FromFile(strImagePath + "\showFrmMain.ico");
  95.             //初始化窗体大小
  96.             this.Width = 171;
  97.             this.Height = 500;
  98.             //初始化窗体位置
  99.             this.Top = 40; ;
  100.             this.Left = Screen.PrimaryScreen.Bounds.Width - 400;
  101.             //
  102.             this.ShowInTaskbar = false;                     
  103.         }
  104.       
  105.         /// <summary>
  106.         /// 窗体加载  初始化ListView的方法
  107.         /// </summary>
  108.         private void InitialListView()
  109.         {
  110.             this.listView1.ItemSelectionChanged += new System.Windows.Forms.ListViewItemSelectionChangedEventHandler(this.listView1_ItemSelectionChanged);
  111.             this.listView1.MouseDown += new MouseEventHandler(this.listView1_MouseDown);// (object sender, MouseEventArgs e)
  112.             splitContainer1.Panel2.Controls.Add(listView1);
  113.             listView1.MultiSelect = false;
  114.             listView1.FullRowSelect = true;
  115.             
  116.             listView1.BackgroundImageTiled = true;
  117.             //listView1.Sorting = SortOrder.Ascending;
  118.             listView1.HideSelection = false;
  119.             listView1.Dock = DockStyle.Fill;
  120.             
  121.             //---------------------------------------
  122.             listView1.Clear(); 
  123.           
  124.             listView1.Dock = DockStyle.Fill;
  125.             btnOne.Dock = DockStyle.Top;
  126.             btnTwo.Dock = DockStyle.Top;
  127.             btnThree.Dock = DockStyle.Top;
  128.             btnFour.Dock = DockStyle.Top;
  129.             listView1.LargeImageList = imageListSmallBlue;
  130.             listView1.ContextMenuStrip = contextMenuStripListView;
  131.             contextMenuStripListView.Items[0].Enabled = false;//int
  132.             contextMenuStripListView.Items["ToolStripMenuItemRemoveItem"].Enabled = false;//name  
  133.   
  134.         }
  135.         /// <summary>
  136.         /// 窗体加载 读取 配置文件的参数设置
  137.         /// </summary>
  138.         /// <param name="fileName">文件路径+名</param>
  139.         private void ReadXmlConfigFile(string fileName)
  140.         {
  141.             
  142.             XmlNode root = clsXMl.GetRootNode();
  143.            
  144.             foreach (XmlNode node in root.ChildNodes)
  145.             {  
  146.                 switch (node.Name)
  147.                 {
  148.                     case "TopMost":
  149.                                     switch (node.InnerText.Trim())
  150.                                     {
  151.                                         case "no":
  152.                                             this.TopMost = false;
  153.                                             ToolStripMenuItemTopMost.Checked = false;
  154.                                             break;
  155.                                         case "yes":
  156.                                             this.TopMost = true;
  157.                                             ToolStripMenuItemTopMost.Checked = true;
  158.                                             break;
  159.                                     };
  160.                                     break;
  161.                     case "AutoRun":
  162.                                     switch (node.InnerText.Trim())
  163.                                     {
  164.                                         case"no":
  165.                                             ToolStripMenuItemAutoRun.Checked = false;
  166.                                             break;
  167.                                         case"yes":
  168.                                             ToolStripMenuItemAutoRun.Checked = true;
  169.                                             break;
  170.                                     }
  171.                                     break;
  172.                     case"BgImage":
  173.                         switch(node.InnerText.Trim())
  174.                         {
  175.                             case "blue":
  176.                                 listView1.BackgroundImage = Image.FromFile(Application.StartupPath.Substring(0) + "\images" + "\bgBlue.jpg");
  177.                                 ToolStripMenuItemBgRed.Checked =false ;
  178.                                 ToolStripMenuItemBgBlue.Checked = true;
  179.                                      break;
  180.                             case"red":
  181.                                 listView1.BackgroundImage = Image.FromFile(Application.StartupPath.Substring(0) + "\images" + "\bgRed.jpg");
  182.                                 ToolStripMenuItemBgRed.Checked = true;
  183.                                 ToolStripMenuItemBgBlue.Checked = false;
  184.                                 break;
  185.                         }
  186.                         break;  
  187.                     case "Icon":
  188.                         switch (node.InnerText.Trim())
  189.                         {
  190.                             case "blue":
  191.                                 if (node.Attributes["size"].Value == "small")
  192.                                 {
  193.                                     listView1.LargeImageList = imageListSmallBlue;
  194.                                     ToolStripMenuItemLargeBlue.Checked = false;
  195.                                     ToolStripMenuItemLargeRed.Checked = false;
  196.                                     ToolStripMenuItemSmallRed.Checked = false;
  197.                                     ToolStripMenuItemSmallBlue.Checked = true;
  198.                                     return;                               
  199.                                 }
  200.                                 else if (node.Attributes["size"].Value == "large")
  201.                                 {
  202.                                     listView1.LargeImageList = imageListLargeBlue;
  203.                                     ToolStripMenuItemLargeBlue.Checked = true;
  204.                                     ToolStripMenuItemLargeRed.Checked = false;
  205.                                     ToolStripMenuItemSmallRed.Checked = false;
  206.                                     ToolStripMenuItemSmallBlue.Checked = false;
  207.                                     return;  
  208.                                 }
  209.                                 break;
  210.                             case "red":
  211.                                 if (node.Attributes["size"].Value== "small")
  212.                                 {
  213.                                     listView1.LargeImageList = imageListSmallRed;
  214.                                     ToolStripMenuItemLargeBlue.Checked = false;
  215.                                     ToolStripMenuItemLargeRed.Checked = false;
  216.                                     ToolStripMenuItemSmallRed.Checked =true ;
  217.                                     ToolStripMenuItemSmallBlue.Checked = false;
  218.                                     return;  
  219.                                 }
  220.                                 else if (node.Attributes["size"].Value == "large")
  221.                                 {
  222.                                     listView1.LargeImageList = imageListLargeRed;
  223.                                     ToolStripMenuItemLargeBlue.Checked = false;
  224.                                     ToolStripMenuItemLargeRed.Checked = true;
  225.                                     ToolStripMenuItemSmallRed.Checked = false;
  226.                                     ToolStripMenuItemSmallBlue.Checked = false;
  227.                                     return;  
  228.                                 }
  229.                                 break;
  230.                         }
  231.                        
  232.                         break;
  233.                         
  234.                         
  235.                 }
  236.             }   
  237.         }
  238.        
  239.      
  240.  //-----------------------------------------------------------------------------------------------------------------------------   
  241.  //-----------------------------------------------------------------------------------------------------------------------------     
  242.            
  243.         private void btnOne_Click(object sender, EventArgs e)
  244.         {
  245.             btnFlag = 1;//修改组标志
  246.             //
  247.             listView1.Dock = DockStyle.None;
  248.             
  249.             btnOne.Dock = DockStyle.Top;
  250.             btnTwo.Dock = DockStyle.Bottom;
  251.             btnThree.SendToBack();
  252.             btnThree.Dock = DockStyle.Bottom;
  253.             btnFour.SendToBack();
  254.             btnFour.Dock = DockStyle.Bottom;
  255.             statusStrip1.SendToBack();
  256.             statusStrip1.Dock = DockStyle.Bottom;
  257.             //
  258.             listView1.BringToFront();
  259.             listView1.Dock = DockStyle.Fill;
  260.             listView1.Clear();          
  261.             //
  262.             InitialAddItem(btnFlag);
  263.          
  264.         }
  265.         private void btnTwo_Click(object sender, EventArgs e)
  266.         {
  267.              btnFlag = 2;//修改组标志
  268.              listView1.Dock = DockStyle.None;
  269.             //
  270.             btnTwo.Dock = DockStyle.Top;
  271.             btnOne.SendToBack();
  272.             btnOne.Dock = DockStyle.Top;
  273.             btnThree.Dock = DockStyle.Bottom;
  274.             btnFour.SendToBack();
  275.             btnFour.Dock = DockStyle.Bottom;
  276.             statusStrip1.SendToBack();
  277.             statusStrip1.Dock = DockStyle.Bottom;
  278.            
  279.             //
  280.            
  281.             listView1.Dock = DockStyle.Fill;
  282.            
  283.             listView1.Clear();
  284.             //
  285.             InitialAddItem(btnFlag);
  286.         }
  287.         private void btnThree_Click(object sender, EventArgs e)
  288.         {
  289.             btnFlag = 3;
  290.            listView1.Dock = DockStyle.None;
  291.             //
  292.             btnThree.SendToBack();
  293.             btnThree.Dock = DockStyle.Top;
  294.             btnTwo.SendToBack();
  295.             btnTwo.Dock = DockStyle.Top;
  296.             btnOne.SendToBack();
  297.             btnOne.Dock = DockStyle.Top;
  298.             btnFour.SendToBack();
  299.             btnFour.Dock = DockStyle.Bottom;
  300.             statusStrip1.SendToBack();
  301.             statusStrip1.Dock = DockStyle.Bottom;
  302.           
  303.             //
  304.             listView1.BringToFront();
  305.             listView1.Dock = DockStyle.Fill;
  306.             listView1.Clear();        
  307.             //
  308.             InitialAddItem(btnFlag);
  309.             
  310.         }
  311.         private void btnFour_Click(object sender, EventArgs e)
  312.         {
  313.             btnFlag = 4;
  314.             listView1.Dock = DockStyle.None;
  315.             btnFour.SendToBack();
  316.             btnFour.Dock = DockStyle.Top;
  317.             btnThree.SendToBack();
  318.             btnThree.Dock = DockStyle.Top;
  319.            
  320.             btnTwo.SendToBack();
  321.             btnTwo.Dock = DockStyle.Top;
  322.             btnOne.SendToBack();
  323.             btnOne.Dock = DockStyle.Top;
  324.          
  325.             listView1.BringToFront();
  326.             listView1.Dock = DockStyle.Fill;
  327.             listView1.Items.Clear();
  328.          
  329.             //
  330.             InitialAddItem(btnFlag);  
  331.         }
  332.         private void btnOne_MouseEnter(object sender, EventArgs e)
  333.         {
  334.             this.btnOne.BackColor = Color.LightSeaGreen;
  335.         }
  336.         private void btnOne_MouseLeave(object sender, EventArgs e)
  337.         {
  338.             this.btnOne.BackColor = Control.DefaultBackColor;
  339.         }
  340.         private void btnTwo_MouseEnter(object sender, EventArgs e)
  341.         {
  342.             this.btnTwo.BackColor = Color.LightSeaGreen;
  343.         }
  344.         private void btnTwo_MouseLeave(object sender, EventArgs e)
  345.         {
  346.             this.btnTwo.BackColor = Control.DefaultBackColor;
  347.         }
  348.         private void btnThree_MouseEnter(object sender, EventArgs e)
  349.         {
  350.             this.btnThree.BackColor = Color.LightSeaGreen;
  351.         }
  352.         private void btnThree_MouseLeave(object sender, EventArgs e)
  353.         {
  354.             this.btnThree.BackColor = Control.DefaultBackColor;
  355.         }
  356.    //-------------------------------------------------------------------------------------------------------------
  357.   //------------------------------------------------------------------------------------------------------------
  358.         /// <summary>
  359.         /// 加载项的方法
  360.         /// </summary>
  361.         /// <param name="btnFlag">分组标志</param>
  362.         private void InitialAddItem(int btnFlag)
  363.         {
  364.             string key = null;
  365.             string text = null;
  366.             int imageIndex = 0;
  367.             XmlNode root = clsXMl.GetRootNode();
  368.           
  369.            
  370.             int i=0;
  371.             foreach (XmlNode node in root.ChildNodes)
  372.             {
  373.                 if (node.Name == "Item" && node.FirstChild.InnerText == btnFlag.ToString())
  374.                 {
  375.                     key = node.ChildNodes[1].InnerText.ToString();
  376.                     text = node.ChildNodes[2].InnerText.ToString();
  377.                     imageIndex = Convert.ToInt32(node.ChildNodes[3].InnerText.ToString());
  378.                     //
  379.                     ListViewItem item = new ListViewItem();
  380.                     item.Tag = (object)key;//-----------------
  381.                     item.Text = text;
  382.                     item.ImageIndex = imageIndex;
  383.                     //
  384.                     listView1.Items.Add(item);
  385.                 }
  386.  
  387.             }
  388.         }
  389.    #region 处理双击打开项的事件
  390.         private void listView1_MouseDown(object sender, MouseEventArgs e)
  391.         {  
  392.             ListViewItem itemSelected = null; // itemSelected = listView1.GetItemAt(e.X, e.Y);
  393.        
  394.             itemSelected = listView1.FocusedItem;
  395.          
  396.             if (itemSelected != null && e.Clicks > 1)// double-click 
  397.             {
  398.                 DoItem(itemSelected);
  399.                 itemSelected = null;
  400.                 GC.Collect();
  401.             }
  402.         }
  403. #endregion
  404.         /// <summary>
  405.         /// 处理打开 项的方法
  406.         /// </summary>
  407.         /// <param name="itemSelected"></param>
  408.         private void DoItem(ListViewItem itemSelected)
  409.         {
  410.             try
  411.             {
  412.                 switch (itemSelected.Text)
  413.                 {
  414.                     default:
  415.                         Process.Start(itemSelected.Tag.ToString());
  416.                         break;
  417.                 }
  418.             }
  419.             catch
  420.             {
  421.                 MessageBox.Show("选中项无效");
  422.             }
  423.         }
  424.    #region   双击notifyIcon事件
  425.         private void notifyIcon1_DoubleClick(object sender, EventArgs e)
  426.         {
  427.             ShowFrmMain();
  428.         }
  429.         #endregion
  430.         /// <summary>
  431.        /// 显示主窗体的方法
  432.        /// </summary>
  433.         private void  ShowFrmMain()
  434.         {
  435.             if (this.WindowState == FormWindowState.Minimized)
  436.             {
  437.                 this.Show();
  438.                 this.WindowState = FormWindowState.Normal;
  439.                 this.Top = 40; ;
  440.                 this.Left = Screen.PrimaryScreen.Bounds.Width - 400;
  441.                 this.Activate();
  442.             }
  443.         }
  444.         /// <summary>
  445.         /// 保存主窗体TopMost设置的方法
  446.         /// </summary>
  447.         private void DoTopMostXML()
  448.         {
  449.             XmlNode root = clsXMl.GetRootNode();
  450.             foreach (XmlNode node in root.ChildNodes)
  451.             {
  452.                 if (node.Name == "TopMost")
  453.                 {
  454.                     if (node.InnerText == "yes")
  455.                     {
  456.                         node.InnerText = "no";
  457.                         this.TopMost = false;
  458.                         ToolStripMenuItemTopMost.Checked = false;
  459.                     }
  460.                     else if (node.InnerText == "no")
  461.                     {
  462.                         node.InnerText = "yes";
  463.                         this.TopMost = true;
  464.                         ToolStripMenuItemTopMost.Checked = true;
  465.                     }
  466.                 }
  467.             }
  468.             clsXMl.xmlSave();//save the config file
  469.           
  470.             root = null;
  471.         }
  472.  
  473.         /// <summary>
  474.         /// 保存开启启动设置的方法
  475.         /// </summary>
  476.         private void DoAutoRunXML()
  477.         {
  478.             XmlNode root = clsXMl.GetRootNode();
  479.             foreach (XmlNode node in root.ChildNodes)
  480.             {
  481.                 if (node.Name == "AutoRun")
  482.                 {
  483.                     if (node.InnerText == "no")
  484.                     {
  485.                         node.InnerText = "yes";//重写XML
  486.                         ToolStripMenuItemAutoRun.Checked = true;
  487.                         RunWhenStart(true, Application.ProductName, Application.StartupPath + "\" + Application.ProductName + ".exe");
  488.                     }
  489.                     else if (node.InnerText == "yes")
  490.                     {
  491.                         node.InnerText = "no";
  492.                         ToolStripMenuItemAutoRun.Checked = false;
  493.                         RunWhenStart(false, Application.ProductName, Application.StartupPath + "\" + Application.ProductName + ".exe");
  494.                     } 
  495.                     clsXMl.xmlSave();
  496.                     return;
  497.                 }
  498.                 
  499.             }
  500.           
  501.           
  502.         }
  503.         /// <summary>
  504.         /// 修改 开机启动 注册表的方法
  505.         /// </summary>
  506.         /// <param name="bFlag"> 是否开机启动</param>
  507.         /// <param name="strName">启动值的名称</param>
  508.         /// <param name="strPath">启动程序的路径</param>
  509.         private void RunWhenStart(bool bFlag, string strName, string strPath)
  510.         {
  511.             Microsoft.Win32.RegistryKey rootKey = Microsoft.Win32.Registry.LocalMachine;//本地计算机数据的配置
  512.             Microsoft.Win32.RegistryKey runKey = rootKey.CreateSubKey(@"SOFTWAREMicrosoftWindowsCurrentVersionRun");
  513.             if (bFlag == true)//创建开机启动
  514.             {
  515.                 try
  516.                 {
  517.                     runKey.SetValue(strName, strPath);
  518.                     rootKey.Close();// 刷新 关闭 保存修改 
  519.                 }
  520.                 catch (Exception ex)
  521.                 {
  522.                     MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  523.                 }
  524.             }
  525.             else
  526.             {
  527.                 try
  528.                 {
  529.                     runKey.DeleteValue(strName);
  530.                     rootKey.Close();
  531.                 }
  532.                 catch (Exception ex)
  533.                 {
  534.                     MessageBox.Show(ex.Message, " 提示", MessageBoxButtons.OK, MessageBoxIcon.Error);
  535.                 }
  536.             }
  537.         }
  538.         [DllImport("user32.dll")]
  539.         private static extern int SendMessageA(int hWnd, int Msg, int wParam, int lParam);
  540.         private const int HWND_BROADCAST = 0xffff;
  541.         private const int WM_SYSCOMMAND = 0x112;
  542.         private const int SC_SCREENSAVE = 0xf140;
  543.   #region  开启屏保事件  
  544.         private void toolStripButtonScreenSave_Click(object sender, EventArgs e)
  545.         {
  546.             SendMessageA(HWND_BROADCAST, WM_SYSCOMMAND, SC_SCREENSAVE, 0);
  547.         }
  548.     #endregion
  549.   #region       处理桌面图标 事件
  550.         private void toolStripButtonShowDesktopIcon_Click(object sender, EventArgs e)
  551.         {
  552.             ShowOrHideDeskTopIcos();
  553.         }
  554.  #endregion
  555.         [DllImport("user32.dll")]
  556.         public static extern int FindWindow(string lpClassName, string lpWindowName);
  557.         [DllImport("user32.dll")]
  558.         public static extern bool ShowWindow(int hWnd, int cmdShow);
  559.         /// <summary>
  560.         /// 处理桌面图标显示或隐藏的方法
  561.         /// </summary>
  562.         private void  ShowOrHideDeskTopIcos()
  563.         {
  564.             int h = FindWindow("Progman", null);
  565.             switch (toolStripButtonShowDesktopIcon.Text)
  566.             {
  567.                 case "隐藏桌面图标":
  568.                     toolStripButtonShowDesktopIcon.Text = "显示桌面图标";
  569.                     toolStripButtonShowDesktopIcon.ToolTipText = "显示桌面图标";
  570.                     ToolStripMenuItemShowDeskTopIco.Text = "显示桌面图标";
  571.                     ShowWindow(h, 0);
  572.                     break;
  573.                 case "显示桌面图标":
  574.                     toolStripButtonShowDesktopIcon.Text = "隐藏桌面图标";
  575.                     toolStripButtonShowDesktopIcon.ToolTipText = "隐藏桌面图标";
  576.                     ToolStripMenuItemShowDeskTopIco.Text = "隐藏桌面图标";
  577.                     ShowWindow(h, 1);
  578.                     break;
  579.             }
  580.         }
  581.        
  582.   #region 单击X 或alt+F4 隐藏窗体
  583.         private void Form1_FormClosing(object sender, FormClosingEventArgs e)
  584.         {
  585.             if (e.CloseReason == CloseReason.UserClosing)
  586.             {
  587.                 this.Hide();//隐藏窗体
  588.                 e.Cancel = true;
  589.                 this.WindowState = FormWindowState.Minimized;
  590.             }
  591.         }
  592.       #endregion
  593.   #region 窗体Resize事件 最小化则隐藏窗体
  594.         private void Form1_Resize(object sender, EventArgs e)
  595.         {
  596.             if (this.WindowState == FormWindowState.Minimized)//窗口状态为最小化时 
  597.                 this.Hide();//隐藏窗口 
  598.         }
  599.         #endregion
  600.         /// <summary>
  601.         /// 退出主窗体 方法
  602.         /// </summary>
  603.         private void FrmMainExit()
  604.         {
  605.             if (MessageBox.Show("你真要退出吗?", "提示", MessageBoxButtons.OKCancel, MessageBoxIcon.Information) == DialogResult.OK)
  606.             {
  607.                 notifyIcon1.Visible = false;
  608.                 Application.Exit();
  609.                 this.Dispose();
  610.             }
  611.         }
  612.        
  613.   #region 工具栏按钮单击事件 退出程序
  614.         private void toolStripButtonExit_Click(object sender, EventArgs e)
  615.         {
  616.             notifyIcon1.Visible = false;
  617.             Application.Exit();
  618.             this.Dispose();
  619.         }
  620.         #endregion
  621.           
  622.   #region listview选中项发生变化所引发的事件
  623.         private void listView1_ItemSelectionChanged(object sender, ListViewItemSelectionChangedEventArgs e)
  624.         {
  625.             if (listView1.SelectedItems.Count > 0)
  626.             {
  627.                 contextMenuStripListView.Items[0].Enabled = true;
  628.                 contextMenuStripListView.Items["ToolStripMenuItemRemoveItem"].Enabled = true;
  629.             }
  630.             else//未选中项
  631.             {
  632.                 contextMenuStripListView.Items[0].Enabled = false;
  633.                 contextMenuStripListView.Items["ToolStripMenuItemRemoveItem"].Enabled = false;
  634.             }
  635.         }
  636.     #endregion
  637.         
  638.   #region contextMenuStripListView菜单的打开程序事件
  639.         private void 打开ToolStripMenuItem_Click(object sender, EventArgs e)
  640.         {
  641.              
  642.             DoItem(listView1.FocusedItem);
  643.         }
  644.  #endregion
  645.       
  646.        
  647.   #region contextMenuStripTaskbar背景绘制
  648.         private void contextMenuStripTaskbar_Paint(object sender, PaintEventArgs e)
  649.         {
  650.             Graphics g = e.Graphics;
  651.             Color FColor = Color.LightGreen;
  652.             Color TColor = Color.MediumPurple;
  653.             Brush b = new LinearGradientBrush(this.contextMenuStripTaskbar.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal);
  654.             g.FillRectangle(b, this.contextMenuStripTaskbar.ClientRectangle);
  655.         }
  656.  #endregion
  657.   #region 设置为红色背景  
  658.         private void ToolStripMenuItemBgBlue_Click(object sender, EventArgs e)
  659.         {
  660.             string strImagePath = Application.StartupPath.Substring(0) + "\images";
  661.             this.listView1.BackgroundImage = Image.FromFile(strImagePath + "\bgBlue.jpg");
  662.             ToolStripMenuItemBgRed.Checked = false;
  663.             ToolStripMenuItemBgBlue.Checked =true ;
  664.             foreach(XmlNode node in clsXMl.GetRootNode())
  665.             {
  666.                 if (node.Name =="BgImage")
  667.                 {
  668.                     if (node.InnerText != "blue")
  669.                     {
  670.                         node.InnerText = "blue";
  671.                     }
  672.                     else
  673.                     {
  674.                         return;
  675.                     }
  676.                 }
  677.                 return;
  678.                 
  679.             }
  680.             clsXMl.xmlSave();
  681.             strImagePath = null;
  682.            
  683.         }
  684.  #endregion
  685.   #region 设置为蓝色背景
  686.         private void ToolStripMenuItemBgRed_Click(object sender, EventArgs e)
  687.         {  
  688.             string strImagePath = Application.StartupPath.Substring(0) + "\images";
  689.             this.listView1.BackgroundImage = Image.FromFile(strImagePath + "\bgRed.jpg");
  690.             ToolStripMenuItemBgRed.Checked = true;
  691.             ToolStripMenuItemBgBlue.Checked = false;
  692.             foreach (XmlNode node in clsXMl.GetRootNode())
  693.             {
  694.                 if (node.Name == "BgImage")
  695.                 {
  696.                     if (node.InnerText != "red")
  697.                     {
  698.                         node.InnerText = "red";
  699.                     }
  700.                     else
  701.                     {
  702.                         return;
  703.                     }
  704.                 }
  705.                 return;
  706.             }
  707.             clsXMl.xmlSave();
  708.             strImagePath = null;
  709.         }
  710.  #endregion
  711.        
  712.   #region  红色 大图标
  713.         private void ToolStripMenuItemLargeRed_Click(object sender, EventArgs e)
  714.         {
  715.             listView1.LargeImageList = imageListLargeRed;
  716.             ToolStripMenuItemLargeBlue.Checked = false;
  717.             ToolStripMenuItemLargeRed.Checked = true;
  718.             ToolStripMenuItemSmallRed.Checked = false;
  719.             ToolStripMenuItemSmallBlue.Checked = false;
  720.             foreach (XmlNode node in clsXMl.GetRootNode().ChildNodes)
  721.             {
  722.                 if (node.Name == "Icon")
  723.                 {
  724.                        
  725.                         if (node.InnerText.Trim() == "blue")
  726.                         {
  727.                             node.InnerText = "red";
  728.                         }
  729.                     
  730.                         if (node.Attributes["size"].Value == "small")
  731.                         {
  732.                             XmlElement xe = (XmlElement)node;
  733.                             xe.SetAttribute("size", "large");
  734.                         }
  735.                         else if ((node.Attributes["size"].Value == "large"))
  736.                         {
  737.                             return;
  738.                         }
  739.                   
  740.                    
  741.                 }
  742.                 return;
  743.             }
  744.             clsXMl.xmlSave();
  745.         }
  746.   #endregion
  747.   #region 蓝色大图标
  748.         private void ToolStripMenuItemLargeBlue_Click(object sender, EventArgs e)
  749.         {
  750.            
  751.             listView1.LargeImageList = imageListLargeBlue;
  752.             ToolStripMenuItemLargeBlue.Checked = true;
  753.             ToolStripMenuItemLargeRed.Checked = false;
  754.             ToolStripMenuItemSmallRed.Checked = false;
  755.             ToolStripMenuItemSmallBlue.Checked = false;
  756.             foreach (XmlNode node in clsXMl.GetRootNode())
  757.             {
  758.                 if (node.Name == "Icon")
  759.                 {
  760.                     if (node.InnerText.Trim() == "red")
  761.                     {
  762.                         node.InnerText = "blue";
  763.                     }
  764.                     if (node.Attributes["size"].Value == "small")
  765.                     {
  766.                         XmlElement xe = (XmlElement)node;
  767.                         xe.SetAttribute("size", "large");
  768.                     }
  769.                     else if ((node.Attributes["size"].Value == "large"))
  770.                     {
  771.                         return;
  772.                     }
  773.                 }
  774.                 return;
  775.             }
  776.             clsXMl.xmlSave();
  777.         }
  778.  #endregion
  779.   #region 红色 小图标
  780.         private void ToolStripMenuItemSmallRed_Click_1(object sender, EventArgs e)
  781.         {
  782.             listView1.LargeImageList = imageListSmallRed;
  783.             ToolStripMenuItemLargeBlue.Checked = false;
  784.             ToolStripMenuItemLargeRed.Checked = false;
  785.             ToolStripMenuItemSmallRed.Checked = true;
  786.             ToolStripMenuItemSmallBlue.Checked = false;
  787.             foreach (XmlNode node in clsXMl.GetRootNode())
  788.             {
  789.                 if (node.Name == "Icon")
  790.                 {
  791.                     if (node.InnerText.Trim() == "blue")
  792.                     {
  793.                         node.InnerText = "red";
  794.                     }
  795.                     if (node.Attributes["size"].Value == "large")
  796.                     {
  797.                         XmlElement xe = (XmlElement)node;
  798.                         xe.SetAttribute("size", "small");
  799.                     }
  800.                     else if ((node.Attributes["size"].Value == "small"))
  801.                     {
  802.                         return;
  803.                     }
  804.                 }
  805.                 return;
  806.             }
  807.             clsXMl.xmlSave();
  808.         }
  809. #endregion
  810.   #region 蓝色 小图标
  811.         private void ToolStripMenuItemSmallBlue_Click(object sender, EventArgs e)
  812.         {
  813.          listView1.LargeImageList = imageListSmallBlue;
  814.          ToolStripMenuItemLargeBlue.Checked = false;
  815.          ToolStripMenuItemLargeRed.Checked = false;
  816.          ToolStripMenuItemSmallRed.Checked = false;
  817.          ToolStripMenuItemSmallBlue.Checked = true;
  818.          foreach (XmlNode node in clsXMl.GetRootNode())
  819.          {
  820.              if (node.Name == "Icon")
  821.              {
  822.                  if (node.InnerText.Trim() == "red")
  823.                  {
  824.                      node.InnerText = "blue";
  825.                  }
  826.                  if (node.Attributes["size"].Value == "large")
  827.                  {
  828.                      XmlElement xe = (XmlElement)node;
  829.                      xe.SetAttribute("size", "small");
  830.                  }
  831.                  else if ((node.Attributes["size"].Value == "small"))
  832.                  {
  833.                      return;
  834.                  }
  835.              }
  836.              return;
  837.          }
  838.          clsXMl.xmlSave();
  839.      }
  840. #endregion
  841.        
  842.  #region  contextMenuStripListView 添加项 事件  
  843.         private void ToolStripMenuItemAddItem_Click(object sender, EventArgs e)
  844.         {               
  845.                    frmAddItem f2 = new frmAddItem();
  846.                                    
  847.                     f2.Intflag= btnFlag;//
  848.                     f2.ShowDialog();
  849.   
  850.                     string[] arrStr = f2.ItemInfo.Split('|');
  851.                     if (arrStr.Length > 2)
  852.                     {
  853.                         ListViewItem item = new ListViewItem();
  854.                         item.Tag = (object)arrStr[0];//key 
  855.                         item.Text = arrStr[1]; //text
  856.                         item.ImageIndex = int.Parse(arrStr[2]);//imageIndex
  857.                         listView1.Items.Add(item);
  858.                            
  859.                     }
  860.                 }
  861.  #endregion
  862.                 
  863.   #region  绘制背景
  864.         private void contextMenuStrip2_Paint(object sender, PaintEventArgs e)
  865.         {
  866.             Graphics g = e.Graphics;
  867.             Color FColor = Color.LightGreen;
  868.             Color TColor = Color.MediumPurple;
  869.             Brush b = new LinearGradientBrush(this.contextMenuStripListView.ClientRectangle, FColor, TColor, LinearGradientMode.ForwardDiagonal);
  870.             g.FillRectangle(b, this.contextMenuStripListView.ClientRectangle);
  871.         }
  872.   #endregion
  873.   #region     右键菜单contextMenuStripTaskbar的单击响应处理
  874.         private void contextMenuStripTaskbar_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
  875.         {
  876.                     switch (e.ClickedItem.Name)
  877.                     {
  878.                         case "ToolStripMenuItemShowFrmMain":
  879.                             ShowFrmMain();
  880.                             break;
  881.                         case "ToolStripMenuItemShowDeskTopIco":
  882.                             ShowOrHideDeskTopIcos();
  883.                             break;
  884.                         case "ToolStripMenuItemAutoRun":
  885.                             DoAutoRunXML();
  886.                             break;
  887.                         case "ToolStripMenuItemTopMost":
  888.                             DoTopMostXML();
  889.                             break;
  890.                         case "ToolStripMenuItemAbout":
  891.                             MessageBox.Show("create by myh65013nmsproject@live.com");
  892.                             break;
  893.                         case "ToolStripMenuItemExit":
  894.                             FrmMainExit();
  895.                             break;  
  896.                 }
  897.             }
  898.    #endregion
  899.        
  900.   #region   移除项
  901.         private void ToolStripMenuItemRemoveItem_Click(object sender, EventArgs e)
  902.         {
  903.             try
  904.             {
  905.                 ListViewItem item = listView1.FocusedItem;
  906.                 listView1.Items.Remove(item);
  907.                 foreach (XmlNode node in clsXMl.GetRootNode().ChildNodes)
  908.                 {
  909.                     if (node.Name == "Item")
  910.                     {
  911.                         if (node.FirstChild.NextSibling.InnerText == item.Tag.ToString())
  912.                         {
  913.                             try
  914.                             {
  915.                                 node.ParentNode.RemoveChild(node);
  916.                             }
  917.                             catch (Exception ex)
  918.                             {
  919.                                 MessageBox.Show(ex.Message);
  920.                             }
  921.                         }
  922.                     }
  923.                 }
  924.             }
  925.             catch 
  926.             {
  927.                 MessageBox.Show("没有选中项");
  928.             }
  929.             clsXMl.xmlSave();
  930.         }
  931.      #endregion
  932.   #region timer事件
  933.         private void timercpu_Tick(object sender, EventArgs e)
  934.         {
  935.             
  936.             toolStripProgressBarCpu.Value = (int)(p.NextValue());
  937.             toolStripStatusLabelCPU.Text = toolStripProgressBarCpu.Value.ToString() + "%";
  938.         }
  939.     #endregion
  940.     }
  941. }