啟動自動載入程式碼(註冊表實現)

來源:互聯網
上載者:User

unit mainunit;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, Buttons,Registry;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    RadioButton1: TRadioButton;
    CheckBox1: TCheckBox;
    RadioButton2: TRadioButton;
    Edit1: TEdit;
    Label1: TLabel;
    Label2: TLabel;
    Edit2: TEdit;
    Button1: TButton;
    BitBtn1: TBitBtn;
    OpenDialog1: TOpenDialog;
    procedure Button1Click(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BitBtn1Click(Sender: TObject);
    procedure RadioButton2Click(Sender: TObject);
    procedure RadioButton1Click(Sender: TObject);
  private
    { Private declarations }
  public
    procedure WriteToCurUser;
    procedure WriteToAllUserAsServ;
    procedure WriteToAllUserAsApp;
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.WriteToCurUser;
var
 Reg:TRegistry;
begin
 Reg:=TRegistry.Create;
 try
  Reg.RootKey:=HKEY_CURRENT_USER;
  if Reg.OpenKey('\SOFTWARE\MICROSOFT\Windows\CurrentVersion\Run',True) then
   begin
    Reg.WriteString(Edit2.Text,Edit1.Text);
   end;
 finally
  Reg.CloseKey;
  Reg.Free;
 end;
end;

procedure TForm1.WriteToAllUserAsApp;
var
 Reg:TRegistry;
begin
 Reg:=TRegistry.Create;
 try
  Reg.RootKey:=HKEY_Local_Machine;
  if Reg.OpenKey('\SOFTWARE\MICROSOFT\Windows\CurrentVersion\Run',True) then
   begin
    Reg.WriteString(Edit2.Text,Edit1.Text);
   end;
 finally
  Reg.CloseKey;
  Reg.Free;
 end;
end;

procedure TForm1.WriteToAllUserAsServ;
var
 Reg:TRegistry;
begin
 Reg:=TRegistry.Create;
 try
  Reg.RootKey:=HKEY_Local_Machine;
  if Reg.OpenKey('\SOFTWARE\MICROSOFT\Windows\CurrentVersion\RunService',True) then
   begin
    Reg.WriteString(Edit2.Text,Edit1.Text);
   end;
 finally
  Reg.CloseKey;
  Reg.Free;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
 if ((Edit1.Text='') or (Edit2.Text='')) then
  begin
   ShowMessage('請選擇應用程式並指定啟動項名稱後,再嘗試寫入操作!');
   if (Edit1.Text='') then
    BitBtn1.Click
   else
    Edit2.SetFocus;
  end
  else
  begin
   if RadioButton1.Checked then
    begin
     WriteToCurUser;
     ShowMessage('該啟動項被成功寫入註冊表!');
    end
   else
   begin
    if CheckBox1.Checked then
     begin
      WriteToAllUserAsServ;
      ShowMessage('該啟動項被成功寫入註冊表!');
     end
    else
     begin
      WriteToAllUserAsApp;
      ShowMessage('該啟動項被成功寫入註冊表!');
     end;
   end;
  end;
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
 OpenDialog1.InitialDir:=ExtractFileDir(Application.ExeName);
 OpenDialog1.FileName:='';
 CheckBox1.Enabled:=False;
end;

procedure TForm1.BitBtn1Click(Sender: TObject);
begin
 With OpenDialog1 do
  begin
   if Execute then
    Edit1.Text:=OpenDialog1.FileName;
  end;
end;

procedure TForm1.RadioButton2Click(Sender: TObject);
begin
 CheckBox1.Enabled:=True;
end;

procedure TForm1.RadioButton1Click(Sender: TObject);
begin
 CheckBox1.Enabled:=False;
end;

end.

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.