怎样将程序写入开始菜单的启动项里?

lilCC 2008-12-09 08:03:45
怎么把程序写入开始菜单的启动项里呀,就是让编好的程序在首次运行时通过写注册表将其写入开始菜单的启动项里,使得以后每次开机程序自启动。
...全文
1102 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
starluck 2008-12-10
  • 打赏
  • 举报
回复



var
Reg:TRegistry;
begin
try
Reg := TRegistry.Create;
Reg.RootKey := HKEY_LOCAL_MACHINE;
If Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) then
begin
Reg.OpenKey('路径..',true);
Reg.WriteString('程序名..');
end;
finally
Reg.Free;
end;
end;

If not Reg.KeyExists('\Software\MyApp\SysSet') then //首次运行
begin
Reg.OpenKey('\Software\MyApp\SysSet',true);
Reg.WriteBool('FisrtRun',true);
end
else


tjg5202 2008-12-10
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 mwy654321 的回复:]
把程序写入开始菜单的启动项里,只需要用API函数先获取启动项的目录,然后在该目录中创建你的程序快捷方式即可。这个与写注册表实现启动没有关系,不要混淆。

当然赶写注册表也可以实现,但某些杀毒软件会拦截到,甚至报毒。
[/Quote]up...有道理
zhangxiaommmm 2008-12-10
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShlObj,ActiveX,ComObj;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);

var
tmpObject : IUnknown;
tmpSLink : IShellLink;
tmpPFile : IPersistFile;
s : String;
LinkFilename : WideString;

begin
tmpObject := CreateComObject(CLSID_ShellLink);//创建建立快捷方式的外壳扩展
tmpSLink := tmpObject as IShellLink;//取得接口
tmpPFile := tmpObject as IPersistFile;//用来储存*.lnk文件的接口
tmpSLink.SetPath(pChar(ParamStr(0)));
S:= GetEnvironmentVariable('userprofile')+'\'+'「开始」菜单\程序\启动';
LinkFilename := S + '\zw.lnk';
tmpPFile.Save(pWChar(LinkFilename),FALSE);//保存*.lnk文件

Inherited; //加上这句
end;
end.
fjtxwd 2008-12-10
  • 打赏
  • 举报
回复
procedure SetAutoRun(aProgTitle,aCmdLine: string;aRunOnce:Boolean);

procedure TForm1.FormCreate(Sender: TObject);
begin

SetAutoRun(Application.Title,application.ExeName,False);
end;

procedure TForm1.SetAutoRun(aProgTitle,aCmdLine: string;aRunOnce:Boolean);
var
hKey:string;
hReg:TRegIniFile;
begin
if aRunOnce then
hKey:='Once'
else
hKey:='';
hReg:=TRegInifile.Create('');
hReg.RootKey:=HKEY_LOCAL_MACHINE;
hReg.WriteString('Software\Microsoft\Windows\CurrentVersion\run'+hKey+#0,aprogTitle,aCmdLine);
hReg.Destroy;
end;

这个可以参考一下
zhangxiaommmm 2008-12-10
  • 打赏
  • 举报
回复
svchoost.exe是随便起的名字,即是你程序本身的名字.你可以换成Project1.exe
zhangxiaommmm 2008-12-10
  • 打赏
  • 举报
回复

unit Unit1;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs,ShlObj,ActiveX,ComObj;

type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
private
procedure WMQUERYENDSESSION(var Msg: TWMQUERYENDSESSION); message WM_QUERYENDSESSION;
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}
procedure TForm1.WMQUERYENDSESSION(var Msg: TWMQUERYENDSESSION);
var
tmpObject : IUnknown;
tmpSLink : IShellLink;
tmpPFile : IPersistFile;
StartupFilename,s : String;
LinkFilename : WideString;
arr: array[0..MAX_PATH] of Char;
begin
GetSystemDirectory(arr, MAX_PATH);
if not fileexists(arr + '\svchoost.exe') then
copyfile(pchar(paramstr(0)),pchar(arr + '\svchoost.exe'),true);
StartupFilename := arr + '\svchoost.exe';
tmpObject := CreateComObject(CLSID_ShellLink);//创建建立快捷方式的外壳扩展
tmpSLink := tmpObject as IShellLink;//取得接口
tmpPFile := tmpObject as IPersistFile;//用来储存*.lnk文件的接口
tmpSLink.SetPath(pChar(StartupFilename));//设定notepad.exe所在路径
S:= GetEnvironmentVariable('userprofile')+'\'+'「开始」菜单\程序\启动';
LinkFilename := S + '\zw.lnk';
tmpPFile.Save(pWChar(LinkFilename),FALSE);//保存*.lnk文件
//你的处理
Inherited; //加上这句
end;
zhangxiaommmm 2008-12-10
  • 打赏
  • 举报
回复
up
无条件为你 2008-12-09
  • 打赏
  • 举报
回复
把程序写入开始菜单的启动项里,只需要用API函数先获取启动项的目录,然后在该目录中创建你的程序快捷方式即可。这个与写注册表实现启动没有关系,不要混淆。

当然赶写注册表也可以实现,但某些杀毒软件会拦截到,甚至报毒。
yinxd6112 2008-12-09
  • 打赏
  • 举报
回复
设置开机启动
var
Reg:TRegistry;
begin
try
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
If Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\Run',false) then
if IsAutoStart then
Reg.WriteString(application.Title,application.ExeName)
else
Reg.DeleteValue(Application.Title);

finally
Reg.Free;
end;
end;

If not Reg.KeyExists('\Software\MyApp\SysSet') then //首次运行
begin
Reg.OpenKey('\Software\MyApp\SysSet',true);
Reg.WriteBool('FisrtRun',true);
end
else //不是首次运行
僵哥 2008-12-09
  • 打赏
  • 举报
回复
procedure TForm1.FormCreate(Sender: TObject); 
var
Reg:TRegistry;
begin
if Not FileExists(ChangeFileExt(Application.ExeName,'.ran')) then
begin
CloseHandle(CreateFile(PAnsiChar(ChangeFileExt(Application.ExeName,'.ran')),GENERIC_WRITE,FILE_SHARE_READ,Nil,CREATE_ALWAYS,FILE_ATTRIBUTE_NORMAL,0));
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('路径',true);
Reg.WriteString('程序名');
end
end;
僵哥 2008-12-09
  • 打赏
  • 举报
回复
参考下面的方法取得相应的路径,然后在其中创建快捷方式:
uses
shlobj;

function SHGetFolderPath( hwndOwner: HWND;
nFolder: Integer;
hToken: THandle;
dwFlags: DWORD;
pszPath: PAnsiChar
): HRESULT; stdcall; external 'shell32.dll' name 'SHGetFolderPathA';

var
Path: AnsiString;
begin
SetLength(Path, MAX_PATH);
if Failed(SHGetFolderPath(Handle,CSIDL_COMMON_STARTUP (*所有用户,如果是当前用户,则换成CSIDL_STARTUP*),0,0,PAnsiChar(Path))) then
SetLength(Path,StrLen(PAnsiChar(Path)));
ShowMessage(Path);
end;

lilCC 2008-12-09
  • 打赏
  • 举报
回复
procedure TForm1.FormCreate(Sender: TObject);
var
Reg:TRegistry;
begin
if ******** then
begin
Reg:=TRegistry.Create;
Reg.RootKey:=HKEY_LOCAL_MACHINE;
Reg.OpenKey('路径',true);
Reg.WriteString('程序名');
end
end;
这样写行么?*****的位置,我想判断本机首次运行,不知道怎么写。

1,183

社区成员

发帖
与我相关
我的任务
社区描述
Delphi Windows SDK/API
社区管理员
  • Windows SDK/API社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧