Author: Christer Johansson
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:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
7 StdCtrls;
8
9 type
10 TForm1 = class(TForm)
11 btnShutDown: TButton;
12 btnReboot: TButton;
13 btnLogOff: TButton;
14 btnForceDown: TButton;
15 btnForceReboot: TButton;
16 btnMonitorOff: TButton;
17 btnSuspend: TButton;
18 procedure btnLogOffClick(Sender: TObject);
19 procedure btnShutDownClick(Sender: TObject);
20 procedure btnRebootClick(Sender: TObject);
21 procedure btnForceDownClick(Sender: TObject);
22 procedure btnForceRebootClick(Sender: TObject);
23 procedure TimerEx1Timer(Sender: TObject);
24 procedure btnMonitorOffClick(Sender: TObject);
25 procedure btnSuspendClick(Sender: TObject);
26 private
27 { Private declarations }
28 public
29 { Public declarations }
30 end;
31
32 var
33 Form1: TForm1;
34
35 implementation
36
37 {$R *.DFM}
38
39 procedure TForm1.btnLogOffClick(Sender: TObject);
40 begin
41 if ExitWindowsEx(EWX_LOGOFF, 1) = False then
42 ShowMessage('Uable to comply !');
43 end;
44
45 procedure TForm1.btnShutDownClick(Sender: TObject);
46 begin
47 if ExitWindowsEx(EWX_SHUTDOWN, 1) = False then
48 ShowMessage('Uable to comply !');
49 end;
50
51 procedure TForm1.btnRebootClick(Sender: TObject);
52 begin
53 if ExitWindowsEx(EWX_REBOOT, 1) = False then
54 ShowMessage('Uable to comply !');
55 end;
56
57 procedure TForm1.btnForceDownClick(Sender: TObject);
58 begin
59 if ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE, 1) = False then
60 ShowMessage('Uable to comply !');
61 end;
62
63 procedure TForm1.btnForceRebootClick(Sender: TObject);
64 begin
65 if ExitWindowsEx(EWX_REBOOT + EWX_FORCE, 1) = False then
66 ShowMessage('Uable to comply !');
67 end;
68
69 procedure TForm1.TimerEx1Timer(Sender: TObject);
70 begin
71 if ExitWindowsEx(EWX_SHUTDOWN + EWX_FORCE, 1) = False then
72 ShowMessage('Uable to comply !');
73 end;
74
75 procedure TForm1.btnMonitorOffClick(Sender: TObject);
76 begin
77 SendMessage(Application.Handle, WM_SYSCOMMAND, SC_MONITORPOWER, 0);
78 end;
79
80 procedure TForm1.btnSuspendClick(Sender: TObject);
81 begin
82 Keybd_event(8, 0, 0, 0); //I don't remember what I was doing when I found this.
83 Keybd_event(95, 0, 0, 0);
84 end;
85
86 end.
|