mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
源码类别:

Delphi/CppBuilder

开发平台:

Delphi

  1. unit mainunit;
  2. interface
  3. uses
  4.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  5.   Dialogs,Registry, StdCtrls;
  6. type
  7.   TForm1 = class(TForm)
  8.     Button1: TButton;
  9.     Label1: TLabel;
  10.     Label2: TLabel;
  11.     procedure Button1Click(Sender: TObject);
  12.     procedure FormCreate(Sender: TObject);
  13.   private
  14.     { Private declarations }
  15.   public
  16.     function GetObjectSize(SpecCode:String):Integer;
  17.     function Get32BitValue(SpecCode:String):Integer;
  18.     procedure HideSpecDrive(SpecCode:String);
  19.     { Public declarations }
  20.   end;
  21. var
  22.   Form1: TForm1;
  23.   BufSize:Integer; //用来存储要写入对象的大小
  24.   P1:Pointer;      //指针类型,指向内存当中表示写入对象的二进制地址
  25.   VisibleState:Boolean;
  26. implementation
  27. {$R *.dfm}
  28. function TForm1.GetObjectSize(SpecCode:String):Integer;
  29. var
  30.  I:Integer;
  31. begin
  32.  Result:=0;
  33.  for I:=1 to Length(SpecCode) do
  34.   begin
  35.     if (SpecCode[I]=' ') then
  36.      INC(Result);
  37.   end;
  38.  if Result<>0 then
  39.   INC(Result); 
  40. end;
  41. procedure TForm1.HideSpecDrive(SpecCode:String);//这里把二进制值用String传递过来
  42. var
  43.  Reg:TRegistry;
  44. begin
  45.  Reg:=TRegistry.Create;
  46.  try
  47.   Reg.RootKey:=HKEY_CURRENT_USER;
  48.   if Reg.OpenKey('SoftwareMicrosoftWindowsCurrentVersionPoliciesExplorer',True)then
  49.    begin
  50.     BufSize:=GetObjectSize(SpecCode);//获取对象大小
  51.     GetMem(P1,BufSize);   //给指针按照对象大小分配内存
  52.     P1:=Ptr(Get32BitValue(SpecCode));//用指针指向写入对象,该对象在内存中为二进制
  53.     Reg.WriteBinaryData('NoDrives',P1,BufSize);//把指针指向的对象写入注册表
  54.    end;
  55.  finally
  56.   Reg.CloseKey;
  57.   Reg.Free;
  58.  end;
  59. end;
  60. function TForm1.Get32BitValue(SpecCode:String):Integer;
  61. var
  62.  I:Integer;
  63.  TempStr:String;
  64. begin
  65.  TempStr:='';
  66.  for i:=Length(SpecCode) downto 1 do
  67.   begin
  68.     if SpecCode[I]=' ' then
  69.      begin
  70.       TempStr:=TempStr+Copy(SpecCode,I+1,2);
  71.      end;
  72.   end;
  73.  TempStr:='$'+TempStr+Copy(SpecCode,1,2);
  74.  Result:=StrToInt(TempStr);
  75. end;
  76. procedure TForm1.Button1Click(Sender: TObject);
  77. var
  78.  Reg:TRegistry;
  79. begin
  80.  if VisibleState then
  81.   begin
  82.    HideSpecDrive('0C 00 00 00');
  83.    VisibleState:=False;
  84.    Label2.Caption:='不可见';
  85.    Button1.Caption:='显示C盘和D盘的驱动器盘符(&S)';
  86.   end
  87.   else
  88.   begin
  89.    HideSpecDrive('00 00 00 00');
  90.    VisibleState:=True;
  91.    Label2.Caption:='可见';
  92.    Button1.Caption:='隐藏C盘和D盘的驱动器盘符(&S)';
  93.   end;
  94. end;
  95. procedure TForm1.FormCreate(Sender: TObject);
  96. var
  97.  Reg:TRegistry;
  98.  State:Integer;
  99. begin
  100.  Reg:=TRegistry.Create;
  101.  try
  102.   Reg.RootKey:=HKEY_CURRENT_USER;
  103.   if Reg.OpenKey('SoftwareMicrosoftWindowsCurrentVersionPoliciesExplorer',True)then
  104.    begin
  105.     if Reg.ValueExists('NoDrives') then
  106.      begin
  107.       State:=Reg.ReadBinaryData('NoDrives',State,SizeOf(State));
  108.      end
  109.      else
  110.       State:=0; 
  111.    end;
  112.  finally
  113.   Reg.CloseKey;
  114.   Reg.Free;
  115.  end;
  116.  if State=4 then
  117.   begin
  118.    VisibleState:=False;
  119.    Label2.Caption:='不可见';
  120.    Button1.Caption:='显示C盘和D盘的驱动器盘符(&S)';
  121.   end
  122.  else
  123.   begin
  124.    VisibleState:=True;
  125.    Label2.Caption:='可见';
  126.    Button1.Caption:='隐藏C盘和D盘的驱动器盘符(&S)';
  127.   end; 
  128. end;
  129. end.