工控编程吧
标题:
上位机MFC如何设置驱动器的卷标
[打印本页]
作者:
qq263946146
时间:
2019-8-16 11:28
标题:
上位机MFC如何设置驱动器的卷标
1.获取卷标
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO);
//获得驱动器名
DWORD dwBitMask = ::GetLogicalDrives();
if (dwBitMask != 0)
{
TCHAR ch = 'A';
while (dwBitMask > 0)
{
if (dwBitMask % 2 == 1)
{
CString strDiriveName = _T("");
strDiriveName.Format(_T("%c:\"), ch);
CString strText = _T("");
strText.Format(_T("%s"), strDiriveName);
pComboBox->AddString(strText);
}
dwBitMask /= 2;
ch++;
}
}
pComboBox->SetCurSel(0);
OnSelchangeCombo();
复制代码
OnSelchangeCombo()为我们添加的组合框选项发生变化时触发的函数。
void CDemoDlg::OnSelchangeCombo()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO);
int nSel = pComboBox->GetCurSel();
CString strDriveName = _T("");
pComboBox->GetLBText(nSel, strDriveName);
//获得驱动器卷标
CString strVolumeName = _T("");
GetVolumeInformation(strDriveName, strVolumeName.GetBuffer(1024),
1024, NULL, NULL, NULL, NULL, 0);
strVolumeName.ReleaseBuffer();
SetDlgItemText(IDC_TEXT, strVolumeName);
}
复制代码
设置卷标可以使用按钮调用下面代码
void CDemoDlg::OnTest()
{
CComboBox* pComboBox = (CComboBox*)GetDlgItem(IDC_COMBO);
int nSel = pComboBox->GetCurSel();
CString strDriveName = _T("");
pComboBox->GetLBText(nSel, strDriveName);
CString strVolumeName = _T("");
GetDlgItemText(IDC_TEXT, strVolumeName);
//设置驱动器卷标
if (::SetVolumeLabel(strDriveName, strVolumeName))
{
AfxMessageBox(_T("设置驱动器卷标成功。"));
}
else
{
AfxMessageBox(_T("设置驱动器卷标失败。"));
}
}
复制代码
IDC_TEXT为编程框控件ID,供输入内容用。
欢迎光临 工控编程吧 (https://www.gkbc8.com/)
Powered by Discuz! X3.4