C++遍历托盘图标,获取输出坐标等信息源码

来源:互联网 发布:淘宝如何管理商家 编辑:程序博客网 时间:2024/06/05 06:14
#include <windows.h>#include <locale.h>#include <string>#include <commctrl.h>#include <atlstr.h>using namespace std;typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLE, PBOOL);BOOL IsWow64(){BOOL bIsWow64 = FALSE;LPFN_ISWOW64PROCESS fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(GetModuleHandle(_T("kernel32")),"IsWow64Process");if (NULL != fnIsWow64Process){if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64)){// handle error}}return bIsWow64;}HWND FindTrayWnd(){HWND hWnd = NULL;hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);hWnd = FindWindowEx(hWnd, NULL, _T("TrayNotifyWnd"), NULL);hWnd = FindWindowEx(hWnd, NULL, _T("SysPager"), NULL);hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);return hWnd;}HWND FindNotifyIconOverflowWindow(){HWND hWnd = NULL;hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);return hWnd;}void EnumNotifyWindow(HWND hWnd){DWORD dwProcessId = 0;GetWindowThreadProcessId(hWnd,&dwProcessId);HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, dwProcessId);if ( hProcess==NULL ){return;}LPVOID pBun = VirtualAllocEx(hProcess, 0, sizeof(RECT), MEM_COMMIT, PAGE_EXECUTE_READWRITE);LPVOID lAddress = VirtualAllocEx(hProcess, 0, 4096, MEM_COMMIT, PAGE_EXECUTE_READWRITE);if ( lAddress==NULL ){return;}DWORD lTextAdr = 0;BYTE buff[1024] = {0};TCHAR ptb[256] = {0};CString strFilePath;CString strTile;HWND hMainWnd = NULL;RECT rccc;int nDataOffset = sizeof(TBBUTTON) - sizeof(INT_PTR) - sizeof(DWORD_PTR);int nStrOffset = 18; if ( IsWow64() ){nDataOffset+=4;nStrOffset+=6;}GetWindowRect(hWnd,&rccc);//获取图标个数int lButton = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);for (int i = 0; i < lButton; i++) {SendMessage(hWnd, TB_GETITEMRECT, i, (LPARAM)pBun);SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)lAddress);//读坐标ReadProcessMemory(hProcess, pBun, (LPVOID)ptb, sizeof(RECT), 0);//读文本地址ReadProcessMemory(hProcess, (LPVOID)((DWORD)lAddress + nDataOffset), &lTextAdr, 4, 0);if ( lTextAdr!=-1 ) {//读文本ReadProcessMemory(hProcess, (LPCVOID)lTextAdr, buff, 1024, 0);hMainWnd = (HWND)(*((DWORD*)buff));strFilePath = (WCHAR *)buff + nStrOffset;strTile = (WCHAR *)buff + nStrOffset + MAX_PATH;//_tprintf(_T("%s %s\n"),strTile,strFilePath);}LPRECT pRect = (LPRECT)ptb;OffsetRect(pRect,rccc.left,rccc.top);_tprintf(_T("X:%d Y:%d "), pRect->right, pRect->bottom);_tprintf(_T("%s %s\n"),strTile,strFilePath);/*CString str360("360安全卫士");if(strTile.Find(str360)!=-1){_tprintf(_T("X:%d Y:%d\n"), pRect->right, pRect->bottom);_tprintf(_T("%s %s\n"),strTile,strFilePath);SetCursorPos(pRect->left+10,pRect->top+20);mouse_event(MOUSEEVENTF_RIGHTDOWN,0,0,0,0);mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);SetCursorPos(800, 800);mouse_event(MOUSEEVENTF_LEFTDOWN,0,0,0,0);}*/}VirtualFreeEx(hProcess, pBun, 4096, MEM_DECOMMIT);VirtualFreeEx(hProcess, lAddress, 4096, MEM_RELEASE);CloseHandle(hProcess);}int _tmain(int argc, _TCHAR* argv[]){setlocale(LC_ALL, "chs");EnumNotifyWindow(FindTrayWnd());_tprintf(_T("\n"));EnumNotifyWindow(FindNotifyIconOverflowWindow());system("pause");return 0;}

 
原创粉丝点击