如何使用DELPHI来实现WINDOWS自动关机?(100分)

  • 主题发起人 主题发起人 hdl18
  • 开始时间 开始时间
ExitWindowsEx(EWX_POWEROFF,0);
 
ExitWindowsEx(EWX_POWEROFF,0);<br><br>OK!<br>
 
100分给kangxy! &nbsp;
 
大家见者有分吧(怎么象是在分东西一样),不过这个就是用ExitWindowsEx,你可以用<br>Win32 Help来看
 
确实,好像这不值100分!
 
晚了,完了 &nbsp; _*
 
强制关机 ExitWindowsEx(EWX_SHUTDOWN or EWX_FORCE,0);<br>强制重启 ExitWindowsEx(EWX_REBOOT OR EWX_FORCE,0);<br>重新登录 ExitWindowsEx(EWX_LOGOFF,0);
 
我做了一个例子:<br>program AutoShutDown;<br><br>uses<br>&nbsp; Windows,<br>&nbsp; ShellAPI,<br>&nbsp; Sysutils,<br>&nbsp; Messages;<br><br>{$R *.RES}<br>var<br>&nbsp; WinClass:TWndClassA;<br>&nbsp; Handle:hwnd;<br>&nbsp; Inst,Button1, Label1, Edit1: Integer;<br>&nbsp; Msg: TMsg;<br>&nbsp; tid: TNotifyIconDataA;<br>&nbsp; sdt:tdatetime;<br>&nbsp; timerid:integer;<br>&nbsp; hFont: Integer;<br>const<br>&nbsp; AppName='AutoShutDown';<br>&nbsp; st='定时关机';<br>&nbsp; About=st+' 1.0, Keyes.chen@263.net, Keyes 1999.10.23';<br>&nbsp; gjs='设定关机时间';<br>procedure SetShutdownTime;<br>var<br>&nbsp; Textlength: Integer;<br>&nbsp; Text: PChar;i:tdatetime;<br>begin<br>&nbsp; TextLength := GetWindowTextLength(Edit1);<br>&nbsp; &nbsp; GetMem(Text, TextLength + 1);<br>&nbsp; &nbsp; GetWindowText(Edit1, Text, TextLength + 1);<br>&nbsp; &nbsp; try<br>&nbsp; &nbsp; &nbsp;i:=Strtodatetime(Text);<br>&nbsp; &nbsp; &nbsp;if i&lt;=now then<br>&nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp;MessageBox(handle,'不对吧!太早了.','错误',mb_ok or MB_ICONERROR);<br>&nbsp; &nbsp; &nbsp;exit;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;sdt:=i;<br>&nbsp; &nbsp; &nbsp;timerid:=Settimer(handle,1000,1000,nil);<br>&nbsp; &nbsp; &nbsp;Showwindow(handle,sw_hide);<br>&nbsp; &nbsp; &nbsp;lstrcpy (tid.szTip,pchar(st+' &nbsp;关机时间:'+Datetimetostr(sdt)));<br>&nbsp; &nbsp; &nbsp;Shell_NotifyIcon (nim_modify, @tid);<br>&nbsp; &nbsp; except<br>&nbsp; &nbsp; &nbsp;Killtimer(handle,timerid);<br>&nbsp; &nbsp; &nbsp;Messagebox(handle,pchar('关机时间设定错误'#13#10#13#10+'格式因该是:'+Datetimetostr(now)),AppName,Mb_ok or MB_ICONINFORMATION);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; FreeMem(Text, TextLength + 1);<br>end;<br>function WindowProc(hWnd, uMsg, wParam, lParam: Integer): Integer; stdcall;<br>var pt:tpoint;pm:Hmenu;<br>begin<br>&nbsp; result:=0;<br>&nbsp; Case uMsg of<br>&nbsp; wm_timer:<br>&nbsp; begin<br>&nbsp; &nbsp; if now&gt;=sdt then<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; Killtimer(handle,timerid);<br>&nbsp; &nbsp; ExitWindowsEx(EWX_SHUTDOWN or EWX_Force, 0);<br>// &nbsp; &nbsp;PostMessage(handle,wm_Destroy,0,0);<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; wm_User:<br>&nbsp; begin<br>&nbsp; &nbsp; Case lparam of<br>&nbsp; &nbsp; WM_LBUTTONDBLCLK:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; showwindow(handle,sw_restore);<br>&nbsp; &nbsp; setforegroundwindow(handle);<br>&nbsp; &nbsp; end;<br>&nbsp; &nbsp; wm_LButtonDown,wm_RButtonDown:<br>&nbsp; &nbsp; begin<br>&nbsp; &nbsp; GetCursorPos (pt);<br>&nbsp; &nbsp; pm := CreatePopupMenu;<br>&nbsp; &nbsp; AppendMenu (pm,0,Ord ('S'),gjs);<br>&nbsp; &nbsp; AppendMenu (pm, 0, Ord ('A'), '关于...');<br>&nbsp; &nbsp; AppendMenu (pm, mf_Separator, 0, Nil);<br>&nbsp; &nbsp; AppendMenu (pm, 0, Ord ('E'), '退出');<br>&nbsp; &nbsp; SetForegroundWindow (handle);<br>&nbsp; &nbsp; if TrackPopupMenu (pm, tpm_BottomAlign or tpm_RightAlign, pt.x,{GetDeviceCaps(dc,Vertres)}pt.y, 0, handle, Nil) then<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;SetForegroundWindow (handle);<br>&nbsp; &nbsp; DestroyMenu (pm)<br>&nbsp; &nbsp; end;<br>&nbsp; end;<br>&nbsp; end;<br>&nbsp; wm_Destroy:<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;Shell_NotifyIcon (nim_Delete, @tid);<br>&nbsp; &nbsp; &nbsp;Killtimer(handle,timerid);<br>&nbsp; &nbsp; &nbsp;halt;<br>&nbsp; end;<br>&nbsp; wm_Command:<br>&nbsp; begin<br>&nbsp; &nbsp; &nbsp;if (lParam = Button1) then begin SetshutdownTime;exit end;<br>&nbsp; &nbsp; &nbsp;Case Loword(wParam) of<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ord ('A'): MessageBox (0, About, AppName, mb_ok or MB_ICONINFORMATION);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ord ('E'): PostMessage (handle, wm_Close, 0, 0);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Ord ('S'):<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Showwindow(handle,sw_restore);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;end;<br>&nbsp; &nbsp; &nbsp;end;<br>&nbsp; end;<br>&nbsp; end;<br>&nbsp; Result := DefWindowProc(hWnd, uMsg, wParam, lParam);<br>end;<br><br>begin<br>&nbsp; { ** Register Custom WndClass ** }<br>&nbsp; if FindWindow (AppName, Nil) &lt;&gt; 0 then begin<br>&nbsp; Messagebox(handle,'已经有一个AutoShutDown运行了',AppName,mb_ok or MB_ICONWARNING);<br>&nbsp; halt(0);<br>&nbsp; end;<br>&nbsp; Inst := hInstance;<br>&nbsp; with WinClass do<br>&nbsp; begin<br>&nbsp; &nbsp; style &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= CS_CLASSDC or CS_PARENTDC;<br>&nbsp; &nbsp; lpfnWndProc &nbsp; &nbsp; &nbsp; &nbsp;:= @WindowProc;<br>&nbsp; &nbsp; hInstance &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= Inst;<br>&nbsp; &nbsp; hbrBackground &nbsp; &nbsp; &nbsp;:= color_btnface + 1;<br>&nbsp; &nbsp; lpszClassname &nbsp; &nbsp; &nbsp;:= AppName;<br>&nbsp; &nbsp; hCursor &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= LoadCursor(0, IDC_ARROW);<br>&nbsp; end;<br>&nbsp; RegisterClass(WinClass);<br>&nbsp; Handle := CreateWindowEx(WS_EX_WINDOWEDGE, AppName, 'AutoShutDown 1.00',<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;WS_VISIBLE {or WS_SIZEBOX} or WS_CAPTION or WS_SYSMENU,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;283, 238, 325, 65, 0, 0, Inst, nil);<br>&nbsp; Button1 := CreateWindow('Button', 'OK', WS_VISIBLE or WS_CHILD or BS_PUSHLIKE or BS_TEXT,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;236, 8, 75, 20, handle, 0, Inst, nil);<br>&nbsp; Label1 := Createwindow('Static', '', WS_VISIBLE or WS_CHILD or SS_LEFT,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;8, 12, 80, 13, Handle, 0, Inst, nil);<br>&nbsp; Edit1 := CreateWindowEx(WS_EX_CLIENTEDGE, 'Edit', Pchar(Datetimetostr(now)), WS_CHILD or WS_VISIBLE or<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; WS_BORDER {or ES_PASSWORD}, 88, 8, 141, 21, Handle, 0, Inst, nil);<br>&nbsp; hFont := CreateFont(-12, 0, 0, 0, 500, 0, 0, 0, GB2312_CHARSET,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; DEFAULT_PITCH or FF_DONTCARE, '宋体');<br>&nbsp; if hFont &lt;&gt; 0 then<br>&nbsp; begin<br>&nbsp; &nbsp; SendMessage(Button1, WM_SETFONT, hFont, 0);<br>&nbsp; &nbsp; SendMessage(Label1, WM_SETFONT, hFont, 0);<br>&nbsp; &nbsp; SendMessage(Edit1, WM_SETFONT, hFont, 0);<br>&nbsp; end;<br>&nbsp; SetWindowText(Label1, pchar(gjs+':'));<br>&nbsp; SetFocus(Edit1);<br>&nbsp; UpdateWindow(Handle);<br>&nbsp; &nbsp; &nbsp;tid.cbSize &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := sizeof (tid);<br>&nbsp; &nbsp; &nbsp;tid.Wnd &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= handle;<br>&nbsp; &nbsp; &nbsp;tid.uID &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= 1;<br>&nbsp; &nbsp; &nbsp;tid.uFlags &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; := nif_Message or nif_Icon or nif_Tip;<br>&nbsp; &nbsp; &nbsp;tid.uCallBackMessage := wm_User;<br>&nbsp; &nbsp; &nbsp;tid.hIcon &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;:= LoadIcon (hInstance, 'MAINICON');<br>&nbsp; &nbsp; &nbsp;lstrcpy (tid.szTip,st);<br>&nbsp; &nbsp; &nbsp;Shell_NotifyIcon (nim_Add, @tid);<br>&nbsp; while(GetMessage(Msg, Handle, 0, 0)) do<br>&nbsp; begin<br>&nbsp; &nbsp; TranslateMessage(msg);<br>&nbsp; &nbsp; DispatchMessage(msg);<br>&nbsp; end;<br>end.<br>
 
备注: win95,win98适用 ,Win NT ,Win2000不行,要先取得权限.
 
hehe,Keyes干脆来了一个程序,不错!<br><br>windows2000的一个问题,c++ build 4,d4装不进去了,
 
BOOL ExitWindowsEx(<br>&nbsp; UINT uFlags, &nbsp; &nbsp; &nbsp; // shutdown operation<br>&nbsp; DWORD dwReserved &nbsp; // reserved<br>);<br>&nbsp;<br>Parameters<br>uFlags:参数<br>&nbsp; 值:<br>&nbsp; &nbsp; &nbsp; &nbsp;EWX_LOGOFF:关闭所有程序以其它用户身份登录。 <br>&nbsp; &nbsp; &nbsp; &nbsp;EWX_POWEROFF:关闭电源,但机器必须支持关闭。NT中必须有 &nbsp;SE_SHUTDOWN_NAME 权限。<br>&nbsp; &nbsp; &nbsp; &nbsp;EWX_REBOOT:重启 NT中必须有SE_SHUTDOWN_NAME 权限。 <br>&nbsp; &nbsp; &nbsp; &nbsp;EWX_SHUTDOWN:关闭系统到“你可以安全的关闭计算机”NT中必须有 &nbsp; &nbsp;SE_SHUTDOWN_NAME 权限。<br><br>下列值也可选<br><br>&nbsp; &nbsp; &nbsp; EWX_FORCE :系统不改送 WM_QUERYENDSESSION 和 WM_ENDSESSION 消息.所有未保存的数据将丢失。<br><br>&nbsp; &nbsp; &nbsp; &nbsp;EWX_FORCEIFHUNG: Windows NT 5.0关闭程序如果它们不响应WM_QUERYENDSESSION 或 WM_ENDSESSION 消息. 如果设置了EWX_FORCE它将被忽略。 <br><br>dwReserved:保留。
 
我从MSDN中找到了一段关机的程序,是用VB写的。改成了Delphi的。<br><br>unit Unit1;<br><br>interface<br><br>uses<br>&nbsp; Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,<br>&nbsp; StdCtrls;<br><br>type<br>&nbsp; TForm1 = class(TForm)<br>&nbsp; &nbsp; Button1: TButton;<br>&nbsp; &nbsp; procedure Button1Click(Sender: TObject);<br><br>&nbsp; private<br>&nbsp; &nbsp; { Private declarations }<br>&nbsp; &nbsp; procedure AdjustToken;<br>&nbsp; public<br>&nbsp; &nbsp; { Public declarations }<br>&nbsp; end;<br><br>var<br>&nbsp; Form1: TForm1;<br><br>implementation<br><br>{$R *.DFM}<br><br>procedure TForm1.AdjustToken();<br>var<br>&nbsp; hdlProcessHandle : Cardinal;<br>&nbsp; hdlTokenHandle : Cardinal;<br>&nbsp; tmpLuid : Int64;<br>&nbsp; tkpPrivilegeCount : Int64;<br>&nbsp; tkp : TOKEN_PRIVILEGES;<br>&nbsp; tkpNewButIgnored : TOKEN_PRIVILEGES;<br>&nbsp; lBufferNeeded : Cardinal;<br>&nbsp; Privilege : array[0..0] of _LUID_AND_ATTRIBUTES;<br>begin<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;hdlProcessHandle := GetCurrentProcess;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;OpenProcessToken(hdlProcessHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;(TOKEN_ADJUST_PRIVILEGES Or TOKEN_QUERY),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; hdlTokenHandle);<br><br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Get the LUID for shutdown privilege.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;LookupPrivilegeValue('', 'SeShutdownPrivilege', tmpLuid);<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Luid := tmpLuid;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Privilege[0].Attributes := SE_PRIVILEGE_ENABLED;<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.PrivilegeCount := 1; &nbsp; // One privilege to set<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp.Privileges[0] := Privilege[0];<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// Enable the shutdown privilege in the access token of this<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;// process.<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;AdjustTokenPrivileges(hdlTokenHandle,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;False,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkp,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;Sizeof(tkpNewButIgnored),<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;tkpNewButIgnored,<br>&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;lBufferNeeded);<br><br>&nbsp;end;<br>procedure TForm1.Button1Click(Sender: TObject);<br>begin<br>&nbsp; &nbsp; &nbsp;AdjustToken;<br>&nbsp; &nbsp; &nbsp;ExitWindowsEx((EWX_SHUTDOWN Or EWX_FORCE Or EWX_REBOOT), $FFFF);<br>end;<br><br>end.<br><br>
 
我的这个对于Nt也好用,给我多一点分好不好?
 
呵呵,真是人才济济;-)
 
后退
顶部