Thema: Delphi Autostart

Einzelnen Beitrag anzeigen

hathor
(Gast)

n/a Beiträge
 
#10

AW: Autostart

  Alt 9. Okt 2015, 20:26
AUTOSTART geht auch anders - über die Registry:

Delphi-Quellcode:
uses
Registry;

function CreateAutorunEntry(const AName, AFilename: String; active: Boolean): Boolean;
var
 Reg: TRegistry;
begin
 Reg := TRegistry.create;
 try
     Reg.Rootkey:= HKEY_CURRENT_USER;
     Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True);
     if active = true then
     Reg.WriteString(AName, AFilename)
     else
     Reg.DeleteValue(AName);
 finally
   Reg.Free;
   Result := true;
 end;
end;

Aufruf:
procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateAutorunEntry(Application.Title, ParamStr(0), TRUE);
end;
Test-Programm

Delphi-Quellcode:
unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, System.Win.Registry, Vcl.StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    CheckBox1: TCheckBox;
    Button2: TButton;
    Button3: TButton;
    procedure Button1Click(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

function CreateAutorunEntry(const AName, AFilename: String; active: Boolean): Boolean;
var
 Reg: TRegistry;
begin
 Reg := TRegistry.create;
 try
     Reg.Rootkey:= HKEY_CURRENT_USER;
     Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True);
     if active = true then
     Reg.WriteString(AName, AFilename)
     else
     Reg.DeleteValue(AName);
 finally
   Reg.Free;
   Result := true;
 end;
end;

// [HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Run]
function IsAutoRun : Boolean;
var Reg : TRegistry;
begin
 Reg := TRegistry.create;
 try
     Reg.Rootkey:= HKEY_CURRENT_USER;
     Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run', True);
// Form1.CheckBox1.Checked :=
     Result:= Reg.ValueExists(Application.Title);
 finally
   Reg.Free;
 end;
end;

procedure TForm1.Button1Click(Sender: TObject);
begin
  CreateAutorunEntry(Application.Title, ParamStr(0), TRUE);
   Sleep(1000); Winapi.Windows.Beep(1000,100); Winapi.Windows.Beep(2000,100);
   CheckBox1.Checked:= IsAutoRun;
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
  CheckBox1.Checked:= IsAutoRun;
end;

procedure TForm1.Button3Click(Sender: TObject);
begin
   CreateAutorunEntry(Application.Title, ParamStr(0), FALSE);
   Sleep(1000); Winapi.Windows.Beep(2000,100); Winapi.Windows.Beep(1000,100);
   CheckBox1.Checked:= IsAutoRun;
end;

procedure TForm1.FormShow(Sender: TObject);
begin
  Caption:= Application.Title;
  CheckBox1.Checked:= IsAutoRun;
end;

end.
Miniaturansicht angehängter Grafiken
autoruntest.jpg  
Angehängte Dateien
Dateityp: zip AutoRunTest.zip (649,4 KB, 15x aufgerufen)

Geändert von hathor ( 9. Okt 2015 um 21:40 Uhr)
  Mit Zitat antworten Zitat