2009-10-28 11 views

Odpowiedz

15

Aby otrzymać powiadomienie, gdy status zmieni się na Vista i Windows 7, można użyć RegisterPowerSettingNotification.

W przypadku systemu Windows 2000 i nowszych wersji spójrz na numer GetSystemPowerStatus lub przejdź do witryny MSDN i przeczytaj artykuł o numerze Power Management.

(Ktoś zawsze stanowisk podczas piszę :-()

function GetBattery : Boolean; 
var 
    SysPowerStatus: TSystemPowerStatus; 
begin 
    Win32Check(GetSystemPowerStatus(SysPowerStatus)); 
    case SysPowerStatus.ACLineStatus of 
    0: Result := False; 
    1: begin 
     Result := True; 
     // You can return life with 
     // String := Format('Battery power left: %u percent.', SysPowerStatus.BatteryLifePercent]); 
    end; 
    else 
     raise Exception.Create('Unknown battery status'); 
    end; 
end; 
+0

Tak, taka jest natura SO.To jest trudniejsze, gdy twój angielski nie jest angielski (mój przypadek) .Przy prostu musisz się do tego przyzwyczaić :-). Pamiętam dyskusję w meta http://meta.stackexchange.com/questions/73/is-the-fastest-gun-in-the-west-solved/93#93, aby uniknąć "desperacji" w publikowaniu czegoś szybko . – GmonC

+0

Właśnie przetestowałem ten kod na Windows 7 i to DZIAŁA !! to była niespodzianka. – Reallyethical

+0

@GmonC Twoje prawo Powinienem się uspokoić, ale szczerze mówiąc, przyzwyczajam się do prędkości tej strony. Wydaje się prawie bezcelowe, gdy inni mają taki sam pomysł jak ja. – Reallyethical

1

Oto część kodu, który wykrywa, gdy laptop pracuje na baterii (jeśli nie powoduje to pewne zdarzenie):

uses 
    Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs, 
    WTSSessionNotification, StdCtrls, MediaPlayer, Buttons, ShellAPI, Settings, 
    ExtCtrls; 

const 
    WM_ICONTRAY = WM_USER + 1; 

type 
    TSettingsForm = class(TForm) 
    OpenDialog: TOpenDialog; 
    pnl1: TPanel; 
    InfoLabel: TLabel; 
    grp1: TGroupBox; 
    AlarmSoundLabel: TLabel; 
    lbl1: TLabel; 
    checkIfLocked: TCheckBox; 
    Filename: TEdit; 
    Browse: TBitBtn; 
    TestSound: TBitBtn; 
    btn1: TBitBtn; 
    lbl2: TLabel; 
    procedure Minimize(Sender: TObject); 
    procedure FormCreate(Sender: TObject); 
    procedure FormDestroy(Sender: TObject); 
    procedure TestSoundClick(Sender: TObject); 
    procedure BrowseClick(Sender: TObject); 
    procedure checkIfLockedClick(Sender: TObject); 
    procedure OpenHomepage(Sender: TObject); 
    procedure btn1Click(Sender: TObject); 
    private 
    TrayIconData: TNotifyIconData; 
    procedure CheckForAC; 
    protected 
    procedure WndProc(var Message: TMessage); override; 
    public 
    { Public declarations } 
    Function SecuredLockWorkStation : Boolean ; 
    end; 

var 
    SettingsForm: TSettingsForm; 

implementation 

{$R *.DFM} 
{$R WindowsXP.RES} 

var 
    MPlayer: TMPlayer; 
    mySettings: TSettings; 
    isLocked: boolean = false; 

// true if A/C is connected, false if not 
function ACConnected: boolean; 
var PowerStatus: TSystemPowerStatus; 
begin 
GetSystemPowerStatus(PowerStatus); 
result := (PowerStatus.ACLineStatus = 1); 
end; 

// handles application.minimize; do not really 
// minimize, but hide settings window 
procedure TSettingsForm.Minimize(Sender: TObject); 
begin 
Application.Restore; 
self.Hide; 
end; 

// processes window messages (notification about 
// power status changes, locking of workstation and 
// tray icon activity) 
procedure TSettingsForm.WndProc(var Message: TMessage); 
begin 
    case Message.Msg of 
    WM_WTSSESSION_CHANGE: 
     begin 
     if Message.wParam = WTS_SESSION_LOCK then 
     isLocked := true; 
     if Message.wParam = WTS_SESSION_UNLOCK then 
     begin 
     isLocked := false; 
     if MPlayer.isPlaying then 
      MPlayer.Close; 
     end; 
     end; 
    WM_POWERBROADCAST: 
     begin 
     if (isLocked) or (checkIfLocked.checked=false) then 
     CheckForAC; 
     end; 
    WM_ICONTRAY: 
     begin 
     case Message.lParam of 
      WM_LBUTTONDOWN: 
      begin 
       if SettingsForm.visible then 
       SettingsForm.Hide 
       else 
       SettingsForm.Show; 
      end; 
      WM_RBUTTONUP: 
      begin 
       if SettingsForm.visible then 
       SettingsForm.Hide 
       else 
       SettingsForm.Close; 
      end; 
     end; 
     end; 
    end; 
    inherited; 
end; 
Powiązane problemy