Visit our Sponsor   Visit our Sponsor
delphi3000.com - the free delphi knowledge platform
delphi3000.com - the free delphi knowledge platform
499 Users Online NOW
Have a look at your member-status

connecting people's knowledge


  - Recent ArticlesRSS feed for Recent Articles on delphi3000.com
  - List of All Articles
  - Top Viewed Articles
  - Articles (+Attachem.)
  - Articles Of Interest
  - Categories
  - Top Uploader
  - Search
  - Index

  - My Home
  - Submit an Article
  - My Articles
  - My Personal Data
  - My Bookmarks
  - Activities
  - Login/Logout

  - Sign Up
  - Why Sign Up
  - Newsletter

  - Press
  - Advertise

  - Contact
  - Feedback





Community
Borland
ClubeDelphi
Dr. Bob
UK-BUG
Delphi Meetings
Planeta Delphi







Startblatt.de






Share this article with friendsShare this article with friends
Rate this articleRate this article - to keep the quality of delphi3000.com !
Comment this article or read through previous comments (0)


How to prevent an application for closing by userGo to manfred süsens's websiteFormat this article printer-friendly!Bookmark function is only available for registered users!
Product:
Delphi all versions
Category:
System
Skill Level:
Scoring:
Last Update:
01/03/2003
Search Keys:
delphi delphi3000 article borland vcl code-snippet Form Menu, Task Menu, ALT + F4, Close application
Times Scored:
2
Visits:
5415
Uploader: manfred süsens
Company: DÜRR Systems GmbH
Reference: N/A
 
Question/Problem/Abstract:
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.





Please rate this article!
Skill level:
BeginnerExpert

Useful:
No!Very!

Overall rating:
PoorExcellent



Comments to this article
Write a new comment













 
Sign up to consume product discounts for Bronze memberships !

read more


  Visit our Sponsor

 

  Community Ad of
D. Souchard
 
   














 







     
  Copyright © 2000 - 2007 delphi3000.com - All rights reserved. Terms of use. || Privacy
delphi3000.com is a service by bluestep.com IT-Services GmbH (Vienna)