VC隐藏系统托盘

#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;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值