VC编程-隐藏某程序托盘的图标
如何隐藏程序

---- 为了处理图标回调消息,如鼠标左键双击、鼠标右键单击消息,我们重载WindowProc()函数。此外,我们还希望在主框架窗口最小化时图标不在任务栏的空白区出现,在此函数中同时作相应处理。
LRESULT CMainFrame::WindowProc
NIF_ICON 设置成员hIcon有效
NIF_MESSAGE 设置成员uCallbackMessage有效
NIF_TIP 设置成员szTiOTIFYICONDATA结构的保护成员变量
m_tnid,并在其OnCreate函数中return
语句前加入生成托盘图标的代码:
m_tnid.cbSize=sizeof(NOTIFYICONDATA);
m_tnid.hWnd=this-> m_hWnd;
//如果是用户定义的消息
if(lParam==WM_LBUTTONDBLCLK)
{ //鼠标双击时主窗口出现
AfxGetApp()-> m_pMainWnd->
ShowWindow(SW_SHOW);
}
else if(lParam==WM_RBUTTONDOWN){
NIM_DELETE 删除图标
NIM_MODIFY 修改图标
---- 三、托盘图标程序设计示例
return 0;
}
break;
}
return CFrameWnd::WindowProc
(message, wParam, lParam);
}
---- 为使应用程序退出时图标消失,映射WM_DESTROY消息,在OnDestroy()函数中加入:
::Shell_NotifyIcon(NIM_DELETE,&m_tnid);
VC中系统托盘图标的实现

VC中系统托盘图标的实现本文以实例代码的形式讲述了在VC中系统托盘图标的实现。
技术实现:在VC中实现系统托盘图标主要用到一个Shell_NotifyIcon系统API。
在本文中我们以对话框程序为例子实现系统托盘图标,步骤如下:1.在StdAfx.h中定义消息ID,如:#define MYWM_NOTIFYICON WM_USER+12.定义一个全局NOTIFYICONDATA变量,如:NOTIFYICONDATA g_nd;3.实现添加系统托盘图标函数,如:void CZTXClientDlg::AddSystrayIcon(){// 将图标放入系统托盘g_nd.cbSize = sizeof (NOTIFYICONDATA);g_nd.hWnd = m_hWnd;g_nd.uID = IDR_MAINFRAME;g_nd.uFlags = NIF_ICON|NIF_MESSAGE|NIF_TIP;g_nd.uCallbackMessage= MYWM_NOTIFYICON;g_nd.hIcon = m_hIcon;strcpy(g_nd.szTip, "知天下娱乐中心[V1.1]");Shell_NotifyIcon(NIM_ADD, &g_nd);}4.实现删除系统托盘图标函数,如:void CZTXClientDlg::DelSystrayIcon(){Shell_NotifyIcon(NIM_DELETE, &g_nd);5.重载WindowProc函数,如:LRESULT CZTXClientDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam){// TODO: Add your specialized code here and/or call the base classswitch( message ){case MYWM_NOTIFYICON:if(lParam==WM_LBUTTONDBLCLK){AfxGetApp()->m_pMainWnd->ShowWindow(SW_SHOW);}else if(lParam==WM_RBUTTONDOWN){CMenu menu;//载入事先定义的选单menu.LoadMenu(IDR_TRADEMEMU);CMenu*pMenu=menu.GetSubMenu(0);CPoint pos;GetCursorPos(&pos);//加入SetForegroundWindow的目的为使用户点菜单之外时菜单可以消失::SetForegroundWindow(m_hWnd);pMenu->TrackPopupMenu(TPM_LEFTALIGN|TPM_RIGHTBU TTON,pos.x,pos.y,AfxGetMainWnd());}break;}return CDialog::WindowProc(message, wParam, lParam);6. OK,现在在我们的OnInitDialog函数中加入如下代码:AddSystrayIcon( );7.在窗口关闭函数中加入如下代码:DelSystrayIcon( );经测试可行!非常感谢!。
vc实现系统托盘图标

vc实现系统托盘图标Windows中,任务栏的右边(托盘)常驻有几个图标,如输入法切换图标、音量控制图标等系统图标,而一些在后台执行实时监控或其他任务的软件如金山词霸、瑞星杀毒软件等也只是在托盘上放一个小小的图标,几乎不占用本来就拥挤不堪的桌面,而且必要时我们还可以通过用鼠标点击图标对其进行菜单操作或激活其主窗口。
如果在我们编制的一些类似的在后台工作的实时监控软件里加入上述功能,无疑会使程序显得很有专业水准。
虽然有不少介绍系统托盘方面编程的文章,但大多是讲述如何在SDK下用Windows API编写的,这样做显然是非常烦琐的,本文以一个SDI(单文档界面)程序为例,讲述了在MFC框架程序中此类程序的实现过程。
[喝小酒的网摘]/a/1117.htm二、程序的设计思路在编制此类程序时,为了不干扰前台程序的运行界面和不显示不必要的窗口,应使其运行时的主窗口不可见。
同时,又要让用户知道该程序正在运行,并且能达到与用户进行交互的目的。
将一个图标显示在任务栏右端系统托盘区中并响应用户的鼠标动作是当前非常流行的方法。
要使程序的主窗口不可见,并且不在任务栏上出现任务按钮,需要分别设置主边框窗口的风格和扩展风格;另外,利用系统函数Shell_NotifyIcon可以将一个图标显示在任务栏的通告区中。
剩下的任务就是如何通过这个图标来响应用户的操作,可以象其他软件如WinAmp 一样通过弹出式菜单来完成同用户的交互。
三、程序的具体实现(一)主界面的隐藏前面已经提过,要求程序运行开始时主窗口不可见,同时也不出现在任务栏中,在SDK下是通过修改入口函数WinMain中的API函数 CreatWindow的参数来实现的,在MFC下已经对其进行了封装,使我们无法直接对WinMain函数内部进行访问与修改,不过可以通过MFC提供的另外一个接口--在主框架类的预创建窗口函数中设定主框架窗口的风格和扩展风格来实现之:BOOL CMainFrame::PreCreateWindow(CREATESTRUCT& cs) {if( !CFrameWnd::PreCreateWindow(cs) )return FALSE;cs.style=WS_POPUP;cs.dwExStyle |=WS_EX_TOOLWINDOW;return TRUE;}其中,CREATESTRUCT结构中的style成员变量设定了主框架窗口的风格,在此设定为WS_POPUP;而dwExStyle成员变量则对主框架窗口的扩展风格做了描述,只需在原有基础上为其再增添一种WS_EX_TOOLWINDOW扩展属性即可使其不会出现在任务条上。
c#win32Api接口隐藏第三方软件应用程序的右下角图标托盘图标

c#win32Api接⼝隐藏第三⽅软件应⽤程序的右下⾓图标托盘图标使⽤⽅法SetTrayIconVisible("qq", false);//获取托盘指针private static IntPtr TrayToolbarWindow32(){IntPtr h = IntPtr.Zero;IntPtr hTemp = IntPtr.Zero;h = Win32API.FindWindow("Shell_TrayWnd", null); //托盘容器h = Win32API.FindWindowEx(h, IntPtr.Zero, "TrayNotifyWnd", null);//找到托盘h = Win32API.FindWindowEx(h, IntPtr.Zero, "SysPager", null);hTemp = Win32API.FindWindowEx(h, IntPtr.Zero, "ToolbarWindow32", null);return hTemp;}//显⽰/隐藏单个系统托盘图标,由参数caption指定图标public static void SetTrayIconVisible(string caption, bool isShow){IntPtr vHandle = TrayToolbarWindow32();int vCount = Win32API.SendMessage(vHandle, Win32API.TB_BUTTONCOUNT, 0, 0);IntPtr vProcessId = IntPtr.Zero;Win32API.GetWindowThreadProcessId(vHandle, ref vProcessId);IntPtr vProcess = Win32API.OpenProcess(Win32API.PROCESS_VM_OPERATION | Win32API.PROCESS_VM_READ | Win32API.PROCESS_VM_WRITE, IntPtr.Zero, vProcessId);IntPtr vPointer = Win32API.VirtualAllocEx(vProcess, (int)IntPtr.Zero, 0x1000,Win32API.MEM_RESERVE | Win32API.MEM_COMMIT, Win32API.PAGE_READWRITE);char[] vBuffer = new char[256];IntPtr pp = Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0);uint vNumberOfBytesRead = 0;try{for (int i = 0; i < vCount; i++){Win32API.SendMessage(vHandle, Win32API.TB_GETBUTTONTEXT, i, vPointer.ToInt32());Win32API.ReadProcessMemoryEx(vProcess, vPointer,Marshal.UnsafeAddrOfPinnedArrayElement(vBuffer, 0),vBuffer.Length * sizeof(char), ref vNumberOfBytesRead);int l = 0;for (int j = 0; j < vBuffer.Length; j++){if (vBuffer[j] == (char)0){l = j;break;}}string s = new string(vBuffer, 0, l);if (s.IndexOf(caption) >= 0){if (isShow)Win32API.SendMessage(vHandle, Win32API.TB_HIDEBUTTON, i, 0);elseWin32API.SendMessage(vHandle, Win32API.TB_HIDEBUTTON, i, 1);}Console.WriteLine(s);}}finally{Win32API.VirtualFreeEx(vProcess, vPointer, 0, Win32API.MEM_RELEASE);Win32API.CloseHandle(vProcess);}}using System;using System.Collections.Generic;using System.Text;using System.Runtime.InteropServices;/********************************************************************************* 资料收集:Jonny Sun ,* ******************************************************************************/namespace HideIcon{/// <summary>/// 操作Windows窗体,系统托盘所⽤的API函数/// </summary>public class Win32API{public const int WM_USER = 0x400;public const int WM_CLOSE = 0x10;public const int WM_GETTEXT = 0x000D;public const int WM_SETTEXT = 0x000C;public const int STANDARD_RIGHTS_REQUIRED = 0xF0000;public const int SYNCHRONIZE = 0x100000;public const int PROCESS_ALL_ACCESS = STANDARD_RIGHTS_REQUIRED | SYNCHRONIZE | 0xFFF;public const int PROCESS_TERMINATE = 0x1;public const int PROCESS_VM_OPERATION = 0x8;public const int PROCESS_VM_READ = 0x10;public const int PROCESS_VM_WRITE = 0x20;public const int MEM_RESERVE = 0x2000;public const int MEM_COMMIT = 0x1000;public const int MEM_RELEASE = 0x8000;public const int PAGE_READWRITE = 0x4;public const int TB_BUTTONCOUNT = (WM_USER + 24);public const int TB_HIDEBUTTON = (WM_USER + 4);public const int TB_GETBUTTON = (WM_USER + 23);public const int TB_GETBUTTONTEXT = WM_USER + 75;public const int TB_GETBITMAP = (WM_USER + 44);public const int TB_DELETEBUTTON = (WM_USER + 22);public const int TB_ADDBUTTONS = (WM_USER + 20);public const int TB_INSERTBUTTON = (WM_USER + 21);public const int TB_ISBUTTONHIDDEN = (WM_USER + 12);public const int ILD_NORMAL = 0x0;public const int TPM_NONOTIFY = 0x80;public const int WS_VISIBLE = 268435456;//窗体可见public const int WS_MINIMIZEBOX = 131072;//有最⼩化按钮public const int WS_MAXIMIZEBOX = 65536;//有最⼤化按钮public const int WS_BORDER = 8388608;//窗体有边框public const int GWL_STYLE = (-16);//窗体样式public const int GW_HWNDFIRST = 0;public const int GW_HWNDNEXT = 2;public const int SW_HIDE = 0;public const int SW_SHOW = 5;[DllImport("User32.Dll")]public static extern void GetClassName(IntPtr hwnd, StringBuilder s, int nMaxCount);[DllImport("user32.dll", EntryPoint = "SetForegroundWindow", SetLastError = true)]public static extern void SetForegroundWindow(IntPtr hwnd);[DllImport("user32.dll", EntryPoint = "GetDlgItem", SetLastError = true)]public static extern IntPtr GetDlgItem(int nID, IntPtr phWnd);[DllImport("user32.dll", CharSet = CharSet.Auto)]public static extern int RegisterWindowMessage(string msg);[DllImport("kernel32", EntryPoint = "OpenProcess")]public static extern IntPtr OpenProcess(int dwDesiredAccess, IntPtr bInheritHandle, IntPtr dwProcessId);[DllImport("kernel32", EntryPoint = "CloseHandle")]public static extern int CloseHandle(IntPtr hObject);[DllImport("user32", EntryPoint = "GetWindowThreadProcessId")]public static extern IntPtr GetWindowThreadProcessId(IntPtr hwnd, ref IntPtr lpdwProcessId);[DllImport("user32.dll")]public static extern bool PostMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);[DllImport("user32", EntryPoint = "SendMessage")]public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);[DllImport("user32", EntryPoint = "SendMessage")]public static extern int SendMessage(IntPtr hwnd, int wMsg, int wParam, IntPtr lParam);[DllImport("kernel32", EntryPoint = "ReadProcessMemory")]public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, ref IntPtr lpBuffer, int nSize, int lpNumberOfBytesWritten);[DllImport("kernel32.dll", EntryPoint = "ReadProcessMemory")]public static extern bool ReadProcessMemoryEx(IntPtr hProcess, IntPtr lpBaseAddress, IntPtr lpBuffer, int nSize, ref uint vNumberOfBytesRead); [DllImport("kernel32", EntryPoint = "ReadProcessMemory")]public static extern int ReadProcessMemory(IntPtr hProcess, IntPtr lpBaseAddress, byte[] lpBuffer, int nSize, int lpNumberOfBytesWritten);[DllImport("kernel32", EntryPoint = "WriteProcessMemory")]public static extern int WriteProcessMemory(IntPtr hProcess, ref int lpBaseAddress, ref int lpBuffer, int nSize, ref int lpNumberOfBytesWritten);[DllImport("kernel32", EntryPoint = "VirtualAllocEx")]public static extern IntPtr VirtualAllocEx(IntPtr hProcess, int lpAddress, int dwSize, int flAllocationType, int flProtect);[DllImport("kernel32", EntryPoint = "VirtualFreeEx")]public static extern int VirtualFreeEx(IntPtr hProcess, IntPtr lpAddress, int dwSize, int dwFreeType);[DllImport("User32.dll")]public extern static int GetWindow(int hWnd, int wCmd);[DllImport("User32.dll")]public extern static int GetWindowLongA(int hWnd, int wIndx);[DllImport("user32.dll")]public static extern bool GetWindowText(int hWnd, StringBuilder title, int maxBufSize);[DllImport("user32.dll", CharSet = CharSet.Auto)]public extern static int GetWindowTextLength(IntPtr hWnd);[DllImport("User32.dll", EntryPoint = "FindWindow")]public static extern IntPtr FindWindow(string lpClassName, string lpWindowName);[DllImport("user32.dll", EntryPoint = "FindWindowEx", SetLastError = true)]public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow); [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);[DllImport("user32.dll", EntryPoint = "SendMessageA")]public static extern int SendMessage(IntPtr hwnd, int wMsg, IntPtr wParam, IntPtr lParam);}}。
隐藏或显示托盘图标(Hideorshowthetrayicon)

隐藏或显示托盘图标(Hide or show the tray icon)Hide or show the tray icon.Txt my life A as B, your life is B S. Failure is not terrible, the key is not the mother of success. The students now not too much quality! Come copy feather, actually by shear! Free to study Feng Shui, after the death of a tomb is made up of living can't afford to buy a house.'definition statementOption ExplicitPrivate Const WM_USER = &H400Private Const = TB_BUTTONCOUNT (WM_USER + 24)Private Const = TB_HIDEBUTTON (WM_USER + 4)Private Const = TB_GETBUTTONTEXTA (WM_USER + 45)Private Const = TB_AUTOSIZE (WM_USER + 33)Private Const MEM_COMMIT = &H1000Private Const MEM_RESERVE = &H2000Private Const MEM_RELEASE = &H8000Private Const PAGE_READWRITE = &H4Private Const = PROCESS_VM_OPERATION (&H8)Private Const = PROCESS_VM_READ (&H10)Private Const = PROCESS_VM_WRITE (&H20)Private Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String ByVal lpWindowName As String As Long)Private Declare Function FindWindowEx Lib "user32" Alias "FindWindowExA" (ByVal hWnd1 As Long ByVal hwnd2 As Long ByVal lpsz1, As String, ByVal lpsz2 As String As Long)Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long ByVal wMsg As Long ByVal wParam, As Long, lParam As Any As Long)Private Declare Function VirtualAllocEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long ByVal flAllocationType, As Long, ByVal flProtect As Long As Long)Private Declare Function VirtualFreeEx Lib "kernel32.dll" (ByVal hProcess As Long, lpAddress As Any, ByRef dwSize As Long ByVal dwFreeType As Long As Long)Private Declare Function OpenProcess Lib "Kernel32" (ByVal dwDesiredAccess As Long ByVal bInheritHandle, As Long, ByVal dwProcessId As Long As Long)Private Declare Function CloseHandle Lib "Kernel32" (ByVal hObject As Long As Long)Private Declare Function GetWindowThreadProcessId Lib "user32" (ByVal hWnd As Long lpdwProcessId As Long As Long)Private Declare Function ReadProcessMemory Lib "Kernel32" (ByVal hProcess As Long, lpBaseAddress As Any,lpBuffer任何,ByVal nSize为长,长,长lpnumberofbyteswritten)私人声明函数WriteProcessMemory lib“功能”(ByVal hprocess 一样长,lpbaseaddress任何lpBuffer任何,ByVal nSize,长,长时间lpnumberofbyteswritten)私人声明函数getcurrentprocessid lib“功能”()长“形式窗体“需要的控件:command1,Command2、Command3,列表框“隐藏指定图标私有子command1_click()昏暗的pidexplorer一样长,二的句柄一样长,hexplorer一样长,lpicontext长我作为整数暗淡昏暗的btncount为整数昏暗的iContext作为字符串二的句柄= FindWindow(“shell_traywnd”:)二的句柄= FindWindowEx(二的句柄,0,“traynotifywnd”:)二的句柄= FindWindowEx(二的句柄,0,“syspager”:)二的句柄= FindWindowEx(二的句柄,0,“toolbarwindow32”:)getwindowthreadprocessid二的句柄,pidexplorerhexplorer = OpenProcess(process_vm_operation或process_vm_read或process_vm_write,虚假,pidexplorer)lpicontext = virtualallocex(ByVal hexplorer,ByVal 0、Len (iContext),mem_commit或mem_reserve,page_readwrite)btncount = SendMessage(二的句柄,tb_buttoncount,0, 0)昏暗的花粉一样长,sbuff作为字符串我btncount = 0 - 1(256)美元的iContext =空间LLEN = SendMessage(二的句柄,tb_getbuttontexta、我、ByVal lpIconText)hexplorer ReadProcessMemory,ByVal lpIconText,ByVal IconText,Len(iContext),0如果同步<> 1然后iContext =美金(iContext,仪器(1美元的iContext,CHR(0))- 1)如果iContext = List1.列表(List1. ListIndex)然后替换为需要隐藏的图标名称:就是鼠标放在图标上时显示的文字SendMessage二的句柄,tb_hidebutton,我,ByVal TrueSendMessage二的句柄,tb_autosize,0, 0最后如果下一个virtualfreeex hexplorer,lpicontext、Len(iContext),mem_releasehexplorer CloseHandle端子“显示指定图标私有子command2_click()昏暗的pidexplorer长,hexplorer = OpenProcess(process_vm_operation或process_vm_read或process_vm_write,虚假,pidexplorer)lpicontext = virtualallocex(ByVal hexplorer,ByVal 0、Len (iContext),mem_commit或mem_reserve,page_readwrite)btncount = SendMessage(二的句柄,tb_buttoncount,0, 0)昏暗的花粉一样长,sbuff作为字符串我btncount = 0 - 1(256)美元的iContext =空间LLEN = SendMessage(二的句柄,tb_getbuttontexta、我、ByVal lpIconText)hexplorer ReadProcessMemory,ByVal lpIconText,ByVal IconText,Len(iContext),0如果同步<> 1然后iContext =美金(iContext,仪器(1美元的iContext,CHR(0))- 1)list1.additem iContext下一个virtualfreeex hexplorer,lpicontext、Len(iContext),mem_releasehexplorer CloseHandle端子私有子form_load()command3_clickcommand1_click端子。
VC实现显示、自动隐藏任务栏

VC 实现显示、自动隐藏任务栏(SHAppBarMessage)VC 实现显示、自动隐藏任务栏(SHAppBarMessage)一、显示和隐藏任务栏要想对任务栏进行操作,首先当然需要获得任务栏的句柄,所以首先利用FindWindow 获取句柄,再操作示例代码:1、隐藏任务栏HWND hWnd = FindWindow(_T("Shell_TrayWnd"),NULL); ShowWindow(hWnd,SW_HIDE);2、显示任务栏HWND hWnd = FindWindow(_T("Shell_TrayWnd"),NULL);ShowWindow(hWnd,SW_SHOW); 二、自动隐藏任务栏和取消自动隐藏任务栏(隐藏于自动隐藏不同)需要利用一API 函数:SHAppBarMessage UINT_PTR SHAppBarMessage(DWORD dwMessage,PAPPBARDATA pData);参数说明:dwMessage 可以为下列参数ABM_ACTIVATEappbarABM_GETAUTOHIDEBAR 的appbar ABM_GETSTATE激活一个检索屏幕边缘检索最顶层的Microsoft Windows 任务栏ABM_GETTASKBARPOS 检索任务栏ABM_NEW 注册一个新的appbar ,该系统并发送一消息给appbarABM_QUERYPOS appbar 大小和屏幕位置ABM_REMOVE 取消注册一个appbar ,并从系统内部列表移出ABM_SETAUTOHIDEBAR 在屏幕边缘注册或取消注册appbarABM_SETPOS 设置appbar 的大小和屏幕位置ABM_SETSTATE appbar 为最顶层设置ABM_WINDOWPOSCHANGED 当appbar 得状态发送改变时向系统发送消息pData一个APPBARDATA 结构体示例代码:void AutoHideTaskBar(BOOL bHide){//这三句视情况加于不加#ifndef ABM_SETSTATE#define ABM_SETSTATE0x0000000a#endifLPARAM lParam;if(bHide == TRUE)lParam = ABS_AUTOHIDE;// 自动隐藏}else{lParam = ABS_ALWAYSONTOP;// 取消自动隐藏}APPBARDATA apBar;memset(&apBar,0,sizeof(apBar));apBar.cbSize = sizeof(apBar); apBar.hWnd =FindWindow("Shell_TrayWnd",NULL);if(apBar.hWnd != NULL){apBar.lParam = lParam;SHAppBarMessage(ABM_SETSTATE,&apBar); // 置任务栏自动隐藏}}调用AutoHideTaskBar(TRUE);//orAutoHideTaskBar(FALSE);//。
C WinForm 最小化或关闭时隐藏到系统托盘

C#WinForm-最小化或关闭时隐藏到系统托盘STEP1、添加托盘图标控件NotifyIcon(直接从工具箱中拖动添加即可)STEP2、添加(重写)窗口尺寸变动函数Form1_Resizeprivate void Form1_Resize(object sender,EventArgs e){if(this.WindowState==FormWindowState.Minimized)//最小化到系统托盘{NotifyIcon1.Visible=true;//显示托盘图标this.Hide();//隐藏窗口}}STEP3、添加(重写)关闭窗口事件private void Form1_FormClosing(object sender,FormClosingEventArgs e){//注意判断关闭事件Reason来源于窗体按钮,否则用菜单退出时无法退出!if(e.CloseReason==erClosing){e.Cancel=true;//取消"关闭窗口"事件this.WindowState=FormWindowState.Minimized;//使关闭时窗口向右下角缩小的效果NotifyIcon1.Visible=true;this.Hide();return;}}STEP4、添加双击托盘图标事件(双击显示窗口)private void NotifyIcon1_MouseDoubleClick(object sender,MouseEventArgs e) {NotifyIcon1.Visible=false;this.Show();WindowState=FormWindowState.Normal;this.Focus();}STEP5、添加托盘图标的右键菜单(具体代码略)"退出"菜单:this.Close();"显示窗口"菜单:参考STEP4PS:解决了设置“关闭时隐藏到系统托盘”时,点击“退出”菜单也无法退出的问题。
C#托盘应用详解

C#托盘应用详解一.托盘程序的主要步骤及解决方法:为什么说用Visual C#可以十分方便的做一个托盘程序,主要的原因是在.Net框架的软件开发包( .Net FrameWork SDK )中的WinForm组件中定义了一个专门用来开发托盘程序的组件--NotifyIcon组件。
下面就来介绍一下这个组件的具体用法和程序设计中的主要的技巧。
(1).如何在程序运行后隐藏窗体:我们知道托盘程序运行后是无法看见主窗体的,他只会显示在工具栏上。
在用Visual C#设计此类程序的时候,可以用二种方法使得程序运行后不显示主窗体。
其中一种方法是重载主窗体中的OnActivated( )事件,OnActivated( )事件是在窗体激活的时候才触发的。
通过重载此事件可以达到隐藏主窗体的目的。
具体程序代码如下:protected override void OnActivated ( EventArgs e ){this.Hide ( ) ;}另外一种方法是在初始化主窗体的时候完成的,通过设定主窗体的属性来达到不显示的目的。
具体的程序代码如下:this.MaximizeBox = false ;this.MinimizeBox = false ;this.WindowState = System.Windows.Forms.FormWindowState.Minimized ;在本文介绍的程序中,使用了第二种方法。
(2).如何为托盘程序设定显示图标:在NotifyIcon组件中有一个属性icon就是来设定托盘图标的,由于Visual C#是一个完全的OOP (面向对象)语言,在Visual C#中任何东西都可以作为对象来处理。
当然对应一个icon来说,也可以用对象的方法来处理他。
我们通过下列语句来得到一个icon对象:private Icon mNetTrayIcon = new Icon ( "Tray.ico" ) ;请注意:在编译好的程序中,必须要在同一个目录中有一个Tray.ico图标文件,否则程序运行时候会出错的。
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
苦苦寻找的隐藏托盘图标的方法,今天终于搞定,献给大家
#include<atlbase.h>
#include<atlconv.h>
#include<CommCtrl.h>
void ShowTrayIcon(char szIcon[],BOOL show)
{
HWND hWnd,hWndPaper;
unsigned long lngPID;
long ret,lngButtons;
HANDLE hProcess;
LPVOID lngAddress;
long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;
char strBuff[1024]={0};
char*str=NULL;
char*pp=NULL;
hWnd=FindWindow("Shell_TrayWnd",NULL);
hWnd=FindWindowEx(hWnd,0,"TrayNotifyWnd",NULL);
hWndPaper=FindWindowEx(hWnd,0,"SysPager",NULL);
if(!hWndPaper)
hWnd=FindWindowEx(hWnd,0,"ToolbarWindow32",NULL);
else
hWnd=FindWindowEx(hWndPaper,0,"ToolbarWindow32",NULL);
ret=GetWindowThreadProcessId(hWnd,&lngPID);
hProcess=OpenProcess(PROCESS_ALL_ACCESS
|PROCESS_VM_OPERATION
|PROCESS_VM_READ
|PROCESS_VM_WRITE,
0,
lngPID);
lngAddress=VirtualAllocEx(hProcess,0,0x4096,MEM_COMMIT,PAGE_READWRI TE);
lngButtons=SendMessage(hWnd,TB_BUTTONCOUNT,0,0);
for(int i=0;i<lngButtons-1;i++)
{
ret=SendMessage(hWnd,TB_GETBUTTON,i,long(lngAddress));
ret=ReadProcessMemory(hProcess,LPVOID(long(lngAddress)+16),&lng TextAdr,4,0);
if(lngTextAdr!=-1)
{
ret=ReadProcessMemory(hProcess,LPVOID(lngTextAdr),strBuff,102 4,0);
ret=ReadProcessMemory(hProcess,LPVOID(long(lngAddress)+12), &lngHwndAdr,4,0);
ret=ReadProcessMemory(hProcess,LPVOID(lngHwndAdr),&lngHwnd,4, 0);
ret=ReadProcessMemory(hProcess,LPVOID(long(lngAddress)+4),& lngButtonID,4,0);
USES_CONVERSION;
str=OLE2T((LPOLESTR)(strBuff));
pp=strstr(str,szIcon);
if(pp!=NULL)
{
if(show)
{
SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,0);
}
else
{
SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,1);
}
}
}
}
VirtualFreeEx(hProcess,lngAddress,0X4096,MEM_RELEASE);
CloseHandle(hProcess);
}
调用方法:
char szIcon[]:要隐藏的托盘图标;BOOL show:false为隐藏图标,true为显示图标。
例如,隐藏金山词霸的托盘图标:
ShowTrayIcon("金山词霸",false);。