发TB_HIDEBUTTON消息来隐藏托盘图标并不完美,会留下一块空白
仔细看一下NOTIFYICONDATA结构,发现了dwState
如果dwStateMask为NIS_HIDDEN,则当dwState为NIS_HIDDEN时图标隐藏;当dwState为NIS_SHAREDICON时图标显示
#define _WIN32_IE 0x0500
#include
#include
struct TRAYDATA
{
HWND hwnd;
UINT uID;
UINT uCallbackMessage;
DWORD Reserved[2];
HICON hIcon;
};
void ShowTrayIcon(LPCWSTR lpwszIcon, BOOL bShow)
{
HWND hWnd,hWndPaper;
DWORD dwProcessId;
int nButtonCount;
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);
nButtonCount = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
for(int i = 0 ; i < nButtonCount - 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, 0, MEM_RELEASE);
CloseHandle(hProcess);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
ShowTrayIcon(L"音量", FALSE); //隐藏音量图标
//ShowTrayIcon(L"音量", TRUE); //显示音量图标
return 0;
}
以上代码稍微解释一下吧:
任务栏的通知区域是个Toolbar,发TB_GETBUTTON消息可以获取Toolbar上某个按钮的信息
因为是要跨进程缓冲区的,所以需要用到ReadProcessMemory
获取到的TBBUTTON的dwData是个结构指针的,微软没有公开,但在网上可以找到:
struct TBBUTTONDATA
{
HWND hwnd; //the handle of the window on the taskbar
UINT uID;
UINT uCallbackMessage;
DWORD Reserved[2];
HICON hIcon;
};