通过找出该图标所相应的进程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;
}