Examples Delphi

Have you ever found yourself adding the same code to different forms? Well now you can use the following as a base class for all forms.
=============== BEGIN CODE ======================
unit BaseForm;
interface
uses SysUtils, Classes, Forms, Windows;
type
TBaseForm = class(TForm)
protected
function IsModal(): Boolean;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure ShowError(E: Exception);
function MsgBox(Msg: string;
uType: Integer = MB_OK or MB_ICONSTOP): Integer; overload;
function MsgBox(Msg, MsgCaption: string;
uType: Integer = MB_OK or MB_ICONSTOP): Integer; overload;
end;
implementation
uses Controls;
{ TPipeCallBaseForm }
function TBaseForm.MsgBox(Msg: string;
uType: Integer = MB_OK or MB_ICONSTOP): Integer;
begin
Result := MsgBox(Msg, Caption, uType);
end;
procedure TBaseForm.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited;
if ((IsModal) and (GetActiveWindow() = Handle) and (Key = VK_ESCAPE)) then
ModalResult := mrCancel;
end;
function TBaseForm.MsgBox(Msg, MsgCaption: string;
uType: Integer = MB_OK or MB_ICONSTOP): Integer;
var
FlashInfo: FLASHWINFO;
begin
if (GetForegroundWindow() <> Handle) then
begin
(* if the current window is not active make it flash to alert user *)
FillChar(FlashInfo, SizeOf(FLASHWINFO), 0);
FlashInfo.cbSize := SizeOf(FLASHWINFO);
FlashInfo.dwFlags := FLASHW_ALL or FLASHW_TIMERNOFG;
FlashInfo.hwnd := Handle;
FlashWindowEx(FlashInfo);
end;
Result := MessageBox(Handle, PChar(Msg), PChar(Caption), uType);
end;
procedure TBaseForm.ShowError(E: Exception);
begin
MsgBox(Format('Error: %s'#13#13'Error Class: %s', [E.Message, E.ClassName]));
end;
function TBaseForm.IsModal: Boolean;
begin
Result := (fsModal in FormState);
end;
end.
=============== END CODE ======================
To use this base form you simple add "BaseForm" to the uses clause and change the forms implementation from:
TfrmMyForm = class(TForm)
To
TfrmMyForm = class(TBaseForm)
Now each form will have the ability to:
Display an error in a consistent manner
Show a MessageDlg whilst flashing the window if not active.
Return mrCancel if the user presses the ESC key on a modal dialog.