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

📄 mainform.cpp

📁 C++Builder程序员编程手记《配书光盘》
💻 CPP
字号:
//---------------------------------------------------------------------------

#include <vcl.h>

#pragma hdrstop

#include "MainForm.h"
#include "shlobj.h"
#include "mmsystem.h"

//---------------------------------------------------------------------------
#pragma package(smart_init)
#pragma resource "*.dfm"
TForm1 *Form1;
TIcon *DriverIcon[5];
HICON Large[50],Small[50];
TImageList *ImageList;
int DriverCount;
AnsiString RootDriArray[26]={"","","","","","","","","",""};
//---------------------------------------------------------------------------
__fastcall TForm1::TForm1(TComponent* Owner)
  : TForm(Owner)
{
}
//---------------------------------------------------------------------------
void TForm1::DriverList()
{
  int DriverFlag,DriverBit,DriverTypeFlag,i;
  char DriverNum;
  AnsiString DriverType[7]={"未知类型","移动设备","本地硬盘","网络驱动器","软驱","光盘","虚拟盘"};
  AnsiString DriverName="(A)",RootDriver="A:\\";
  TListItem *ListItem;

  DriverBit=GetLogicalDrives();
  for(DriverFlag=1,DriverNum='A',i=0;DriverNum<'Z';DriverFlag<<=1,DriverNum++,i++)
  {
    if(DriverFlag&DriverBit)
    {
      DriverName.c_str()[1]=RootDriver.c_str()[0]=DriverNum;
      RootDriArray[i]=(AnsiString)DriverNum+":\\";
      switch(GetDriveType(RootDriver.c_str()))
      {
        case 0:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[0]+DriverName;
          ListItem->ImageIndex=0;
          break;
        case DRIVE_REMOVABLE:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[4]+DriverName;
          ListItem->ImageIndex=2;
          break;
        case DRIVE_FIXED:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[2]+DriverName;
          ListItem->ImageIndex=0;
          break;
        case DRIVE_REMOTE:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[3]+DriverName;
          ListItem->ImageIndex=3;
          break;
        case DRIVE_CDROM:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[5]+DriverName;
          ListItem->ImageIndex=4;
          break;
        case DRIVE_RAMDISK:
          ListItem=ListView1->Items->Add();
          ListItem->Caption=DriverType[6]+DriverName;
          ListItem->ImageIndex=0;
          break;
        default:
          break;
      }
    }
    else
     i--;
  }
}
//----------------------------------------------------------------------------------
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  ImageList=new TImageList(this);
  ImageList->Width=32;
  ImageList->Height=32;
  for(int i=0;i<5;i++)
    DriverIcon[i]=new TIcon;
  AnsiString SystemDir;
  GetSystemDirectory(SystemDir.c_str(),30);
  ExtractIconEx((SystemDir+"Shell32.dll").c_str(),1,Large,Small,30);
  DriverIcon[0]->Handle=Large[7];     //LOCAL DISK
  DriverIcon[1]->Handle=Large[25];    //MOVEBLE DISK
  DriverIcon[2]->Handle=Large[5];     //FLOPPY
  DriverIcon[3]->Handle=Large[8];     //NETDISK
  DriverIcon[4]->Handle=Large[10];    //CDROM
  for(int i=0;i<5;i++)
    ImageList->AddIcon(DriverIcon[i]);
  ListView1->LargeImages=ImageList;
  DriverList();
}
//---------------------------------------------------------------------------


void __fastcall TForm1::ListView1SelectItem(TObject *Sender,
      TListItem *Item, bool Selected)
{
  __int64 NFreeSpace[2],FreeSpace;
  char VolumeName[16];
  unsigned long VolumeSN;
  unsigned long MaxCompLength;
  unsigned long FileSystemFlags;
  char FileSystemName[16];

  if(Selected)
  {
  Memo1->Lines->Clear();
  if(Sysutils::GetDiskFreeSpaceEx(RootDriArray[Item->Index].c_str(),NFreeSpace[1],NFreeSpace[0],&FreeSpace))
  {
    if(GetVolumeInformation(RootDriArray[Item->Index].c_str(),VolumeName,255,&VolumeSN,
                          &MaxCompLength,&FileSystemFlags,FileSystemName,255))
   {
    Memo1->Lines->Add("\r\r");
    Memo1->Lines->Add("  "+Item->Caption);
    Memo1->Lines->Add("  卷标:"+(AnsiString)VolumeName);
    Memo1->Lines->Add("  卷标序列号:"+IntToStr(VolumeSN));
    Memo1->Lines->Add("  文件系统:"+(AnsiString)FileSystemName);
    Memo1->Lines->Add("  总空间:"+ByteToGM(NFreeSpace[0]));
    Memo1->Lines->Add("  已使用空间:"+ByteToGM(NFreeSpace[0]-FreeSpace));
    Memo1->Lines->Add("  空闲空间:"+ByteToGM(FreeSpace));
    }
    else
      Memo1->Lines->Add(ListView1->Selected->Caption+"\t"+"无法读取驱动器信息");
  }
  else
    Memo1->Lines->Add(ListView1->Selected->Caption+"\t"+"磁盘未准备好");
  }
}
//---------------------------------------------------------------------------


AnsiString __fastcall TForm1::ByteToGM(__int64 bytes)
{
  AnsiString DriverSpace;

  if(bytes>>30)
    DriverSpace=FormatFloat("0.00",(float)bytes/1024/1024/1024)+"GB";
  else if(bytes>>20)
    DriverSpace=IntToStr(bytes>>20)+"MB";
  else if(bytes>>10)
    DriverSpace=IntToStr(bytes>>10)+"KB";
  else
    DriverSpace=IntToStr(bytes)+"Byte";
  return DriverSpace;
}
void __fastcall TForm1::MemButtonClick(TObject *Sender)
{
  MEMORYSTATUS lpBuffer;

  lpBuffer.dwLength=sizeof(MEMORYSTATUS);
  GlobalMemoryStatus(&lpBuffer);
  Memo2->Lines->Add("本机内存使用信息");
  Memo2->Lines->Add("\r\r");
  Memo2->Lines->Add("  系统物理内存:"+ByteToGM(lpBuffer.dwTotalPhys));
  Memo2->Lines->Add("  已用物理内存:"+IntToStr(lpBuffer.dwMemoryLoad)+"%");
  Memo2->Lines->Add("  剩余空闲内存:"+ByteToGM(lpBuffer.dwAvailPhys));
  Memo2->Lines->Add("  交换页面大小:"+ByteToGM(lpBuffer.dwTotalPageFile));
  Memo2->Lines->Add("  可用交换空间:"+ByteToGM(lpBuffer.dwAvailPageFile));
  Memo2->Lines->Add("  虚拟内存空间:"+ByteToGM(lpBuffer.dwTotalVirtual));
  Memo2->Lines->Add("  可用虚拟空间:"+ByteToGM(lpBuffer.dwAvailVirtual));
}
//---------------------------------------------------------------------------

void __fastcall TForm1::DirButtonClick(TObject *Sender)
{
  char DirName[MAX_PATH]="";

  Memo3->Lines->Add("\n");
  switch((dynamic_cast<TButton*>(Sender))->Tag)
  {
    case 0:
      GetSystemDirectory(DirName,MAX_PATH-1);
      Memo3->Lines->Add("  系统目录为:"+(AnsiString)DirName+"\r");
      break;
    case 1:
      GetWindowsDirectory(DirName,MAX_PATH-1);
      Memo3->Lines->Add("  Windows目录为:"+(AnsiString)DirName+"\r");
      break;
    case 2:
      GetTempPath(MAX_PATH-1,DirName);
      Memo3->Lines->Add("  临时文件目录为:"+(AnsiString)DirName+"\r");
      break;
    case 3:
      GetCurrentDirectory(MAX_PATH-1,DirName);
      Memo3->Lines->Add("  当前目录为:"+(AnsiString)DirName+"\r");
      break;
    case 4:
      Memo3->Lines->Add("  网络邻居目录为:"+GetSpecialDir(CSIDL_NETHOOD));
      break;
    case 5:
      Memo3->Lines->Add("  桌面目录为:"+GetSpecialDir(CSIDL_DESKTOPDIRECTORY));
      break;
    case 6:
      Memo3->Lines->Add("  文档目录为:"+GetSpecialDir(CSIDL_RECENT));
      break;
    case 7:
      Memo3->Lines->Add("  发送到目录为:"+GetSpecialDir(CSIDL_SENDTO));
      break;
    case 8:
      Memo3->Lines->Add("  开始菜单目录为:"+GetSpecialDir(CSIDL_STARTMENU));
      break;
    default:
      break;
  }
}
//---------------------------------------------------------------------------
/*
CSIDL_BITBUCKET	Recycle bin 

⌨️ 快捷键说明

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