www.pudn.com > ShortCutEx.rar > CSCToolBar.cpp, change:2004-08-11,size:8721b


//CSCToolBar.cpp : Implementation of CCSCToolBar 
 
//***************************************************************************// 
//                                                                           // 
//  This file was created using the CWindowImpl ATL Object Wizard            // 
//  By Erik Thompson  2000                                                  // 
//	Version 1.1                                                              // 
//  Email questions and comments to ErikT@RadBytes.com                       // 
//                                                                           // 
//***************************************************************************// 
 
#include "stdafx.h" 
#include "CSCToolBar.h" 
#include <Shellapi.h> 
#include <stdio.h> 
#include "resource.h" 
 
int GetShortName(LPCSTR lpcOrg, LPSTR lpRet) 
{ 
//	char strDriver[10]; 
//	char strPath[256]; 
	char strFileName[256] = {0}; 
	char strExt[128] = {0}; 
	_splitpath(lpcOrg,NULL,NULL,strFileName,strExt); 
	strcpy(lpRet,strFileName); 
	strcat(lpRet,strExt); 
	return 0; 
} 
 
///////////////////////////////////////////////////////////////////////////// 
// CCSCToolBar 
CCSCToolBar::CCSCToolBar() 
{ 
	m_dwCommandIndex = 0; 
} 
 
CCSCToolBar::~CCSCToolBar() 
{ 
	ImageList_Destroy(m_hImageList); 
	if (IsWindow()) 
		DestroyWindow(); 
} 
 
LRESULT CCSCToolBar::OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
{ 
	SendMessage(m_hWnd, TB_SETEXTENDEDSTYLE, 0, (LPARAM)TBSTYLE_EX_MIXEDBUTTONS); 
	// Sets the size of the TBBUTTON structure. 
	SendMessage(m_hWnd, TB_BUTTONSTRUCTSIZE, sizeof(TBBUTTON), 0); 
	// Set the maximum number of text rows and bitmap size. 
	SendMessage(m_hWnd, TB_SETMAXTEXTROWS, 1, 0L); 
 
	DWORD dwStyle = SendMessage(m_hWnd,TB_GETEXTENDEDSTYLE ,0,0); 
	dwStyle |=  TBSTYLE_EX_DRAWDDARROWS;  
	SendMessage(m_hWnd,TB_SETEXTENDEDSTYLE ,0,dwStyle); 
 
	m_hImageList = ImageList_Create(20,20, ILC_MASK | ILC_COLOR32 , 0, 16); 
	 
	::SendMessage(m_hWnd, TB_SETIMAGELIST, 0, (LPARAM)m_hImageList); 
 
	m_pDropTarget = new CDropTargetToolBar(m_hWnd); 
 
	if(m_pDropTarget == NULL) 
		return false; 
	m_pDropTarget->AddRef(); 
	 
	if(FAILED(RegisterDragDrop(m_hWnd,m_pDropTarget))) //calls addref 
	{ 
		m_pDropTarget = NULL; 
		return false; 
	} 
	else 
		m_pDropTarget->Release(); //i decided to AddRef explicitly after new 
 
	m_pDropTarget->SetToolBar(this); 
	 
	FORMATETC ftetc={0}; 
	ftetc.dwAspect = DVASPECT_CONTENT; 
	ftetc.lindex = -1; 
	ftetc.tymed = TYMED_HGLOBAL; 
	ftetc.cfFormat=CF_HDROP; 
	m_pDropTarget->AddSuportedFormat(ftetc);	 
	 
	m_Items.RemoveAll(); 
 
	AddFile("DELETE",FALSE,"Delete"); 
	 
	AddFile(NULL,0); 
 
	LoadSetting(); 
 
	return TRUE; 
 
} 
 
LRESULT CCSCToolBar::OnSize(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
{ 
	return TRUE; 
} 
 
LRESULT CCSCToolBar::OnCommand(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
{ 
	int nID = LOWORD(wParam); 
 
	for( int i=0;i< m_Items.GetSize();i++ ) 
	{ 
		if( m_Items[i].nCommandIndex == nID ) 
		{ 
			if( m_Items[i].nTrashButton == 1) 
			{ 
 
			} 
			else 
			{ 
				HWND hParant,hBak = NULL; 
				hParant = ::GetParent(m_hWnd); 
				while(hParant) 
				{ 
					hBak = hParant; 
					hParant = ::GetParent(hParant); 
				} 
 
				TCHAR szPath[MAX_PATH]; 
				if( hBak == NULL) 
				{ 
					GetCurrentDirectory(MAX_PATH,szPath);				 
				} 
				else 
				{ 
					::GetWindowText(hBak,szPath,MAX_PATH-1); 
				} 
 
				::ShellExecute(m_hWnd,TEXT("open"),m_Items[nID].chFilePath,NULL,szPath,SW_SHOW); 
			} 
			bHandled = TRUE; 
			return TRUE; 
		} 
		else if(m_Items[i].nCommandIndex == nID - 2000 ) 
		{ 
			SendMessage(TB_DELETEBUTTON, 
				SendMessage(TB_COMMANDTOINDEX,m_Items[i].nCommandIndex,0),0); 
			m_Items.RemoveAt(i); 
			SaveSetting(); 
		} 
	} 
 
 
	return TRUE; 
} 
 
 
int	CCSCToolBar::AddFile(LPCSTR lpcFile, BOOL bSave, LPCSTR lpcDiscription) 
{ 
	ToolBarItem Item; 
 
	if( lpcFile == NULL ) 
	{ 
		Item.nCommandIndex  = m_dwCommandIndex++; 
		Item.nTrashButton = 1; 
		TBBUTTON Button; 
		ZeroMemory((void*)&Button, sizeof(TBBUTTON)); 
		Button.idCommand = Item.nCommandIndex ; 
		Button.fsState = TBSTATE_ENABLED; 
		Button.fsStyle = BTNS_SEP;//BTNS_BUTTON | BTNS_AUTOSIZE ;//| BTNS_SHOWTEXT; 
		Button.dwData = 0; 
		Button.iString = 0; 
		Button.iBitmap = 0;//Item.nImageIndex; 
		::SendMessage(m_hWnd, TB_INSERTBUTTON, -1, (LPARAM)&Button); 
		m_Items.Add(Item); 
		return 0; 
	} 
 
	strncpy(Item.chFilePath,lpcFile, MAX_PATH -1); 
	Item.nCommandIndex = m_dwCommandIndex ++; 
 
	SHFILEINFO  sfi = {0}; 
	SHGetFileInfo(lpcFile,0,&sfi,sizeof(sfi), SHGFI_ICON );//| SHGFI_SMALLICON ); 
 
	if( sfi.hIcon ) 
	{ 
		Item.nImageIndex = ImageList_AddIcon(m_hImageList, sfi.hIcon); 
//		Item.hBitmap = sfi.hIcon; 
		DestroyIcon(sfi.hIcon); 
	} 
	else 
	{ 
		if( strcmp(lpcFile,"DELETE") == 0 ) 
		{ 
			HICON hDelIcon = LoadIcon(_Module.m_hInstResource,(LPCSTR)(IDI_ICON1 )); 
			if( hDelIcon != NULL ) 
			{ 
				Item.nImageIndex = ImageList_AddIcon(m_hImageList, hDelIcon); 
				DestroyIcon(hDelIcon); 
			} 
		} 
	} 
 
	int iIndex ; 
	if( lpcDiscription != NULL ) 
	{ 
		iIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, 0,(LPARAM)lpcDiscription); 
		Item.nTrashButton = 1; 
	} 
	else 
	{ 
		iIndex = ::SendMessage(m_hWnd, TB_ADDSTRING, 0,(LPARAM)lpcFile); 
	} 
	m_Items.Add(Item); 
 
	TBBUTTON Button; 
	ZeroMemory((void*)&Button, sizeof(TBBUTTON)); 
	Button.idCommand = Item.nCommandIndex ; 
	Button.fsState = TBSTATE_ENABLED; 
	if( lpcDiscription != NULL ) 
		Button.fsStyle = BTNS_WHOLEDROPDOWN ; 
	else 
		Button.fsStyle = BTNS_BUTTON | BTNS_AUTOSIZE ;//| BTNS_SHOWTEXT; 
	Button.dwData = 0; 
	Button.iString = iIndex; 
	Button.iBitmap = Item.nImageIndex; 
	::SendMessage(m_hWnd, TB_INSERTBUTTON, -1, (LPARAM)&Button); 
 
	if( bSave ) 
		SaveSetting(); 
 
	return 0; 
} 
 
int CCSCToolBar::LoadSetting() 
{ 
	TCHAR scFile[MAX_PATH + 10]; 
 
	GetModuleFileName(_Module.m_hInst,scFile, MAX_PATH); 
	strcat(scFile,".ini"); 
 
	int nCount = GetPrivateProfileInt( TEXT("Setting"),TEXT("Count"),0,scFile); 
	if(nCount > 100 ) 
		nCount = 100; 
 
	TCHAR szPath[MAX_PATH]; 
	TCHAR szSection[MAX_PATH]; 
 
	for( int i=0;i < nCount ;i++ ) 
	{ 
		sprintf(szSection,"Path%d",i+1); 
		memset(szPath, 0, MAX_PATH); 
		GetPrivateProfileString( TEXT("Setting"),szSection,"",szPath,MAX_PATH,scFile); 
 
		if( strlen(szPath) != 0) 
		{ 
			AddFile(szPath,	FALSE); 
		} 
	} 
	 
	return 0; 
} 
 
int CCSCToolBar::SaveSetting() 
{ 
	TCHAR scFile[MAX_PATH + 10]; 
 
	GetModuleFileName(_Module.m_hInst,scFile, MAX_PATH); 
	strcat(scFile,".ini"); 
 
	int nCount = m_Items.GetSize() -2 ; 
 
	if(nCount > 100 ) 
		nCount = 100; 
 
	TCHAR szSection[MAX_PATH]; 
	sprintf(szSection,"%d",nCount); 
 
	WritePrivateProfileString( TEXT("Setting"),TEXT("Count"),szSection,scFile); 
 
	TCHAR szPath[MAX_PATH]; 
 
	for( int i=0;i < nCount ;i++ ) 
	{ 
		sprintf(szSection,"Path%d",i+1); 
		memset(szPath, 0, MAX_PATH); 
		WritePrivateProfileString( TEXT("Setting"),szSection,m_Items[i+2].chFilePath,scFile); 
 
	} 
	return 0; 
} 
 
LRESULT CCSCToolBar::OnNotify(UINT uMsg, WPARAM wParam, LPARAM lParam, BOOL& bHandled) 
{ 
#define lpnm   ((LPNMHDR)lParam) 
#define lpnmTB ((LPNMTOOLBAR)lParam) 
	 
	HMENU hPopupMenu = NULL; 
	RECT      rc; 
 
	switch(lpnm->code){ 
	case TBN_DROPDOWN: 
		SendMessage(lpnmTB->hdr.hwndFrom, TB_GETRECT, 
			lpnmTB->iItem, (LPARAM)&rc); 
		 
		::MapWindowPoints(lpnmTB->hdr.hwndFrom, 
			HWND_DESKTOP, (LPPOINT)&rc, 2); 
 
		hPopupMenu = CreatePopupMenu(); 
		 
		int nAdded = FALSE; 
		int nItemCount = 0; 
 
		for( int i=0;i< m_Items.GetSize();i++ ) 
		{ 
			if( m_Items[i].nTrashButton ) 
				continue; 
			if( m_Items[i].nCommandIndex > 999 ) 
				continue; 
			 
			MENUITEMINFO mnuInfo = {0}; 
			mnuInfo.cbSize = sizeof(MENUITEMINFO); 
			mnuInfo.fMask =   MIIM_TYPE | MIIM_STATE| MIIM_ID ; 
			mnuInfo.wID = m_Items[i].nCommandIndex + 2000; 
			mnuInfo.fState = MFS_ENABLED; 
//			mnuInfo.hbmpItem =(HBITMAP) m_Items[i].hBitmap; 
			mnuInfo.fType = MFT_STRING; 
			char path[MAX_PATH]; 
//			strcpy(path,m_Items[i].chFilePath); 
			GetShortName(m_Items[i].chFilePath,path); 
			mnuInfo.dwTypeData = path; 
//			mnuInfo.cch = strlen(m_Items[i].chFilePath); 
			InsertMenuItem(hPopupMenu,nItemCount++,TRUE,&mnuInfo); 
			nAdded = TRUE; 
		} 
 
		if( nAdded == TRUE ) 
		{ 
			TPMPARAMS tpm; 
			tpm.cbSize = sizeof(TPMPARAMS); 
			tpm.rcExclude = rc; 
			 
			TrackPopupMenuEx(hPopupMenu, 
				TPM_LEFTALIGN|TPM_LEFTBUTTON|TPM_VERTICAL, 
				rc.left, rc.bottom, m_hWnd, &tpm); 
			DestroyMenu(hPopupMenu); 
		} 
//		bHandled = TRUE; 
		return FALSE; 
//	case TBN_GETBUTTONINFO: 
//		lpnmTB-> 
//		return FALSE; 
	} 
	bHandled = FALSE; 
	return FALSE; 
}