VC 获取托盘图标信息

//本例以获取程序托盘图标位置为例

//根据需要还可以获取不少信息

参考:http://www.cnblogs.com/daxingxing/archive/2012/02/06/2340384.html

  1. //获取托盘区域数据  
  2. RECT CTray::GetTrayRect()  
  3. {  
  4.     RECT rect = {0};  
  5.     HWND hWnd = NULL;  
  6.   
  7.     hWnd = FindTrayWnd();  
  8.     if (hWnd != NULL)  
  9.     {  
  10.         if (!EnumNotifyWindow(rect,hWnd))//如果没在普通托盘区  
  11.         {  
  12.             hWnd = FindNotifyIconOverflowWindow();//在溢出区(win7)  
  13.             if (hWnd != NULL)  
  14.             {  
  15.                 EnumNotifyWindow(rect,hWnd);  
  16.             }  
  17.         }  
  18.     }  
  19.   
  20.     return rect;  
  21. }  
  22. //枚举获取托盘区域位置  
  23. bool CTray::EnumNotifyWindow(RECT &rect,HWND hWnd)  
  24. {  
  25.     //RECT rect = {0};  
  26.     bool bSuc = false;  
  27.     unsigned long lngPID = 0;  
  28.     long ret = 0,lngButtons = 0;  
  29.     long lngHwndAdr = 0,lngHwnd = 0;//,lngTextAdr,lngButtonID;  
  30.     HANDLE hProcess = NULL;  
  31.     LPVOID lngAddress = NULL,lngRect = NULL;  
  32.   
  33.     if (hWnd != NULL)  
  34.     {  
  35.         ret = GetWindowThreadProcessId(hWnd, &lngPID);  
  36.         if(ret != 0 && lngPID != 0)  
  37.         {  
  38.             hProcess = OpenProcess(PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE,0,lngPID);//  
  39.             if (hProcess != NULL)  
  40.             {  
  41.                 lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);  
  42.                 lngRect = VirtualAllocEx(hProcess,0,sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);  
  43.                 lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0); //发送消息获取托盘button数量  
  44.                 if (lngAddress != NULL  && lngRect != NULL)  
  45.                 {  
  46.                     for(int i=0 ;i< lngButtons;i++)  
  47.                     {  
  48.                         RECT rc = {0};   
  49.                         int j = i;  
  50.                         ret = SendMessage(hWnd,TB_GETBUTTON,j,long(lngAddress));//发送消息获取托盘项数据起始地址  
  51.                         ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0);  
  52.                         if(ret != 0 && lngHwndAdr != -1)  
  53.                         {  
  54.                             ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);//获取句柄  
  55.                             if(ret != 0 && (HWND)lngHwnd == m_NotifyIconData.hWnd)//  
  56.                             {  
  57.                                 ret = ::SendMessage(hWnd,TB_GETITEMRECT,(WPARAM)j,(LPARAM)lngRect); //发送消息获取托盘项区域数据  
  58.                                 ret = ReadProcessMemory(hProcess,lngRect,&rc, sizeof(rc),0);  //读取托盘区域数据  
  59.                                 if(ret != 0)  
  60.                                 {  
  61.                                     CWnd::FromHandle(hWnd)->ClientToScreen(&rc);  
  62.                                     rect = rc;  
  63.                                 }  
  64.                                 bSuc = true;//在普通托盘区找到,在溢出区不再查找  
  65.                                 break;  
  66.                             }  
  67.                         }  
  68.                     }   
  69.                 }  
  70.                 if (lngAddress != NULL)  
  71.                 {  
  72.                     VirtualFreeEx( hProcess, lngAddress, 0x4096, MEM_DECOMMIT);  
  73.                     VirtualFreeEx( hProcess, lngAddress, 0, MEM_RELEASE);  
  74.                 }  
  75.                 if (lngRect != NULL)  
  76.                 {  
  77.                     VirtualFreeEx( hProcess, lngRect, sizeof(RECT), MEM_DECOMMIT);  
  78.                     VirtualFreeEx( hProcess, lngRect, 0, MEM_RELEASE);  
  79.                 }  
  80.                 CloseHandle(hProcess);  
  81.             }  
  82.         }  
  83.     }  
  84.     return bSuc;  
  85. }  
  86. //获取普通托盘区窗口句柄  
  87. HWND CTray::FindTrayWnd()  
  88. {  
  89.     HWND hWnd = NULL;  
  90.     HWND hWndPaper = NULL;  
  91.   
  92.     if ((hWnd = FindWindow(_T("Shell_TrayWnd"), NULL)) != NULL)  
  93.     {  
  94.         if ((hWnd = FindWindowEx(hWnd, 0, _T("TrayNotifyWnd"), NULL)) != NULL)  
  95.         {  
  96.             hWndPaper = FindWindowEx(hWnd, 0, _T("SysPager"), NULL);  
  97.             if(!hWndPaper)  
  98.                 hWnd = FindWindowEx(hWnd, 0, _T("ToolbarWindow32"), NULL);  
  99.             else  
  100.                 hWnd = FindWindowEx(hWndPaper, 0, _T("ToolbarWindow32"), NULL);  
  101.         }  
  102.     }  
  103.   
  104.     return hWnd;  
  105. }  
  106. //获取溢出托盘区窗口句柄  
  107. HWND CTray::FindNotifyIconOverflowWindow()  
  108. {  
  109.     HWND hWnd = NULL;  
  110.   
  111.     hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);  
  112.     if (hWnd != NULL)  
  113.     {  
  114.         hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);  
  115.     }  
  116.       
  117.     return hWnd;  
  118. }  
//获取托盘区域数据
RECT CTray::GetTrayRect()
{
	RECT rect = {0};
	HWND hWnd = NULL;

	hWnd = FindTrayWnd();
	if (hWnd != NULL)
	{
		if (!EnumNotifyWindow(rect,hWnd))//如果没在普通托盘区
		{
			hWnd = FindNotifyIconOverflowWindow();//在溢出区(win7)
			if (hWnd != NULL)
			{
				EnumNotifyWindow(rect,hWnd);
			}
		}
	}

	return rect;
}
//枚举获取托盘区域位置
bool CTray::EnumNotifyWindow(RECT &rect,HWND hWnd)
{
	//RECT rect = {0};
	bool bSuc = false;
	unsigned long lngPID = 0;
	long ret = 0,lngButtons = 0;
	long lngHwndAdr = 0,lngHwnd = 0;//,lngTextAdr,lngButtonID;
	HANDLE hProcess = NULL;
	LPVOID lngAddress = NULL,lngRect = NULL;

	if (hWnd != NULL)
	{
		ret = GetWindowThreadProcessId(hWnd, &lngPID);
		if(ret != 0 && lngPID != 0)
		{
			hProcess = OpenProcess(PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE,0,lngPID);//
			if (hProcess != NULL)
			{
				lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
				lngRect = VirtualAllocEx(hProcess,0,sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);
				lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0); //发送消息获取托盘button数量
				if (lngAddress != NULL  && lngRect != NULL)
				{
					for(int i=0 ;i< lngButtons;i++)
					{
						RECT rc = {0}; 
						int j = i;
						ret = SendMessage(hWnd,TB_GETBUTTON,j,long(lngAddress));//发送消息获取托盘项数据起始地址
						ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0);
						if(ret != 0 && lngHwndAdr != -1)
						{
							ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);//获取句柄
							if(ret != 0 && (HWND)lngHwnd == m_NotifyIconData.hWnd)//
							{
								ret = ::SendMessage(hWnd,TB_GETITEMRECT,(WPARAM)j,(LPARAM)lngRect); //发送消息获取托盘项区域数据
								ret = ReadProcessMemory(hProcess,lngRect,&rc, sizeof(rc),0);  //读取托盘区域数据
								if(ret != 0)
								{
									CWnd::FromHandle(hWnd)->ClientToScreen(&rc);
									rect = rc;
								}
								bSuc = true;//在普通托盘区找到,在溢出区不再查找
								break;
							}
						}
					} 
				}
				if (lngAddress != NULL)
				{
					VirtualFreeEx( hProcess, lngAddress, 0x4096, MEM_DECOMMIT);
					VirtualFreeEx( hProcess, lngAddress, 0, MEM_RELEASE);
				}
				if (lngRect != NULL)
				{
					VirtualFreeEx( hProcess, lngRect, sizeof(RECT), MEM_DECOMMIT);
					VirtualFreeEx( hProcess, lngRect, 0, MEM_RELEASE);
				}
				CloseHandle(hProcess);
			}
		}
	}
	return bSuc;
}
//获取普通托盘区窗口句柄
HWND CTray::FindTrayWnd()
{
	HWND hWnd = NULL;
	HWND hWndPaper = NULL;

	if ((hWnd = FindWindow(_T("Shell_TrayWnd"), NULL)) != NULL)
	{
		if ((hWnd = FindWindowEx(hWnd, 0, _T("TrayNotifyWnd"), NULL)) != NULL)
		{
			hWndPaper = FindWindowEx(hWnd, 0, _T("SysPager"), NULL);
			if(!hWndPaper)
				hWnd = FindWindowEx(hWnd, 0, _T("ToolbarWindow32"), NULL);
			else
				hWnd = FindWindowEx(hWndPaper, 0, _T("ToolbarWindow32"), NULL);
		}
	}

	return hWnd;
}
//获取溢出托盘区窗口句柄
HWND CTray::FindNotifyIconOverflowWindow()
{
	HWND hWnd = NULL;

	hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);
	if (hWnd != NULL)
	{
		hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);
	}
	
	return hWnd;
}


//以下代码网上收集的,变量 初始化 指针句柄 及函数是否成功都没判定

//需要的自己加下判定,有时间再改了

  1. struct TRAYDATA  
  2. {  
  3.     HWND hwnd;                                 
  4.     UINT uID;                                 
  5.     UINT uCallbackMessage;         
  6.     DWORD Reserved[2];                 
  7.     HICON hIcon;                                 
  8. };  
  9. void CTray::GetTrayRect()  
  10. {  
  11. HWND hWnd,hWndPaper;  
  12. unsigned long lngPID;  
  13. long ret,lngButtons;  
  14. HANDLE hProcess;  
  15. LPVOID lngAddress;  
  16. long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;  
  17. TCHAR strBuff[1024]={0};  
  18.   TRAYDATA trayData = {0};  
  19.   TBBUTTON btnData={0};  
  20.   
  21.   
  22. hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);  
  23. hWnd = FindWindowEx(hWnd, 0, _T("TrayNotifyWnd"), NULL);  
  24. hWndPaper = FindWindowEx(hWnd, 0, _T("SysPager"), NULL);  
  25. if(!hWndPaper)  
  26. hWnd = FindWindowEx(hWnd, 0, _T("ToolbarWindow32"), NULL);  
  27. else  
  28. hWnd = FindWindowEx(hWndPaper, 0, _T("ToolbarWindow32"), NULL);  
  29. ret = GetWindowThreadProcessId(hWnd, &lngPID);  
  30. hProcess = OpenProcess(PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE,0,lngPID);  
  31. lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);  
  32. lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);                        
  33. RECT rc; POINT point;  
  34. LPVOID lngRect = VirtualAllocEx(hProcess,0,sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);  
  35.   
  36. CRect rect;  
  37. for(int i=0 ;i< lngButtons;i++)  
  38. {  
  39. int j = i;  
  40. ret = SendMessage(hWnd,TB_GETBUTTON,j,long(lngAddress));  
  41. ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16),&lngTextAdr,4,0);  
  42. if(lngTextAdr != -1)  
  43. {  
  44. ret = ReadProcessMemory(hProcess, LPVOID(lngTextAdr),strBuff,1024,0);  
  45. //ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0); //获取句柄  
  46. //ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);  
  47. //ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4),&lngButtonID,4,0);//获取buttonID  
  48. CString str(strBuff);  
  49. if (str.Compare(m_NotifyIconData.szTip) == 0)  
  50. {  
  51. ::SendMessage(hWnd,TB_GETITEMRECT,(WPARAM)j,(LPARAM)lngRect);  
  52. ReadProcessMemory(hProcess,lngRect,&rc, sizeof(rc),0);  //获取托盘图标区域  
  53. CWnd::FromHandle(hWnd)->ClientToScreen(&rc);  
  54. }  
  55.   
  56. //以下是隐藏托盘图标  
  57. //    {  
  58. //    if(show)  
  59. //    {  
  60. //    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,0);  
  61. //    }  
  62. //    else  
  63. //    {   
  64. //    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,1);  
  65. //    }  
  66. //    }  
  67.   
  68.   
  69. }  
  70. }  
  71. VirtualFreeEx( hProcess, lngAddress, 0x4096, MEM_DECOMMIT);  
  72. VirtualFreeEx( hProcess, lngAddress, 0, MEM_RELEASE);  
  73. VirtualFreeEx( hProcess, lngRect, sizeof(RECT), MEM_DECOMMIT);  
  74. VirtualFreeEx( hProcess, lngRect, 0, MEM_RELEASE);  
  75. CloseHandle(hProcess);  
  76. }  
struct TRAYDATA
{
    HWND hwnd;                               
    UINT uID;                               
    UINT uCallbackMessage;       
    DWORD Reserved[2];               
    HICON hIcon;                               
};
void CTray::GetTrayRect()
{
HWND hWnd,hWndPaper;
unsigned long lngPID;
long ret,lngButtons;
HANDLE hProcess;
LPVOID lngAddress;
long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;
TCHAR strBuff[1024]={0};
  TRAYDATA trayData = {0};
  TBBUTTON btnData={0};


hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);
hWnd = FindWindowEx(hWnd, 0, _T("TrayNotifyWnd"), NULL);
hWndPaper = FindWindowEx(hWnd, 0, _T("SysPager"), NULL);
if(!hWndPaper)
hWnd = FindWindowEx(hWnd, 0, _T("ToolbarWindow32"), NULL);
else
hWnd = FindWindowEx(hWndPaper, 0, _T("ToolbarWindow32"), NULL);
ret = GetWindowThreadProcessId(hWnd, &lngPID);
hProcess = OpenProcess(PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE,0,lngPID);
lngAddress = VirtualAllocEx(hProcess,0, 0x4096, MEM_COMMIT, PAGE_READWRITE);
lngButtons = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);                      
RECT rc; POINT point;
LPVOID lngRect = VirtualAllocEx(hProcess,0,sizeof(RECT), MEM_COMMIT, PAGE_READWRITE);

CRect rect;
for(int i=0 ;i< lngButtons;i++)
{
int j = i;
ret = SendMessage(hWnd,TB_GETBUTTON,j,long(lngAddress));
ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 16),&lngTextAdr,4,0);
if(lngTextAdr != -1)
{
ret = ReadProcessMemory(hProcess, LPVOID(lngTextAdr),strBuff,1024,0);
//ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 12),&lngHwndAdr,4,0); //获取句柄
//ret = ReadProcessMemory(hProcess, LPVOID(lngHwndAdr),&lngHwnd, 4,0);
//ret = ReadProcessMemory(hProcess, LPVOID(long(lngAddress) + 4),&lngButtonID,4,0);//获取buttonID
CString str(strBuff);
if (str.Compare(m_NotifyIconData.szTip) == 0)
{
::SendMessage(hWnd,TB_GETITEMRECT,(WPARAM)j,(LPARAM)lngRect);
ReadProcessMemory(hProcess,lngRect,&rc, sizeof(rc),0);  //获取托盘图标区域
CWnd::FromHandle(hWnd)->ClientToScreen(&rc);
}

//以下是隐藏托盘图标
//    {
//    if(show)
//    {
//    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,0);
//    }
//    else
//    { 
//    SendMessage(hWnd,TB_HIDEBUTTON,lngButtonID,1);
//    }
//    }


}
}
VirtualFreeEx( hProcess, lngAddress, 0x4096, MEM_DECOMMIT);
VirtualFreeEx( hProcess, lngAddress, 0, MEM_RELEASE);
VirtualFreeEx( hProcess, lngRect, sizeof(RECT), MEM_DECOMMIT);
VirtualFreeEx( hProcess, lngRect, 0, MEM_RELEASE);
CloseHandle(hProcess);
}


//另一例子

  1. VOID StartStorm()  
  2. {  
  3.          HWND hMain = FindWindow("animate_layered_window_class""暴风媒体中心");  
  4.          if ( hMain )  
  5.          {  
  6.                  ShowWindow(hMain, SW_HIDE);  
  7.          }  
  8.           
  9.          
  10.         //得到工具栏句柄  
  11.      HWND hTray = FindWindow("Shell_TrayWnd", NULL);  
  12.      hTray = FindWindowEx(hTray, 0, "TrayNotifyWnd", NULL);  
  13.      hTray = FindWindowEx(hTray, 0, "SysPager", NULL);  
  14.      hTray = FindWindowEx(hTray, 0, "ToolbarWindow32", NULL);  
  15.           
  16.         //获取explore进程ID  
  17.          DWORD TrayPid;  
  18.          GetWindowThreadProcessId(hTray, &TrayPid);  
  19.           
  20.         //打开进程 并且开辟进程空间  
  21.          RECT rect;  
  22.          TBBUTTON tb;  
  23.          TBBUTTON pTb;  
  24.          LPVOID lpAddr;  
  25.          DWORD dwThreadIdOfICO;  
  26.          DWORD dwTempId = FindStorm("Stormtray.exe"); //你要点击的进程的PID  
  27.           
  28.          TRAYDATA traydata;  
  29.           
  30.         HANDLE hOpen = OpenProcess(PROCESS_ALL_ACCESS, FALSE, TrayPid);  
  31.          lpAddr = VirtualAllocEx(hOpen, NULL, sizeof(tb) + sizeof(rect), MEM_COMMIT, PAGE_READWRITE);  
  32.           
  33.         int nCount = SendMessage(hTray, TB_BUTTONCOUNT, 0, 0);  
  34.          int i;  
  35.          DWORD dwOutWrite;  
  36.          for ( i = 0; i < nCount; i ++)  
  37.          {  
  38.                  ZeroMemory(&tb, sizeof(tb));  
  39.                  ZeroMemory(&rect, sizeof(rect));  
  40.                  //把参数写进目标进程  
  41.                  WriteProcessMemory(hOpen, lpAddr, &tb, sizeof(tb), &dwOutWrite);  
  42.                  //WriteProcessMemory(hOpen, (LPVOID)((DWORD)lpAddr + sizeof(pTb)), &rect, sizeof(rect), &dwOutWrite);  
  43.                  //获取BUTTON  
  44.                  SendMessage(hTray, TB_GETBUTTON, i, LPARAM(lpAddr));  
  45.                  //读取TBBUTTON结构  
  46.                  ReadProcessMemory(hOpen, lpAddr, &pTb, sizeof(TBBUTTON), &dwOutWrite);  
  47.                  //读取TRAYDATA结构  
  48.                  ReadProcessMemory(hOpen, (LPVOID)pTb.dwData, &traydata, sizeof(TRAYDATA), &dwOutWrite);  
  49.                   
  50.                  GetWindowThreadProcessId(traydata.hwnd, &dwThreadIdOfICO);  
  51.                  if ( dwThreadIdOfICO == dwTempId )  
  52.                  {  
  53.                          //获取ICO的RECT  
  54.                          LPVOID lp = (LPVOID)((DWORD)lpAddr + sizeof(pTb));  
  55.                          SendMessage(hTray, TB_GETITEMRECT, i, (LPARAM)lp);  
  56.                          LPVOID lpdata = (LPVOID)((DWORD)lpAddr + sizeof(TBBUTTON));  
  57.                          ReadProcessMemory(hOpen, lpdata, &rect, sizeof(rect), &dwOutWrite);  
  58.                          int iGap = rect.right/2; //得到图标的中间坐标的间隔  
  59.                          //点击  
  60.                          SendMessage(hTray, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(rect.right - iGap, rect.bottom - iGap));  
  61.                          SendMessage(hTray, WM_LBUTTONUP, 0, MAKELPARAM(rect.right - iGap, rect.bottom - iGap));  
  62.                          //  
  63.                          CloseHandle(hOpen);  
  64.                          break;;  
  65.                  }  
  66.          }  
  67.           
  68. }  
VOID StartStorm()
{
         HWND hMain = FindWindow("animate_layered_window_class", "暴风媒体中心");
         if ( hMain )
         {
                 ShowWindow(hMain, SW_HIDE);
         }
        
       
        //得到工具栏句柄
     HWND hTray = FindWindow("Shell_TrayWnd", NULL);
     hTray = FindWindowEx(hTray, 0, "TrayNotifyWnd", NULL);
     hTray = FindWindowEx(hTray, 0, "SysPager", NULL);
     hTray = FindWindowEx(hTray, 0, "ToolbarWindow32", NULL);
        
        //获取explore进程ID
         DWORD TrayPid;
         GetWindowThreadProcessId(hTray, &TrayPid);
        
        //打开进程 并且开辟进程空间
         RECT rect;
         TBBUTTON tb;
         TBBUTTON pTb;
         LPVOID lpAddr;
         DWORD dwThreadIdOfICO;
         DWORD dwTempId = FindStorm("Stormtray.exe"); //你要点击的进程的PID
        
         TRAYDATA traydata;
        
        HANDLE hOpen = OpenProcess(PROCESS_ALL_ACCESS, FALSE, TrayPid);
         lpAddr = VirtualAllocEx(hOpen, NULL, sizeof(tb) + sizeof(rect), MEM_COMMIT, PAGE_READWRITE);
        
        int nCount = SendMessage(hTray, TB_BUTTONCOUNT, 0, 0);
         int i;
         DWORD dwOutWrite;
         for ( i = 0; i < nCount; i ++)
         {
                 ZeroMemory(&tb, sizeof(tb));
                 ZeroMemory(&rect, sizeof(rect));
                 //把参数写进目标进程
                 WriteProcessMemory(hOpen, lpAddr, &tb, sizeof(tb), &dwOutWrite);
                 //WriteProcessMemory(hOpen, (LPVOID)((DWORD)lpAddr + sizeof(pTb)), &rect, sizeof(rect), &dwOutWrite);
                 //获取BUTTON
                 SendMessage(hTray, TB_GETBUTTON, i, LPARAM(lpAddr));
                 //读取TBBUTTON结构
                 ReadProcessMemory(hOpen, lpAddr, &pTb, sizeof(TBBUTTON), &dwOutWrite);
                 //读取TRAYDATA结构
                 ReadProcessMemory(hOpen, (LPVOID)pTb.dwData, &traydata, sizeof(TRAYDATA), &dwOutWrite);
                
                 GetWindowThreadProcessId(traydata.hwnd, &dwThreadIdOfICO);
                 if ( dwThreadIdOfICO == dwTempId )
                 {
                         //获取ICO的RECT
                         LPVOID lp = (LPVOID)((DWORD)lpAddr + sizeof(pTb));
                         SendMessage(hTray, TB_GETITEMRECT, i, (LPARAM)lp);
                         LPVOID lpdata = (LPVOID)((DWORD)lpAddr + sizeof(TBBUTTON));
                         ReadProcessMemory(hOpen, lpdata, &rect, sizeof(rect), &dwOutWrite);
                         int iGap = rect.right/2; //得到图标的中间坐标的间隔
                         //点击
                         SendMessage(hTray, WM_LBUTTONDOWN, MK_LBUTTON, MAKELPARAM(rect.right - iGap, rect.bottom - iGap));
                         SendMessage(hTray, WM_LBUTTONUP, 0, MAKELPARAM(rect.right - iGap, rect.bottom - iGap));
                         //
                         CloseHandle(hOpen);
                         break;;
                 }
         }
        
}


//win7有一个溢出托盘区:以下是隐藏在托盘区中的托盘信息,用以上的方法找不到,因为在NotifyIconOverflowWindow里

Fhwnd = FindWindow("NotifyIconOverflowWindow", NULL) 

参考文章:http://topic.csdn.net/u/20101003/23/859851ee-5aa1-4476-8ce1-1359826df2b0.html

  1. #include "stdafx.h"  
  2. #include <afx.h>  
  3. #include <locale.h>  
  4. #include <string>  
  5. using namespace std;  
  6.   
  7.   
  8.   
  9. typedef BOOL (WINAPI *LPFN_ISWOW64PROCESS) (HANDLEPBOOL);  
  10.   
  11. BOOL IsWow64()  
  12. {  
  13.     BOOL bIsWow64 = FALSE;  
  14.   
  15.     LPFN_ISWOW64PROCESS   
  16.         fnIsWow64Process = (LPFN_ISWOW64PROCESS)GetProcAddress(  
  17.         GetModuleHandle(_T("kernel32")),"IsWow64Process");  
  18.   
  19.     if (NULL != fnIsWow64Process)  
  20.     {  
  21.         if (!fnIsWow64Process(GetCurrentProcess(),&bIsWow64))  
  22.         {  
  23.             // handle error  
  24.         }  
  25.     }  
  26.     return bIsWow64;  
  27. }  
  28.   
  29.   
  30. HWND FindTrayWnd()  
  31. {  
  32.     HWND hWnd = NULL;  
  33.       
  34.     hWnd = FindWindow(_T("Shell_TrayWnd"), NULL);  
  35.     hWnd = FindWindowEx(hWnd, NULL, _T("TrayNotifyWnd"), NULL);  
  36.     hWnd = FindWindowEx(hWnd, NULL, _T("SysPager"), NULL);  
  37.     hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);  
  38.   
  39.   
  40.     return hWnd;  
  41. }  
  42.   
  43. HWND FindNotifyIconOverflowWindow()  
  44. {  
  45.     HWND hWnd = NULL;  
  46.   
  47.     hWnd = FindWindow(_T("NotifyIconOverflowWindow"), NULL);  
  48.     hWnd = FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);  
  49.   
  50.     return hWnd;  
  51. }  
  52.   
  53. void EnumNotifyWindow(HWND hWnd)  
  54. {  
  55.     DWORD dwProcessId = 0;  
  56.     GetWindowThreadProcessId(hWnd,&dwProcessId);  
  57.   
  58.     HANDLE hProcess = OpenProcess(PROCESS_VM_OPERATION | PROCESS_VM_READ | PROCESS_VM_WRITE, FALSE, dwProcessId);  
  59.     if ( hProcess==NULL ){  
  60.         return;  
  61.     }  
  62.     LPVOID lAddress = VirtualAllocEx(hProcess, 0, 4096, MEM_COMMIT, PAGE_READWRITE);  
  63.     if ( lAddress==NULL ){  
  64.         return;  
  65.     }  
  66.     DWORD lTextAdr = 0;  
  67.     BYTE buff[1024] = {0};  
  68.     CString strFilePath;  
  69.     CString strTile;  
  70.     HWND hMainWnd = NULL;  
  71.     int nDataOffset = sizeof(TBBUTTON) - sizeof(INT_PTR) - sizeof(DWORD_PTR);  
  72.     int nStrOffset = 18;   
  73.     if ( IsWow64() ){  
  74.         nDataOffset+=4;  
  75.         nStrOffset+=6;  
  76.     }  
  77.   
  78.     //得到圖標個數  
  79.     int lButton = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);  
  80.     for (int i = 0; i < lButton; i++) {  
  81.         SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)lAddress);  
  82.         //讀文本地址  
  83.         ReadProcessMemory(hProcess, (LPVOID)((DWORD)lAddress + nDataOffset), &lTextAdr, 4, 0);  
  84.         if ( lTextAdr!=-1 ) {  
  85.             //讀文本  
  86.             ReadProcessMemory(hProcess, (LPCVOID)lTextAdr, buff, 1024, 0);  
  87.             hMainWnd = (HWND)(*((DWORD*)buff));  
  88.             strFilePath = (WCHAR *)buff + nStrOffset;  
  89.             strTile = (WCHAR *)buff + nStrOffset + MAX_PATH;  
  90.             _tprintf(_T("%s %s\n"),strTile,strFilePath);  
  91.         }  
  92.     }  
  93.     VirtualFreeEx(hProcess, lAddress, 4096, MEM_RELEASE);  
  94.     CloseHandle(hProcess);  
  95. }  
  96.   
  97.   
  98. int _tmain(int argc, _TCHAR* argv[])  
  99. {  
  100.     setlocale(LC_ALL, "chs");  
  101.     EnumNotifyWindow(FindTrayWnd());  
  102.     _tprintf(_T("\n"));  
  103.     EnumNotifyWindow(FindNotifyIconOverflowWindow());  
  104.     system("pause");  
  105.     return 0;  
  106. }  
#include "stdafx.h"
#include <afx.h>
#include <locale.h>
#include <string>
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 lAddress = VirtualAllocEx(hProcess, 0, 4096, MEM_COMMIT, PAGE_READWRITE);
    if ( lAddress==NULL ){
        return;
    }
    DWORD lTextAdr = 0;
    BYTE buff[1024] = {0};
    CString strFilePath;
    CString strTile;
    HWND hMainWnd = NULL;
    int nDataOffset = sizeof(TBBUTTON) - sizeof(INT_PTR) - sizeof(DWORD_PTR);
    int nStrOffset = 18; 
    if ( IsWow64() ){
        nDataOffset+=4;
        nStrOffset+=6;
    }

    //得到圖標個數
    int lButton = SendMessage(hWnd, TB_BUTTONCOUNT, 0, 0);
    for (int i = 0; i < lButton; i++) {
        SendMessage(hWnd, TB_GETBUTTON, i, (LPARAM)lAddress);
        //讀文本地址
        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);
        }
    }
    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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值