Click here to Skip to main content
15,892,839 members
Articles / Desktop Programming / MFC

XDriveBar - Display drive buttons in a fixed toolbar

Rate me:
Please Sign up or sign in to vote.
4.70/5 (17 votes)
11 Aug 2003CPOL3 min read 111.2K   2K   58  
XDriveBar implements a drive bar for the drives in a system. The demo project shows how to keep the drive bar in sync by handling the WM_DEVICECHANGE message.
// XDriveBar.cpp  Version 1.0
//
// Author:  Hans Dietrich
//          hdietrich2@hotmail.com
//
// Description:
//     XDriveBar.cpp implements CXDriveBar, a class for displaying drives
//     in a fixed toolbar.
//
// History
//     Version 1.0 - 2003 August 11
//     - Initial public release
//
// This software is released into the public domain.  You are free to use it
// in any way you like.
//
// This software is provided "as is" with no expressed or implied warranty.
// I accept no liability for any damage or loss of business that this software
// may cause.
//
///////////////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "XDriveBar.h"
#include "SystemImageList.h"
#include <afxpriv.h>
#include "resource.h"

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

#define ID_BASE 1000
#define ID_MAX	(ID_BASE+26)

// for right-click menu
#define ID_EXPLORE                      32771
#define ID_SEARCH                       32772
#define ID_PROPERTIES                   32773

// The one and only system image list instance
CSystemImageList theSystemImageList;

UINT WM_DRIVE_SELECTED = ::RegisterWindowMessage((LPTSTR) _T("WM_DRIVE_SELECTED"));

///////////////////////////////////////////////////////////////////////////////
// CXDriveBar

BEGIN_MESSAGE_MAP(CXDriveBar, CXToolBar)
	//{{AFX_MSG_MAP(CXDriveBar)
	ON_WM_RBUTTONUP()
	//}}AFX_MSG_MAP
	ON_COMMAND_RANGE(ID_BASE, ID_MAX, OnButton)
END_MESSAGE_MAP()

///////////////////////////////////////////////////////////////////////////////
// ctor
CXDriveBar::CXDriveBar()
{
	m_pTheme   = NULL;
	m_pFont    = NULL;
	m_strDrive = _T("");
}

///////////////////////////////////////////////////////////////////////////////
// dtor
CXDriveBar::~CXDriveBar()
{
}

///////////////////////////////////////////////////////////////////////////////
// OnButton
void CXDriveBar::OnButton(UINT nID)
{
	if (nID >= ID_BASE && nID <= (ID_BASE+ID_MAX))
	{
		// left click

		GetParent()->SendMessage(WM_DRIVE_SELECTED, 
			(WPARAM)(LPCTSTR)GetButtonText(nID-ID_BASE));
	}
	else
	{
		// possibly right-click

		CString strVerb = _T("");

		switch (nID)
		{
			case ID_EXPLORE:
				strVerb = _T("explore");
				break;

			case ID_SEARCH:
				strVerb = _T("find");
				break;

			case ID_PROPERTIES:
				strVerb = _T("properties");
				break;
		}

		if (!strVerb.IsEmpty())
		{
			// execute verb on selected drive
			CString strDrive = m_strDrive;

			if (!strDrive.IsEmpty())
			{
				strDrive += _T(":\\");

				if (strVerb == _T("explore"))
				{
					// explore verb doesn't work on Win98, start Explorer
					CString str = _T("Explorer /e,");
					str += strDrive;

					STARTUPINFO si;
					ZeroMemory(&si, sizeof(si));
					si.cb = sizeof(si);
					si.dwFlags = STARTF_USESHOWWINDOW;
					si.wShowWindow = SW_SHOW;

					PROCESS_INFORMATION pi;
					ZeroMemory(&pi, sizeof(pi));

					CreateProcess(
						NULL,					// name of executable module
						(LPTSTR)(LPCTSTR)str,	// command line string
						NULL,					// process attributes
						NULL,					// thread attributes
						FALSE,					// handle inheritance option
						0,						// creation flags
						NULL,					// new environment block
						NULL,					// current directory name
						&si,					// startup information
						&pi);					// process information
				}
				else
				{
					SHELLEXECUTEINFO sei;

					ZeroMemory(&sei,sizeof(sei));
					sei.cbSize = sizeof(sei);
					sei.lpFile = strDrive;
					sei.lpVerb = strVerb;
					sei.fMask  = SEE_MASK_INVOKEIDLIST;
					sei.nShow  = SW_SHOWNORMAL;

					ShellExecuteEx(&sei); 
				}
			}
		}
	}
}

///////////////////////////////////////////////////////////////////////////////
// CreateDriveButtons
BOOL CXDriveBar::CreateDriveButtons()
{
	DWORD dwDrives = GetLogicalDrives();
	DWORD dwMask = 1;

	int nButton = 0;

	for (int i = 0; i < 26; i++)
	{
		if (dwDrives & dwMask)
		{
			CString strDriveLetter = (TCHAR) (i + _T('A'));
			CString strDrive = strDriveLetter;
			strDrive += _T(":\\");

			CXPStyleButtonST *pButton = AddButton(ID_BASE+nButton, strDriveLetter);
			if (m_pFont)
				pButton->SetFont(m_pFont);
			if (m_pTheme)
				pButton->SetThemeHelper(m_pTheme);

			CString strLabel = GetDriveLabel(strDrive);
			strLabel = GetDriveLabel(strDrive);	// first time doesn't always work
												// with removable media drives
			TRACE(_T("Drive label for %s = %s\n"), strDrive, strLabel);
			pButton->SetTooltipText(strLabel);

			HICON hIcon = theSystemImageList.GetIcon(strDrive);
			pButton->SetIcon(hIcon);

			nButton++;
		}
		dwMask <<= 1;
	}

	return TRUE;
}

///////////////////////////////////////////////////////////////////////////////
// GetDriveLabel
CString CXDriveBar::GetDriveLabel(LPCTSTR lpszDrive)
{
	// start with the drive letter
	CString strLabel(lpszDrive);

	UINT uFlags = SHGFI_DISPLAYNAME;
	SHFILEINFO sfi;
	ZeroMemory(&sfi, sizeof(SHFILEINFO));

    if (SHGetFileInfo(lpszDrive, 0, &sfi, sizeof(SHFILEINFO), uFlags))
	{
		strLabel = sfi.szDisplayName;
	}
	else
	{
		TRACE(_T("ERROR  SHGetFileInfo failed\n"));
	}

	return strLabel;
}

// from windowsx.h
#ifndef GET_X_LPARAM
#define GET_X_LPARAM(lp)                        ((int)(short)LOWORD(lp))
#define GET_Y_LPARAM(lp)                        ((int)(short)HIWORD(lp))
#endif

///////////////////////////////////////////////////////////////////////////////
// OnRButtonUp
void CXDriveBar::OnRButtonUp(UINT nFlags, CPoint point) 
{
	DWORD dwPos = GetMessagePos();
	point.x = GET_X_LPARAM(dwPos);
	point.y = GET_Y_LPARAM(dwPos);

	BOOL bFound = FALSE;
	CRect rectButton;

	for (int i = 0; i < m_aButtons.GetSize(); i++)
	{
		CXPStyleButtonST *pButton = (CXPStyleButtonST *) m_aButtons.GetAt(i);
		if (!pButton)
			break;

		pButton->GetWindowRect(&rectButton);
		if (rectButton.PtInRect(point))
		{
			m_strDrive = GetButtonText(i);
			bFound = TRUE;
			break;
		}
	}

	if (bFound)
	{
		CMenu menu;
		VERIFY(menu.CreatePopupMenu());

		VERIFY(menu.AppendMenu(MF_STRING, ID_EXPLORE,    _T("Explore")));
		VERIFY(menu.AppendMenu(MF_STRING, ID_SEARCH,     _T("Search...")));
		VERIFY(menu.AppendMenu(MF_SEPARATOR, 0, (LPCTSTR)NULL));
		VERIFY(menu.AppendMenu(MF_STRING, ID_PROPERTIES, _T("Properties")));

		menu.SetDefaultItem(0, TRUE);

		int x = rectButton.left;
		int y = rectButton.bottom;

		// display the menu - selection will be handled in OnButton()
		VERIFY(menu.TrackPopupMenu(TPM_LEFTALIGN | TPM_LEFTBUTTON, x, y, this));
	}

	CXToolBar::OnRButtonUp(nFlags, point);
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Software Developer (Senior) Hans Dietrich Software
United States United States
I attended St. Michael's College of the University of Toronto, with the intention of becoming a priest. A friend in the University's Computer Science Department got me interested in programming, and I have been hooked ever since.

Recently, I have moved to Los Angeles where I am doing consulting and development work.

For consulting and custom software development, please see www.hdsoft.org.






Comments and Discussions