Some applications can be used to remind users and user-friendly operation, will be in the system tray to create their own tray icon, so that the user will be able to pop up the bubble hint to understand the operation of the corresponding software, or through the associated menu convenient operation of the software. In some cases, however, the application may experience an abnormal shutdown and leave it behind in the system tray when the corresponding system tray icon is removed, and when the software is run again, a plurality of the same icons appear in the pallet area. So how do I remove the invalid/specified system tray icon?
First, we need to find the system tray bar in the window, because we do not know whether the need to delete the icon is in the normal system tray area or overflow system tray area, so need to find out these two windows:
1. Find the normal system tray Area window
hwnd Findtraywindow () {HWND hwnd=NULL; HWND Hwndpage=NULL;
Find Shell_traywnd window hWnd=:: FindWindow (_t ("Shell_traywnd"), NULL); if(HWnd! =NULL) {
// find Traynotifywnd window hWnd=:: FindWindowEx (HWnd, NULL, _t ("Traynotifywnd"), NULL); if(HWnd! =NULL) {
// find Syspager window. Hwndpage=:: FindWindowEx (HWnd, NULL, _t ("Syspager"), NULL); if(Hwndpage! =NULL)
// find ToobarWindow32 window. HWnd=:: FindWindowEx (Hwndpage, NULL, _t ("ToolbarWindow32"), NULL); Else //Win2000 No Syspager window, you can find the ToobarWindow32 window directly.hwnd =:: FindWindowEx (hwnd, NULL, _t ("ToolbarWindow32"), NULL); } } returnhWnd;}
2. Find the overflow system Tray Area window
hwnd Findoverflowtraywindow () {HWND hwnd = // find Notifyiconoverflowwindow window HWnd =:: FindWindow (_t ( Notifyiconoverflowwindow if (hWnd!= // find ToobarWindow32 window hwnd = FindWindowEx (hwnd, NULL, _t ( " toolbarwindow32 " ), NULL); return hWnd;}
Second, query the number of icons contained in the window and traverse all the icons to locate the target icon and delete the target icon by passing in the Nim_delete parameter to the Shell_NotifyIcon function
VOID Deleteinvalidtrayicon (hwnd hwnd) {if(HWnd = =NULL)return; structTraydata {HWND hwnd; UINT UID; UINT Ucallbackmessage; DWORD reserved1[2]; Hicon Hicon; DWORD reserved2[3]; TCHAR Szexepath[max_path]; TCHAR sztip[ -]; }; DWORD Dwprocessid=0; DWORD Dwbuttoncount=0; HANDLE hprocess=Invalid_handle_value; Tbbutton Tbbutton; LPVOID PTB; Traydata TD; Notifyicondata nid; TCHAR Szsyntpenhpath[max_path]= {0 }; TCHAR*Pszapplicationname; //Query the number of icons contained in the specified window, one button for each iconDwbuttoncount = (DWORD):: SendMessage (HWnd, Tb_buttoncount,0,0); if(Dwbuttoncount = =0) return; //gets the thread on which the window resides if((:: GetWindowThreadProcessId (HWnd, &dwprocessid)! =0) && (Dwprocessid! =0) ) {hprocess=:: OpenProcess (Process_all_access | process_vm_operation | Process_vm_read |Process_vm_write, FALSE, Dwprocessid); if(Hprocess! =NULL) {PTB=:: VirtualAllocEx (hprocess, NULL,sizeof(Tbbutton), Mem_commit |Mem_reserve, Page_readwrite); if(PTB! =NULL) { //Traverse all the icons and match the target information to find the target icon and delete it for(DWORD i =0; i < Dwbuttoncount; i++) { if((SendMessage (hWnd, Tb_getbutton, I, (LPARAM) pTB) = = TRUE) &&(:: ReadProcessMemory (hprocess, PTB,&tbbutton,sizeof(Tbbutton), NULL)! =0) &&(:: ReadProcessMemory (hprocess, (LPVOID) Tbbutton.dwdata,&TD,sizeof(Traydata), NULL)! =0)) {//if the tray icon Szexepath or sztip contains specific information, the icon is the icon we are ready to clear, locate and delete it if(_tcsstr (Td.szexepath +2, Szspecifiedapplictionname) &&_tcsstr (Td.sztip+2, Szspecifiedtip)) {Nid.cbsize =notifyicondata_v2_size; Nid.uid=Td.uid; Nid.hwnd=Td.hwnd; Nid.hicon=Td.hicon; Nid.ucallbackmessage=Td.ucallbackmessage; Nid.uflags= Nif_icon | Nif_message |Nif_tip; :: Shell_NotifyIcon (Nim_delete,&nid); }}}:: VirtualFreeEx (hprocess, PTB,sizeof(Tbbutton), mem_free); }:: CloseHandle (hprocess); } }}
The above method can be used to find the tray icon that was not deleted at the time of abnormal shutdown, and clean it up, so that the system tray only retains the only valid icon associated with the software.
Delete the specified icon in the system tray area