www.gusucode.com > vc++编程实现清除无用的托盘图标源码程序 > vc++编程实现清除无用的托盘图标源码程序\code\DeleteTrayNullIcon.cpp

    // DeleteTrayNullIcon.cpp : Defines the class behaviors for the application.
//

#include "stdafx.h"
#include "DeleteTrayNullIcon.h"
#include "DeleteTrayNullIconDlg.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDeleteTrayNullIconApp

BEGIN_MESSAGE_MAP(CDeleteTrayNullIconApp, CWinApp)
	//{{AFX_MSG_MAP(CDeleteTrayNullIconApp)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG
	ON_COMMAND(ID_HELP, CWinApp::OnHelp)
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDeleteTrayNullIconApp construction

CDeleteTrayNullIconApp::CDeleteTrayNullIconApp()
{
	// TODO: add construction code here,
	// Place all significant initialization in InitInstance
}

/////////////////////////////////////////////////////////////////////////////
// The one and only CDeleteTrayNullIconApp object
// Download by http://www.NewXing.com
CDeleteTrayNullIconApp theApp;

/////////////////////////////////////////////////////////////////////////////
// CDeleteTrayNullIconApp initialization

BOOL CDeleteTrayNullIconApp::InitInstance()
{
	AfxEnableControlContainer();

	// Standard initialization
	// If you are not using these features and wish to reduce the size
	//  of your final executable, you should remove from the following
	//  the specific initialization routines you do not need.

#ifdef _AFXDLL
	Enable3dControls();			// Call this when using MFC in a shared DLL
#else
	Enable3dControlsStatic();	// Call this when linking to MFC statically
#endif

	BOOL bNullBeDel = FALSE;
	do
	{
		Handle_DeleteTrayNullIcon ( bNullBeDel );
	} while ( bNullBeDel );
	AfxMessageBox ( "清除无效的托盘图标已经完成", MB_ICONINFORMATION );

	CDeleteTrayNullIconDlg dlg;
	m_pMainWnd = &dlg;
	int nResponse = dlg.DoModal();
	if (nResponse == IDOK)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with OK
	}
	else if (nResponse == IDCANCEL)
	{
		// TODO: Place code here to handle when the dialog is
		//  dismissed with Cancel
	}

	// Since the dialog has been closed, return FALSE so that we exit the
	//  application, rather than start the application's message pump.
	return FALSE;
}

int CDeleteTrayNullIconApp::Handle_DeleteTrayNullIcon( BOOL &bNullBeDel )
{
	bNullBeDel = FALSE;
	HWND  hStatus=::FindWindow("Shell_TrayWnd",NULL);  //得到任务栏句柄
	if  (hStatus==NULL)  
	{  
		AfxMessageBox ( "Get Shell_TrayWnd error!" );
		return -1;  
	}  
	HWND  hNotify=FindWindowEx(hStatus,NULL,"TrayNotifyWnd",NULL); //右下角区域 
	if  (hNotify==NULL)
	{  
		AfxMessageBox ( "Get TrayNotifyWnd error!" ); 
		return -1;  
	}  
	HWND  hNotify1=FindWindowEx(hNotify,NULL,"SysPager",NULL);
	if  (hNotify==NULL)  
	{  
		AfxMessageBox ( "Get SysPager error!" ); 
		return -1;  
	} 
	HWND  hNotify1_0=FindWindowEx(hNotify1,NULL,"ToolBarWindow32",NULL);//右下角区域(不包括时间)
	if  (hNotify1_0==NULL)   
	{  
		AfxMessageBox ( "Get ToolBarWindow32 error!" ); 
		return -1;  
	}  
	//-------------------以上是得到任务栏右下脚一块地方的句柄
	DWORD  pid = 0;  
	GetWindowThreadProcessId(hNotify1_0,&pid);  
	if  (pid==NULL)  
	{  
		AfxMessageBox ( "Get pid error!" ); 
		return -1;  
	}   
	
	HANDLE  hProcess=OpenProcess(PROCESS_QUERY_INFORMATION|PROCESS_ALL_ACCESS,true,pid);  
	if  (hProcess==NULL)  
	{  
		AfxMessageBox ( "Get hd error!" ); 
		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);
		}
		
		try
		{   
			whd=0;   
			ReadProcessMemory(hProcess,(void   *)pButton->dwData,&whd,4,&n);   
		}   
		catch(...)
		{   
		}   
		proid=NULL;
		GetWindowThreadProcessId((HWND)whd,&proid);   
		if(proid==NULL)
		{
			bNullBeDel = TRUE;
			::SendMessage(hNotify1_0,TB_DELETEBUTTON,i,0);
		}
	} 
	delete pButton;

	return 0;
}