Delphi 注册表启动项管理代码


本文整理自网络,侵删。

 


interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.StdCtrls, Vcl.Menus,
  Vcl.ExtCtrls, Vcl.Buttons;

type
  TForm1 = class(TForm)
    GroupBox1: TGroupBox;
    ListView1: TListView;
    StatusBar1: TStatusBar;
    PopupMenu1: TPopupMenu;
    N1: TMenuItem;
    N2: TMenuItem;
    N3: TMenuItem;
    TrayIcon1: TTrayIcon;
    Panel1: TPanel;
    CheckBox1: TCheckBox;
    Timer1: TTimer;
    procedure FormCreate(Sender: TObject);
    procedure N1Click(Sender: TObject);
    procedure N3Click(Sender: TObject);
    procedure ShowStart;
    procedure N2Click(Sender: TObject);
    procedure ListView1Change(Sender: TObject; Item: TListItem;
      Change: TItemChange);
    procedure Timer1Timer(Sender: TObject);

  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;
  var
  H1: THandle;
  dwRes,ID: DWORD;
  hNotify : THandle;
  hKeyx   : HKEY;
const
 RootR=HKEY_CLASSES_ROOT;
 RootU=HKEY_CURRENT_USER;
 RootL=HKEY_LOCAL_MACHINE;
 RootS=HKEY_USERS;
 RootC=HKEY_CURRENT_CONFIG;



implementation

{$R *.dfm}
 uses Registry;
procedure TForm1.FormCreate(Sender: TObject);
begin
ListView1.Clear;
ListView1.Columns.Clear;
ListView1.Columns.Add;
ListView1.Columns.Add;
ListView1.Columns.Add;

ListView1.Columns.Items[0].Caption:='根键位置';
ListView1.Columns.Items[1].Caption:='名称';
ListView1.Columns.Items[2].Caption:='路径';

ListView1.Columns.Items[0].Width:=85;
ListView1.Columns.Items[1].Width:=120;
ListView1.Columns.Items[2].Width:=215;

listView1.Checkboxes := true;
Listview1.ViewStyle:=vsreport;
Listview1.GridLines:=true;
ShowStart;
CheckBox1.Checked:=True;

end;
function ThreadFun1(p: Pointer): Integer; stdcall;
begin
 hNotify := CreateEvent( nil, //不使用SECURITY_ATTRIBUTES结构
        FALSE, //不自动重置
        TRUE,  //设置初始状态
        'RegistryNotify' //事件对象的名称
        );

  if hNotify = 0 then
  begin
    //Showmessage('CreateEvent failed.');
    Form1.StatusBar1.Panels[1].Text:='监视创建失败!';
    exit;
  end;

  if RegOpenKeyEx( HKEY_CURRENT_USER, //跟键
        'Software\Microsoft\Windows\CurrentVersion\Run', //子键
        0, //reserved
        KEY_NOTIFY, //监视用
        hKeyx //保存句柄
        ) <> ERROR_SUCCESS then
  begin
    CloseHandle( hNotify );
    //Showmessage('RegOpenKeyEx failed.');
    Form1.StatusBar1.Panels[1].Text:='打开主键失败!';
    exit;
  end;

  if RegNotifyChangeKeyValue( hKeyx, //监视子键句柄
        TRUE, //监视此项的子键
        REG_NOTIFY_CHANGE_NAME or REG_NOTIFY_CHANGE_LAST_SET,
        hNotify, //接受注册表变化事件的事件对象句柄
        TRUE //注册表变化前报告
        ) <> ERROR_SUCCESS then
  begin
    CloseHandle( hNotify );
    RegCloseKey( hKeyx );
    //Showmessage('RegNotifyChangeKeyValue failed');
    Form1.StatusBar1.Panels[1].Text:='监视变化失败!';
    exit;
  end;
  dwRes := WaitForSingleObject( hNotify, 60 * 1000 ); //监视一分钟
      if dwRes = 0 then
     //Showmessage( 'Registry will be changed.' );
         Form1.StatusBar1.Panels[1].Text:='注册表被改变!';

  CloseHandle( hNotify );
  RegCloseKey( hKeyx );
  CloseHandle(H1);
  Result := 0;

end;
procedure TForm1.ShowStart;
var T:TStringList;
REG,REG1:TRegistry;
I:integer;
begin
ListView1.Clear;
///HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Run
REG:=TRegistry.Create;
REG.RootKey:=HKEY_CURRENT_USER;
REG1:=TRegistry.Create;
REG1.RootKey:=HKEY_LOCAL_MACHINE;
T:=TStringList.Create;

if REG.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False) then
 begin
 //Reg.GetKeyNames(T);
 REG.GetValueNames(T);
 T.Sort;
  for I := 0 to t.Count-1 do
  begin
    with Form1.listview1.items.add do
         begin
         caption:='HKEY_CURRENT_USER';
         subitems.add(T.Strings[i]);
         subitems.add(Reg.ReadString(T.Strings[i]));
         //subitems.add(Reg.ReadString(''));
        end;
  end;
  REG.CloseKey;
  REG.Free;
 end;
 //SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Run
 if REG1.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False) then
 begin
 //Reg.GetKeyNames(T);
 REG1.GetValueNames(T);
 T.Sort;
  for I := 0 to T.Count-1 do
  begin
    with Form1.listview1.items.add do
         begin
         caption:='HKEY_LOCAL_MACHINE';
         subitems.add(T.Strings[i]);
         subitems.add(REG1.ReadString(T.Strings[i]));
        end;
  end;
  REG1.CloseKey;
  REG1.Free;
 end;
  T.Free;
end;
procedure TForm1.Timer1Timer(Sender: TObject);
begin
h1 := CreateThread(nil, 0, @ThreadFun1, nil, 0, ID); //启用线程
end;

procedure TForm1.ListView1Change(Sender: TObject; Item: TListItem;
  Change: TItemChange);
begin
StatusBar1.Panels[0].Text:='共: '+inttostr(ListView1.Items.Count)+' 条数据.';
end;

procedure TForm1.N1Click(Sender: TObject);
begin
ListView1.Clear;
ShowStart;
end;

procedure TForm1.N2Click(Sender: TObject);
var I:integer;
Reg,Reg1:TRegistry;
begin
//delete
for I := 0 to ListView1.Items.Count-1 do
   begin
     if ListView1.Items[i].Checked then
        begin
          if ListView1.Items[i].Caption='HKEY_CURRENT_USER' then
           begin
            Reg:=TRegistry.Create;
            Reg.RootKey:=HKEY_CURRENT_USER;
            try
            Reg.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False);
            Reg.deleteValue(listview1.Items[i].SubItems.strings[0]);
            Reg.CloseKey;
            finally
            Reg.Free;
            end;
           end else
           begin
           Reg1:=TRegistry.Create;
           Reg1.RootKey:=HKEY_LOCAL_MACHINE;
            try
            Reg1.OpenKey('Software\Microsoft\Windows\CurrentVersion\Run',False);
            Reg1.deleteValue(listview1.Items[i].SubItems.strings[0]);
            Reg1.CloseKey;
            finally
            Reg1.Free;
            end;
           end;

        end;
   end;
   ShowStart;
end;

procedure TForm1.N3Click(Sender: TObject);
var I:integer;
begin
for I := 0 to ListView1.Items.Count-1 do
    begin
    ListView1.Items[i].Checked:=True;
    end;

end;

end.

相关阅读 >>

Delphi共享软件防破解的实用招法

Delphi如何实现浏览文件夹

Delphi richedit根据鼠标位置定位光标的方法

Delphi firemonkey的tedit七大变化

Delphi 在电脑屏幕上显示图片

Delphi ado通用操作数据单元

Delphi xe7 ios 取得系统字型名称

Delphi ioutils tdirectory获取一个目录下所有文件名,包括子目录

Delphi windows 编程[6] - 学习窗体生成的过程六: 最终的代码!

Delphi access数据库 主键

更多相关阅读请进入《Delphi》频道 >>



打赏

取消

感谢您的支持,我会继续努力的!

扫码支持
扫码打赏,您说多少就多少

打开支付宝扫一扫,即可进行扫码打赏哦

分享从这里开始,精彩与您同在

评论

管理员已关闭评论功能...