找回密码
 加入我们

QQ登录

只需一步,快速开始

搜索
查看: 6717|回复: 2

[开源] vc 刷新系统托盘图标

[复制链接]

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

发表于 2010-12-17 12:38:17 | 显示全部楼层 |阅读模式
/************************************************************************/
/*
该函数主要是实现对系统托盘的图标刷新的问题 */
/* */
/* 由于托盘程序如果被强行杀掉进程等操作后,在托盘中的图标不能自动删除 */
/* 顾运用以下的方法,实现模拟鼠标点击托盘图标,使得图标刷新 */
/* */
/* 使用方法: 直接调用 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 ;
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

 楼主| 发表于 2010-12-17 12:38:33 | 显示全部楼层
刷新系统托盘图标


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 [osXP, os2K3])then
Result := FindWindowEx(Result, 0, 'SysPager', nil);
if(OS in [os2K, osXP, os2K3])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;

【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755

1214

主题

352

回帖

11

精华

管理员

菜鸟

积分
93755

贡献奖关注奖人气王精英奖乐于助人勋章

 楼主| 发表于 2010-12-17 12:39:01 | 显示全部楼层
刷新系统托盘图标


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;
【VB】QQ群:1422505加的请打上VB好友
【易语言】QQ群:9531809  或 177048
【FOXPRO】QQ群:6580324  或 33659603
【C/C++/VC】QQ群:3777552
【NiceBasic】QQ群:3703755
您需要登录后才可以回帖 登录 | 加入我们

本版积分规则

快速回复 返回顶部 返回列表