15,979
社区成员
发帖
与我相关
我的任务
分享
// 判断在通知区域是否有托盘图标
BOOL32 CMainFrameWnd::IsTrayIconExsitInNotifyArea()
{
HWND hWnd = NULL, hWndPager = NULL;
unsigned long ulPID = 0;
long lRet = 0, lButtons = 0;
HANDLE hProcess = NULL;
LPVOID pAddress = NULL;
long lTextAdr = 0, lHwndAdr = 0, lHwnd = 0, lButtonID = 0;
char strBuff[1024] = { 0 };
char *pStr = NULL;
char *pTemp = NULL;
hWnd = ::FindWindow( _T("Shell_TrayWnd"), NULL );
hWnd = ::FindWindowEx( hWnd, 0, _T("TrayNotifyWnd"), NULL );
hWndPager = ::FindWindowEx( hWnd, 0, _T("SysPager"), NULL );
if( hWndPager == NULL )
{
hWnd = ::FindWindowEx( hWnd, 0, _T("ToolbarWindow32"), NULL ); // 对于
Win2000,没有SysPager窗口
}
else
{
hWnd = ::FindWindowEx( hWndPager, 0, _T("ToolbarWindow32"), NULL );
}
lRet = GetWindowThreadProcessId( hWnd, &ulPID );
hProcess = OpenProcess(
PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE, 0, ulPID );
pAddress = VirtualAllocEx( hProcess, 0, 0x4096, MEM_COMMIT, PAGE_READWRITE );
lButtons = ::SendMessage( hWnd, TB_BUTTONCOUNT, 0, 0 );
for( int i=0; i< lButtons; i++ )
{
lRet = ::SendMessage( hWnd, TB_GETBUTTON, i, long(pAddress) );
lRet = ReadProcessMemory( hProcess, LPVOID(long(pAddress) + 16), <extAdr,
4, 0 );
if( lTextAdr != -1 )
{
lRet = ReadProcessMemory( hProcess, LPVOID(lTextAdr), strBuff, 1024,
0 );
USES_CONVERSION;
pStr = OLE2T( (LPOLESTR)( strBuff ) );
pTemp = strstr( pStr, MAIN_DLG_WINDOWNAME ); // 通过Toolbar中的按钮
名称(“XXXXXX”)来比对
if ( pTemp != NULL ) // 找到对应字串
{
VirtualFreeEx( hProcess, pAddress, 0X4096, MEM_RELEASE );
CloseHandle( hProcess );
return TRUE;
}
}
}
VirtualFreeEx( hProcess, pAddress, 0X4096, MEM_RELEASE );
CloseHandle( hProcess );
return FALSE;
}
// 判断在托盘溢出区域是否有托盘图标(此种情况针对Win7和Win8系统)
BOOL32 CMainFrameWnd::IsTrayIconExsitInOverflowWindow()
{
HWND hWnd = NULL;
unsigned long ulPID = 0;
long lRet = 0, lButtons = 0;
HANDLE hProcess = NULL;
LPVOID pAddress = NULL;
long lTextAdr = 0, lHwndAdr = 0, lHwnd = 0, lButtonID = 0;
char strBuff[1024] = { 0 };
char *pStr = NULL;
char *pTemp = NULL;
// 对于Win7、Win8系统,新增了通知溢出区域,所以要检查该区域中是否有托盘图标
hWnd = ::FindWindow( _T("NotifyIconOverflowWindow"), NULL ); // 获取通知溢出区域句柄
hWnd = ::FindWindowEx( hWnd, NULL, _T("ToolbarWindow32"), NULL );
lRet = GetWindowThreadProcessId( hWnd, &ulPID );
hProcess = OpenProcess(
PROCESS_ALL_ACCESS|PROCESS_VM_OPERATION|PROCESS_VM_READ|PROCESS_VM_WRITE, 0, ulPID );
pAddress = VirtualAllocEx( hProcess, 0, 0x4096, MEM_COMMIT, PAGE_READWRITE );
lButtons = ::SendMessage( hWnd, TB_BUTTONCOUNT, 0, 0 );
for( int i=0; i< lButtons - 1; i++ )
{
lRet = ::SendMessage( hWnd, TB_GETBUTTON, i, long(pAddress) );
lRet = ReadProcessMemory( hProcess, LPVOID(long(pAddress) + 16), <extAdr,
4, 0 );
if( lTextAdr != -1 )
{
lRet = ReadProcessMemory( hProcess, LPVOID(lTextAdr), strBuff, 1024,
0 );
USES_CONVERSION;
pStr = OLE2T( (LPOLESTR)( strBuff ) );
pTemp = strstr( pStr, _T("XXXXXXXX") ); // 通过窗口名称(“XXXXXXX”
)来比对
if ( pTemp != NULL ) // 找到对应字串
{
VirtualFreeEx( hProcess, pAddress, 0X4096, MEM_RELEASE );
CloseHandle( hProcess );
return TRUE;
}
}
}
VirtualFreeEx( hProcess, pAddress, 0X4096, MEM_RELEASE );
CloseHandle( hProcess );
return FALSE;
}
typedef struct _TBBUTTON {
int iBitmap;
int idCommand;
BYTE fsState;
BYTE fsStyle;
#ifdef _WIN64
BYTE bReserved[6]; // padding for alignment
#elif defined(_WIN32)
BYTE bReserved[2]; // padding for alignment
#endif
DWORD_PTR dwData;
INT_PTR iString;
} TBBUTTON, NEAR* PTBBUTTON, *LPTBBUTTON;
在32位和64位程序中的内存分布是不一样的,所以对于32位的程序在64位的操作系统上运行,是要重新计算偏移的