⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 drivecombo.cpp

📁 《突破Visual C++.NET编程实例五十讲+源文件,初学者学习的好东东!
💻 CPP
字号:

#include "stdafx.h"
#include "DriveCombo.h"

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


#define MAX_BUFFER				1024		



// CDriveCombo
CDriveCombo::CDriveCombo()
{
}

CDriveCombo::~CDriveCombo()
{
}


BEGIN_MESSAGE_MAP(CDriveCombo, CComboBox)
	//{{AFX_MSG_MAP(CDriveCombo)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


// CDriveCombo 消息函数
BOOL CDriveCombo::Initialize()
{
	BOOL    bFound = FALSE;
	DWORD   dwRes  = 0;		
	int     i      = 0;		
	CString str;			
	
	char  szVolInfo[MAX_BUFFER];
	char  szDrives[26] = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I',
						  'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
						  'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'};
	dwRes = GetLogicalDrives();
	if (0 == dwRes)
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
					  NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					  (LPTSTR) &lpMsgBuf, 0, NULL);
		MessageBox((LPTSTR)lpMsgBuf, "GetLastError - GetLogicalDrives failed", MB_OK|MB_ICONERROR);
		LocalFree(lpMsgBuf);
		return FALSE;
	}

	// 分离盘符,并增加到组合框中
	while (dwRes)
	{
		if (dwRes & 1)
		{
			// There's a drive for this bit position.
			str.Format("%c: ", szDrives[i]);

			// 获取盘符名
			SetErrorMode(SEM_FAILCRITICALERRORS);
			bFound = GetVolumeInformation(str, szVolInfo, MAX_BUFFER, NULL, NULL, NULL, NULL, NULL);
			if (bFound)
				str += szVolInfo;

			// 增加到组合框
			AddString(str);
		}

		dwRes >>= 1;
		i++;
	}

	// 获取当前目录
	dwRes = GetCurrentDirectory(MAX_BUFFER, szVolInfo);
	if (0 == dwRes)
	{
		LPVOID lpMsgBuf;
		FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER|FORMAT_MESSAGE_FROM_SYSTEM,
					  NULL, GetLastError(), MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
					  (LPTSTR) &lpMsgBuf, 0, NULL);
		MessageBox((LPTSTR)lpMsgBuf, "GetLastError - GetCurrentDirectory failed", MB_OK|MB_ICONERROR);
		LocalFree(lpMsgBuf);
		return FALSE;
	}
	str.Format("%c", szVolInfo[0]);
	i = FindString(-1, str);
	SetCurSel(i);

	return TRUE;
}

BOOL CDriveCombo::IsDriveReady(int nIndex)
{
	HANDLE          hFile;			
	WIN32_FIND_DATA stFindData;		
	CString         szDrive;		
	CString         szSearch;		
	CString         szError;		

	GetLBText(nIndex, szDrive);
	szSearch = szDrive.Left(1);
	szSearch += ":\\*.*";

	hFile = FindFirstFile((LPCTSTR)szSearch, &stFindData);
	if (INVALID_HANDLE_VALUE == hFile)
	{
		szError.Format("Drive %s: is not ready.", szDrive.Left(1));
		MessageBox(szError, "Error", MB_OK|MB_ICONEXCLAMATION);
		return FALSE;
	}
	FindClose(hFile);

	return TRUE;
}

void CDriveCombo::ResetDrive(char cDrive)
{
	CString szStr;
	int     nIndex = 0;

	szStr.Format("%c:", cDrive);
	nIndex = FindString(-1, szStr);
	if (CB_ERR != nIndex)
		SetCurSel(nIndex);
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -