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, StdCtrls, Buttons,Registry;
  6. type
  7.   TForm1 = class(TForm)
  8.     GroupBox1: TGroupBox;
  9.     RadioButton1: TRadioButton;
  10.     CheckBox1: TCheckBox;
  11.     RadioButton2: TRadioButton;
  12.     Edit1: TEdit;
  13.     Label1: TLabel;
  14.     Label2: TLabel;
  15.     Edit2: TEdit;
  16.     Button1: TButton;
  17.     BitBtn1: TBitBtn;
  18.     OpenDialog1: TOpenDialog;
  19.     procedure Button1Click(Sender: TObject);
  20.     procedure FormCreate(Sender: TObject);
  21.     procedure BitBtn1Click(Sender: TObject);
  22.     procedure RadioButton2Click(Sender: TObject);
  23.     procedure RadioButton1Click(Sender: TObject);
  24.   private
  25.     { Private declarations }
  26.   public
  27.     procedure WriteToCurUser;
  28.     procedure WriteToAllUserAsServ;
  29.     procedure WriteToAllUserAsApp;
  30.     { Public declarations }
  31.   end;
  32. var
  33.   Form1: TForm1;
  34. implementation
  35. {$R *.dfm}
  36. procedure TForm1.WriteToCurUser;
  37. var
  38.  Reg:TRegistry;
  39. begin
  40.  Reg:=TRegistry.Create;
  41.  try
  42.   Reg.RootKey:=HKEY_CURRENT_USER;
  43.   if Reg.OpenKey('SOFTWAREMICROSOFTWindowsCurrentVersionRun',True) then
  44.    begin
  45.     Reg.WriteString(Edit2.Text,Edit1.Text);
  46.    end;
  47.  finally
  48.   Reg.CloseKey;
  49.   Reg.Free;
  50.  end;
  51. end;
  52. procedure TForm1.WriteToAllUserAsApp;
  53. var
  54.  Reg:TRegistry;
  55. begin
  56.  Reg:=TRegistry.Create;
  57.  try
  58.   Reg.RootKey:=HKEY_Local_Machine;
  59.   if Reg.OpenKey('SOFTWAREMICROSOFTWindowsCurrentVersionRun',True) then
  60.    begin
  61.     Reg.WriteString(Edit2.Text,Edit1.Text);
  62.    end;
  63.  finally
  64.   Reg.CloseKey;
  65.   Reg.Free;
  66.  end;
  67. end;
  68. procedure TForm1.WriteToAllUserAsServ;
  69. var
  70.  Reg:TRegistry;
  71. begin
  72.  Reg:=TRegistry.Create;
  73.  try
  74.   Reg.RootKey:=HKEY_Local_Machine;
  75.   if Reg.OpenKey('SOFTWAREMICROSOFTWindowsCurrentVersionRunService',True) then
  76.    begin
  77.     Reg.WriteString(Edit2.Text,Edit1.Text);
  78.    end;
  79.  finally
  80.   Reg.CloseKey;
  81.   Reg.Free;
  82.  end;
  83. end;
  84. procedure TForm1.Button1Click(Sender: TObject);
  85. begin
  86.  if ((Edit1.Text='') or (Edit2.Text='')) then
  87.   begin
  88.    ShowMessage('请选择应用程序并指定启动项名称后,再尝试写入操作!');
  89.    if (Edit1.Text='') then
  90.     BitBtn1.Click
  91.    else
  92.     Edit2.SetFocus; 
  93.   end
  94.   else
  95.   begin
  96.    if RadioButton1.Checked then
  97.     begin
  98.      WriteToCurUser;
  99.      ShowMessage('该启动项被成功写入注册表!');
  100.     end
  101.    else
  102.    begin
  103.     if CheckBox1.Checked then
  104.      begin
  105.       WriteToAllUserAsServ;
  106.       ShowMessage('该启动项被成功写入注册表!');
  107.      end
  108.     else
  109.      begin
  110.       WriteToAllUserAsApp;
  111.       ShowMessage('该启动项被成功写入注册表!');
  112.      end;
  113.    end;
  114.   end;
  115. end;
  116. procedure TForm1.FormCreate(Sender: TObject);
  117. begin
  118.  OpenDialog1.InitialDir:=ExtractFileDir(Application.ExeName);
  119.  OpenDialog1.FileName:='';
  120.  CheckBox1.Enabled:=False;
  121. end;
  122. procedure TForm1.BitBtn1Click(Sender: TObject);
  123. begin
  124.  With OpenDialog1 do
  125.   begin
  126.    if Execute then
  127.     Edit1.Text:=OpenDialog1.FileName;
  128.   end;
  129. end;
  130. procedure TForm1.RadioButton2Click(Sender: TObject);
  131. begin
  132.  CheckBox1.Enabled:=True;
  133. end;
  134. procedure TForm1.RadioButton1Click(Sender: TObject);
  135. begin
  136.  CheckBox1.Enabled:=False;
  137. end;
  138. end.