⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 mainform.cs

📁 USB device eject 的源文件
💻 CS
📖 第 1 页 / 共 2 页
字号:
            this.helpToolStripMenuItem.Text = "&Help";
            // 
            // aToolStripMenuItem
            // 
            this.aToolStripMenuItem.Name = "aToolStripMenuItem";
            this.aToolStripMenuItem.Size = new System.Drawing.Size(115, 22);
            this.aToolStripMenuItem.Text = "&About...";
            this.aToolStripMenuItem.Click += new System.EventHandler(this.aToolStripMenuItem_Click);
            // 
            // propertyGridDevice
            // 
            this.propertyGridDevice.Dock = System.Windows.Forms.DockStyle.Fill;
            this.propertyGridDevice.HelpVisible = false;
            this.propertyGridDevice.Location = new System.Drawing.Point(0, 0);
            this.propertyGridDevice.Name = "propertyGridDevice";
            this.propertyGridDevice.PropertySort = System.Windows.Forms.PropertySort.NoSort;
            this.propertyGridDevice.Size = new System.Drawing.Size(541, 252);
            this.propertyGridDevice.TabIndex = 0;
            this.propertyGridDevice.ToolbarVisible = false;
            // 
            // MainForm
            // 
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(541, 486);
            this.Controls.Add(this.splitContainer1);
            this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon")));
            this.MainMenuStrip = this.mainMenuStrip;
            this.Name = "MainForm";
            this.Text = "USB Disk Eject";
            this.splitContainer1.Panel1.ResumeLayout(false);
            this.splitContainer1.Panel1.PerformLayout();
            this.splitContainer1.Panel2.ResumeLayout(false);
            this.splitContainer1.ResumeLayout(false);
            this.contextMenuStrip.ResumeLayout(false);
            this.mainMenuStrip.ResumeLayout(false);
            this.mainMenuStrip.PerformLayout();
            this.ResumeLayout(false);

		}
		#endregion

		[STAThread]
		static void Main() 
		{
			Application.Run(new MainForm());
		}

		private void menuItemExit_Click(object sender, System.EventArgs e)
		{
			Close();
		}

		private void menuItemAbout_Click(object sender, System.EventArgs e)
		{
			About about = new About();
			about.ShowDialog(this);
		}

		private void menuItemRefresh_Click(object sender, System.EventArgs e)
		{
            LoadItems();
		}

        protected override void WndProc(ref Message m)
        {
            if (m.Msg == Native.WM_DEVICECHANGE)
            {
                if (!_loading)
                {
                    LoadItems();
                }
            }
            base.WndProc(ref m);
        }

        private void LoadItems()
		{
            _loading = true;
			treeViewDisks.Nodes.Clear();

			TreeNode root = treeViewDisks.Nodes.Add("Computer");
			root.ImageIndex = (int)IconIndex.MyComputer;
			root.SelectedImageIndex = root.ImageIndex;

            // display volumes
            VolumeDeviceClass volumeDeviceClass = new VolumeDeviceClass();
            TreeNode volumesNode = new TreeNode("Volumes");
            volumesNode.ImageIndex = (int)IconIndex.Volume;
            volumesNode.SelectedImageIndex = volumesNode.ImageIndex;
            root.Nodes.Add(volumesNode);

            foreach (Volume device in volumeDeviceClass.Devices)
            {
                if ((usbOnlyToolStripMenuItem.Checked) && (!device.IsUsb))
                    continue;

                string text = null;
                if ((device.LogicalDrive != null) && (device.LogicalDrive.Length > 0))
                {
                    text += device.LogicalDrive;
                }

                if (text != null)
                {
                    text += " ";
                }
                text += device.Description;
                if (device.FriendlyName != null)
                {
                    if (text != null)
                    {
                        text += " - ";
                    }
                    text += device.FriendlyName;
                }

                TreeNode deviceNode = volumesNode.Nodes.Add(text);

                if (device.IsUsb)
                {
                    deviceNode.ImageIndex = (int)IconIndex.Box;
                    deviceNode.SelectedImageIndex = deviceNode.ImageIndex;
                }
                deviceNode.Tag = device;

                foreach (Device disk in device.Disks)
                {
                    TreeNode diskNode = deviceNode.Nodes.Add(disk.Description + " - " + disk.FriendlyName);
                    diskNode.ImageIndex = deviceNode.ImageIndex;
                    diskNode.SelectedImageIndex = diskNode.ImageIndex;
                    diskNode.Tag = device;
                }
            }

            root.ExpandAll();
            _loading = false;
        }

        private void treeViewDisks_AfterSelect(object sender, TreeViewEventArgs e)
        {
            // update property grid
            Device device = (Device)e.Node.Tag;
            if (device == null)
            {
                propertyGridDevice.SelectedObject = null;
                return;
            }

            propertyGridDevice.SelectedObject = device;
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void aToolStripMenuItem_Click(object sender, EventArgs e)
        {
            About about = new About();
            about.ShowDialog(this);
        }

        private void usbOnlyToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadItems();
        }

        private void refreshToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LoadItems();
        }

        private void ejectToolStripMenuItem_Click(object sender, EventArgs e)
        {
            Device device = GetSelectedDevice();
            if (device == null)
                return;

            string s = device.Eject(true);
            if (s != null)
            {
                MessageBox.Show(this, s, Text, MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void treeViewDisks_NodeMouseClick(object sender, TreeNodeMouseClickEventArgs e)
        {
            treeViewDisks.SelectedNode = e.Node;
        }

        private Device GetSelectedDevice()
        {
            TreeNode node = treeViewDisks.SelectedNode;
            if (node == null)
            {
                return null;
            }

            return (Device)node.Tag;
        }

        private void contextMenuStrip_Opening(object sender, CancelEventArgs e)
        {
            Device device = GetSelectedDevice();
            if ((device == null) || (!device.IsUsb))
            {
                e.Cancel = true;
                return;
            }
        }
	}
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -