www.pudn.com > VCShell_Samples.rar > Shell.cpp


///////////////////////////////////////////////////////////// 
//    
//  Shell Console module 
//  
 
#define INC_OLE2 
#define WIN32_LEAN_AND_MEAN 
#define STRICT 
 
#include  
#include  
#include  
#include  
#include  
#include "taskbar.h" 
 
#include "resource.h" 
 
 
// data 
static WNDPROC g_pfnOldProc; 
static HWND g_hwndButton=NULL; 
static HWND g_hDlg=NULL; 
static HINSTANCE g_hInstance; 
static HICON g_hIconLarge; 
static HICON g_hIconSmall; 
 
 
// functions 
static VOID OnRunPrograms( VOID ); 
static VOID OnFindFiles( VOID ); 
static VOID OnMinimizeAll( VOID ); 
static VOID OnUndoMinimize( VOID ); 
static VOID OnTaskbarProperties( VOID ); 
static VOID OnAddTab( HWND ); 
static VOID OnDeleteTab( VOID ); 
static VOID OnInitDialog( HWND ); 
static VOID OnButtonActivation( VOID ); 
 
// callbacks 
LRESULT CALLBACK APP_DlgProc( HWND, UINT, WPARAM, LPARAM ); 
LRESULT CALLBACK ButtonProc( HWND, UINT, WPARAM, LPARAM ); 
 
 
 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure....: WinMain() 
/*-----------------------------------------------------------*/ 
INT APIENTRY WinMain( HINSTANCE hInstance, HINSTANCE hPrevious,  
	LPSTR lpsz, INT iCmd ) 
{ 
	BOOL b; 
 
	g_hIconLarge = (HICON) LoadImage( hInstance,  
		"APP_ICON", IMAGE_ICON,  
		GetSystemMetrics(SM_CXICON),  
		GetSystemMetrics(SM_CXICON), 0 ); 
	g_hIconSmall = (HICON) LoadImage( hInstance,  
		"APP_ICON", IMAGE_ICON,  
		GetSystemMetrics(SM_CXSMICON),  
		GetSystemMetrics(SM_CXSMICON), 0 ); 
 
	// initialize OLE libraries 
	CoInitialize(NULL); 
	InitCommonControls(); 
 
	// run main dialog 
	g_hInstance = hInstance; 
	b = DialogBox( hInstance, "DLG_MAIN", NULL,  
		(DLGPROC)APP_DlgProc ); 
 
	// exit 
	DestroyIcon( g_hIconLarge ); 
	DestroyIcon( g_hIconSmall ); 
 
	// free the objects used by ITaskbarList 
	DestroyWindow( g_hwndButton ); 
	OnDeleteTab(); 
 
	CoUninitialize(); 
	return b; 
} 
 
 
 
 
 
/*------------------------------------------------------------*/ 
// Procedure....: APP_DlgProc() 
/*------------------------------------------------------------*/ 
LRESULT CALLBACK APP_DlgProc( HWND hDlg, UINT uiMsg, WPARAM wParam, LPARAM lParam ) 
{ 
 
   switch( uiMsg ) 
   { 
		case WM_INITDIALOG: 
			OnInitDialog( hDlg ); 
			break; 
 
		 case WM_COMMAND: 
			switch( wParam )		     
			{ 
				 case IDC_FINDFILES: 
					OnFindFiles(); 
					return 1; 
 
				 case IDC_RUNPROGRAMS: 
					OnRunPrograms(); 
					return 1; 
 
				 case IDC_MINIMIZE: 
					OnMinimizeAll(); 
					return 1; 
 
				 case IDC_UNDOMINIMIZE: 
					OnUndoMinimize(); 
					return 1; 
 
				 case IDC_PROPERTIES: 
					OnTaskbarProperties(); 
					return 1; 
 
				 case IDC_ADDTAB: 
					OnAddTab( hDlg ); 
					return 1; 
 
				 case IDC_DELETETAB: 
					OnDeleteTab(); 
					return 1; 
 
				 case IDCANCEL: 
					EndDialog( hDlg, FALSE ); 
					return 0; 
			 } 
			 break; 
	} 
 
	return 0; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnFindFiles() 
/*-----------------------------------------------------------*/ 
VOID OnFindFiles( VOID ) 
{ 
	HRESULT sc; 
	IShellDispatch *pShellDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,  
		IID_IDispatch, (LPVOID *) &pShellDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pShellDisp->FindFiles();   
	pShellDisp->Release(); 
 
	return; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnTaskbarProperties() 
/*-----------------------------------------------------------*/ 
VOID OnTaskbarProperties( VOID ) 
{ 
	HRESULT sc; 
	IShellDispatch *pShellDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,  
		IID_IDispatch, (LPVOID *) &pShellDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pShellDisp->TrayProperties();   
	pShellDisp->Release(); 
 
	return; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnRunPrograms() 
/*-----------------------------------------------------------*/ 
VOID OnRunPrograms( VOID ) 
{ 
	HRESULT sc; 
	IShellDispatch *pShellDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,  
		IID_IShellDispatch, (LPVOID *) &pShellDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pShellDisp->FileRun();   
	pShellDisp->Release(); 
 
	return; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnMinimizeAll() 
/*-----------------------------------------------------------*/ 
VOID OnMinimizeAll( VOID ) 
{ 
    HRESULT sc; 
	IShellDispatch *pShellDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,  
		IID_IDispatch, (LPVOID *) &pShellDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pShellDisp->MinimizeAll();   
	pShellDisp->Release(); 
 
	return; 
} 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnUndoMinimize() 
/*-----------------------------------------------------------*/ 
VOID OnUndoMinimize( VOID ) 
{ 
	HRESULT sc; 
	IShellDispatch *pShellDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_Shell, NULL, CLSCTX_SERVER,  
		IID_IDispatch, (LPVOID *) &pShellDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pShellDisp->UndoMinimizeALL();   
	pShellDisp->Release(); 
 
	return; 
} 
 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnInitDialog() 
/*-----------------------------------------------------------*/ 
VOID OnInitDialog( HWND hDlg ) 
{ 
	// set the icons (T/F as to Large/Small icon) 
	g_hDlg = hDlg; 
	SendMessage( hDlg, WM_SETICON, FALSE,  
		(LPARAM)g_hIconSmall ); 
	SendMessage( hDlg, WM_SETICON, TRUE,  
		(LPARAM)g_hIconLarge ); 
} 
 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnAddTab() 
/*-----------------------------------------------------------*/ 
VOID OnAddTab( HWND hWnd ) 
{ 
	static BOOL g_bFirstTime=TRUE; 
	HRESULT sc; 
	ITaskbarList *pDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_SERVER,  
		IID_ITaskbarList, (LPVOID *) &pDisp ); 
	if( FAILED(sc) ) 
		return; 
	 
	// call the first time only 
	if( g_bFirstTime ) 
	{ 
		g_bFirstTime = FALSE; 
		pDisp->HrInit(); 
 
		// create a new button window 
		g_hwndButton = CreateWindow( "button", "My Button",  
			WS_CLIPSIBLINGS|BS_PUSHBUTTON, 
			0, 0, 58, 14, hWnd, NULL, g_hInstance, NULL );  
		g_pfnOldProc = (WNDPROC) SubclassWindow( g_hwndButton, ButtonProc ); 
	} 
 
	pDisp->AddTab( g_hwndButton );   
	pDisp->Release(); 
 
	return; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnDeleteTab() 
/*-----------------------------------------------------------*/ 
VOID OnDeleteTab( VOID ) 
{ 
	HRESULT sc; 
	ITaskbarList *pDisp = NULL; 
 
    sc = CoCreateInstance( CLSID_TaskbarList, NULL, CLSCTX_SERVER,  
		IID_ITaskbarList, (LPVOID *) &pDisp ); 
	if( FAILED(sc) ) 
		return; 
 
	pDisp->DeleteTab( g_hwndButton );  
	pDisp->Release(); 
 
	 
	return; 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: ButtonProc() 
/*-----------------------------------------------------------*/ 
LRESULT CALLBACK ButtonProc( HWND hwnd, UINT uiMsg,  
		WPARAM wParam, LPARAM lParam ) 
{ 
   switch( uiMsg ) 
   { 
		case WM_NCACTIVATE: 
			if( wParam==TRUE ) 
				OnButtonActivation(); 
			break; 
   } 
   return CallWindowProc( g_pfnOldProc, hwnd, uiMsg,  
	   wParam, lParam ); 
} 
 
 
 
/*-----------------------------------------------------------*/ 
// Procedure...: OnButtonActivation() 
/*-----------------------------------------------------------*/ 
VOID OnButtonActivation(  VOID ) 
{ 
	HMENU hmenu; 
	RECT r; 
	LONG x, y; 
 
	// get some window handles 
	HWND h0=FindWindow("Shell_TrayWnd", NULL ); 
	HWND h1=FindWindowEx(h0,NULL,"RebarWindow32", NULL); 
	HWND h2=FindWindowEx(h1,NULL,"MSTaskSwWClass", NULL); 
	HWND h3=FindWindowEx(h2,NULL,"SysTabControl32", NULL); 
	GetWindowRect( h3, &r );		 
 
	// get the currently selected button and  
	// create a new popup menu	 
	hmenu = CreatePopupMenu(); 
	INT i=TabCtrl_GetCurSel( h3 ); 
	if( i==-1 ) 
	{ 
		AppendMenu( hmenu, MF_STRING, IDC_DELETETAB,  
			"&Close" ); 
	} 
	else 
	{ 
		AppendMenu( hmenu, MF_STRING, IDC_MINIMIZE,  
			"&Minimize All" ); 
		AppendMenu( hmenu, MF_STRING, IDC_UNDOMINIMIZE,  
			"&Undo Minimize All" ); 
		AppendMenu( hmenu, MF_SEPARATOR, 0, NULL ); 
		AppendMenu( hmenu, MF_STRING, IDC_PROPERTIES,  
			"&Taskbar Properties" ); 
	} 
 
	// set and immediately reset its size to get  
	// the current width and height 
	LONG l = TabCtrl_SetItemSize( h3, 0, 0 ); 
	TabCtrl_SetItemSize( h3, LOWORD(l), HIWORD(l) ); 
 
    // have the menu to appear just above the button 
	if( i==-1 ) 
	{ 
		POINT pt; 
		GetCursorPos( &pt ); 
		x = pt.x; 
		y = pt.y; 
	} 
	else 
	{ 
		x = r.left + LOWORD(l)*i+3; 
		y = GetSystemMetrics(SM_CYSCREEN)-(HIWORD(l)+1); 
	} 
	TrackPopupMenu( hmenu, TPM_BOTTOMALIGN, x, y, 0, g_hDlg,  0); 
	DestroyMenu( hmenu ); 
	return; 
} 
 
 
 
 
/* 	End of file: Shell.cpp  */