Einzelnen Beitrag anzeigen

Benutzerbild von Sprint
Sprint

Registriert seit: 18. Aug 2004
Ort: Edewecht
712 Beiträge
 
Delphi 5 Professional
 
#13

Re: Problem bei der Verarbeitung von Systemnachrichten

  Alt 4. Sep 2004, 15:46
Hier mal eine Lösung wie du mit SetWindowsHookEx die WM_QUIT Nachricht abfangen kannst.

Delphi-Quellcode:
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    { Private-Deklarationen }
    procedure SetMsgHook;
    procedure Unhook;
  public
    { Public-Deklarationen }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  MsgHook: HHOOK;

{--------------------------------------------------------------------------------------------------}

function GetMsgProc(Code: Integer; wParam: WPARAM; lParam: LPARAM): LRESULT; stdcall;
const
  S_MSG = 'Eine WM_QUIT Nachricht wird an dieses Programm geschickt.' + #13#10 +
          'Soll die Nachricht verarbeitet werden?';
var
  Msg: PMsg;
begin

  if Code = HC_ACTION then
  begin

    Msg := PMsg(lParam);

    if Msg^.message = WM_QUIT then
      if Application.MessageBox(S_MSG, nil, MB_YESNO or MB_ICONINFORMATION) = IDNO then
      begin
        Msg^.message := WM_COMMAND;
        Msg.wParam := WM_NULL;
        Msg.lParam := 0;
        Result := 1;
        Exit;
      end;

  end;

  Result := CallNextHookEx(MsgHook, Code, wParam, lParam);

end;

{--------------------------------------------------------------------------------------------------}

procedure TForm1.SetMsgHook;
begin

  if MsgHook <> 0 then
    Exit;

  MsgHook := SetWindowsHookEx(WH_GETMESSAGE, @GetMsgProc, 0, GetCurrentThreadId);

  if MsgHook = 0 then
    Application.MessageBox(PChar(SysErrorMessage(GetLastError)), nil, MB_OK or MB_ICONQUESTION);

end;

{--------------------------------------------------------------------------------------------------}

procedure TForm1.Unhook;
begin

  if MsgHook = 0 then
    Exit;

  UnhookWindowsHookEx(MsgHook);
  MsgHook := 0;

end;

{--------------------------------------------------------------------------------------------------}

procedure TForm1.FormCreate(Sender: TObject);
begin

  SetMsgHook;
  
end;

{--------------------------------------------------------------------------------------------------}

procedure TForm1.FormDestroy(Sender: TObject);
begin

  Unhook;

end;

{--------------------------------------------------------------------------------------------------}

end.
  Mit Zitat antworten Zitat