Subscribe:

Ads 468x60px

Loading

Senin, 04 Juli 2011

Preventing the user from closing a form

Question/Problem/Abstract:
How can I prevent the user from closing a form?
Answer:



To prevent the user from closing a form you need to disable the close
button in the title bar of a form and at the same time disable the
"Close" menu item in the form's system menu. This is done by calling
the EnableMenuItem API function (see the example below). Nonetheless,
the user can still close the form using the Alt+F4 key combination,
so we have to set the KeyPreview form property to True and write an
event handler for the OnKeyDown event to cancel out this hot key:

procedure TForm1.FormCreate(Sender: TObject);
var
hSysMenu: HMENU;
begin
hSysMenu := GetSystemMenu(Self.Handle, False);
if hSysMenu <> 0 then begin
EnableMenuItem(hSysMenu, SC_CLOSE,
MF_BYCOMMAND Or MF_GRAYED);
DrawMenuBar(Self.Handle);
end;
KeyPreview := True;
end;

procedure TForm1.FormKeyDown(Sender: TObject;
var Key: Word; Shift: TShiftState);
begin
if (Key = VK_F4) and (ssAlt in Shift) then
Key := 0;
end;

0 komentar:

Posting Komentar