MainForm.cs
上传用户:lyzyl198
上传日期:2008-05-19
资源大小:174k
文件大小:17k
源码类别:

C#编程

开发平台:

C#

  1. // UsbEject version 1.0 March 2006
  2. // written by Simon Mourier <email: simon [underscore] mourier [at] hotmail [dot] com>
  3. using System;
  4. using System.Drawing;
  5. using System.Collections;
  6. using System.ComponentModel;
  7. using System.Resources;
  8. using System.Reflection;
  9. using System.Windows.Forms;
  10. using System.Data;
  11. using UsbEject.Library;
  12. namespace UsbEject
  13. {
  14. public class MainForm: System.Windows.Forms.Form
  15.     {
  16.         private SplitContainer splitContainer1;
  17.         private TreeView treeViewDisks;
  18.         private PropertyGrid propertyGridDevice;
  19.         private MenuStrip mainMenuStrip;
  20.         private ToolStripMenuItem fileToolStripMenuItem;
  21.         private ToolStripMenuItem exitToolStripMenuItem;
  22.         private ToolStripMenuItem viewToolStripMenuItem;
  23.         private ToolStripMenuItem helpToolStripMenuItem;
  24.         private ContextMenuStrip contextMenuStrip;
  25.         private ToolStripMenuItem aToolStripMenuItem;
  26.         private ToolStripMenuItem refreshToolStripMenuItem;
  27.         private ToolStripMenuItem usbOnlyToolStripMenuItem;
  28.         private ToolStripSeparator toolStripSeparator1;
  29.         private IContainer components;
  30.         private ToolStripMenuItem ejectToolStripMenuItem;
  31.         private bool _loading;
  32. private enum IconIndex
  33. {
  34. Volume = 0,
  35. MyComputer = 1,
  36. Box = 2,
  37. Disk = 3,
  38. Eject = 4
  39. }
  40. public MainForm()
  41. {
  42. InitializeComponent();
  43. // load icons from resource
  44. ResourceManager resourceManager = new ResourceManager("UsbEject.Icons", GetType().Module.Assembly);
  45. treeViewDisks.ImageList = new ImageList();
  46. treeViewDisks.ImageList.Images.Add((Icon)resourceManager.GetObject("ico9.ico"));
  47. treeViewDisks.ImageList.Images.Add((Icon)resourceManager.GetObject("ico16.ico"));
  48. treeViewDisks.ImageList.Images.Add((Icon)resourceManager.GetObject("ico27.ico"));
  49. treeViewDisks.ImageList.Images.Add((Icon)resourceManager.GetObject("ico233.ico"));
  50. treeViewDisks.ImageList.Images.Add((Icon)resourceManager.GetObject("UsbEject.ico"));
  51.             LoadItems();
  52.         }
  53. protected override void Dispose( bool disposing )
  54. {
  55. if( disposing )
  56. {
  57. if (components != null) 
  58. {
  59. components.Dispose();
  60. }
  61. }
  62. base.Dispose( disposing );
  63. }
  64. #region Windows Form Designer generated code
  65. /// <summary>
  66. /// Required method for Designer support - do not modify
  67. /// the contents of this method with the code editor.
  68. /// </summary>
  69. private void InitializeComponent()
  70. {
  71.             this.components = new System.ComponentModel.Container();
  72.             System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(MainForm));
  73.             this.splitContainer1 = new System.Windows.Forms.SplitContainer();
  74.             this.treeViewDisks = new System.Windows.Forms.TreeView();
  75.             this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
  76.             this.ejectToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  77.             this.mainMenuStrip = new System.Windows.Forms.MenuStrip();
  78.             this.fileToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  79.             this.exitToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  80.             this.viewToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  81.             this.usbOnlyToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  82.             this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
  83.             this.refreshToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  84.             this.helpToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  85.             this.aToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
  86.             this.propertyGridDevice = new System.Windows.Forms.PropertyGrid();
  87.             this.splitContainer1.Panel1.SuspendLayout();
  88.             this.splitContainer1.Panel2.SuspendLayout();
  89.             this.splitContainer1.SuspendLayout();
  90.             this.contextMenuStrip.SuspendLayout();
  91.             this.mainMenuStrip.SuspendLayout();
  92.             this.SuspendLayout();
  93.             // 
  94.             // splitContainer1
  95.             // 
  96.             this.splitContainer1.Dock = System.Windows.Forms.DockStyle.Fill;
  97.             this.splitContainer1.Location = new System.Drawing.Point(0, 0);
  98.             this.splitContainer1.Name = "splitContainer1";
  99.             this.splitContainer1.Orientation = System.Windows.Forms.Orientation.Horizontal;
  100.             // 
  101.             // splitContainer1.Panel1
  102.             // 
  103.             this.splitContainer1.Panel1.Controls.Add(this.treeViewDisks);
  104.             this.splitContainer1.Panel1.Controls.Add(this.mainMenuStrip);
  105.             // 
  106.             // splitContainer1.Panel2
  107.             // 
  108.             this.splitContainer1.Panel2.Controls.Add(this.propertyGridDevice);
  109.             this.splitContainer1.Size = new System.Drawing.Size(541, 486);
  110.             this.splitContainer1.SplitterDistance = 230;
  111.             this.splitContainer1.TabIndex = 0;
  112.             // 
  113.             // treeViewDisks
  114.             // 
  115.             this.treeViewDisks.ContextMenuStrip = this.contextMenuStrip;
  116.             this.treeViewDisks.Dock = System.Windows.Forms.DockStyle.Fill;
  117.             this.treeViewDisks.FullRowSelect = true;
  118.             this.treeViewDisks.HideSelection = false;
  119.             this.treeViewDisks.Location = new System.Drawing.Point(0, 24);
  120.             this.treeViewDisks.Name = "treeViewDisks";
  121.             this.treeViewDisks.Size = new System.Drawing.Size(541, 206);
  122.             this.treeViewDisks.TabIndex = 1;
  123.             this.treeViewDisks.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeViewDisks_AfterSelect);
  124.             this.treeViewDisks.NodeMouseClick += new System.Windows.Forms.TreeNodeMouseClickEventHandler(this.treeViewDisks_NodeMouseClick);
  125.             // 
  126.             // contextMenuStrip
  127.             // 
  128.             this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  129.             this.ejectToolStripMenuItem});
  130.             this.contextMenuStrip.Name = "contextMenuStrip";
  131.             this.contextMenuStrip.Size = new System.Drawing.Size(101, 26);
  132.             this.contextMenuStrip.Opening += new System.ComponentModel.CancelEventHandler(this.contextMenuStrip_Opening);
  133.             // 
  134.             // ejectToolStripMenuItem
  135.             // 
  136.             this.ejectToolStripMenuItem.Name = "ejectToolStripMenuItem";
  137.             this.ejectToolStripMenuItem.Size = new System.Drawing.Size(100, 22);
  138.             this.ejectToolStripMenuItem.Text = "&Eject";
  139.             this.ejectToolStripMenuItem.Click += new System.EventHandler(this.ejectToolStripMenuItem_Click);
  140.             // 
  141.             // mainMenuStrip
  142.             // 
  143.             this.mainMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
  144.             this.fileToolStripMenuItem,
  145.             this.viewToolStripMenuItem,
  146.             this.helpToolStripMenuItem});
  147.             this.mainMenuStrip.Location = new System.Drawing.Point(0, 0);
  148.             this.mainMenuStrip.Name = "mainMenuStrip";
  149.             this.mainMenuStrip.Size = new System.Drawing.Size(541, 24);
  150.             this.mainMenuStrip.TabIndex = 2;
  151.             this.mainMenuStrip.Text = "menuStrip1";
  152.             // 
  153.             // fileToolStripMenuItem
  154.             // 
  155.             this.fileToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  156.             this.exitToolStripMenuItem});
  157.             this.fileToolStripMenuItem.Name = "fileToolStripMenuItem";
  158.             this.fileToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
  159.             this.fileToolStripMenuItem.Text = "&File";
  160.             // 
  161.             // exitToolStripMenuItem
  162.             // 
  163.             this.exitToolStripMenuItem.Name = "exitToolStripMenuItem";
  164.             this.exitToolStripMenuItem.Size = new System.Drawing.Size(94, 22);
  165.             this.exitToolStripMenuItem.Text = "E&xit";
  166.             this.exitToolStripMenuItem.Click += new System.EventHandler(this.exitToolStripMenuItem_Click);
  167.             // 
  168.             // viewToolStripMenuItem
  169.             // 
  170.             this.viewToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  171.             this.usbOnlyToolStripMenuItem,
  172.             this.toolStripSeparator1,
  173.             this.refreshToolStripMenuItem});
  174.             this.viewToolStripMenuItem.Name = "viewToolStripMenuItem";
  175.             this.viewToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
  176.             this.viewToolStripMenuItem.Text = "&View";
  177.             // 
  178.             // usbOnlyToolStripMenuItem
  179.             // 
  180.             this.usbOnlyToolStripMenuItem.Checked = true;
  181.             this.usbOnlyToolStripMenuItem.CheckOnClick = true;
  182.             this.usbOnlyToolStripMenuItem.CheckState = System.Windows.Forms.CheckState.Checked;
  183.             this.usbOnlyToolStripMenuItem.Name = "usbOnlyToolStripMenuItem";
  184.             this.usbOnlyToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
  185.             this.usbOnlyToolStripMenuItem.Text = "USB Only";
  186.             this.usbOnlyToolStripMenuItem.Click += new System.EventHandler(this.usbOnlyToolStripMenuItem_Click);
  187.             // 
  188.             // toolStripSeparator1
  189.             // 
  190.             this.toolStripSeparator1.Name = "toolStripSeparator1";
  191.             this.toolStripSeparator1.Size = new System.Drawing.Size(115, 6);
  192.             // 
  193.             // refreshToolStripMenuItem
  194.             // 
  195.             this.refreshToolStripMenuItem.Name = "refreshToolStripMenuItem";
  196.             this.refreshToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
  197.             this.refreshToolStripMenuItem.Text = "&Refresh";
  198.             this.refreshToolStripMenuItem.Click += new System.EventHandler(this.refreshToolStripMenuItem_Click);
  199.             // 
  200.             // helpToolStripMenuItem
  201.             // 
  202.             this.helpToolStripMenuItem.DropDownItems.AddRange(new System.Windows.Forms.ToolStripItem[] {
  203.             this.aToolStripMenuItem});
  204.             this.helpToolStripMenuItem.Name = "helpToolStripMenuItem";
  205.             this.helpToolStripMenuItem.Size = new System.Drawing.Size(41, 20);
  206.             this.helpToolStripMenuItem.Text = "&Help";
  207.             // 
  208.             // aToolStripMenuItem
  209.             // 
  210.             this.aToolStripMenuItem.Name = "aToolStripMenuItem";
  211.             this.aToolStripMenuItem.Size = new System.Drawing.Size(118, 22);
  212.             this.aToolStripMenuItem.Text = "&About...";
  213.             this.aToolStripMenuItem.Click += new System.EventHandler(this.aToolStripMenuItem_Click);
  214.             // 
  215.             // propertyGridDevice
  216.             // 
  217.             this.propertyGridDevice.Dock = System.Windows.Forms.DockStyle.Fill;
  218.             this.propertyGridDevice.HelpVisible = false;
  219.             this.propertyGridDevice.Location = new System.Drawing.Point(0, 0);
  220.             this.propertyGridDevice.Name = "propertyGridDevice";
  221.             this.propertyGridDevice.PropertySort = System.Windows.Forms.PropertySort.NoSort;
  222.             this.propertyGridDevice.Size = new System.Drawing.Size(541, 252);
  223.             this.propertyGridDevice.TabIndex = 0;
  224.             this.propertyGridDevice.ToolbarVisible = false;
  225.             // 
  226.             // MainForm
  227.             // 
  228.             this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
  229.             this.ClientSize = new System.Drawing.Size(541, 486);
  230.             this.Controls.Add(this.splitContainer1);
  231.             this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
  232.             this.MainMenuStrip = this.mainMenuStrip;
  233.             this.Name = "MainForm";
  234.             this.Text = "USB Disk Eject";
  235.             this.splitContainer1.Panel1.ResumeLayout(false);
  236.             this.splitContainer1.Panel1.PerformLayout();
  237.             this.splitContainer1.Panel2.ResumeLayout(false);
  238.             this.splitContainer1.ResumeLayout(false);
  239.             this.contextMenuStrip.ResumeLayout(false);
  240.             this.mainMenuStrip.ResumeLayout(false);
  241.             this.mainMenuStrip.PerformLayout();
  242.             this.ResumeLayout(false);
  243. }
  244. #endregion
  245. [STAThread]
  246. static void Main() 
  247. {
  248. Application.Run(new MainForm());
  249. }
  250. private void menuItemExit_Click(object sender, System.EventArgs e)
  251. {
  252. Close();
  253. }
  254. private void menuItemAbout_Click(object sender, System.EventArgs e)
  255. {
  256. About about = new About();
  257. about.ShowDialog(this);
  258. }
  259. private void menuItemRefresh_Click(object sender, System.EventArgs e)
  260. {
  261.             LoadItems();
  262. }
  263.         protected override void WndProc(ref Message m)
  264.         {
  265.             if (m.Msg == Native.WM_DEVICECHANGE)
  266.             {
  267.                 if (!_loading)
  268.                 {
  269.                     LoadItems();
  270.                 }
  271.             }
  272.             base.WndProc(ref m);
  273.         }
  274.         private void LoadItems()
  275. {
  276.             _loading = true;
  277. treeViewDisks.Nodes.Clear();
  278. TreeNode root = treeViewDisks.Nodes.Add("Computer");
  279. root.ImageIndex = (int)IconIndex.MyComputer;
  280. root.SelectedImageIndex = root.ImageIndex;
  281.             // display volumes
  282.             VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
  283.             TreeNode volumesNode = new TreeNode("Volumes");
  284.             volumesNode.ImageIndex = (int)IconIndex.Volume;
  285.             volumesNode.SelectedImageIndex = volumesNode.ImageIndex;
  286.             root.Nodes.Add(volumesNode);
  287.             foreach (Volume device in volumeDeviceClass.Devices)
  288.             {
  289.                 if ((usbOnlyToolStripMenuItem.Checked) && (!device.IsUsb))
  290.                     continue;
  291.                 string text = null;
  292.                 if ((device.LogicalDrive != null) && (device.LogicalDrive.Length > 0))
  293.                 {
  294.                     text += device.LogicalDrive;
  295.                 }
  296.                 if (text != null)
  297.                 {
  298.                     text += " ";
  299.                 }
  300.                 text += device.Description;
  301.                 if (device.FriendlyName != null)
  302.                 {
  303.                     if (text != null)
  304.                     {
  305.                         text += " - ";
  306.                     }
  307.                     text += device.FriendlyName;
  308.                 }
  309.                 TreeNode deviceNode = volumesNode.Nodes.Add(text);
  310.                 if (device.IsUsb)
  311.                 {
  312.                     deviceNode.ImageIndex = (int)IconIndex.Box;
  313.                     deviceNode.SelectedImageIndex = deviceNode.ImageIndex;
  314.                 }
  315.                 deviceNode.Tag = device;
  316.                 foreach (Device disk in device.Disks)
  317.                 {
  318.                     TreeNode diskNode = deviceNode.Nodes.Add(disk.Description + " - " + disk.FriendlyName);
  319.                     diskNode.ImageIndex = deviceNode.ImageIndex;
  320.                     diskNode.SelectedImageIndex = diskNode.ImageIndex;
  321.                     diskNode.Tag = device;
  322.                 }
  323.             }
  324.             root.ExpandAll();
  325.             _loading = false;
  326.         }
  327.         private void treeViewDisks_AfterSelect(object sender, TreeViewEventArgs e)
  328.         {
  329.             // update property grid
  330.             Device device = (Device)e.Node.Tag;
  331.             if (device == null)
  332.             {
  333.                 propertyGridDevice.SelectedObject = null;
  334.                 return;
  335.             }
  336.             propertyGridDevice.SelectedObject = device;
  337.         }
  338.         private void exitToolStripMenuItem_Click(object sender, EventArgs e)
  339.         {
  340.             Close();
  341.         }
  342.         private void aToolStripMenuItem_Click(object sender, EventArgs e)
  343.         {
  344.             About about = new About();
  345.             about.ShowDialog(this);
  346.         }
  347.         private void usbOnlyToolStripMenuItem_Click(object sender, EventArgs e)
  348.         {
  349.             LoadItems();
  350.         }
  351.         private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
  352.         {
  353.             LoadItems();
  354.         }
  355.         private void ejectToolStripMenuItem_Click(object sender, EventArgs e)
  356.         {
  357.             Device device = GetSelectedDevice();
  358.             if (device == null)
  359.                 return;
  360.             string s = device.Eject(true);
  361.             if (s != null)
  362.             {
  363.                 MessageBox.Show(this, s, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
  364.             }
  365.         }
  366.         private void treeViewDisks_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
  367.         {
  368.             treeViewDisks.SelectedNode = e.Node;
  369.         }
  370.         private Device GetSelectedDevice()
  371.         {
  372.             TreeNode node = treeViewDisks.SelectedNode;
  373.             if (node == null)
  374.             {
  375.                 return null;
  376.             }
  377.             return (Device)node.Tag;
  378.         }
  379.         private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
  380.         {
  381.             Device device = GetSelectedDevice();
  382.             if ((device == null) || (!device.IsUsb))
  383.             {
  384.                 e.Cancel = true;
  385.                 return;
  386.             }
  387.         }
  388. }
  389. }