刷新托盘图标

struct AppData
{
HWND hwnd;
UINT uID;
};

void Refurbish(HWND hToolbarWindow32)

{
if (hToolbarWindow32)
{
DWORD pid = 0;
LPVOID lngAdress = NULL;
long lngButtonID = 0;
LPVOID lngText = NULL;
LPVOID lngRect = NULL;
RECT rc = { 0 };
char strBuff[1024] = { 0 };
GetWindowThreadProcessId(hToolbarWindow32, &pid);
if (NULL == pid)
{
return;
}
HANDLE hProcess = OpenProcess(PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, true, pid);
if (NULL == hProcess)
{
return;
}
int iNum = ::SendMessage(hToolbarWindow32, TB_BUTTONCOUNT, NULL, NULL);
lngAdress = VirtualAllocEx(hProcess, 0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
lngRect = VirtualAllocEx(hProcess, 0, sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
lngText = VirtualAllocEx(hProcess, 0, 1024, MEM_COMMIT, PAGE_READWRITE);
for (int i = 0; i <= iNum; i++)
{

long lngTempID = lngButtonID;

SIZE_T numBytes = 0;
AppData appData = { 0, 0 };

int ret = ::SendMessage(hToolbarWindow32, TB_GETBUTTON, i, (LPARAM)(lngAdress));
if (ret != 0)
{
ret = ReadProcessMemory(hProcess, LPVOID((long)lngAdress + 4), &lngButtonID, 4, 0);
ret = ::SendMessage(hToolbarWindow32, TB_GETBUTTONTEXT, (WPARAM)lngButtonID, (LPARAM)(lngText));//一定是buttonID,网上都是i,这样过去到的text不是对应托盘的text.
ret = ReadProcessMemory(hProcess, lngText, strBuff, 1024, 0);
ret = ::SendMessage(hToolbarWindow32, TB_GETITEMRECT, (WPARAM)i, (LPARAM)(lngRect));

ret = ReadProcessMemory(hProcess, lngRect, &rc, sizeof(rc), 0);

if (!ReadProcessMemory(trayProcess, lngAdress, &buttonData, sizeof(TBBUTTON), &numBytes))
    continue;
if (!ReadProcessMemory(trayProcess, (LPVOID)buttonData.dwData, &appData, sizeof(AppData), &numBytes))
continue;//获取托盘图标窗口句柄和ID


if (strcmp(strBuff,"微信")==0)//通过托盘tooltip信息查找自己的程序
{
int iGap = rc.right - rc.left;
::SendMessage(hToolbarWindow32, WM_MOUSEMOVE, 0, MAKELPARAM(rc.right - (iGap / 2), rc.bottom -(iGap / 2)));
i--;
//::SendMessage(hToolbarWindow32, TB_DELETEBUTTON, i, 0);
}
}
}
VirtualFreeEx(hProcess, lngAdress, 0x4096, MEM_RELEASE);
VirtualFreeEx(hProcess, lngText, 1024, MEM_RELEASE);
VirtualFreeEx(hProcess, lngRect, sizeof(RECT), MEM_RELEASE);
CloseHandle(hProcess);
}
}


//获取普通托盘区窗口句柄  
HWND FindTrayWnd()
{
HWND hWnd = NULL;
HWND hWndPaper = NULL;
//任务栏窗口 
if ((hWnd = FindWindow(_T("Shell_TrayWnd"), NULL)) != NULL)
{
//任务栏右边托盘图标+时间区
if ((hWnd = FindWindowEx(hWnd, 0, _T("TrayNotifyWnd"), NULL)) != NULL)
{
//不同系统可能有可能没有这层  
hWndPaper = FindWindowEx(hWnd, 0, _T("SysPager"), NULL);
//托盘图标窗口  
if (!hWndPaper)
hWnd = FindWindowEx(hWnd, 0, _T("ToolbarWindow32"), NULL);
else
hWnd = FindWindowEx(hWndPaper, 0, _T("ToolbarWindow32"), NULL);
}
}


return hWnd;
}
//获取溢出托盘区窗口句柄  
HWND FindNotifyIconOverflowWindow()
{
HWND hWnd = NULL;


hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);
if (hWnd != NULL)
{
hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);
}


return hWnd;
}


//刷新托盘区域
void RefurbishTray()

HWND hToolbarWindow32=NULL;
HWND hToolbarOverWindow32=NULL;
hToolbarWindow32 = FindTrayWnd();
hToolbarOverWindow32 = FindNotifyIconOverflowWindow();
if (NULL != hToolbarWindow32)
{
Refurbish(hToolbarWindow32);//刷新托盘区域
}
if (NULL != hToolbarOverWindow32)
{
Refurbish(hToolbarOverWindow32);//刷新溢出区域
}
}

猜你喜欢

转载自blog.csdn.net/zy1031393630/article/details/78442527