控制台怎么写托盘程序( 三 )


蛋疼的可以,搞个这还这么麻烦!别慌,还需要你解决的问题:先了解我给出的代码实现了什么 。实现了禁用关闭按钮,托盘图标的添加和事件的处理 。
你要做的是什么,当然你可以不做,如果你也想蛋疼一下,就来解决下这个问题吧 。退出控制台时,托盘图标没有消失,唉,这是bug,怎么解决?捕获关闭事件,在要关闭时清除托盘图标 。
先告诉你可以实现,我以实验成功,这里没有放出来是让你也蛋疼下 。好了,代码如下:/* * 控制台禁用关闭按钮并最小化到系统托盘演示 * * 通过ConsoleWin32类来进行控制 * 添加引用 System.Runtime.InteropServices; 和 System.Threading; 用于禁用关闭按钮 * 添加引用 System.Drawing; 和 System.Windows.Forms; 用于系统托盘 * */ using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Runtime.InteropServices; using System.Threading; using System.Drawing; using System.Windows.Forms; namespace Tray_beta_1 { class Program { staticbool _IsExit =false; staticvoid Main(string[] args) { Console.Title ="TestConsoleLikeWin32"; ConsoleWin32Helper.ShowNotifyIcon(); ConsoleWin32Helper.DisableCloseButton(Console.Title); Thread threadMonitorInput =new Thread(new ThreadStart(MonitorInput)); threadMonitorInput.Start(); while (true) { Application.DoEvents(); if (_IsExit) { break; } } } staticvoid MonitorInput() { while (true) { string input = Console.ReadLine(); if (input =="exit") { _IsExit =true; Thread.CurrentThread.Abort(); } } } } class ConsoleWin32Helper { static ConsoleWin32Helper() { _NotifyIcon.Icon =new Icon(@"G:\BruceLi Test\ConsoleAppTest\ConsoleApps\Tray\small.ico"); _NotifyIcon.Visible =false; _NotifyIcon.Text ="tray"; ContextMenu menu =new ContextMenu(); MenuItem item =new MenuItem(); item.Text ="右键菜单,还没有添加事件"; item.Index =0; menu.MenuItems.Add(item); _NotifyIcon.ContextMenu = menu; _NotifyIcon.MouseDoubleClick +=new MouseEventHandler(_NotifyIcon_MouseDoubleClick); } staticvoid _NotifyIcon_MouseDoubleClick(object sender, MouseEventArgs e) { Console.WriteLine("托盘被双击."); }#region 禁用关闭按钮 [DllImport("User32.dll", EntryPoint ="FindWindow")] staticextern IntPtr FindWindow(string lpClassName, string lpWindowName); [DllImport("user32.dll", EntryPoint ="GetSystemMenu")] staticextern IntPtr GetSystemMenu(IntPtr hWnd, IntPtr bRevert); [DllImport("user32.dll", EntryPoint ="RemoveMenu")] staticextern IntPtr RemoveMenu(IntPtr hMenu, uint uPosition, uint uFlags);////// 禁用关闭按钮//////控制台名字 publicstaticvoid DisableCloseButton(string title) {//线程睡眠,确保closebtn中能够正常FindWindow,否则有时会Find失败 。
Thread.Sleep(100); IntPtr windowHandle = FindWindow(null, title); IntPtr closeMenu = GetSystemMenu(windowHandle, IntPtr.Zero); uint SC_CLOSE =0xF060; RemoveMenu(closeMenu, SC_CLOSE, 0x0); } publicstaticbool IsExistsConsole(string title) { IntPtr windowHandle = FindWindow(null, title); if (windowHandle.Equals(IntPtr.Zero)) returnfalse; returntrue; }#endregion#region 托盘图标 static NotifyIcon _NotifyIcon =new NotifyIcon(); publicstaticvoid ShowNotifyIcon() { _NotifyIcon.Visible =true; _NotifyIcon.ShowBalloonTip(3000, "", "我是托盘图标,用右键点击我试试,还可以双击看看 。
", ToolTipIcon.None); } publicstaticvoid HideNotifyIcon() { _NotifyIcon.Visible =false; }#endregion } } 。
4.VC中怎样建立一个只有系统托盘的程序其实这个建立的过程很简单,与系统托盘通信的函数只有一个:Shell_NotifyIcon(DWORD dwMessage ,PNOTIFYICONDATA pnid) 。
第一个参数dwMessage 的取值有NIM_ADD 、NIM_MODIFY及NIM_DELETE 。第二个参数pnid 是NOTIFYICONDATA 结构体一个指针,结构体内容用来配合第一个参数dwMessage进行图标操作 。
下面的程序,将AddSystemTrayIcon( )放到OnPaint( )下增加一个系统托盘图标,在OnDestroy或OnClose中调用DeleteSystemTrayIcon( )来删除这个系统托盘图标 。BOOL AddSystemTrayIcon(){HICON hIcon = AfxGetApp()->LoadIcon(IDI_ICONTRAY);char lpszTip[]="Text Spy"; //提示信息//给NOTIFYICONDATA 结构赋值NOTIFYICONDATA tnid;tnid.cbSize = sizeof(NOTIFYICONDATA);tnid.hIcon = hIcon;tnid.uID = IDI_ICONTRAY;tnid.hWnd = m_hWnd;tnid.uFlags = NIF_MESSAGE|NIF_ICON|NIF_TIP;tnid.uCallbackMessage = WM_TASKBAR; //可以写OnTaskBar(WPARAM wParam,LPARAM lParam)函数来处理收到的消息lstrcpyn(tnid.szTip,lpszTip,sizeof(tnid.szTip));// lstrcpy(tnid.szTip,"Text Spy");// ShowWindow(SW_HIDE);return Shell_NotifyIcon(NIM_ADD,&tnid);}BOOL DeleteSystemTrayIcon(){NOTIFYICONDATA tnid;tnid.cbSize = sizeof(NOTIFYICONDATA);tnid.uID = IDI_ICONTRAY;tnid.hWnd = m_hWnd;return Shell_NotifyIcon(NIM_DELETE,&tnid);}LRESULT OnTaskBar(WPARRM wParam,LPARAM lParam)//WM_TASKBAR消息的响应函数{UINT uMouseMsg = (UINT)lParam;switch(uMouseMsg){ case WM_LBUTTONDOWN: ShowWindow(SW_SHOWNORMAL); break; case WM_RBUTTONUP; MessageBox("The mouse rightkey !"); break} return 0;} 。