Delphi学习笔记(2)续 获取磁盘信息 应用程序

昨天发布的程序还有很多的问题,比如按钮字体不居中等。今天除修改上述问题之外,继续增加一个新功能:显示磁盘详细信息。

一,创建Form,如下图


注:1,此处新建了一个按钮和总共12个Label。

2,这里我们处理界面字体显示问题,在这里我曾遇到标签内的冒号不显示以及按钮字体不居中等问题。其实这里的解决办法相当简单,只需在Button和Label属性中找到Font,将Charset修改为GB2312即可,然后设置你需要的字体大小即可。另外,Delphi默认按钮字体是居中显示的。

二,编写代码

1,界面初使化,代码如下

procedure TForm1.FormCreate(Sender: TObject);
begin
  ClearForm;
end;

procedure TForm1.ClearForm;
begin
    lblSectorNum.Caption := '';
    lblSectorByte.Caption := '';
    lblEmptySector.Caption := '';
    lblFreeSpace.Caption := '';
    lblTotalSpace.Caption := '';
    lblTotalCluster.Caption := '';
end;

注:i),此处代码作用是在生成界面时清除需要显示数据的标签的初使内容。

ii),在此处编写的代码,必须在Type下声明

iii),如果我们想要FormCreate在界面生成时被执行,就必须在Form事件的OnCreat属性中添加你要执行的过程,如FormCreate。

2,获取磁盘详细信息。在Button事件中选择单击事件Delphi会自动生成一个空的过程,然后写入以下代码:

procedure TForm1.lst1Click(Sender: TObject);
var
  RootPath: string;
  SectorsPerCluster: DWORD;
  BytesPerSector: DWORD;
  NumFreeClusters: DWORD;
  TotalClusters: DWORD;
  DriveByte: Byte;
  FreeSapce: Int64;
  TotalSpace: Int64;
  DriveNum: Integer;

begin
with lst1 do
  begin
    DriveByte := Integer(Items.Objects[ItemIndex]) - 64;
    RootPath := chr(Integer(Items. Objects[ItemIndex])) + ':\';
    {GetDiskFreeSpace 函数 获取磁盘信息}
    if GetDiskFreeSpace(PChar(RootPath), SectorsPerCluster,
      BytesPerSector, NumFreeClusters, TotalClusters) then
    begin
      lblSectorNum.Caption := Format('%.0n',[SectorsPerCluster*1.0]);
      lblSectorByte.Caption := Format('%.0n',[BytesPerSector*1.0]);
      lblEmptySector.Caption := Format('%.0n',[NumFreeClusters*1.0]);
      lblTotalCluster.Caption := Format('%.0n',[TotalClusters*1.0]);
      FreeSapce := DiskFree(DriveByte);
      TotalSpace := DiskSize(DriveByte);
      lblFreeSpace.Caption := Format('%.0n',[FreeSapce*1.0]);
      lblTotalSpace.Caption := Format('%.0n',[TotalSpace*1.0]);
    end
    else begin
      //ShowMessage('不能获得磁盘信息');
      MessageBox(Self.Handle,'不能获得磁盘信息','Error',0);
      ClearForm;
    end;
  end;
end;

3,实现清空按钮。双击清空按钮,在自动生成的代码中添加以下内容:

procedure TForm1.btn2Click(Sender: TObject);
begin
  lst1.Items.Clear;
  ClearForm;
end;

三,运行代码,即可得到我们想要的信息


注:若要想界面在屏幕上剧中显示,可以在Form中的position属性中设置podeskcenter即可。



程序源码可以在:http://download.csdn.net/detail/welcome000yy/4455048 下载

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值