怎么才能让机器开机时就能自动运行我的程序而且要在注册表里注册!

解决方案 »

  1.   

    给你贴一段代码,用CheckBox设置是否自启动,用按钮确认或取消: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;
      

  2.   

    窗体创建时读注册表值以设定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;
      

  3.   

    生成一个快捷方式,然后COPY到....../程序/启动/ 文件夹 中