300分求定时关机原程序,原理。

qufo 2003-05-23 12:15:01
RT.
因电脑是公司人公用的,所以他们在走的时候并不关机。

我想写一个程序定时把机器给关了。

不知该怎么做。

环境(Delphi 6.0 + Win98)

谁提供源程序,原理,分分次给出.
...全文
126 13 打赏 收藏 转发到动态 举报
写回复
用AI写文章
13 条回复
切换为时间正序
请发表友善的回复…
发表回复
dingfeng1977 2003-05-23
  • 打赏
  • 举报
回复
但是机箱是ATX,主机电源无法切断
qufo 2003-05-23
  • 打赏
  • 举报
回复
搞定。
dudunono 2003-05-23
  • 打赏
  • 举报
回复
up
qxj 2003-05-23
  • 打赏
  • 举报
回复
:)
belllab 2003-05-23
  • 打赏
  • 举报
回复
一楼的就够了
firetoucher 2003-05-23
  • 打赏
  • 举报
回复
:)
我也来灌点水:
功能无非两个:定时和关机,其实两个在以前的帖子都有
1 定时
你可以简单使用timer,如果你想精确到毫秒级那另当别论,不过我觉得简单一个timer就可以满足你这程序的需要
2 关机
关机的方法,如果你想强行关机可以使用:
ExitWindowsEx(EWX_POWEROFF or EWX_SHUTDOWN or EWX_FORCE,0)
该api用法很简单,你可以自己在msdn 里pick it.
--------------------------------------
看见了么,
那支蛾子,
正飞向太阳,
那就是我!
--------------------------------------
liclin 2003-05-23
  • 打赏
  • 举报
回复
用Timer加API函数ExitWindowsEx一下子就能搞掂了
不过对于win2000,要先提升权限
lance09 2003-05-23
  • 打赏
  • 举报
回复
感动中...............
shadowfish 2003-05-23
  • 打赏
  • 举报
回复
呵呵
zhang21cnboy 2003-05-23
  • 打赏
  • 举报
回复
哈哈,真有热心人啊!
yoisyois 2003-05-23
  • 打赏
  • 举报
回复
OK
:)
智商无下限 2003-05-23
  • 打赏
  • 举报
回复
楼上的程序已经可以了,对98和2000都可用:
对于WIN98来说直接使用 ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0);就可以了,如果要实现定时关机,就把它放到定时器中即可!
对于WIN2000则不可以这样简单关机: 因为2000下关机需要一定的权限,上面程序中的
function GetOperatingSystem: string;
是判断操作系统版本的函数,OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
用来获取权限,有了相应的权限就可以关闭系统了!
这个函数ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0);中的不同参数可以设置不同的关机类型,比如关闭,重新启动等等。。。
hnzwei2008 2003-05-23
  • 打赏
  • 举报
回复
unit AutoShut;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls, Menus,AppEvnts,shellapi, WinSkinForm,
WinSkinData;
type
Tformautoshut = class(TForm)
Timer1: TTimer;
Timer2: TTimer;
Edit1: TEdit;
Edit2: TEdit;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
button1: TButton;
button2: TButton;
SkinData1: TSkinData;
WinSkinForm1: TWinSkinForm;
GroupBox1: TGroupBox;
procedure Timer1Timer(Sender: TObject);
procedure button1Click(Sender: TObject);
procedure button2Click(Sender: TObject);
procedure Timer2Timer(Sender: TObject);

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

var
formautoshut: Tformautoshut;
P,Ti1:Pchar;
Flags:Longint;
i:integer;
TimeDelay:integer;
atom:integer;
implementation

uses main;
{$R *.dfm}


{判断时间S格式是否是有效}
function IsValidTime(s:string):bool;
begin
if Length(s)<>5 then IsValidTime:=False
else
begin
if (s[1]<'0') or (s[1]>'2') or (s[2]<'0') or
(s[2]>'9') or (s[3] <> ':') or
(s[4]<'0') or (s[4]>'5') or
(s[5]<'0') or (s[5]>'9')then IsValidTime:=False
else
IsValidTime:=True;
end;
end;

{判断是哪类操作系统,以确定关机方式}
function GetOperatingSystem: string;
var osVerInfo: TOSVersionInfo;
begin
Result :='';
osVerInfo.dwOSVersionInfoSize := SizeOf(TOSVersionInfo);
if GetVersionEx(osVerInfo) then
case osVerInfo.dwPlatformId of
VER_PLATFORM_WIN32_NT:
begin
Result := 'Windows NT/2000/XP'
end;
VER_PLATFORM_WIN32_WINDOWS:
begin
Result := 'Windows 95/98/98SE/Me';
end;
end;
end;

{获得计算机名}
function GetComputerName: string;
var
buffer: array[0..MAX_COMPUTERNAME_LENGTH + 1] of Char;
Size: Cardinal;
begin
Size := MAX_COMPUTERNAME_LENGTH + 1;
Windows.GetComputerName(@buffer, Size);
Result := strpas(buffer);
end;

{定时关机函数 ,各参数的意义如下:
Computer: 计算机名;Msg:显示的提示信息;
Time:时间延迟; Force:是否强制关机;
Reboot: 是否重启动}
function TimedShutDown(Computer: string; Msg: string;
Time: Word; Force: Boolean; Reboot: Boolean): Boolean;
var
rl: Cardinal;
hToken: Cardinal;
tkp: TOKEN_PRIVILEGES;
begin
{获得用户关机特权,仅对Windows NT/2000/XP}
OpenProcessToken(GetCurrentProcess, TOKEN_ADJUST_PRIVILEGES or TOKEN_QUERY, hToken);
if LookupPrivilegeValue(nil, 'SeShutdownPrivilege', tkp.Privileges[0].Luid) then
begin
tkp.Privileges[0].Attributes := SE_PRIVILEGE_ENABLED;
tkp.PrivilegeCount := 1;
AdjustTokenPrivileges(hToken, False, tkp, 0, nil, rl);
end;
Result := InitiateSystemShutdown(PChar(Computer), PChar(Msg), Time, Force, Reboot)
end;



procedure Tformautoshut.Timer1Timer(Sender: TObject);
begin
Edit1.Text:=FormatDateTime('hh:mm', Now);
{两个时间相等,计算机将在TimeDelay秒内强制关机}
if edit1.text=edit2.Text then
Begin
TimeDelay:=160;
timer1.Enabled:=False;
if GetOperatingSystem='Windows NT/2000/XP' then
begin
{调用系统的关机提示窗口,只限于Windows NT/2000/XP。}
TimedShutDown(getcomputername, ' 系统即将要关机!请保存好你的数据退出!如要取消自动关机,请再点击自动关机设置中的放弃!',
TimeDelay, true, false);
button2.Enabled :=true;
timer2.Enabled :=true;
end;
if GetOperatingSystem='Windows 95/98/98SE/Me' then
begin
timer2.Enabled :=true;//直接关机
end;
end;
end;

procedure Tformautoshut.Timer2Timer(Sender: TObject);
begin
button2.Enabled :=true;
label3.Caption :='现在离关机时间还有'+inttostr(timedelay)+'秒。';
if timedelay>0 then timedelay:=timedelay-1
else
begin
timer2.Enabled :=false;
{强制Windows 95/98/98SE/Me关机}
ExitWindowsEx(EWX_SHUTDOWN+EWX_FORCE,0);
end;
end;

{确定按钮}
procedure Tformautoshut.button1Click(Sender: TObject);
begin
button2.Enabled :=false;
label3.Caption :='提示:关机时间格式 HH:MM';
if timer1.Enabled =false then timer1.Enabled :=true;
{关机时间设置有效,程序将显示在托盘中,无效则提示。}
if IsValidTime(edit2.Text) then
begin
formautoshut.Hide;
formautoshut.close;
end
else
showmessage('提示:时间格式错误,'+chr(13)+
'请输入正确的关机时间 HH:MM。');
end;

{取消关机按钮}
procedure Tformautoshut.button2Click(Sender: TObject);
begin
if GetOperatingSystem='Windows NT/2000/XP' then
{对于Windows NT/2000/XP,取消关机}
begin
AbortSystemShutdown(pchar(getcomputername));
close;
end;
{停止倒记时}
if timer2.Enabled =true then timer2.Enabled :=false;
button2.Enabled :=false;
end;


end.

1,183

社区成员

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

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