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

📄 diskshowdlg.cpp

📁 磁盘信息实时读取 U盘信息 包括磁盘空间的显示
💻 CPP
字号:
// DiskShowDlg.cpp : implementation file
//

#include "stdafx.h"
#include "DiskShow.h"
#include "DiskShowDlg.h"


#include <Dbt.h>

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

/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About

class CAboutDlg : public CDialog
{
public:
	CAboutDlg();

// Dialog Data
	//{{AFX_DATA(CAboutDlg)
	enum { IDD = IDD_ABOUTBOX };
	//}}AFX_DATA

	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CAboutDlg)
	protected:
	virtual void DoDataExchange(CDataExchange* pDX);    // DDX/DDV support
	//}}AFX_VIRTUAL

// Implementation
protected:
	//{{AFX_MSG(CAboutDlg)
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
	//{{AFX_DATA_INIT(CAboutDlg)
	//}}AFX_DATA_INIT
}

void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CAboutDlg)
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
	//{{AFX_MSG_MAP(CAboutDlg)
		// No message handlers
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiskShowDlg dialog

CDiskShowDlg::CDiskShowDlg(CWnd* pParent /*=NULL*/)
	: CDialog(CDiskShowDlg::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDiskShowDlg)
		// NOTE: the ClassWizard will add member initialization here
	//}}AFX_DATA_INIT
	// Note that LoadIcon does not require a subsequent DestroyIcon in Win32
	m_hIcon = AfxGetApp()->LoadIcon(IDR_MAINFRAME);
}

void CDiskShowDlg::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDiskShowDlg)
	DDX_Control(pDX, IDC_LIST_DISK_SHOW, m_ListDiskInfo);
	//}}AFX_DATA_MAP
}

BEGIN_MESSAGE_MAP(CDiskShowDlg, CDialog)
	//{{AFX_MSG_MAP(CDiskShowDlg)
	ON_WM_SYSCOMMAND()
	ON_WM_PAINT()
	ON_WM_QUERYDRAGICON()
	ON_WM_DEVICECHANGE()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDiskShowDlg message handlers

BOOL CDiskShowDlg::OnInitDialog()
{
	CDialog::OnInitDialog();

	// Add "About..." menu item to system menu.

	// IDM_ABOUTBOX must be in the system command range.
	ASSERT((IDM_ABOUTBOX & 0xFFF0) == IDM_ABOUTBOX);
	ASSERT(IDM_ABOUTBOX < 0xF000);

	CMenu* pSysMenu = GetSystemMenu(FALSE);
	if (pSysMenu != NULL)
	{
		CString strAboutMenu;
		strAboutMenu.LoadString(IDS_ABOUTBOX);
		if (!strAboutMenu.IsEmpty())
		{
			pSysMenu->AppendMenu(MF_SEPARATOR);
			pSysMenu->AppendMenu(MF_STRING, IDM_ABOUTBOX, strAboutMenu);
		}
	}

	// Set the icon for this dialog.  The framework does this automatically
	//  when the application's main window is not a dialog
	SetIcon(m_hIcon, TRUE);			// Set big icon
	SetIcon(m_hIcon, FALSE);		// Set small icon
	
	// TODO: Add extra initialization here
	InitListCtrl();
	ShowDiskInfo();
	
	return TRUE;  // return TRUE  unless you set the focus to a control
}

void CDiskShowDlg::OnSysCommand(UINT nID, LPARAM lParam)
{
	if ((nID & 0xFFF0) == IDM_ABOUTBOX)
	{
		CAboutDlg dlgAbout;
		dlgAbout.DoModal();
	}
	else
	{
		CDialog::OnSysCommand(nID, lParam);
	}
}

// If you add a minimize button to your dialog, you will need the code below
//  to draw the icon.  For MFC applications using the document/view model,
//  this is automatically done for you by the framework.

void CDiskShowDlg::OnPaint() 
{
	if (IsIconic())
	{
		CPaintDC dc(this); // device context for painting

		SendMessage(WM_ICONERASEBKGND, (WPARAM) dc.GetSafeHdc(), 0);

		// Center icon in client rectangle
		int cxIcon = GetSystemMetrics(SM_CXICON);
		int cyIcon = GetSystemMetrics(SM_CYICON);
		CRect rect;
		GetClientRect(&rect);
		int x = (rect.Width() - cxIcon + 1) / 2;
		int y = (rect.Height() - cyIcon + 1) / 2;

		// Draw the icon
		dc.DrawIcon(x, y, m_hIcon);
	}
	else
	{
		CDialog::OnPaint();
	}
}

// The system calls this to obtain the cursor to display while the user drags
//  the minimized window.
HCURSOR CDiskShowDlg::OnQueryDragIcon()
{
	return (HCURSOR) m_hIcon;
}

//初始化列表控件

void CDiskShowDlg::InitListCtrl()
{
	LONG lStyle = m_ListDiskInfo.SendMessage(LVM_GETEXTENDEDLISTVIEWSTYLE);
	lStyle |= LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES | LVS_EX_HEADERDRAGDROP;
	m_ListDiskInfo.SendMessage(LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)lStyle);

	m_ListDiskInfo.InsertColumn(0,"盘符",LVCFMT_LEFT,40,-1);
	m_ListDiskInfo.InsertColumn(1,"卷标",LVCFMT_LEFT,80,-1);
	m_ListDiskInfo.InsertColumn(2,"驱动器类型",LVCFMT_LEFT,75,-1);
	m_ListDiskInfo.InsertColumn(3,"文件系统类型",LVCFMT_LEFT,85,-1);
	m_ListDiskInfo.InsertColumn(4,"总容量(MB)",LVCFMT_LEFT,80,-1);
	m_ListDiskInfo.InsertColumn(5,"已用空间(MB)",LVCFMT_LEFT,90,-1);
}


//在列表控件中显示磁盘信息
void CDiskShowDlg::ShowDiskInfo()
{
	m_ListDiskInfo.DeleteAllItems();
	int i=0, j;
	char DriveStrings[MAX_PATH];

	GetLogicalDriveStrings(MAX_PATH, DriveStrings);//获取所有逻辑驱动器的根路径名

	for (char* str=DriveStrings; *str; str+=strlen(str)+1) 
	{
		j=1;
		CString Driver;
		Driver.Format("%c", str[0]);
		m_ListDiskInfo.InsertItem(i, Driver);//再表中插入一行信息

		DISK_INFO DiskInfo;
		memset(&DiskInfo, 0, sizeof(DISK_INFO));
		GetDiskInfo(str, &DiskInfo);////获得磁盘信息

		CString strTemp;
		strTemp.Format("%s", DiskInfo.VolumeName);
		m_ListDiskInfo.SetItemText(i, j++, strTemp);

		strTemp.Format("%s", DiskInfo.DriveType);
		m_ListDiskInfo.SetItemText(i, j++, strTemp);

		strTemp.Format("%s", DiskInfo.FileSystemName);
		m_ListDiskInfo.SetItemText(i, j++, strTemp);

		ULONG lTemp;
		lTemp = (ULONG)(DiskInfo.TotalBytes.QuadPart / 1024 / 1024);
		strTemp.Format("%ld", lTemp);
		m_ListDiskInfo.SetItemText(i, j++, strTemp);

		lTemp = (ULONG)(DiskInfo.FreeBytes.QuadPart / 1024 / 1024);
		strTemp.Format("%ld", lTemp);
		m_ListDiskInfo.SetItemText(i, j++, strTemp);
		i++;
	}
}

//获得磁盘信息
BOOL CDiskShowDlg::GetDiskInfo(LPCTSTR Drive, DISK_INFO *pDiskInfo)
{
	BOOL ret = TRUE;
	UINT DriveType;

	if((DriveType = GetDriveType(Drive)) == DRIVE_NO_ROOT_DIR)
	{
		ret = FALSE;
		return ret;
	}

	switch(DriveType)
	{
	case DRIVE_UNKNOWN:
		strcpy(pDiskInfo->DriveType, "未知");
		break;
	case DRIVE_REMOVABLE:
		strcpy(pDiskInfo->DriveType, "可移动");
		break;
	case DRIVE_FIXED:
		strcpy(pDiskInfo->DriveType, "本地磁盘");
		break;
	case DRIVE_REMOTE:
		strcpy(pDiskInfo->DriveType, "网络驱动器");
		break;
	case DRIVE_CDROM:
		strcpy(pDiskInfo->DriveType, "DVD\\CD-ROM");
		break;
	case DRIVE_RAMDISK:
		strcpy(pDiskInfo->DriveType, "随机存取磁盘");
		break;
	default:
		break;
	}
		
	GetVolumeInformation(Drive,pDiskInfo->VolumeName,MAX_PATH, NULL, NULL, 
		                 NULL, pDiskInfo->FileSystemName, MAX_PATH);

	GetDiskFreeSpaceEx(Drive, &pDiskInfo->FreeBytes, &pDiskInfo->TotalBytes, NULL);
	
	return ret;
}
BOOL CDiskShowDlg::OnDeviceChange( UINT nEventType, DWORD dwData )
{
	if(nEventType == DBT_DEVICEARRIVAL || nEventType == DBT_DEVICEREMOVECOMPLETE)
	{
		ShowDiskInfo();
	}
	return TRUE;
}

⌨️ 快捷键说明

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