简单的说:
这个例子显示了如何让Windows在启动时运行你的程序。
procedure TForm1.WMEndSession(var Message: TWMEndSession);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\RunOnce', True) then
begin
Reg.WriteString('MyApp','"' + ParamStr(0) + '"');
Reg.CloseKey;
end;
finally
Reg.Free;
inherited;
end;end;为了能调用这个方法,它必须声明在你主窗体的Class下:^_^
private procedure WMEndSession(var Msg:TWMEndSession); message WM_ENDSESSION;{这应该是一个Windows要关闭的消息}
这个例子显示了如何让Windows在启动时运行你的程序。
procedure TForm1.WMEndSession(var Message: TWMEndSession);
var
Reg: TRegistry;
begin
Reg := TRegistry.Create;
try
Reg.RootKey := HKEY_CURRENT_USER;
if Reg.OpenKey('\Software\Microsoft\Windows\CurrentVersion\RunOnce', True) then
begin
Reg.WriteString('MyApp','"' + ParamStr(0) + '"');
Reg.CloseKey;
end;
finally
Reg.Free;
inherited;
end;end;为了能调用这个方法,它必须声明在你主窗体的Class下:^_^
private procedure WMEndSession(var Msg:TWMEndSession); message WM_ENDSESSION;{这应该是一个Windows要关闭的消息}
可以重新运行你的程序!
该键值也可用在程序非正常退出后,重启Windows时你的程序可以自动运行。
具体实现方法:
(1)、程序启动时在runonce键中写入你的程序。
(2)、程序退出时删除该键值。
实现原理:
因为你的应用程序如非正常退出后,该键值未删除。Windows重新启动后,其会读取该键值,并运行你的程序。然后删除该键值,从而实现只运行一次。
这段程序是当你的应用程序正在运行时就被关机,当下次Windows启动时还会打开那个应用程序
When Windows starts up, it launches each application listed in the RunOnce key and then deletes the entry for that application.
当Windows启动时它将运行所有在RunOnce键里列出的程序,并删除该程序的入口。
Therefore, you do not need to remove the entry written here.
所以,你不用(自己)去掉在那里(RunOnce)写的程序入口。
为了能调用这个方法,它必须声明在你主窗体的Class下:
private procedure WMEndSession(var Msg:TWMEndSession); message WM_ENDSESSION;{这应该是一个Windows要关闭的消息}
The following example shows how to tell Windows to relaunch your application when Windows starts up if it was running when the system shut down. When Windows starts up, it launches each application listed in the RunOnce key and then deletes the entry for that application. Therefore, you do not need to remove the entry written here.
翻译一下:
下面的例子显示:当你的程序运行而系统关闭,这时当你重新启动windows时,该程序重新启动。当windows启动时,他把每个应用程序列表放置在runonce键中,然后删除应用程序的入口 ,但是你不必删除写在这里的键值
是Windows去删除,而不是程序本身去删除。