比如强杀了进程后销毁相应的托盘图标。
方式一:
/****************************************************************/
/* RefurbishTray函数主要是实现对系统托盘的图标刷新的问题 */
/* 由于托盘程序如果被强行杀掉进程等操作后,在托盘中的图标不能自动删除 */
/* 运用以下的方法,实现模拟鼠标点击托盘图标,使得图标刷新 */
/*****************************************************************/
void __fastcall RemoveDeadIcons()
{
RECT rctTrayIcon;
// Get tray window handle and bounding rectangle
HWND hTrayWindow = FindWindowEx(
FindWindow("Shell_TrayWnd", NULL), 0, "TrayNotifyWnd", NULL);
if(!GetWindowRect(hTrayWindow, &rctTrayIcon))
return;
// Get small icon metrics
int nIconWidth = GetSystemMetrics(SM_CXSMICON);
int nIconHeight = GetSystemMetrics(SM_CYSMICON);
// Save current mouse position }
CPoint CursorPos;
GetCursorPos(&CursorPos);
// Sweep the mouse cursor over each icon in the tray in both dimensions
for(int nRow=0; nRow<(rctTrayIcon.bottom-rctTrayIcon.top)/nIconHeight; nRow++)
{
for(int nCol=0; nCol<(rctTrayIcon.right-rctTrayIcon.left)/nIconWidth; nCol++)
{
SetCursorPos(rctTrayIcon.left + nCol * nIconWidth + 5,
rctTrayIcon.top + nRow * nIconHeight + 5);
Sleep(0);
}
}
// Restore mouse position
SetCursorPos(CursorPos.x, CursorPos.x);
// Redraw tray window (to fix bug in multi-line tray area)
RedrawWindow(hTrayWindow, NULL, 0, RDW_INVALIDATE | RDW_ERASE | RDW_UPDATENOW);
}
方式二:
#define _WIN32_IE 0x0500
#include <windows.h>
#include <CommCtrl.h>
struct TRAYDATA
{
HWND hwnd;
UINT uID;
UINT uCallbackMessage;
DWORD Reserved[2];
HICON hIcon;
};
void ShowTrayIcon(LPCWSTR lpwszIcon, BOOL bShow)
{
HWND hWnd,hWndPaper;
DWORD dwProcessId;
DWORD dwButtonCount;
HANDLE hProcess;
LPVOID lpAddress;
TBBUTTON tb;
hWnd = FindWindow(TEXT("Shell_TrayWnd"), NULL);
hWnd = FindWindowEx(hWnd, 0, TEXT("TrayNotifyWnd"), NULL);
hWndPaper = FindWindowEx(hWnd, 0, TEXT("SysPager"), NULL);
if(!hWndPaper)
hWnd = FindWindowEx(hWnd, 0, TEXT("ToolbarWindow32"), NULL);
else
hWnd = FindWindowEx(hWndPaper, 0, TEXT("ToolbarWindow32"), NULL);
GetWindowThreadProcessId(hWnd, &dwProcessId);
hProcess = OpenProcess(PROCESS_ALL_ACCESS
|PROCESS_VM_OPERATION
|PROCESS_VM_READ
|PROCESS_VM_WRITE,
0,
dwProcessId);
lpAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
dwButtonCount = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
for(UINT i = 0 ; i < dwButtonCount - 1; i++)
{
SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)lpAddress);
ReadProcessMemory(hProcess, lpAddress, &tb, sizeof(TBBUTTON), 0);
if(tb.iString != -1)
{
TRAYDATA trayData;
WCHAR wszBuff[MAX_PATH] = {0};
WCHAR *pwsz = NULL;
ReadProcessMemory(hProcess, (LPVOID)tb.iString, wszBuff, MAX_PATH, 0);
ReadProcessMemory(hProcess, (LPVOID)tb.dwData, &trayData, sizeof(TRAYDATA), 0);
pwsz = wcsstr(wszBuff, lpwszIcon);
if (pwsz)
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = trayData.hwnd;
nid.uID = trayData.uID;
nid.uFlags = NIF_STATE;
nid.dwState = bShow?NIS_SHAREDICON:NIS_HIDDEN;
nid.dwStateMask = NIS_HIDDEN;
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
}
}
VirtualFreeEx( hProcess, lpAddress, 0x4096, MEM_RELEASE);
CloseHandle(hProcess);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
ShowTrayIcon(L"音量", FALSE); //隐藏音量图标
//ShowTrayIcon(L"音量", TRUE); //显示音量图标
return 0;
}
转自:http://bbs.youkuaiyun.com/topics/390224391
其他:
XP/WIN7系统中删除已结束进程托盘图标的方法
http://blog.youkuaiyun.com/bzhxuexi/article/details/25238523