FAQ1343D.txt   Disabling ALT-F4 to prevent form closing

Category   :Object Pascal

Platform    :All

Product    :All 32 bit 

 

Question:

How can I disable the Alt+F4 key combination to keep my form

from closing?

 

Answer:

If the intent is to make sure the user doesn't exit prematurely

before performing some required task, use the OnCloseQuery event

of the form and set the CanClose property appropriately.

 

If you need to override the default behavior for normal form

handling and prevent the Alt+F4 keystrokes from being responded

to, then set the forms KeyPreview property to true and

trap the key during the forms KeyDown event.

 

Example:

 

procedure TForm1.FormCreate(Sender: TObject);

begin

  KeyPreview := true;

end;

 

procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word;

  Shift: TShiftState);

begin

  if ((ssAlt in Shift) and (Key = VK_F4)) then

    Key := 0;

end;

 

 

 

Other source:

 

 

prevent Alt+F4?

Author: Simon Grossenbacher 

Homepage: http://www.swissdelphicenter.ch

1 Comment to this tip [Write new comment]

[ Print tip ]    

 

Tip Rating (20):  

Skill:  

Useful:  

Overall:  

 

 

 

public

  procedure AppMessage(var Msg: TMSG; var HAndled: Boolean);

end;

 

{...}

 

implementation

 

{...}

 

procedure TForm1.FormCreate(Sender: TObject);

begin

  // set your applications message handler to your new one

  Application.OnMessage := AppMessage;

end;

 

procedure TForm1.AppMessage(var Msg: TMSG; var Handled: Boolean);

begin

  // let your application handle all messages initially

  Handled := False;

  case Msg.Message of

    WM_SYSKEYDOWN:

      if Msg.wParam = VK_F4 then

        Handled := True; // don't allow ALT-F4

  end;

end;

 

//Or Write in the OnCloseQuery handler CanClose :=  False

//Oder schreibe im OnCloseQuery Ereignis CanClose := False