Author: Jonas Bilinkevicius
How to disable the close button of a TForm
Answer:
Solve 1:
1
2 procedure EnableCloseButton(const bEnable: Boolean);
3 const
4 MenuFlags: array[Boolean] of Integer = (MF_DISABLED, MF_ENABLED);
5 var
6 hSysMenu: HMENU;
7 begin
8 hSysMenu := GetSystemMenu(Handle, False);
9 if hSysMenu > 0 then
10 EnableMenuItem(hSysMenu, SC_CLOSE, MF_BYCOMMAND or MenuFlags[bEnable]);
11 end;
Solve 2:
The usual approach is to disable or enable the corresponding item in the forms
system menu. However, that does not work for all of them. You can always trap the
WM_SYSCOMMAND message caused by clicking on the items and not pass it on, but this
way the border icons do not appear disabled.
12 { ... }
13 EnableMenuItem(GetSystemMenu(handle, False), SC_CLOSE, MF_BYCOMMAND or MF_GRAYED);
That will disable the close box, for example.
Solve 3:
Remember that certain combinations will cause different results (ie. removing the
system menu will also disable minimize/ maximize etc.)
14 procedure TForm1.Button1Click(Sender: TObject);
15 var
16 Style: Integer;
17 begin
18 Style := GetWindowLong(Handle, GWL_STYLE);
19 {disable minimize}
20 Style := Style - WS_MINIMIZEBOX;
21 {disable maximize}
22 Style := Style - WS_MAXIMIZEBOX;
23 {remove system menu}
24 Style := Style - WS_SYSMENU;
25 {set new style}
26 SetWindowLong(Handle, GWL_STYLE, Style);
27 {repaint the title bar}
28 RedrawWindow(Handle, nil, 0, RDW_FRAME or RDW_INVALIDATE);
29 end;
|