Einzelnen Beitrag anzeigen

Benutzerbild von d3g
d3g

Registriert seit: 21. Jun 2002
602 Beiträge
 
#4
  Alt 3. Jul 2002, 19:40
Hi Teracon,

Ich hab mal zur Verdeutlichung und weil mir langweilig war (die Ferien haben in SH heute begonnen *g*) so ein Programm geschrieben:
Code:
{
--------------------------------------------------------------------------------

     Shutdown Spy v 0.1

     Copyright (c) 2002/07/03 by Benedikt Gollatz

     This program is free software; you can redistribute it and/or
     modify it under the terms of the GNU General Public License
     as published by the Free Software Foundation; either version 2
     of the License, or (at your option) any later version.

     This program is distributed in the hope that it will be useful,
     but WITHOUT ANY WARRANTY; without even the implied warranty of
     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
     GNU General Public License for more details.

--------------------------------------------------------------------------------
}

program ShutdownSpy;

uses
  Windows,
  Messages,
  ShellAPI,
  SysUtils;

const
  WINDOW_CLASS = 'ShutdownSpy';
  WINDOW_TITLE = 'Shutdown Spy v. 0.1';
  WM_TRAYEVENT = WM_USER + 1;

var
  WndClass: TWndClassEx;
  Msg: TMsg;
  hPopupMenu: HMENU;
  nid: TNotifyIconData;

function LeadingZero(n: Integer): String;
begin
  Str(n, Result);
  if (n < 9) then
    Result := '0' + Result;
end;

function WndProc(hWnd: HWND; uMsg: UINT; wParam: WPARAM;
                 lParam: LPARAM): LRESULT; stdcall;
var
  p: TPoint;
  SystemTime: _SYSTEMTIME;
  f: TextFile;
begin
  Result := 0;
  case uMsg of
    WM_CREATE:
      begin
        hPopupMenu := CreatePopupMenu();
        AppendMenu(hPopupMenu, MF_STRING, 1000, 'Exit');
        SetMenuDefaultItem(hPopupMenu, 1000, 0);

        with nid do begin
          cbSize := SizeOf(TNotifyIconData);
          uFlags := NIF_ICON or NIF_MESSAGE or NIF_TIP;
          uCallbackMessage := WM_TRAYEVENT;
          uID := 2000;
          hIcon := LoadIcon(0, IDI_APPLICATION);
          szTip := 'ShutdownSpy v 1.0';
          Wnd := hWnd;
        end;

        Shell_NotifyIcon(NIM_ADD, @nid);
      end;
    WM_TRAYEVENT:
      if (lParam = WM_RBUTTONUP) then begin
        GetCursorPos(p);
        TrackPopupMenu(hPopupMenu, TPM_RIGHTALIGN or TPM_RIGHTBUTTON, p.x, p.y,
                       0, hWnd, nil);
      end;
    WM_COMMAND:
      if (LoWord(wParam) = 1000) then
        SendMessage(hWnd, WM_CLOSE, 0, 0);
    WM_QUERYENDSESSION:
      begin
        AssignFile(f, 'log.txt');
        if (FileExists('log.txt')) then
          Append(f)
        else
          Rewrite(f);

        GetSystemTime(SystemTime);
        if (lParam or Integer(ENDSESSION_LOGOFF) = lParam) then
          WriteLn(f, 'The user has logged off at ',
                     LeadingZero(SystemTime.wYear), '/',
                     LeadingZero(SystemTime.wMonth), '/',
                     LeadingZero(SystemTime.wDay), ' ',
                     LeadingZero(SystemTime.wHour), ':',
                     LeadingZero(SystemTime.wMinute), ':',
                     LeadingZero(SystemTime.wSecond), '.')
        else
          WriteLn(f, 'The system was shut down at ',
                     LeadingZero(SystemTime.wYear), '/',
                     LeadingZero(SystemTime.wMonth), '/',
                     LeadingZero(SystemTime.wDay), ' ',
                     LeadingZero(SystemTime.wHour), ':',
                     LeadingZero(SystemTime.wMinute), ':',
                     LeadingZero(SystemTime.wSecond), '.');

        CloseFile(f);
        SendMessage(hWnd, WM_CLOSE, 0, 0);
      end;
    WM_DESTROY:
      begin
        DestroyMenu(hPopupMenu);
        Shell_NotifyIcon(NIM_DELETE, @nid);
        PostQuitMessage(0);
      end;
  else
    Result := DefWindowProc(hWnd, uMsg, wParam, lParam);
  end;
end;

begin
  with WndClass do begin
    cbSize := SizeOf(TWndClassEx);
    style := 0;
    lpfnWndProc := @WndProc;
    cbClsExtra := 0;
    cbWndExtra := 0;
    hInstance := SysInit.hInstance;
    hIcon := LoadIcon(0, IDI_APPLICATION);
    hCursor := LoadCursor(0, IDC_ARROW);
    hbrBackground := COLOR_APPWORKSPACE;
    lpszMenuName := '';
    lpszClassName := WINDOW_CLASS;
    hIconSm := 0;
  end;

  RegisterClassEx(WndClass);

  CreateWindowEx(0,
                 WINDOW_CLASS,
                 WINDOW_TITLE,
                 0,
                 Integer(CW_USEDEFAULT),
                 Integer(CW_USEDEFAULT),
                 0,
                 0,
                 0,
                 0,
                 hInstance,
                 nil);

  while (GetMessage(Msg, 0, 0, 0)) do begin
    TranslateMessage(Msg);
    DispatchMessage(Msg);
    Sleep(150);
  end;
end.
MfG,
d3g
-- Crucifixion?
-- Yes.
-- Good. Out of the door, line on the left, one cross each.
  Mit Zitat antworten Zitat