如何调用WINDOWS关机画面(100分)

  • 主题发起人 主题发起人 张自骞
  • 开始时间 开始时间

张自骞

Unregistered / Unconfirmed
GUEST, unregistred user!
各位大侠:
请问如何在程序中直接调用WINDOWS关机画面,即调用开始菜单--》关闭系统时显示
的画面?
 
function TerminateAllProcesses:Boolean;
begin
ExecuteFile('C:/Windows/Rundll32.exe','user,exitwindows','',0);
Application.Terminate;
end;


This is an example of using this function

...
...
if NOT TerminateAllProcesses then begin;
ShowMessage('不能退出Windows。');
end;
...
 
感谢LanHer!但是ExecuteFile('C:/Windows/Rundll32.exe','user,exitwindows','',0);
无法编译!
 
控制WINDOWS的开关:如关闭WINDOWS,重新启动WINDOWS等, ExitWindowsEx(UINT uFlags,DWORD dwReserved);是实现这一功能的API函数
首先定义常数
const
EWX_FORCE=4; //关闭所有程序并以其他用户身份登录
EWX_LOGOFF=0; //重新启动计算机并切换到MS-DOS方式
EWX_REBOOT=2; //重新启动计算机
EWX_SHUTDOWN=1;//关闭计算机
运行时给How赋值,让他等于EWX_SHUTDOWN或其他,调用以下语句
ExitWindowsEx(How,0);


 
将这个保存成pas文件,然后安装上,产生两个控件,很好用的。
unit ExitDialogs;

{**********************************************************}
{ }
{ TShutdownDialog & TRestartDialog Unit }
{ Copyright ?999 Workshell Software. }
{ }
{ Version 1.0 }
{ }
{ }
{ Web -> http://www.workshell.uni.cc/ }
{ E - Mail -> exitdlgs@kinsella.u-net.com }
{ }
{**********************************************************}

interface

uses
Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
TShutdownDialog = class(TComponent)
private
{ Private declarations }
protected
{ Protected declarations }
public
{ Public declarations }
procedure Execute;
published
{ Published declarations }
end;

type
TRestartKind = (rkLogoff, rkShutdown, rkReboot, rkRestart);

type
TRestartDialog = class(TComponent)
private
{ Private declarations }
FRestartKind: TRestartKind;
procedure SetRestartKind(Value: TRestartKind);
protected
{ Protected declarations }
constructor Create(AOwner: TComponent); override;
public
{ Public declarations }
function Execute: Boolean;
function ExecuteEx(Kind: TRestartKind): Boolean;
published
{ Published declarations }
property RestartKind: TRestartKind read FRestartKind write SetRestartKind;
end;

var
Flags: LongInt = 2;

procedure Register;

implementation

procedure ExitWindowsDialog(ParentWnd: HWND); stdcall;
external 'Shell32.dll' index 60;

function RestartDialog(ParentWnd: HWND; Reason: PAnsiChar; Flags: LongInt): LongInt;
stdcall; external 'Shell32.dll' index 59;

procedure TShutdownDialog.Execute;
begin
ExitWindowsDialog(0);
end;

constructor TRestartDialog.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
RestartKind := rkRestart;
end;

function TRestartDialog.Execute: Boolean;
begin
case RestartKind of
rkLogoff: Flags := EWX_LOGOFF;
rkShutdown: Flags := EWX_SHUTDOWN;
rkReboot: Flags := EWX_REBOOT;
rkRestart: Flags := EW_RESTARTWINDOWS;
end;
if RestartDialog(0,nil,Flags) = IDYES then
begin
Result := True;
end
else
Result := False;
end;

function TRestartDialog.ExecuteEx(Kind: TRestartKind): Boolean;
begin
case Kind of
rkLogoff: Flags := EWX_LOGOFF;
rkShutdown: Flags := EWX_SHUTDOWN;
rkReboot: Flags := EWX_REBOOT;
rkRestart: Flags := EW_RESTARTWINDOWS;
end;
if RestartDialog(0,nil,Flags) = IDYES then
begin
Result := True;
end
else
Result := False;
end;

procedure TRestartDialog.SetRestartKind(Value: TRestartKind);
begin
if Value <> FRestartKind then
begin
FRestartKind := Value;
end;
end;

procedure Register;
begin
RegisterComponents('Dialogs', [TShutdownDialog,TRestartDialog]);
end;

end.
 
多人接受答案了。
 
后退
顶部