Board logo

标题: [求助]懂delph的朋友能否帮个忙?帮忙编译成exe文件 谢谢 [打印本页]

作者: coolwei88     时间: 2008-11-4 22:12    标题: [求助]懂delph的朋友能否帮个忙?帮忙编译成exe文件 谢谢

懂delph的朋友能否帮个忙?帮忙编译成exe文件 谢谢


用Delphi瞬间消除无用托盘图标2008-10-05 11:52
用TerminateProcess把一个进程结束后有个问题,就是如果该程序在托盘有图标的话,这个图标并不会在它被结束时也消失。当然你用鼠标从上面移过可以解决这个问题,但本人在用自己的刷新辅助软件“疯狂刷新”的时候,每5分钟结束一次,一天下来如果不动电脑,就会在托盘区生成NNN个死图标,即使你拚命去点,也要点半天才能全部消除。有没有办法一下子搞定它呢?答案是肯定的,在网上搜了很久……

用Delphi编写几行代码搞定。

添加自定义类型,用作判断系统类型,
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 KillTrayIcons (Sender: TObject);
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;

//调用 KillTrayIcons 过程就可以瞬间清除所有无用的托盘图标了。
作者: coolwei88     时间: 2008-11-5 19:30
还有c++的一个方法:找出该图标所对应的进程ID,如果该ID为NULL则删除该图标.
下面是源代码(代码引用自网上,作了些修改):
#include <AFX.H>
#include <COMMCTRL.H>
#include <iostream.h>
int main()
{
HWND  hStatus=::FindWindow("Shell_TrayWnd",NULL);  //得到任务栏句柄
if  (hStatus==NULL)  
{  
cout<<"Get Shell_TrayWnd error!\n";  
return -1;  
}  
HWND  hNotify=FindWindowEx(hStatus,NULL,"TrayNotifyWnd",NULL); //右下角区域
if  (hNotify==NULL)  
{  
cout<<"Get TrayNotifyWnd error!\n";  
return -1;  
}  
HWND  hNotify1=FindWindowEx(hNotify,NULL,"SysPager",NULL);
if  (hNotify==NULL)  
{  
cout<<"Get SysPager error!\n";  
return -1;  
}
HWND  hNotify1_0=FindWindowEx(hNotify1,NULL,"ToolBarWindow32",NULL);//右下角区域(不包括时间)
if  (hNotify1_0==NULL)   
{  
cout<<"Get ToolBarWindow32 error!\n";  
return -1;  
}  
//-------------------以上是得到任务栏右下脚一块地方的句柄
DWORD  pid = 0;  
GetWindowThreadProcessId(hNotify1_0,&pid);  
if  (pid==NULL)  
{  
cout<<"Get pid error!\n";  
return -1;  
}   

HANDLE  hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_ALL_ACCESS,true,pid);  
if  (hProcess==NULL)  
{  
cout<<"Get hd error!\n";  
return -1;  
}  
::SendMessage(hNotify1_0,WM_PAINT ,NULL,NULL);
CRect rect;
::GetWindowRect(hNotify1_0,&rect);
::InvalidateRect(hNotify1_0,&rect,false);
int  iNum=::SendMessage(hNotify1_0,TB_BUTTONCOUNT ,NULL,NULL);  //获取任务栏上图标个数

unsigned long n = 0;  
TBBUTTON  *pButton = new TBBUTTON;  
CString  strInfo = _T("");  
wchar_t  name[256] = {0};  
TBBUTTON  BButton;
unsigned   long    whd,proid;
CString x;

for(int i=0; i<iNum; i++)  
{  
::SendMessage(hNotify1_0,TB_GETBUTTON,i,(LPARAM)(&BButton));  
ReadProcessMemory(hProcess,&BButton,pButton,sizeof(TBBUTTON),&n);   
if  (pButton->iString != 0xffffffff)  
{  
try  
{  
ReadProcessMemory(hProcess,(void *)pButton->iString,name,255,&n);
}  
catch(...)  
{  
}      
strInfo.Format("%d : %s\n",i+1,CString(name));
TRACE(strInfo);
cout<<strInfo<<endl;
}

try   
{   
whd=0;   
ReadProcessMemory(hProcess,(void   *)pButton->dwData,&whd,4,&n);   
}   
catch(...)   
{   
}   
proid=NULL;   
GetWindowThreadProcessId((HWND)whd,&proid);   
if(proid==NULL)
::SendMessage(hNotify1_0,TB_DELETEBUTTON,i,0);
}
delete pButton;
return 0;
}