mainunit.pas
上传用户:psxgmh
上传日期:2013-04-08
资源大小:15112k
文件大小:3k
- 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('SOFTWAREMICROSOFTWindowsCurrentVersionRun',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('SOFTWAREMICROSOFTWindowsCurrentVersionRun',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('SOFTWAREMICROSOFTWindowsCurrentVersionRunService',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.