您现在的位置是:首页 >

拒绝访问注册表路径 Delphi编程访问注册表(三)

火烧 2021-08-29 03:46:21 1057
Del hi编程访问注册表(三)   三 设计过程  (一) 界面的建立   启动DelPhi 新建一个窗体Form    在窗体Form 上建立三个T itBt 按钮 两个复选框TcheckBox

Delphi编程访问注册表(三)  

  三 设计过程

  (一) 界面的建立

   启动DelPhi 新建一个窗体Form

   在窗体Form 上建立三个TbitBtn按钮 两个复选框TcheckBox 一个GroupBox 调整各对象的位置 使其与图一所示

  (二) 代码编辑

   要使程序能够正确访问注册表 必须在单元文件unit Unit 的interface的 uses中增加注册表类 Registry 此句千万不能少

   按钮Bit_all( 全选 按钮)中的代码如下

  procedure TForm Bit_allClick(Sender: TObject);

  begin

  A_check Checked:=true;

  B_check Checked:=true;

  end; //当用户单击 全选 按钮时 将两个复选框都设为选中状态

   按钮Bit_cancel( 取消 按钮)中的代码如下

  procedure TForm Bit_cancelClick(Sender: TObject);

  begin

  A_check Checked:=false;

  B_check Checked:=false;

  end; //当用户单击 取消 按钮时 则取消复选框的选中状态

   按钮Bit_hide( 隐藏 按钮)中的代码如下(主要程序代码均放在这里 当用户按此按钮时 即完成隐藏驱动器的目的)

  procedure TForm Bit_hideClick(Sender: TObject);

  var myreg:TRegistry; // 定义Tregistry型变量myreg

  temp temp myval:integer;//temp 和temp 分别保存复选框A_check和B_check的值

  //myval用于保存写入注册表的最终值 它等于temp 和temp 的和

begin

  temp := ;

  temp := ;

  myval:= ; //初始三个变量

  myreg:=TRegistry Create;//利用Tregistry的Create方法创建一个实例myreg

  with myreg do

  begin

  rootkey:=HKEY_LOCAL_MACHINE;//确定打开注册表的主键

  if openkey( SOFAREMicrosoftWindowsCurrentVersionPoliciesexplorer true) then

  begin //打开NoDrives所在的键

  case form A_Check Checked of

  true:

  temp := ;//当用户点击 A驱 复选框时 将临时变量temp 置

  end;

  case form B_Check Checked of

  true:

  temp := ; //当用户点击 B驱 复选框时 将临时变量temp 置

  end;

  myval:=temp +temp ;//将临时变量temp 和temp 中的值求和赋给myval 因为用户可

  //能选中两个复选框 此时写入注册表的值应为 + 所以用temp 与temp 的和赋给myval

  WriteInteger( NoDrives myval);//利用函数创建一个Dword值 NoDrives 其值为myval

  //并写入注册表

  myreg CloseKey //关闭前面打开的主键 完成隐藏操作

  myreg Free;

  end;

  end;

  end;

   为了实现程序启动时能自动判断系统驱动器的当前状态 还要在Form 的FormCreate()方法中利用ReadInteger()和ValueExists()读取注册表中NoDrives的值 然后根据读出的NoDrives的值设置窗体上两个复选的选择状态 程序如下

  procedure TForm FormCreate(Sender: TObject);

拒绝访问注册表路径 Delphi编程访问注册表(三)

  var myreg:TRegistry;

  begin

  myreg:=TRegistry Create;

  with myreg do

  begin

  rootkey:=HKEY_LOCAL_MACHINE;

  if openkey( SOFAREMicrosoftWindowsCurrentVersionPoliciesexplorer true) and ValueExists( NoDrives ) then //如果打开键成功且NoDrives存在 则进行下面操作

  begin

  if readinteger( NoDrives )= then //读取NoDrives的值 若为 则将A_check设为选中

  A_check Checked:=true;

  if readinteger( NoDrives )= then//读取NoDrives的值 若为 则将B_check设为选中

  B_check Checked:=true;

  if readinteger( NoDrives )= then//读取NoDrives的值 若为 则将两个复选框均选中

begin

  A_check Checked:=true;

  B_check Checked:=true;

  end;

  myreg CloseKey;

  myreg Free;

  end;

  end;

  end;

   将以上程序保存为Hide_drive 然后编译成可执行程序Hide_Drive exe即可 此程序在Win 和Delphi 中运行通过 操作时隐藏了驱动器 请重新注销windows即可看到实际效果

  四 结束语

  在程序中访问注册表的基本方法是 打开注册表 读写注册表 关闭打开的主键 此实例程序本身虽然比较简单 但其访问注册表的方法却比较通用 你可以将此方法灵活运用到其他程序语言中 设计出满足要求的实用程序 其实在DelPhi中 Tregistry类为我们提供了更多更丰富的方法来操作注册表 但操作的方法仍然是一样的 希望通过本文的实例 能为广大Delphi爱好者或程序设计爱好者提供一点帮助

  附 为了使大家能对此程序有一个完整的了解 下面附上源程序 请大家指教!

lishixinzhi/Article/program/Delphi/201311/8547  
永远跟党走
  • 如果你觉得本站很棒,可以通过扫码支付打赏哦!

    • 微信收款码
    • 支付宝收款码