Mega Code Archive

 
Categories / Delphi / Examples
 

How to prevent an application for closing by user

Title: How to prevent an application for closing by user Question: Enable and disable the closing points (close in system menu at Task line, close in Window menu under Window icon, close button at window left head line, hot key function ALT + F4) for example log on and log off and prevent the application for closing by user but not for windows system. Answer: There are some articles describing manipulating the system menu, but they are all not very clear or not complete. Here is the complete sample to do this. The trick is, to work with menu in the task line you have to use application.handle and to work with the menu in the window headline, you have to use form1.handle. Also the buttons in the window headline are connecting with the window menu and will shown the same state like the menu. Feel free to add or delete menu items to the menus (use: Menu Functions at Windows SDK help e.g.: "DeleteMenu", "InsertMenuItem"). Here is a sample with a logon and logoff button: *** and for completeness: an additional menu point in window menu *** unit Unit1; interface uses Windows, Messages, SysUtils, Classes, Forms, Controls, StdCtrls, menus; type TForm1 = class(TForm) BtnLogOn: TButton; BtnLogOff: TButton; procedure FormCreate(Sender: TObject); procedure BtnLogOnClick(Sender: TObject); procedure BtnLogOffClick(Sender: TObject); procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); private BeepMenu:TMenuItem; procedure WMSYSCOMMAND(var message: TMessage) ;message WM_SYSCOMMAND; public end; var Form1: TForm1; Logon: Boolean = false; MenuCheck:Boolean = false; implementation {$R *.DFM} procedure TForm1.FormCreate(Sender: TObject); var MenuHndl:HMENU; begin BtnLogOffClick(Sender); //add administrator menus to system window menu MenuHndl:=GetSystemMenu(Handle,False); InsertMenu(MenuHndl,GetMenuItemCount(MenuHndl), MF_BYPOSITION+MF_SEPARATOR,0,nil); BeepMenu:=TMenuItem.Create(self); BeepMenu.Caption:='Beep'; InsertMenu(MenuHndl,GetMenuItemCount(MenuHndl),MF_BYPOSITION, BeepMenu.Handle,PChar(BeepMenu.Caption)); end; procedure TForm1.BtnLogOnClick(Sender: TObject); var MenuHndl:HMENU; begin Logon:=true; //Enable the close option on applic menu MenuHndl:=GetSystemMenu(application.Handle,False); EnableMenuItem(MenuHndl,SC_CLOSE,MF_BYCOMMAND OR MF_ENABLED); //Enable the close option at menu and close button at the window MenuHndl:=GetSystemMenu(Handle,False); EnableMenuItem(MenuHndl,SC_CLOSE,MF_BYCOMMAND OR MF_ENABLED); end; procedure TForm1.BtnLogOffClick(Sender: TObject); var MenuHndl:HMENU; begin Logon:=false; //Disable the close option on applic menu MenuHndl:=GetSystemMenu(application.Handle,False); EnableMenuItem(MenuHndl,SC_CLOSE,MF_BYCOMMAND OR MF_DISABLED OR MF_GRAYED); //Disable the close option at menu and close button at the window MenuHndl:=GetSystemMenu(Handle,False); EnableMenuItem(MenuHndl,SC_CLOSE,MF_BYCOMMAND OR MF_DISABLED OR MF_GRAYED); end; procedure TForm1.FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState); begin if logon then exit; if (key=VK_F4) AND (ssAlt in Shift) then key:=0; end; procedure TForm1.WMSYSCOMMAND(var message: Tmessage) ; var MenuHndl:HMENU; begin If (HMENU(message.WParam)=BeepMenu.Handle) then begin Windows.Beep(4000,40); MenuCheck:= NOT MenuCheck; MenuHndl:=GetSystemMenu(Handle,False); if MenuCheck then CheckMenuItem(MenuHndl,GetMenuItemCount(MenuHndl)-1, MF_BYPOSITION OR MF_CHECKED) else CheckMenuItem(MenuHndl,GetMenuItemCount(MenuHndl)-1, MF_BYPOSITION OR MF_UNCHECKED); end; inherited; end; end.