using System; using System.Runtime.InteropServices; using System.Drawing; namespace QuickStart { /// <summary> Summary description for ExtractIcon.</summary> public class ExtractIcon { [DllImport("shell32.dll", CharSet = CharSet.Auto)] private static extern int SHGetFileInfo( string pszPath, int dwFileAttributes, out SHFILEINFO psfi, uint cbfileInfo, SHGFI uFlags); /// <summary>Maximal Length of unmanaged Windows-Path-strings</summary> private const int MAX_PATH = 260; /// <summary>Maximal Length of unmanaged Typename</summary> private const int MAX_TYPE = 80; [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Auto)] private struct SHFILEINFO { public SHFILEINFO(bool b) { hIcon = IntPtr.Zero; iIcon = 0; dwAttributes = 0; szDisplayName = ""; szTypeName = ""; } public IntPtr hIcon; public int iIcon; public uint dwAttributes; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_PATH)] public string szDisplayName; [MarshalAs(UnmanagedType.ByValTStr, SizeConst = MAX_TYPE)] public string szTypeName; }; private ExtractIcon() { } [Flags] enum SHGFI : int { /// <summary>get icon</summary> Icon = 0x000000100, /// <summary>get display name</summary> DisplayName = 0x000000200, /// <summary>get type name</summary> TypeName = 0x000000400, /// <summary>get attributes</summary> Attributes = 0x000000800, /// <summary>get icon location</summary> IconLocation = 0x000001000, /// <summary>return exe type</summary> ExeType = 0x000002000, /// <summary>get system icon index</summary> SysIconIndex = 0x000004000, /// <summary>put a link overlay on icon</summary> LinkOverlay = 0x000008000, /// <summary>show icon in selected state</summary> Selected = 0x000010000, /// <summary>get only specified attributes</summary> Attr_Specified = 0x000020000, /// <summary>get large icon</summary> LargeIcon = 0x000000000, /// <summary>get small icon</summary> SmallIcon = 0x000000001, /// <summary>get open icon</summary> OpenIcon = 0x000000002, /// <summary>get shell size icon</summary> ShellIconSize = 0x000000004, /// <summary>pszPath is a pidl</summary> PIDL = 0x000000008, /// <summary>use passed dwFileAttribute</summary> UseFileAttributes = 0x000000010, /// <summary>apply the appropriate overlays</summary> AddOverlays = 0x000000020, /// <summary>Get the index of the overlay in the upper 8 bits of the iIcon</summary> OverlayIndex = 0x000000040, } /// <summary> /// Get the associated Icon for a file or application, this method always returns /// an icon. If the strPath is invalid or there is no idonc the default icon is returned /// </summary> /// <param name="strPath">full path to the file</param> /// <param name="bSmall">if true, the 16x16 icon is returned otherwise the 32x32</param> /// <returns></returns> public static Icon GetIcon(string strPath, bool bSmall) { SHFILEINFO info = new SHFILEINFO(true); int cbFileInfo = Marshal.SizeOf(info); SHGFI flags; if (bSmall) flags = SHGFI.Icon | SHGFI.SmallIcon | SHGFI.UseFileAttributes; else flags = SHGFI.Icon | SHGFI.LargeIcon | SHGFI.UseFileAttributes; SHGetFileInfo(strPath, 256, out info, (uint)cbFileInfo, flags); return Icon.FromHandle(info.hIcon); } } }
namespace QuickStart { partial class FrmMain { /// <summary> /// Required designer variable. /// </summary> private System.ComponentModel.IContainer components = null; /// <summary> /// Clean up any resources being used. /// </summary> /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param> protected override void Dispose(bool disposing) { if (disposing && (components != null)) { components.Dispose(); } base.Dispose(disposing); } #region Windows Form Designer generated code /// <summary> /// Required method for Designer support - do not modify /// the contents of this method with the code editor. /// </summary> private void InitializeComponent() { this.components = new System.ComponentModel.Container(); System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(FrmMain)); this.notifyIcon1 = new System.Windows.Forms.NotifyIcon(this.components); this.contextMenuStrip1 = new System.Windows.Forms.ContextMenuStrip(this.components); this.退出ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.RadLarge = new System.Windows.Forms.RadioButton(); this.RadSmall = new System.Windows.Forms.RadioButton(); this.RadDetails = new System.Windows.Forms.RadioButton(); this.RadList = new System.Windows.Forms.RadioButton(); this.BtnOpenDir = new System.Windows.Forms.Button(); this.splitContainerMaster = new System.Windows.Forms.SplitContainer(); this.splitContainerInnerLeft = new System.Windows.Forms.SplitContainer(); this.LvSoft = new QuickStart.ListViewExtend(); this.contextMenuStrip2 = new System.Windows.Forms.ContextMenuStrip(this.components); this.刷新ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem(); this.LvFolder = new QuickStart.ListViewExtend(); this.splitContainerInnerRight = new System.Windows.Forms.SplitContainer(); this.LvDocument = new QuickStart.ListViewExtend(); this.LvUrl = new QuickStart.ListViewExtend(); this.contextMenuStrip1.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainerMaster)).BeginInit(); this.splitContainerMaster.Panel1.SuspendLayout(); this.splitContainerMaster.Panel2.SuspendLayout(); this.splitContainerMaster.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainerInnerLeft)).BeginInit(); this.splitContainerInnerLeft.Panel1.SuspendLayout(); this.splitContainerInnerLeft.Panel2.SuspendLayout(); this.splitContainerInnerLeft.SuspendLayout(); this.contextMenuStrip2.SuspendLayout(); ((System.ComponentModel.ISupportInitialize)(this.splitContainerInnerRight)).BeginInit(); this.splitContainerInnerRight.Panel1.SuspendLayout(); this.splitContainerInnerRight.Panel2.SuspendLayout(); this.splitContainerInnerRight.SuspendLayout(); this.SuspendLayout(); // // notifyIcon1 // this.notifyIcon1.ContextMenuStrip = this.contextMenuStrip1; this.notifyIcon1.Icon = ((System.Drawing.Icon)(resources.GetObject("notifyIcon1.Icon"))); this.notifyIcon1.Text = "notifyIcon1"; this.notifyIcon1.Visible = true; this.notifyIcon1.DoubleClick += new System.EventHandler(this.notifyIcon1_DoubleClick); // // contextMenuStrip1 // this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.退出ToolStripMenuItem}); this.contextMenuStrip1.Name = "contextMenuStrip1"; this.contextMenuStrip1.Size = new System.Drawing.Size(99, 26); // // 退出ToolStripMenuItem // this.退出ToolStripMenuItem.Name = "退出ToolStripMenuItem"; this.退出ToolStripMenuItem.Size = new System.Drawing.Size(98, 22); this.退出ToolStripMenuItem.Text = "退出"; this.退出ToolStripMenuItem.Click += new System.EventHandler(this.退出ToolStripMenuItem_Click); // // RadLarge // this.RadLarge.AutoSize = true; this.RadLarge.Location = new System.Drawing.Point(13, 13); this.RadLarge.Name = "RadLarge"; this.RadLarge.Size = new System.Drawing.Size(59, 16); this.RadLarge.TabIndex = 1; this.RadLarge.TabStop = true; this.RadLarge.Text = "大图标"; this.RadLarge.UseVisualStyleBackColor = true; this.RadLarge.CheckedChanged += new System.EventHandler(this.RadLarge_CheckedChanged); // // RadSmall // this.RadSmall.AutoSize = true; this.RadSmall.Location = new System.Drawing.Point(100, 13); this.RadSmall.Name = "RadSmall"; this.RadSmall.Size = new System.Drawing.Size(59, 16); this.RadSmall.TabIndex = 2; this.RadSmall.TabStop = true; this.RadSmall.Text = "小图标"; this.RadSmall.UseVisualStyleBackColor = true; this.RadSmall.CheckedChanged += new System.EventHandler(this.RadSmall_CheckedChanged); // // RadDetails // this.RadDetails.AutoSize = true; this.RadDetails.Location = new System.Drawing.Point(187, 13); this.RadDetails.Name = "RadDetails"; this.RadDetails.Size = new System.Drawing.Size(71, 16); this.RadDetails.TabIndex = 3; this.RadDetails.TabStop = true; this.RadDetails.Text = "详细信息"; this.RadDetails.UseVisualStyleBackColor = true; this.RadDetails.CheckedChanged += new System.EventHandler(this.RadDetails_CheckedChanged); // // RadList // this.RadList.AutoSize = true; this.RadList.Location = new System.Drawing.Point(286, 13); this.RadList.Name = "RadList"; this.RadList.Size = new System.Drawing.Size(47, 16); this.RadList.TabIndex = 5; this.RadList.TabStop = true; this.RadList.Text = "列表"; this.RadList.UseVisualStyleBackColor = true; this.RadList.CheckedChanged += new System.EventHandler(this.RadList_CheckedChanged); // // BtnOpenDir // this.BtnOpenDir.Location = new System.Drawing.Point(552, 5); this.BtnOpenDir.Name = "BtnOpenDir"; this.BtnOpenDir.Size = new System.Drawing.Size(75, 23); this.BtnOpenDir.TabIndex = 6; this.BtnOpenDir.Text = "打开目录"; this.BtnOpenDir.UseVisualStyleBackColor = true; this.BtnOpenDir.Click += new System.EventHandler(this.BtnOpenDir_Click); // // splitContainerMaster // this.splitContainerMaster.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom) | System.Windows.Forms.AnchorStyles.Left) | System.Windows.Forms.AnchorStyles.Right))); this.splitContainerMaster.Location = new System.Drawing.Point(1, 35); this.splitContainerMaster.Name = "splitContainerMaster"; // // splitContainerMaster.Panel1 // this.splitContainerMaster.Panel1.Controls.Add(this.splitContainerInnerLeft); // // splitContainerMaster.Panel2 // this.splitContainerMaster.Panel2.Controls.Add(this.splitContainerInnerRight); this.splitContainerMaster.Size = new System.Drawing.Size(1151, 554); this.splitContainerMaster.SplitterDistance = 587; this.splitContainerMaster.SplitterWidth = 1; this.splitContainerMaster.TabIndex = 7; // // splitContainerInnerLeft // this.splitContainerInnerLeft.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainerInnerLeft.Location = new System.Drawing.Point(0, 0); this.splitContainerInnerLeft.Name = "splitContainerInnerLeft"; this.splitContainerInnerLeft.Orientation = System.Windows.Forms.Orientation.Horizontal; // // splitContainerInnerLeft.Panel1 // this.splitContainerInnerLeft.Panel1.Controls.Add(this.LvSoft); // // splitContainerInnerLeft.Panel2 // this.splitContainerInnerLeft.Panel2.Controls.Add(this.LvFolder); this.splitContainerInnerLeft.Size = new System.Drawing.Size(587, 554); this.splitContainerInnerLeft.SplitterDistance = 393; this.splitContainerInnerLeft.SplitterWidth = 1; this.splitContainerInnerLeft.TabIndex = 0; // // LvSoft // this.LvSoft.ContextMenuStrip = this.contextMenuStrip2; this.LvSoft.Dock = System.Windows.Forms.DockStyle.Fill; this.LvSoft.Location = new System.Drawing.Point(0, 0); this.LvSoft.Name = "LvSoft"; this.LvSoft.Size = new System.Drawing.Size(587, 393); this.LvSoft.TabIndex = 0; this.LvSoft.UseCompatibleStateImageBehavior = false; // // contextMenuStrip2 // this.contextMenuStrip2.Items.AddRange(new System.Windows.Forms.ToolStripItem[] { this.刷新ToolStripMenuItem}); this.contextMenuStrip2.Name = "contextMenuStrip2"; this.contextMenuStrip2.Size = new System.Drawing.Size(99, 26); // // 刷新ToolStripMenuItem // this.刷新ToolStripMenuItem.Name = "刷新ToolStripMenuItem"; this.刷新ToolStripMenuItem.Size = new System.Drawing.Size(98, 22); this.刷新ToolStripMenuItem.Text = "刷新"; this.刷新ToolStripMenuItem.Click += new System.EventHandler(this.刷新ToolStripMenuItem_Click); // // LvFolder // this.LvFolder.ContextMenuStrip = this.contextMenuStrip2; this.LvFolder.Dock = System.Windows.Forms.DockStyle.Fill; this.LvFolder.Location = new System.Drawing.Point(0, 0); this.LvFolder.Name = "LvFolder"; this.LvFolder.Size = new System.Drawing.Size(587, 160); this.LvFolder.TabIndex = 0; this.LvFolder.UseCompatibleStateImageBehavior = false; // // splitContainerInnerRight // this.splitContainerInnerRight.Dock = System.Windows.Forms.DockStyle.Fill; this.splitContainerInnerRight.Location = new System.Drawing.Point(0, 0); this.splitContainerInnerRight.Name = "splitContainerInnerRight"; this.splitContainerInnerRight.Orientation = System.Windows.Forms.Orientation.Horizontal; // // splitContainerInnerRight.Panel1 // this.splitContainerInnerRight.Panel1.Controls.Add(this.LvDocument); // // splitContainerInnerRight.Panel2 // this.splitContainerInnerRight.Panel2.Controls.Add(this.LvUrl); this.splitContainerInnerRight.Size = new System.Drawing.Size(563, 554); this.splitContainerInnerRight.SplitterDistance = 393; this.splitContainerInnerRight.SplitterWidth = 1; this.splitContainerInnerRight.TabIndex = 0; // // LvDocument // this.LvDocument.ContextMenuStrip = this.contextMenuStrip2; this.LvDocument.Dock = System.Windows.Forms.DockStyle.Fill; this.LvDocument.Location = new System.Drawing.Point(0, 0); this.LvDocument.Name = "LvDocument"; this.LvDocument.Size = new System.Drawing.Size(563, 393); this.LvDocument.TabIndex = 0; this.LvDocument.UseCompatibleStateImageBehavior = false; // // LvUrl // this.LvUrl.AllowDrop = true; this.LvUrl.ContextMenuStrip = this.contextMenuStrip2; this.LvUrl.Dock = System.Windows.Forms.DockStyle.Fill; this.LvUrl.Location = new System.Drawing.Point(0, 0); this.LvUrl.Name = "LvUrl"; this.LvUrl.Size = new System.Drawing.Size(563, 160); this.LvUrl.TabIndex = 0; this.LvUrl.UseCompatibleStateImageBehavior = false; // // FrmMain // this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F); this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; this.BackColor = System.Drawing.SystemColors.GradientActiveCaption; this.ClientSize = new System.Drawing.Size(1151, 590); this.Controls.Add(this.splitContainerMaster); this.Controls.Add(this.BtnOpenDir); this.Controls.Add(this.RadList); this.Controls.Add(this.RadDetails); this.Controls.Add(this.RadSmall); this.Controls.Add(this.RadLarge); this.Icon = ((System.Drawing.Icon)(resources.GetObject("$this.Icon"))); this.Name = "FrmMain"; this.Text = "快速启动"; this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.FrmMain_FormClosing); this.Load += new System.EventHandler(this.FrmMain_Load); this.contextMenuStrip1.ResumeLayout(false); this.splitContainerMaster.Panel1.ResumeLayout(false); this.splitContainerMaster.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainerMaster)).EndInit(); this.splitContainerMaster.ResumeLayout(false); this.splitContainerInnerLeft.Panel1.ResumeLayout(false); this.splitContainerInnerLeft.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainerInnerLeft)).EndInit(); this.splitContainerInnerLeft.ResumeLayout(false); this.contextMenuStrip2.ResumeLayout(false); this.splitContainerInnerRight.Panel1.ResumeLayout(false); this.splitContainerInnerRight.Panel2.ResumeLayout(false); ((System.ComponentModel.ISupportInitialize)(this.splitContainerInnerRight)).EndInit(); this.splitContainerInnerRight.ResumeLayout(false); this.ResumeLayout(false); this.PerformLayout(); } #endregion private System.Windows.Forms.NotifyIcon notifyIcon1; private ListViewExtend LvSoft; private ListViewExtend LvFolder; private ListViewExtend LvDocument; private ListViewExtend LvUrl; private System.Windows.Forms.RadioButton RadLarge; private System.Windows.Forms.RadioButton RadSmall; private System.Windows.Forms.RadioButton RadDetails; private System.Windows.Forms.RadioButton RadList; private System.Windows.Forms.Button BtnOpenDir; private System.Windows.Forms.SplitContainer splitContainerMaster; private System.Windows.Forms.SplitContainer splitContainerInnerLeft; private System.Windows.Forms.SplitContainer splitContainerInnerRight; private System.Windows.Forms.ContextMenuStrip contextMenuStrip1; private System.Windows.Forms.ToolStripMenuItem 退出ToolStripMenuItem; private System.Windows.Forms.ContextMenuStrip contextMenuStrip2; private System.Windows.Forms.ToolStripMenuItem 刷新ToolStripMenuItem; } }
using System; using System.Diagnostics; using System.Drawing; using System.Windows.Forms; namespace QuickStart { public partial class FrmMain : Form { private int _hotKeyId; private delegate void MyDelegate(); private string[] _files; private AutoHideForm _autoHideForm; public FrmMain() { InitializeComponent(); } private void FrmMain_Load(object sender, EventArgs e) { Width = Convert.ToInt32(Screen.PrimaryScreen.WorkingArea.Width * 0.9); Height = Convert.ToInt32(Screen.PrimaryScreen.WorkingArea.Height * 0.9); LvUrl.Init("Url"); LvDocument.Init("Document"); LvFolder.Init("Folder"); LvSoft.Init("Soft"); //this.WindowState = FormWindowState.Maximized; SetListViewStyles(); ShowInTaskbar = false; RadList.Checked = true; _hotKeyId = HotKeyManager.RegisterHotKey(Keys.C, KeyModifiers.Alt); HotKeyManager.HotKeyPressed += HotKeyManager_HotKeyPressed; //HideMe(); LvUrl.SelectedIndexChanged += ListView_SelectedIndexChanged; LvDocument.SelectedIndexChanged += ListView_SelectedIndexChanged; LvFolder.SelectedIndexChanged += ListView_SelectedIndexChanged; LvSoft.SelectedIndexChanged += ListView_SelectedIndexChanged; _autoHideForm = new AutoHideForm(this); } private void ListView_SelectedIndexChanged(object sender, EventArgs e) { //var listView = (ListView) sender; //foreach (ListViewItem item in listView.Items) //{ // item.BackColor = listView.BackColor; //} //if (listView.FocusedItem!=null) //{ // listView.FocusedItem.BackColor = Color.Coral; // //listView.FocusedItem.Selected = false; //} } private void HotKeyManager_HotKeyPressed(object sender, HotKeyEventArgs e) { SwitchMe(); } private void FrmMain_FormClosing(object sender, FormClosingEventArgs e) { if (e.CloseReason == CloseReason.UserClosing) { e.Cancel = true; HideMe(); } else { // 在关闭窗体之前注销id为0的热键。如果您计划注册多个热键,则可能需要使用不同的id值多次调用此函数 HotKeyManager.UnregisterHotKey(_hotKeyId); } } private void HideMe() { if (this.InvokeRequired) { MyDelegate md = HideMe; this.Invoke(md); } else { this.WindowState = FormWindowState.Minimized; this.notifyIcon1.Visible = true; this.Hide(); } } private void ShowMe() { if (this.InvokeRequired) { MyDelegate md = ShowMe; this.Invoke(md); } else { this.Visible = true; this.WindowState = FormWindowState.Normal; this.Activate(); _autoHideForm.ShowSeconds(); } } private void SwitchMe() { if (this.Visible) { HideMe(); } else { ShowMe(); } } private void notifyIcon1_DoubleClick(object sender, EventArgs e) { SwitchMe(); } private void RadLarge_CheckedChanged(object sender, EventArgs e) { if (RadLarge.Checked) { SetViews(View.LargeIcon); LoadIcons(); } } private void RadSmall_CheckedChanged(object sender, EventArgs e) { if (RadSmall.Checked) { SetViews(View.SmallIcon); LoadIcons(); } } private void RadDetails_CheckedChanged(object sender, EventArgs e) { if (RadDetails.Checked) { SetViews(View.Details); LoadIcons(); } } private void RadList_CheckedChanged(object sender, EventArgs e) { if (RadList.Checked) { SetViews(View.List); LoadIcons(); } } private void SetViews(View view) { LvUrl.View = view; LvFolder.View = view; LvSoft.View = view; LvDocument.View = view; } private void LoadIcons() { LvUrl.LoadIcon(); LvFolder.LoadIcon(); LvSoft.LoadIcon(); LvDocument.LoadIcon(); } private void SetListViewStyles() { SetListViewStyle(LvUrl); SetListViewStyle(LvDocument); SetListViewStyle(LvFolder); SetListViewStyle(LvSoft); } private void SetListViewStyle(ListView listView) { listView.Dock = DockStyle.Fill; listView.BackColor = Color.FromArgb(43, 87, 154);//Color.FromArgb(45, 125, 154); listView.ForeColor = Color.White; } private void BtnOpenDir_Click(object sender, EventArgs e) { Process.Start(ListViewExtend.GetShortcutBasePath()); } private void 退出ToolStripMenuItem_Click(object sender, EventArgs e) { this.notifyIcon1.Visible = false; this.Close(); this.Dispose(); System.Environment.Exit(System.Environment.ExitCode); } private void 刷新ToolStripMenuItem_Click(object sender, EventArgs e) { var listView = (ListViewExtend)((ContextMenuStrip)((ToolStripMenuItem)sender).Owner).SourceControl; listView.LoadIcon(); } } }
using System; using System.Runtime.InteropServices; using System.Threading; using System.Windows.Forms; namespace QuickStart { public static class HotKeyManager { public static event EventHandler<HotKeyEventArgs> HotKeyPressed; public static int RegisterHotKey(Keys key, KeyModifiers modifiers) { _windowReadyEvent.WaitOne(); int id = System.Threading.Interlocked.Increment(ref _id); _wnd.Invoke(new RegisterHotKeyDelegate(RegisterHotKeyInternal), _hwnd, id, (uint)modifiers, (uint)key); return id; } public static void UnregisterHotKey(int id) { _wnd.Invoke(new UnRegisterHotKeyDelegate(UnRegisterHotKeyInternal), _hwnd, id); } delegate void RegisterHotKeyDelegate(IntPtr hwnd, int id, uint modifiers, uint key); delegate void UnRegisterHotKeyDelegate(IntPtr hwnd, int id); private static void RegisterHotKeyInternal(IntPtr hwnd, int id, uint modifiers, uint key) { RegisterHotKey(hwnd, id, modifiers, key); } private static void UnRegisterHotKeyInternal(IntPtr hwnd, int id) { UnregisterHotKey(_hwnd, id); } private static void OnHotKeyPressed(HotKeyEventArgs e) { if (HotKeyManager.HotKeyPressed != null) { HotKeyManager.HotKeyPressed(null, e); } } private static volatile MessageWindow _wnd; private static volatile IntPtr _hwnd; private static ManualResetEvent _windowReadyEvent = new ManualResetEvent(false); static HotKeyManager() { Thread messageLoop = new Thread(delegate () { Application.Run(new MessageWindow()); }); messageLoop.Name = "MessageLoopThread"; messageLoop.IsBackground = true; messageLoop.Start(); } private class MessageWindow : Form { public MessageWindow() { _wnd = this; _hwnd = this.Handle; _windowReadyEvent.Set(); } protected override void WndProc(ref Message m) { if (m.Msg == WM_HOTKEY) { HotKeyEventArgs e = new HotKeyEventArgs(m.LParam); HotKeyManager.OnHotKeyPressed(e); } base.WndProc(ref m); } protected override void SetVisibleCore(bool value) { // Ensure the window never becomes visible base.SetVisibleCore(false); } private const int WM_HOTKEY = 0x312; } [DllImport("user32", SetLastError = true)] private static extern bool RegisterHotKey(IntPtr hWnd, int id, uint fsModifiers, uint vk); [DllImport("user32", SetLastError = true)] private static extern bool UnregisterHotKey(IntPtr hWnd, int id); private static int _id = 0; } public class HotKeyEventArgs : EventArgs { public readonly Keys Key; public readonly KeyModifiers Modifiers; public HotKeyEventArgs(Keys key, KeyModifiers modifiers) { this.Key = key; this.Modifiers = modifiers; } public HotKeyEventArgs(IntPtr hotKeyParam) { uint param = (uint)hotKeyParam.ToInt64(); Key = (Keys)((param & 0xffff0000) >> 16); Modifiers = (KeyModifiers)(param & 0x0000ffff); } } [Flags] public enum KeyModifiers { Alt = 1, Control = 2, Shift = 4, Windows = 8, NoRepeat = 0x4000 } }
using System; using System.Diagnostics; using System.Drawing; using System.IO; using System.Runtime.InteropServices; using System.Runtime.InteropServices.ComTypes; using System.Windows.Forms; namespace QuickStart { public class ListViewExtend : ListView { private string _fold; private string _fullPath; [DllImport("uxtheme", CharSet = CharSet.Unicode)] public static extern int SetWindowTheme(IntPtr hWnd, string pszSubAppName, string pszSubIdList); public void Init(string fold) { _fold = fold; _fullPath = GetPath(fold); AllowDrop = true; DragEnter += ListViewExtend_DragEnter; DragDrop += ListViewExtend_DragDrop; MouseDoubleClick += ListViewExtend_MouseDoubleClick; KeyUp += ListViewExtend_KeyUp; } private void ListViewExtend_KeyUp(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.Delete) { Delete(); } } private void ListViewExtend_MouseDoubleClick(object sender, MouseEventArgs e) { ListViewHitTestInfo info = HitTest(e.X, e.Y); if (info.Item != null) { if (!string.IsNullOrWhiteSpace(info.Item.SubItems[1].Text)) { try { Process.Start(info.Item.SubItems[1].Text); } catch (Exception) { // ignored } } } } private void ListViewExtend_DragDrop(object sender, DragEventArgs e) { var files = (Array)e.Data.GetData(DataFormats.FileDrop); foreach (var file in files) { var fileName = file.ToString(); try { var path = GetPath(_fold); var fullName = CreateLink(fileName, path); AddItem(fullName); } catch (Exception e1) { MessageBox.Show(e1.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error); } } } private void ListViewExtend_DragEnter(object sender, DragEventArgs e) { if (e.Data.GetDataPresent(DataFormats.FileDrop)) { e.Effect = DragDropEffects.Link; } else { e.Effect = DragDropEffects.None; } } public void LoadIcon() { Columns.Clear(); Items.Clear(); SmallImageList?.Dispose(); SmallImageList = new ImageList { //ImageSize = new Size(32, 32), ColorDepth = ColorDepth.Depth32Bit }; LargeImageList?.Dispose(); LargeImageList = new ImageList { //ImageSize = new Size(48, 48), ImageSize = new Size(32, 32), ColorDepth = ColorDepth.Depth32Bit }; if (View == View.Details) { Columns.Add("Name", 500); } SetWindowTheme(Handle, "Explorer", null); string[] files = Directory.GetFileSystemEntries(_fullPath); foreach (string file in files) { AddItem(file); } } private void AddItem(string file) { var fileShortName = Path.GetFileNameWithoutExtension(file); var smallIcon = ExtractIcon.GetIcon(file, true); var largeIcon = ExtractIcon.GetIcon(file, false); var count = SmallImageList.Images.Count; SmallImageList.Images.Add(smallIcon); LargeImageList.Images.Add(largeIcon); var item = Items.Add(fileShortName, count); item.Tag = file; item.Font= new Font(this.Font.FontFamily,12/*, FontStyle.Bold*/); item.SubItems.Add(file); } private string GetPath(string folder) { var fullPath = Path.Combine(GetShortcutBasePath(), folder); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } return fullPath; } public static string GetShortcutBasePath() { var fullPath = Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "FileLinks"); if (!Directory.Exists(fullPath)) { Directory.CreateDirectory(fullPath); } return fullPath; } private string CreateLink(string linkToFile, string savePath) { IShellLink link = (IShellLink)new ShellLink(); // 设置快捷方式信息 var shortName = Path.GetFileNameWithoutExtension(linkToFile) + ".lnk"; link.SetDescription(shortName); link.SetPath(linkToFile); // 保存快捷方式 IPersistFile file = (IPersistFile)link; var fullName = Path.Combine(savePath, shortName); file.Save(fullName, false); return fullName; } /// <summary> /// 删除快捷方式 /// </summary> private void Delete() { var result = MessageBox.Show("您确定要删除这些快捷方式吗?", "删除确认", MessageBoxButtons.YesNo); if (result == DialogResult.Yes) { for (int i = Items.Count - 1; i >= 0; i--) { if (Items[i].Selected) { File.Delete(Items[i].Tag.ToString()); Items[i].Remove(); } } } } } }
using System; using System.Runtime.InteropServices; using System.Text; namespace QuickStart { [ComImport] [Guid("00021401-0000-0000-C000-000000000046")] internal class ShellLink { } [ComImport] [InterfaceType(ComInterfaceType.InterfaceIsIUnknown)] [Guid("000214F9-0000-0000-C000-000000000046")] internal interface IShellLink { void GetPath([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszFile, int cchMaxPath, out IntPtr pfd, int fFlags); void GetIDList(out IntPtr ppidl); void SetIDList(IntPtr pidl); void GetDescription([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszName, int cchMaxName); void SetDescription([MarshalAs(UnmanagedType.LPWStr)] string pszName); void GetWorkingDirectory([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszDir, int cchMaxPath); void SetWorkingDirectory([MarshalAs(UnmanagedType.LPWStr)] string pszDir); void GetArguments([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszArgs, int cchMaxPath); void SetArguments([MarshalAs(UnmanagedType.LPWStr)] string pszArgs); void GetHotkey(out short pwHotkey); void SetHotkey(short wHotkey); void GetShowCmd(out int piShowCmd); void SetShowCmd(int iShowCmd); void GetIconLocation([Out, MarshalAs(UnmanagedType.LPWStr)] StringBuilder pszIconPath, int cchIconPath, out int piIcon); void SetIconLocation([MarshalAs(UnmanagedType.LPWStr)] string pszIconPath, int iIcon); void SetRelativePath([MarshalAs(UnmanagedType.LPWStr)] string pszPathRel, int dwReserved); void Resolve(IntPtr hwnd, int fFlags); void SetPath([MarshalAs(UnmanagedType.LPWStr)] string pszFile); } }
using System; using System.Windows.Forms; namespace QuickStart { static class Program { /// <summary> /// The main entry point for the application. /// </summary> [STAThread] static void Main() { Application.EnableVisualStyles(); Application.SetCompatibleTextRenderingDefault(false); //檢測系統是否有“XXXXX.vshost.exe”這一進程存在,如果已有,則不允許再打開。 if (System.Diagnostics.Process .GetProcessesByName(System.Diagnostics.Process.GetCurrentProcess().ProcessName).Length <= 1) { Application.Run(new FrmMain()); } } } }
using System; using System.Drawing; using System.Windows.Forms; using Timer = System.Windows.Forms.Timer; namespace QuickStart { /// <summary> /// 贴边自动隐藏窗体 /// </summary> public class AutoHideForm { private readonly Form _form; private Timer _checkDockTimer; private Timer _delayTimer; public AutoHideForm(Form form) { _form = form; Init(); _form.LocationChanged += Me_LocationChanged; SubControlMouseLeaveBind(_form); } private void SubControlMouseLeaveBind(Control control) { if (control.Controls.Count > 0) { foreach (Control subControl in control.Controls) { SubControlMouseLeaveBind(subControl); } } control.MouseLeave += _form_MouseLeave; } private void _form_MouseLeave(object sender, EventArgs e) { TimerExecute(); } /// <summary> /// 更改窗体的位置时,根据和各个窗体边缘的距离赋值停靠的位置 /// </summary> /// <param name="sender"></param> /// <param name="e"></param> private void Me_LocationChanged(object sender, EventArgs e) { if (_form.Top <= 0) { _form.Anchor = AnchorStyles.Top; } else if (_form.Bottom >= Screen.PrimaryScreen.Bounds.Height) { _form.Anchor = AnchorStyles.Bottom; } else if (_form.Left <= 0) { _form.Anchor = AnchorStyles.Left; } else if (_form.Left >= Screen.PrimaryScreen.Bounds.Width - _form.Width) { _form.Anchor = AnchorStyles.Right; } else { _form.Anchor = AnchorStyles.None; } } private void Init() { _form.TopMost = true; _checkDockTimer = new Timer(); _checkDockTimer.Tick += StopRectTimer_Tick; _checkDockTimer.Interval = 100; _checkDockTimer.Enabled = true; _form.Top = 0; _form.Anchor = AnchorStyles.Top; _form.WindowState = FormWindowState.Normal; TimerExecute(); } public void ShowSeconds(int seconds = 5) { _checkDockTimer.Stop(); Show(); _delayTimer = new Timer(); _delayTimer.Tick += _delayTimer_Tick; ; _delayTimer.Interval = seconds * 1000; _delayTimer.Enabled = true; } private void _delayTimer_Tick(object sender, EventArgs e) { _checkDockTimer.Enabled = true; _delayTimer.Stop(); _delayTimer.Dispose(); } public void Show() { switch (_form.Anchor) { case AnchorStyles.Top: _form.Location = new Point(_form.Location.X, 0); break; case AnchorStyles.Bottom: _form.Location = new Point(_form.Location.X, Screen.PrimaryScreen.Bounds.Height - _form.Height); break; case AnchorStyles.Left: _form.Location = new Point(0, _form.Location.Y); break; case AnchorStyles.Right: _form.Location = new Point(Screen.PrimaryScreen.Bounds.Width - _form.Width, _form.Location.Y); break; } } public void Hide() { switch (_form.Anchor) { case AnchorStyles.Top: _form.Location = new Point(_form.Location.X, (_form.Height - 3) * (-1)); break; case AnchorStyles.Bottom: _form.Location = new Point(_form.Location.X, Screen.PrimaryScreen.Bounds.Height - 5); break; case AnchorStyles.Left: _form.Location = new Point((-1) * (_form.Width - 3), _form.Location.Y); break; case AnchorStyles.Right: _form.Location = new Point(Screen.PrimaryScreen.Bounds.Width - 2, _form.Location.Y); break; } } private void TimerExecute() { //如果鼠标在窗体上,则根据停靠位置显示整个窗体 if (_form.Bounds.Contains(Cursor.Position)) { Show(); } else //如果鼠标离开窗体,则根据停靠位置隐藏窗体,但须留出部分窗体边缘以便鼠标选中窗体 { Hide(); } } private void StopRectTimer_Tick(object sender, EventArgs e) { TimerExecute(); ; } } }