分享

完成布鲁斯.李的蛋疼C#托盘控制台

 _明心见性_ 2019-01-05
复制代码
  1  /*
  2      * 控制台禁用关闭按钮并最小化到系统托盘演示
  3      * 
  4      * 通过ConsoleWin32类来进行控制
  5      * 添加引用 System.Runtime.InteropServices; 和 System.Threading; 用于禁用关闭按钮
  6      * 添加引用 System.Drawing; 和 System.Windows.Forms; 用于系统托盘
  7      * 
  8      * Original By Bruceli
  9      * Complete By Ay
 10      */
 11 
 12     using System;
 13     using System.Collections.Generic;
 14     using System.Linq;
 15     using System.Text;
 16     using System.Runtime.InteropServices;
 17     using System.Threading;
 18     using System.Drawing;
 19     using System.Windows.Forms;
 20     using System.Diagnostics;
 21 
 22 
 23 
 24     class Program
 25     {
 26         public static bool Runing = true;
 27 
 28         [STAThread]
 29         static void Main(string[] args)
 30         {
 31             Console.Title = "ConsoleEx";
 32 
 33             ConsoleWin32Helper.Initialization();
 34             ConsoleWin32Helper.ShowNotifyIcon();
 35             ConsoleWin32Helper.DisableCloseButton();
 36 
 37             Thread threadWork = new Thread(new ThreadStart(MyWork.EntryPoint));
 38             threadWork.Start();
 39 
 40 
 41             ConsoleWin32Helper.Visable = false;
 42 
 43             while (Runing)
 44             {
 45                 Application.DoEvents();
 46             }
 47 
 48             ConsoleWin32Helper.HideNotifyIcon();
 49             threadWork.Abort();
 50         }
 51     }
 52 
 53     class ConsoleWin32Helper
 54     {
 55         public static void Initialization()
 56         {
 57             NotifyIconInitialization();
 58         }
 59 
 60         #region 句柄实用工具
 61 
 62         [DllImport("User32.dll", EntryPoint = "FindWindow")]
 63         static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
 64 
 65         [DllImport("user32.dll", EntryPoint = "GetSystemMenu")]
 66         static extern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert);
 67 
 68         [DllImport("user32.dll", EntryPoint = "RemoveMenu")]
 69         static extern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);
 70 
 71         [DllImport("user32.dll")]
 72         static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
 73 
 74         private class __mc_IWin32Window : IWin32Window
 75         {
 76             public IntPtr Handle { get; private set; }
 77             public __mc_IWin32Window(IntPtr handle)
 78             {
 79                 Handle = handle;
 80             }
 81         }
 82 
 83         /// <summary>自动寻找当前控制台句柄</summary>        
 84         public static IntPtr GetConsoleWindowHandle()
 85         {
 86             //线程睡眠,确保closebtn中能够正常FindWindow,否则有时会Find失败。。
 87             Thread.Sleep(100);
 88 
 89             lock (Console.Title)
 90             {
 91                 var newtitle = Guid.NewGuid().ToString();
 92                 var orgtitle = Console.Title;
 93                 Console.Title = newtitle;
 94 
 95                 IntPtr handle = FindWindow(null, Console.Title);
 96 
 97                 Console.Title = orgtitle;
 98 
 99                 return handle;
100             }
101         }
102 
103         /// <summary>禁用关闭按钮</summary>        
104         public static void DisableCloseButton()
105         {
106             IntPtr closeMenu = GetSystemMenu(GetConsoleWindowHandle(), IntPtr.Zero);
107             uint SC_CLOSE = 0xF060;
108             RemoveMenu(closeMenu, SC_CLOSE, 0x0);
109         }
110 
111         /// <summary>自动寻找当前控制台句柄,并封装成Winform对象</summary>        
112         public static IWin32Window GetConsoleWindowHandleObj()
113         {
114             return new __mc_IWin32Window(GetConsoleWindowHandle());
115         }
116 
117         static void HiddenConsoleWindow()
118         {
119             uint SW_HIDE = 0;
120             ShowWindow(GetConsoleWindowHandle(), SW_HIDE);
121         }
122 
123         static void ShowConsoleWindow()
124         {
125             uint SW_SHOW = 5;
126             ShowWindow(GetConsoleWindowHandle(), SW_SHOW);
127         }
128 
129         static bool _Visable = true;
130         public static bool Visable
131         {
132             get { return _Visable; }
133             set
134             {
135                 _Visable = value;
136                 if (_Visable)
137                     ShowConsoleWindow();
138                 else
139                     HiddenConsoleWindow();
140             }
141         }
142 
143         #endregion
144 
145         #region 托盘图标
146 
147         static NotifyIcon _NotifyIcon = new NotifyIcon();
148 
149         private static void NotifyIconInitialization()
150         {
151             _NotifyIcon.Icon = Properties.Resources.MainIcon;
152             _NotifyIcon.Visible = false;
153             _NotifyIcon.Text = Console.Title;
154 
155             ContextMenuStrip menu = new ContextMenuStrip();
156             menu.RenderMode = ToolStripRenderMode.System;
157 
158             ToolStripMenuItem item = new ToolStripMenuItem();
159             item.Name = "exit";
160             item.Text = "退出";
161             menu.Items.Add(item);
162 
163             item = new ToolStripMenuItem();
164             item.Name = "open";
165             item.Text = "打开浏览器";
166             menu.Items.Add(item);
167 
168 
169             _NotifyIcon.ContextMenuStrip = menu;
170 
171             menu.ItemClicked += new ToolStripItemClickedEventHandler(menu_ItemClicked);
172 
173             _NotifyIcon.MouseDoubleClick += new MouseEventHandler(_NotifyIcon_MouseDoubleClick);
174         }
175 
176         public static void ShowNotifyIcon()
177         {
178             _NotifyIcon.Visible = true;
179             _NotifyIcon.ShowBalloonTip(3000, "", "我是托盘图标,用右键点击我试试,还可以双击看看。", ToolTipIcon.None);
180         }
181 
182         public static void HideNotifyIcon()
183         {
184             _NotifyIcon.Visible = false;
185         }
186 
187         static void _NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e)
188         {
189             Console.WriteLine("托盘被双击.");
190             Visable = !Visable;
191         }
192 
193         static void menu_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
194         {
195             Console.WriteLine("托盘菜单被点击");
196             Console.WriteLine("被点击的菜单是:{0}", e.ClickedItem.Text);
197             switch (e.ClickedItem.Name)
198             {
199                 case "exit":
200                     Program.Runing = false;
201                     break;
202                 case "open":
203                     Process.Start(MyWork.SVR_URL);
204                     break;
205             }
206         }
207 
208         #endregion
209     }
复制代码

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约