Mirror

Shutdown, Reboot, Logoff, Monitor Off and Suspend mode functions (Views: 716)


Problem/Question/Abstract:

The way to make your computer to sleep, reboot or shutdown. It also have the code to force shutdown and force reboot. To try this example you need seven buttons. The Suspend Mode is a magic sendkey that I triped over and it force the computer in to suspend mode.

Answer:

unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    btnShutDown: TButton;
    btnReboot: TButton;
    btnLogOff: TButton;
    btnForceDown: TButton;
    btnForceReboot: TButton;
    btnMonitorOff: TButton;
    btnSuspend: TButton;
    procedure btnLogOffClick(Sender: TObject);
    procedure btnShutDownClick(Sender: TObject);
    procedure btnRebootClick(Sender: TObject);
    procedure btnForceDownClick(Sender: TObject);
    procedure btnForceRebootClick(Sender: TObject);
    procedure TimerEx1Timer(Sender: TObject);
    procedure btnMonitorOffClick(Sender: TObject);
    procedure btnSuspendClick(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.DFM}

procedure TForm1.btnLogOffClick(Sender: TObject);
begin
  if ExitWindowsEx(EWX_LOGOFF, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.btnShutDownClick(Sender: TObject);
begin
  if ExitWindowsEx(EWX_SHUTDOWN, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.btnRebootClick(Sender: TObject);
begin
  if ExitWindowsEx(EWX_REBOOT, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.btnForceDownClick(Sender: TObject);
begin
  if ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.btnForceRebootClick(Sender: TObject);
begin
  if ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.TimerEx1Timer(Sender: TObject);
begin
  if ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE, 1) = False then
    ShowMessage('Uable to comply !');
end;

procedure TForm1.btnMonitorOffClick(Sender: TObject);
begin
  SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 0);
end;

procedure TForm1.btnSuspendClick(Sender: TObject);
begin
  Keybd_event(8, 0, 0, 0); //I don't remember what I was doing when I found this.
  Keybd_event(95, 0, 0, 0);
end;

end.

<< Back to main page