procedure TPwrSave.WndProc(var Msg: TMessage);
var Allow: boolean;
begin
inherited;
if Msg.Msg = WM_POWERBROADCAST then
case Msg.WParam of
PBT_APMQUERYSUSPEND:
begin
if (Msg.LParam and 1) = 1 then
Allow := QuerySuspend // Check for User Interaction
else
Allow := AllowSuspend;
if Allow then
Msg.Result := integer(true)
else
Msg.Result := BROADCAST_QUERY_DENY;
end;
PBT_APMQUERYSTANDBY:
begin
if (Msg.LParam and 1) = 1 then
Allow := QueryStandby // Check for User Interaction
else
Allow := AllowStandby;
if Allow then
Msg.Result := integer(true)
else
Msg.Result := BROADCAST_QUERY_DENY;
end;
PBT_APMBATTERYLOW:
if Assigned(FOnBatteryLow) then
FOnBatteryLow(Self);
PBT_APMPOWERSTATUSCHANGE:
PowerStatusChange;
PBT_APMOEMEVENT:
if Assigned(FOnOemEvent) then
FOnOemEvent(Self, Msg.LParam);
PBT_APMQUERYSUSPENDFAILED:
if Assigned(FOnQuerySuspendFailed) then
FOnQuerySuspendFailed(Self);
PBT_APMQUERYSTANDBYFAILED:
if Assigned(FOnQueryStandbyFailed) then
FOnQueryStandbyFailed(Self);
PBT_APMRESUMECRITICAL:
if Assigned(FOnResumeCritical) then
FOnResumeCritical(Self);
PBT_APMRESUMESUSPEND:
if Assigned(FOnResumeSuspend) then
FOnResumeSuspend(Self);
PBT_APMRESUMESTANDBY:
if Assigned(FOnResumeStandby) then
FOnResumeStandby(Self);
PBT_APMSUSPEND:
if Assigned(FOnSuspend) then
FOnSuspend(Self);
PBT_APMSTANDBY:
if Assigned(FOnStandby) then
FOnStandby(Self);
end
else
if Msg.Msg = WM_QUERYENDSESSION then
begin
Msg.Result := integer(QueryEndSession);
end;
end;
function TPwrSave.QuerySuspend: boolean;
begin
if Assigned(FOnQuerySuspend) then
FOnQuerySuspend(Self, Result)
else
Result := AllowSuspend;
end;
function TPwrSave.QueryStandby: boolean;
begin
if Assigned(FOnQueryStandby) then
FOnQueryStandby(Self, Result)
else
Result := AllowStandby;
end;
function TPwrSave.QueryEndSession: boolean;
begin
if Assigned(FOnQueryEndSession) then
FOnQueryEndSession(Self, Result)
else
Result := AllowEndSession;
end;
procedure TPwrSave.PowerStatusChange;
var Status: _SYSTEM_POWER_STATUS;
begin
if Assigned(FOnPowerStatusChange) then
if GetSystemPowerStatus(Status) then
begin
FOnPowerStatusChange(Self, Status.ACLineStatus, Status.BatteryFlag,
Status.BatteryLifePercent, Status.BatteryLifeTime,
Status.BatteryFullLifeTime);
end;
end;
constructor TPwrSave.Create(AOwner: TComponent);
begin
inherited;
if not (csDesigning in ComponentState) then
FHWnd := AllocateHWnd(WndProc);
end;
destructor TPwrSave.Destroy;
begin
if not (csDesigning in ComponentState) then
DeallocateHWnd(FHwnd);
inherited;
end;
在网上找了段相关代码,好象是Delphi的,不懂仅供参考!
unit PwrSave;
//==============================================================================
// Purpose:
// Windows has the ability to shutdown / hibernate / standby when no user
// interaction takes place for a certain time. When writing programs for
// data aqcuisition, control systems or servers this might be very undesired.
// This component will prevent these events and/or gives you the opportunity
// to take necessary action when such events occur.
// In addition this program can as well prevent shutdown / log off initiated
// by the user.
//
// Function:
// This little component will catch the listed Windows messages that might
// intercept program execution.
// WM_POWERBROADCAST with
// PBT_APMQUERYSUSPEND -> OnQuerySuspend
// PBT_APMQUERYSTANDBY -> OnQueryStandby
// WM_ENDSESSION -> OnQueryEndSession
//
// It will fire the corresponding events, if an eventhandler has been assigned.
// If no eventhandler is assigned or user interaction is not allowed by Windows,
// then it will respond to the messages as defined by the properties:
// AllowSuspend, AllowStandby, AllowEndSession
//
// In addition to this, events for the other APM messages are included.
//
// Usage:
// Drop the component on your main form.
// If you do not assign any event handlers and do not change the default values,
// then Windows will not shutdown / hibernate / standby (unless this is forced
// by a program / driver) and the user can not log off / shutdown while the
// program is running.
// Use the Events and the proporties to fine tune the behaviour.
// The Query events will give you a chance to e.g. display a dialog to ask the
// user what to do.
//
//==============================================================================
TQueryEvent = procedure(Sender: TObject; var Allow: boolean) of object;
TPowerEvent = procedure(Sender: TObject) of object;
TOEMEvent = procedure(Sender: TObject; dwEventCode: integer) of object;
// dwEventCode depends on the hardware manufacturer
TStatusEvent = procedure(Sender: TObject;
ACLineStatus, BatteryFlag, BatteryLifePercent: byte;
BatteryLifeTime, BatteryFullLifeTime: longword)
of object;
// BatteryLifeTime and BatteryFullLifeTime is given in seconds