admin管理员组

文章数量:1532030

对于使用托盘图标的程序,当我们通过强制结束进程,或者程序异常退出时,托盘图标区域总会有托盘图标的残留,需要我们用鼠标移到残留区域时,托盘图标才会自动消失。有时我们需要去做相关的处理,比如在软件升级时,会强制结束进程,这时需要自动的去清除残留的托盘图标。通过研究和查阅网络,整理如下的方法。

        1、通过向托盘区域发送WM_MOUSEMOVE消息,模拟鼠标移动到图潘区域的效果,使图标自动消失,代码如下:

        HWND hWnd, hWndPager;

hWnd = ::FindWindow( "Shell_TrayWnd", NULL );

hWnd = ::FindWindowEx( hWnd, 0, "TrayNotifyWnd", NULL );

hWndPager = ::FindWindowEx( hWnd, 0, "SysPager", NULL );

    if( !hWndPager )

hWnd = ::FindWindowEx( hWnd, 0, "ToolbarWindow32", NULL); // 对于Win2000,没有SysPager窗口

    else

hWnd = ::FindWindowEx( hWndPager, 0, "ToolbarWindow32", NULL ); // XP有SysPager窗口


RECT rTrayToolBar;

::GetClientRect(hwnd, &rTrayToolBar); // 获取托盘图标区域坐标

for(int x = 1; x < rTrayToolBar.right - 1; x++)

{

for (int y = 1; y < rTrayToolBar.bottom; y++ )

{

::SendMessage(hwnd, WM_MOUSEMOVE, 0, MAKELPARAM(x, y));  // 发送WM_MOUSEMOVE消息

}

}

 

      这种方法简单明了,但是有如下的问题:

 

        (1) 通过两个for循环想每个区域都发送一个WM_MOUSEMOVE消息,貌似不太合理;

        (2) 当有QQ在运行,并且有未读消息,如果给QQ托盘图标发送WM_MOUSEMOVE消息,会弹出消息盒子提示窗口,这也不太合理。

        2、实际上托盘图标区域对应一个ToolbarWindow32工具条窗口,每个托盘图标对应一个按钮,通过遍历找到指定程序的按钮ID,向这个按钮ID发送TB_HIDEBUTTON,使托盘图标消失,代码如下:

HWND hWnd,hWndPaper;

unsigned long lngPID;

    long ret,lngButtons;

HANDLE hProcess;

LPVOID lngAddress;

    long lngTextAdr,lngHwndAdr,lngHwnd,lngButtonID;

    char strBuff[1024]={0};

    char* str = NULL;

    char *pp = NULL;

hWnd = ::FindWindow( "Shell_TrayWnd", NULL );

hWnd = ::FindWindowEx( hWnd, 0, "TrayNotifyWnd", NULL );

hWndPaper = ::FindWindowEx( hWnd, 0, "SysPager", NULL );

    if( !hWndPaper )

hWnd = ::FindWindowEx( hWnd, 0, "ToolbarWindow32", NULL );

    else

hWnd = ::FindWindowEx( hWndPaper, 0, "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 );

    for( int i=0; i< lngButtons - 1; i++ )

{

ret = ::SendMessage( hWnd, TB_GETBUTTON, i, 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 );

USES_CONVERSION;

str = OLE2T( (LPOLESTR)( strBuff ) );

pp=strstr( str,"阿里旺旺" ); // 通过窗口名称来比对

            if(pp != NULL)

{

::SendMessage( hWnd, TB_HIDEBUTTON, lngButtonID, 1 );

}

}

}

VirtualFreeEx( hProcess, lngAddress, 0X4096, MEM_RELEASE );

CloseHandle( hProcess );

 

   上述方法比较负责,但相对合理很多,但是这种方法只对win2000,XP系统有效,对与Win7却无能为力,没有效果。

 

        3、对于Win7系统,程序的托盘图标最终是放置在和XP一样的ToolbarWindow32工具条窗口,但是有两个地方,同样是ToolbarWindow32工具条窗口,父窗口是不一样的。在右下角可见区域,ToolbarWindow32工具条窗口的父窗口是SysPager窗口,遍历方法同XP一样。对于掩藏的ToolbarWindow32工具条窗口,必须通过点击桌面右下角的可见区域左边的一个按钮,才会显示出来,其父窗口则是一个叫做托盘溢出的窗口NotifyIconOverflowWindow。所以除了这两个地方都要查找,查找后面的ToolbarWindow32工具条窗口,则只要直接查找NotifyIconOverflowWindow即可,代码如下:

    //获取托盘溢出区域窗口句柄  

    hWnd = ::FindWindow(_T("NotifyIconOverflowWindow"), NULL);  

    hWnd = ::FindWindowEx(hWnd, NULL, _T("ToolbarWindow32"), NULL);

 

        上述相关代码已通过实际测试,均能达到指定的效果。

本文标签: 托盘图标方法系统XP