贴一段写注册表的代码。。下次启动就自动启动了。  
  var  
      myreg:TRegistry;  
      MySysPath:string;  
   
  MySysPath:=ExtractFilePath(Application.ExeName);  
        if   MySysPath[length(MySysPath)]<>'\'   then  
              MySysPath:=MySysPath+'\';  
        myreg:=Tregistry.Create;                                                

      //在注册表设置启动  
        try  
            myreg.RootKey:=HKEY_LOCAL_MACHINE;  
            myreg.OpenKey

('Software\Microsoft\Windows\CurrentVersion\Run',false);  
            if   length(myreg.ReadString(application.Title))=0   then  
            begin  
                  myreg.RootKey:=HKEY_LOCAL_MACHINE;  
                  myreg.OpenKey

('Software\Microsoft\Windows\CurrentVersion\Run',false);  
                  myreg.WriteString(application.Title,MySysPath+extractfilename

(application.ExeName));  
            end;  
            myreg.CloseKey;  
        Finally  
            myreg.Free;  
        end;

///////////////////////////////////////////////
var
  FileName, FilePath: string;
  Reg: Tregistry;
begin
  FileName := ExtractFileName(application.ExeName); //开机启动的执行程序名
  FilePath := application.ExeName; //完整路径
  Reg := Tregistry.Create;
  try
    Reg.Rootkey := HKEY_LOCAL_MACHINE;
    Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run', true);
    Reg.WriteString(FileName, FilePath) //添加注册表项和键值
    Reg.CloseKey; //关闭注册表
  finally
    Reg.Free;
  end;
end;

//////////////////////////////////////////////////////
uses中加入Registry;

procedure   TfrmAutoRun.btnOkClick(Sender:   TObject);  
  var  
      rAutoRun:   TRegistry;  
  begin  
      rAutoRun   :=   TRegistry.Create;  
      rAutoRun.RootKey   :=   HKEY_LOCAL_MACHINE;  
   
      try  
          rAutorun.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',  

true);  
   
          if   cbAutoRun.Checked   =   true   then  
          begin  
              rAutorun.WriteString('firApp',   ExpandFileName

(Application.ExeName));  
              MessageBox(self.Handle,   '自启动设置成功!',   '提示',  

mb_iconInformation   +   mb_Ok)  
          end  
          else  
          begin  
              if   rAutoRun.ValueExists('firApp')   then  
              begin  
                  rAutoRun.DeleteValue('firApp');  
                  cbAutoRun.Checked   :=   false;  
                  MessageBox(self.Handle,   '自启动已取消!',   '提示',  

mb_iconInformation   +   mb_Ok)  
              end;  
          end;  
      finally  
          rAutoRun.CloseKey;  
          rAutoRun.Free;  
      end;  
  end;

窗体创建时读注册表值以设定CheckBox状态:  
   
  procedure   TfrmAutoRun.FormShow(Sender:   TObject);  
  var  
      rAutoRun:   TRegistry;  
  begin  
      rAutoRun   :=   TRegistry.Create;  
      rAutoRun.RootKey   :=   HKEY_LOCAL_MACHINE;  
   
      try  
          rAutorun.OpenKey('SOFTWARE\Microsoft\Windows\CurrentVersion\Run',  

false);  
   
          if   rAutoRun.ValueExists('firApp')   then  
              cbAutoRun.Checked   :=   true  
          else  
              cbAutoRun.Checked   :=   false;  
      finally  
          rAutoRun.CloseKey;  
          rAutoRun.Free;  
      end;  
  end;  

 

posted on 2009-06-26 16:54  恩恩爸爸  阅读(395)  评论(0编辑  收藏  举报