#include "..\\..\\Static Library\\Encode\\Encode.h"
#ifdef _DEBUG
#pragma comment(lib, "..\\..\\Static Library\\Debug\\Encode.lib")
#else
#pragma comment(lib, "..\\..\\Static Library\\Release\\Encode.lib")
#endif
#include <crtdbg.h>
......
......
BOOL CHideTrayIconDlg::ShowSysTrayIcon(LPCTSTR TipText, BOOL bShow, BOOL bSimilar)
{
_ASSERT(TipText != NULL);
LPVOID lpIconText = NULL;
DWORD pIdExplorer;
_TCHAR *_tIconText;
int nSysTrayTextBuffSize = 256;
HWND hwnd = ::FindWindow(_T("Shell_TrayWnd"), NULL);
hwnd = ::FindWindowEx(hwnd, 0, _T("TrayNotifyWnd"), NULL);
hwnd = ::FindWindowEx(hwnd, 0, _T("SysPager"), NULL);
hwnd = ::FindWindowEx(hwnd, 0, _T("ToolbarWindow32"), NULL);
::GetWindowThreadProcessId(hwnd, &pIdExplorer);
HANDLE hExplorer = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, pIdExplorer);
if (hExplorer == NULL)
{
#ifdef _DEBUG
MessageBox(_T("无法获取Explorer进程句柄!"), _T(""), MB_ICONERROR);
#endif
return FALSE;
}
lpIconText = VirtualAllocEx(hExplorer,NULL,256, MEM_COMMIT | MEM_RESERVE,PAGE_READWRITE);
if (lpIconText == NULL)
{
#ifdef _DEBUG
MessageBox(_T("无法在Explorer地址空间中分配内存!"), NULL, MB_ICONERROR);
#endif
goto ErrClose;
}
LRESULT BtnCount =::SendMessage(hwnd, TB_BUTTONCOUNT, 0, 0);
if (!BtnCount)
{
#ifdef _DEBUG
MessageBox(_T("无法读取系统托盘图标个数!"), NULL, MB_ICONERROR);
#endif
goto ErrFree;
}
for (int index = 0; index < BtnCount; index++)
{
char *pszIconText = new char[nSysTrayTextBuffSize];
memset(pszIconText, 0, nSysTrayTextBuffSize * sizeof(char));
LRESULT lLen = ::SendMessage(hwnd, TB_GETBUTTONTEXTA, index,(LPARAM)lpIconText);
if (!ReadProcessMemory(hExplorer, lpIconText, (LPVOID)pszIconText, nSysTrayTextBuffSize, 0))
{
#ifdef _DEBUG
MessageBox(_T("ReadProcessMemory函数执行失败!"), _T(""), MB_ICONERROR);
#endif
continue;
}
#ifdef UNICODE
_tIconText = Encode::mbstowcs(pszIconText);
#else
_tIconText = new char[strlen(pszIconText)];
memset(_tIconText, 0, strlen(pszIconText) + 1);
strcpy(_tIconText, pszIconText);
#endif
// 模糊定义
if(bSimilar && _tcsstr(_tIconText, TipText) != NULL || !bSimilar && lstrcmpi(_tIconText, TipText) == 0)
{
::SendMessage(hwnd, TB_HIDEBUTTON, index,!bShow);
::SendMessage(hwnd, TB_AUTOSIZE, 0, 0);
#ifdef _DEBUG
MessageBox(_tIconText, _T("_tIconText"), MB_ICONINFORMATION);
#endif
}
}
ErrFree:
VirtualFreeEx(hExplorer,lpIconText, nSysTrayTextBuffSize, MEM_RELEASE);
ErrClose:
CloseHandle(hExplorer);
return TRUE;
}
VC隐藏系统托盘
最新推荐文章于 2019-01-07 23:53:39 发布