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 sizbut on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Removing form close icon

Status
Not open for further replies.

sggaunt

Programmer
Jul 4, 2001
8,620
GB
Does anyone know how to remove the Form close 'X' icon from a form, it's easy to remove the other buttons, or all of them, but just the form close?



Steve:
A Delphi Programmer
 
You can't (at least, not easily).

However, you can effectively diasable the close icon by adding a boolean variable that is set when you DO want to close:

procedure TfrmMain.FormCloseQuery(Sender: TObject; var CanClose: Boolean);
begin
CanClose := FblnClose
end;

procedure TfrmMain.Button3Click(Sender: TObject);
begin
FblnClose := True; // usually False
close;
end;
 
You can disable the 'X' using the following code/component but unfortunately it doesnt "hide" the icon, merely disables it.

Still, it may give you a starting point to work from.

Code:
unit UNoCloseForm;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;

type
  TNoCloseForm = class(TComponent)
  private
    { Private declarations }
    FCloseEnabled: boolean;
    procedure SetCloseEnabled (Value: boolean);
  protected
    { Protected declarations }
  public
    { Public declarations }
    constructor Create (AOwner: TComponent); override;
    destructor Destroy; override;
    procedure Toggle;
  published
    { Published declarations }
    property CloseEnabled: boolean read FCloseEnabled write SetCloseEnabled default False;
  end;

procedure Register;

implementation

type THackWinControl = class (TWinControl);

constructor TNoCloseForm.Create (AOwner: TComponent);
begin
 inherited Create (AOwner);
 FCloseEnabled := False;
 Toggle;
end;

destructor TNoCloseForm.Destroy;
begin
 if (csDesigning in ComponentState) then
  CloseEnabled:=True;
 inherited Destroy;
end;

procedure TNoCloseForm.SetCloseEnabled (Value: boolean);
begin
 if Value<>FCloseEnabled then
  try
  Toggle;
  FCloseEnabled := Value;
  except end;
end;

procedure TNoCloseForm.Toggle;
var WCTRL: THackWinControl; LI: LongInt;
begin
  if not Assigned(Owner) or (csDesigning in ComponentState) then Exit;
  WCTRL := THackWinControl (Owner);
  LI := GetClassLong(WCTRL.Handle, GCL_STYLE);
  LI := LI xor CS_NOCLOSE;
  SetClassLong(WCTRL.Handle, GCL_STYLE, LI);
  WCTRL.RecreateWnd;
end;

procedure Register;
begin
  RegisterComponents('Samples', [TNoCloseForm]);
end;

end.

[blue]"Damn, we're in a tight spot![/blue]
 

As you know, you can remove all of them by setting biSystemMenu in the TForm.BorderIcons property to False.

If you really want to allow Minimize/Maximize you can put SpeedButtons in the upper-right-hand corner of the form. Resize them, and make your own glyphs. Then train your users that if they minimize the funny window, they have to double-click to restore it.

Not the best perhaps, but it gets the job done.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top