vc 刷新系统托盘图标
/************************************************************************//* 该函数主要是实现对系统托盘的图标刷新的问题 */
/* */
/* 由于托盘程序如果被强行杀掉进程等操作后,在托盘中的图标不能自动删除 */
/* 顾运用以下的方法,实现模拟鼠标点击托盘图标,使得图标刷新 */
/* */
/* 使用方法: 直接调用 RefurbishTray 函数就可以了 */ */
/************************************************************************/
void CGreenSpanDlg::RefurbishTray()
{
RECT WindowRect ;
POINT point ;
int x ;
int y ;
// int SmallIconWidth , SmallIconHeight ;
// SmallIconWidth = GetSystemMetrics(SM_CXSMICON);
// SmallIconHeight = GetSystemMetrics(SM_CYSMICON);
HWND hwnd = GetSysTrayWnd() ;
::GetWindowRect(hwnd , &WindowRect ) ;
::GetCursorPos(&point) ;
for( x = 1 ; x < WindowRect.right - WindowRect.left - 1 ; x ++ )
{
for( y = 1 ; y < WindowRect.bottom - WindowRect.top - 1 ; y ++ )
{
SetCursorPos( WindowRect.left + x, WindowRect.top + y ) ;
Sleep(0);
}
}
// SetCursorPos(SmallIconWidth,SmallIconHeight);
}
/************************************************************************/
/* 该函数主要是获取系统托盘句柄 */
/* */
/* 这个方法只支持NT以上的版本,如果是NT 以下的版本,则需要根据
/* 任务栏窗口以及其子窗口结构 修改 */ */
/************************************************************************/
HWND CGreenSpanDlg::GetSysTrayWnd()
{
HWND hwnd ;
hwnd = ::FindWindow("Shell_TrayWnd", NULL ) ;
hwnd = ::FindWindowEx(hwnd, 0, "TrayNotifyWnd", NULL );
return hwnd ;
刷新系统托盘图标
type
TOSVersion = (osUnknown, os95, os98, osME, osNT3, osNT4, os2K, osXP, os2K3);
......
function GetOS: TOSVersion;
var
OS: TOSVersionInfo;
begin
ZeroMemory(@OS,SizeOf(OS));
OS.dwOSVersionInfoSize := SizeOf(OS);
GetVersionEx(OS);
Result := osUnknown;
if OS.dwPlatformId = VER_PLATFORM_WIN32_NT then
begin
case OS.dwMajorVersion of
3: Result := osNT3;
4: Result := osNT4;
5:
begin
case OS.dwMinorVersion of
0: Result:= os2K;
1: Result:= osXP;
2: Result:= os2K3;
end;
end;
end;
end
else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 0) then
Result := os95
else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 10) then
Result := os98
else if (OS.dwMajorVersion = 4) and (OS.dwMinorVersion = 90) then
Result := osME
end;
function GetSysTrayWnd(): HWND;
//返回系统托盘的句柄,适合于Windows各版本
var OS: TOSVersion;
begin
OS := GetOS;
Result := FindWindow('Shell_TrayWnd', nil);
Result := FindWindowEx(Result, 0, 'TrayNotifyWnd', nil);
if(OS in )then
Result := FindWindowEx(Result, 0, 'SysPager', nil);
if(OS in )then
Result := FindWindowEx(Result, 0, 'ToolbarWindow32', nil);
end;
procedure RefreshTrayIcon();
//刷新系统托盘图标
var
hwndTrayToolBar : HWND;
rTrayToolBar : tRect;
x , y : Word;
begin
hwndTrayToolBar := GetSysTrayWnd;
Windows.GetClientRect(hwndTrayToolBar, rTrayToolBar);
for x:=1 to rTrayToolBar.right-1 do
begin
for y:=1 to rTrayToolBar.bottom-1 do
begin
SendMessage(hwndTrayToolBar , WM_MOUSEMOVE, 0, MAKELPARAM(x,y));
end;
end;
end;
刷新系统托盘图标
procedure TForm1.RemoveDeadIcons();
var
hTrayWindow:HWND;
rctTrayIcon:TRECT;
nIconWidth,nIconHeight:integer;
CursorPos:TPoint;
nRow,nCol:Integer;
Begin
// Get tray window handle and boun* rectangle
hTrayWindow := FindWindowEx(FindWindow( 'Shell_TrayWnd ', nil), 0, 'TrayNotifyWnd ', nil);
if Not (GetWindowRect(hTrayWindow, rctTrayIcon)) then
Exit;
// Get small icon metrics
nIconWidth := GetSystemMetrics(SM_CXSMICON);
nIconHeight := GetSystemMetrics(SM_CYSMICON);
// Save current mouse position }
GetCursorPos(CursorPos);
// Sweep the mouse cursor over each icon in the tray in both dimensions
for nRow:=0 To ((rctTrayIcon.bottom-rctTrayIcon.top) div nIconHeight) Do
Begin
for nCol:=0 To ((rctTrayIcon.right-rctTrayIcon.left)div nIconWidth) Do
Begin
SetCursorPos(rctTrayIcon.left + nCol * nIconWidth + 5,
rctTrayIcon.top + nRow * nIconHeight + 5);
Sleep(0);
end;
end;
// Restore mouse position
SetCursorPos(CursorPos.x, CursorPos.x);
// Redraw tray window (to fix bug in multi-line tray area)
RedrawWindow(hTrayWindow, nil, 0, RDW_INVALIDATE Or RDW_ERASE Or RDW_UPDATENOW);
end;
页:
[1]