Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

How do I disable the [ X ] close button on forms?

How To

How do I disable the [ X ] close button on forms?

by  djjd47130  Posted    (Edited  )
This is not necessarily how to Hide this button, nor is it how to ignore the click event of it either. Instead, this is how to actually disable the close button of a form by graying it out. Windows provides this ability, but it is not implemented in Delphi's VCL forms. Therefore, you need to do it yourself.

I added this to my form by adding a new boolean property "CloseButtonEnabled". By default of course this will be true, then set it to false as needed to disable (or gray out) the close button, and back to true to be normal again.

In the form's class...

Code:
type
  TForm1 = class(TForm)
  private
    fCloseButtonEnabled: Bool;
    procedure SetCloseButtonEnabled(const Value: Bool);
  public
    property CloseButtonEnabled: Bool read fCloseButtonEnabled write SetCloseButtonEnabled;
  end;

And in the implementation...

Code:
procedure TForm1.SetCloseButtonEnabled(const Value: Bool);
var
  hSysMenu: HMENU;
begin
  fCloseButtonEnabled := Value;
  hSysMenu:= GetSystemMenu(Self.Handle, False);
  if hSysMenu <> 0 then begin
    if Value then
      EnableMenuItem(hSysMenu, SC_CLOSE, MF_BYCOMMAND or MF_ENABLED)
    else
      EnableMenuItem(hSysMenu, SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
    DrawMenuBar(Self.Handle);
  end;
end;
Register to rate this FAQ  : BAD 1 2 3 4 5 6 7 8 9 10 GOOD
Please Note: 1 is Bad, 10 is Good :-)

Part and Inventory Search

Back
Top