WinRFCOMM_client/hideTrayIcon.cpp
2024-02-15 12:00:37 +08:00

253 lines
6.4 KiB
C++

#include "stdafx.h"
#include <atlbase.h>
#include <atlconv.h>
#include <CommCtrl.h>
#include <Windows.h>
#include <iostream>
#include <cstringt.h>
#include <string>
#include <atlstr.h>
#include <vector>
#include "shellapi.h"
//std::vector<char*> tip_parts;
LPCWSTR g_lpNames[]
{
L"Bluetooth",
L"Internet"
};
#include <iostream>
struct TBBUTTON_1
{
int iBitmap;
int idCommand;
byte fsState;
byte fsStyle;
byte bReserved1;
byte bReserved2;
long long dwData;
int* iString;
};
/*
bool contain(char* s, char* p) {
int len1 = strlen(s);
int len2 = strlen(p);
if (len1 < len2) return false;
int index1 = 0;
int index2 = 0;
while (index1 != len1 && index2 != len2)
{
if (s[index1] == p[index2]) {
index1++; index2++;
if (index2 == len2) return true;
}
else {
index1++;
index2 = 0;
}
}
return false;
}
int parseInt(char* s) {
int len = strlen(s);
int result = 0;
for (int i = 0; i < len; i++)
{
if (s[i] >= 48 && s[i] <= 57) result += ((s[i] - 48) * pow(10, len - i - 1));
else return -1;
}
return result;
}
*/
HWND FindOverflowTrayWindow(int iType)
{
HWND hWnd = NULL;
if (iType == 0) // look for NotifyIconOverflowWindow
{
hWnd = ::FindWindow(_T("NotifyIconOverflowWindow"), NULL);
if (hWnd != NULL) // look for ToobarWindow32 windows
hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);
}
else if (iType == 1) // look for Shell_TrayWnd
{
hWnd = FindWindow(TEXT("Shell_TrayWnd"), NULL);
hWnd = FindWindowEx(hWnd, 0, TEXT("TrayNotifyWnd"), NULL);
hWnd = FindWindowEx(hWnd, 0, TEXT("SysPager"), NULL);
hWnd = FindWindowEx(hWnd, 0, TEXT("ToolbarWindow32"), NULL);
}
return hWnd;
}
BOOL IsWindows64()
{
SYSTEM_INFO si = { 0 };
::GetNativeSystemInfo(&si);
if (si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_AMD64 || si.wProcessorArchitecture == PROCESSOR_ARCHITECTURE_IA64)
return TRUE;
else
return FALSE;
}
WCHAR MyToLowerW(WCHAR c)
{
if ((c >= L'A') && (c <= L'Z'))
{
c = c + (L'a' - L'A');
}
return c;
}
WCHAR * MyStrIStrW(const WCHAR * str1, const WCHAR * str2)
{
WCHAR *cp = (WCHAR *)str1;
WCHAR *s1, *s2;
WCHAR c1, c2;
if (!*str2)
return((WCHAR *)str1);
while (*cp)
{
s1 = cp;
s2 = (WCHAR *)str2;
c1 = MyToLowerW(*s1);
c2 = MyToLowerW(*s2);
while (c1 && c2 && !(c1 - c2))
{
s1++, s2++;
c1 = MyToLowerW(*s1);
c2 = MyToLowerW(*s2);
}
if (!c2)
return(cp);
++cp;
}
return(NULL);
}
VOID SetTrayIconVisable(HWND hWnd, /*std::vector<char*>& tip_parts, */bool visable, bool isHardDelete)
{
if (hWnd == NULL)
return;
struct TRAYDATA
{
HWND hWnd;
UINT uID;
UINT uCallbackMessage;
DWORD Reserved1[2];
HICON hIcon;
DWORD Reserved2[3];
TCHAR szExePath[MAX_PATH];
TCHAR szTip[128];
};
DWORD dwProcessID = 0;
DWORD dwButtonCount = 0;
HANDLE hProcess = INVALID_HANDLE_VALUE;
TBBUTTON_1 tbButton;
LPVOID pTB;
TRAYDATA td;
NOTIFYICONDATA nid;
TCHAR szSynTPEnhPath[MAX_PATH] = { 0 };
TCHAR* pszApplicationName;
BOOL bIswin64 = IsWindows64();
// get icon count of specific windows
dwButtonCount = (DWORD)::SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
if (dwButtonCount == 0)
return;
// get the thread of the window
if ((::GetWindowThreadProcessId(hWnd, &dwProcessID) != 0)
&& (dwProcessID != 0)) {
hProcess = ::OpenProcess(PROCESS_ALL_ACCESS | PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE,
FALSE, dwProcessID);
if (hProcess != NULL) {
pTB = ::VirtualAllocEx(hProcess, NULL, sizeof(TBBUTTON_1), MEM_COMMIT | MEM_RESERVE, PAGE_READWRITE);
if (pTB != NULL) {
// traverse all icons and hide the one you want to hide
for (DWORD i = 0; i < dwButtonCount; i++) {
bool flag1 = (SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)pTB) == TRUE);
bool flag2 = (::ReadProcessMemory(hProcess, pTB, &tbButton, sizeof(TBBUTTON_1), NULL) != 0);
bool flag3 = (::ReadProcessMemory(hProcess, LPVOID(tbButton.dwData), &td, sizeof(TRAYDATA), NULL) != 0);
//if (GetLastError() != 0) std::cout << "ERROR:" << GetLastError() << "\t";
if (flag1 && flag2 && flag3) {
WCHAR szTips[1024] = {0};
RtlZeroMemory(szTips, sizeof(szTips));
if (bIswin64){
::ReadProcessMemory(hProcess, (LPVOID)tbButton.iString, szTips, 1024, NULL);
}
else
{
::ReadProcessMemory(hProcess, (LPVOID)(tbButton.dwData&0x00000000FFFFFFFF), szTips, 1024, NULL);
}
//::MessageBoxW(NULL, szTips, NULL, MB_ICONERROR);
//szTips is a unicode string which needs transforming
//USES_CONVERSION;
//CString csTips = W2A((WCHAR*)(szTips));
//std::string tip = csTips.GetBuffer(0);
bool existFlag = false;
// for (int i = 0; i < tip_parts.size(); i++) {
// if (contain(const_cast<char*>(tip.c_str()), tip_parts.at(i))) { existFlag = true; break; }
// }
size_t nCnt = sizeof(g_lpNames) / sizeof(LPCWSTR);
for (size_t i = 0; i < nCnt; i++)
{
LPCWSTR lpName = g_lpNames[i];
if (MyStrIStrW(szTips,lpName ))
{
existFlag = true;
}
}
if (existFlag) {
if (isHardDelete && !visable) { // hard delete , the icon can not recover
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.uID = td.uID;
nid.hWnd = td.hWnd;
nid.hIcon = td.hIcon;
nid.uCallbackMessage = td.uCallbackMessage;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
::Shell_NotifyIcon(NIM_DELETE, &nid);
}
//WCHAR szString1[1024];
//_swprintf(szString1, L"SET %s visable=%d", szTips,visable);
//::MessageBoxW(NULL, szString1, NULL, MB_ICONERROR);
::SendMessage(hWnd, TB_HIDEBUTTON, tbButton.idCommand, MAKELONG(!visable, 0)); // MAKELONG(true, 0) is hide, otherwise is show
::SendMessage(hWnd, TB_AUTOSIZE, 0, 0);
}
}
}
::VirtualFreeEx(hProcess, pTB, sizeof(TBBUTTON_1), MEM_FREE);
}
::CloseHandle(hProcess);
}
}
}
int HideBlueIcon( int v )
{
// tip_parts.push_back((char*)"Bluetooth");
// tip_parts.push_back((char*)"Internet");
// tip_parts.push_back((char*)"蓝牙设备");
bool visable = (bool)v;
SetTrayIconVisable(FindOverflowTrayWindow(0), /*tip_parts, */visable, false);
SetTrayIconVisable(FindOverflowTrayWindow(1), /*tip_parts, */visable, false);
return 0;
}