清除无效的托盘
int
CDeleteTrayNullIconDlg::DeleteTrayNullIcon(BOOL
&
bNullBeDel)
![](https://i-blog.csdnimg.cn/blog_migrate/a41954a27d6ad96fa2c2cf816e677448.gif)
...
{
HWND hNotify1;
int iMethod=0;
bNullBeDel = FALSE;
HWND hStatus=::FindWindow("Shell_TrayWnd",NULL); //得到任务栏句柄
if (hStatus==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get Shell_TrayWnd error!" );
return -1;
}
HWND hNotify=FindWindowEx(hStatus,NULL,"TrayNotifyWnd",NULL); //右下角区域
if (hNotify==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get TrayNotifyWnd error!" );
return -1;
}
goto1:
if (iMethod<1)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
hNotify1=FindWindowEx(hNotify,NULL,"SysPager",NULL);
if (hNotify==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get SysPager error!" );
return -1;
}
if (hNotify1==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
hNotify1=hNotify;
}
}
else
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
hNotify1=hNotify;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
HWND hNotify1_0=FindWindowEx(hNotify1,NULL,"ToolBarWindow32",NULL);//右下角区域(不包括时间)
if (hNotify1_0==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get ToolBarWindow32 error!" );
return -1;
}
//-------------------以上是得到任务栏右下脚一块地方的句柄
DWORD pid = 0;
GetWindowThreadProcessId(hNotify1_0,&pid);
if (pid==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get pid error!" );
return -1;
}
HANDLE hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_ALL_ACCESS,true,pid);
if (hProcess==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
//AfxMessageBox ( "Get hd error!" );
return -1;
}
::SendMessage(hNotify1_0,WM_PAINT ,NULL,NULL);
CRect rect;
::GetWindowRect(hNotify1_0,&rect);
::InvalidateRect(hNotify1_0,&rect,FALSE);
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
int iNum=::SendMessage(hNotify1_0,TB_BUTTONCOUNT ,NULL,NULL); //获取任务栏上图标个数
if (iNum<1 && iMethod==0)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
iMethod=2;
goto goto1;
}
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
unsigned long n = 0;
TBBUTTON *pButton = new TBBUTTON;
CString strInfo = _T("");
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
wchar_t name[256] = ...{0};
TBBUTTON BButton;
unsigned long whd,proid;
CString x;
for(int i=0; i<iNum; i++)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
::SendMessage(hNotify1_0,TB_GETBUTTON,i,(LPARAM)(&BButton));
ReadProcessMemory(hProcess,&BButton,pButton,sizeof(TBBUTTON),&n);
if (pButton->iString != 0xffffffff)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
ReadProcessMemory(hProcess,(void *)pButton->iString,name,255,&n);
}
catch(...)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
strInfo.Format("%d : %s ",i+1,CString(name));
TRACE(strInfo);
}
try
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
whd=0;
ReadProcessMemory(hProcess,(void *)pButton->dwData,&whd,4,&n);
}
catch(...)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
}
proid=NULL;
GetWindowThreadProcessId((HWND)whd,&proid);
if(proid==NULL)
![](https://i-blog.csdnimg.cn/blog_migrate/37c8bf68cdc3cc81759c34160776bc53.gif)
...{
bNullBeDel = TRUE;
::SendMessage(hNotify1_0,TB_DELETEBUTTON,i,0);
}
}
delete pButton;
![](https://i-blog.csdnimg.cn/blog_migrate/6a9c071a08f1dae2d3e1c512000eef41.gif)
return 0;
}
原作者谢红伟,上代码我作了几个地方修改与优化
#define WM_NOTIFYICON WM_USER+5
void CDeleteTrayNullIconDlg::OnButton1()
{
BOOL bNullBeDel = FALSE;
do
{
DeleteTrayNullIcon ( bNullBeDel );
} while ( bNullBeDel );
NOTIFYICONDATA ni;
ni.cbSize=sizeof(ni);
ni.hIcon=m_hIcon;
//ni.hWnd=m_pMainWnd->m_hWnd;
//strcpy(nd.szTip, "我的托盘");
ni.uCallbackMessage=WM_NOTIFYICON;
ni.uFlags= NIF_ICON | NIF_MESSAGE | NIF_TIP ;
ni.uID=IDR_MAINFRAME;
Shell_NotifyIcon(NIM_ADD, &ni);
Shell_NotifyIcon(NIM_DELETE, &ni);
AfxMessageBox ( "清除无效的托盘图标已经完成", MB_ICONINFORMATION );
}