Mega Code Archive

 
Categories / Delphi / System
 

A better way to disable ALT+F4 to prevent form closing

Title: A better way to disable ALT+F4 to prevent form closing Question: How can I disable the Alt+F4 key combination to keep my form from closing? (A better way) Answer: After I read the article about Peter Lieber's Disabling ALT-F4 to prevent form closing, I think I know a better way to resolve it. In Windows, when you press ALT+F4 in a form, a WM_SYSCOMMAND will occur. (For more about WM_SYSCOMMAND see MSDN or other win32 help.) So if you catch the WM_SYSCOMMAND message it is easy to prevent form closing. Example: TForm1 = class(TForm) {...} private procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND; {...} end; procedure TForm1.WMSysCommand(var Msg: TWMSysCommand); begin if Msg.CmdType SC_CLOSE then // Prevent ALT+F4 inherited; end; Gook luck! (Note: I am a Chinese, so my english may be not good. I wish you can understand the article.)