Mega Code Archive

 
Categories / Delphi / Examples
 

How to modify the behaviour of a component without subclassing

I needed links "à la" IE for a form. So I basically needed a TLabel but "TabStopable" and the TStaticText qualifies for that. But it doesn't draw a FocusRect when focused. Here is my solution. interface type TStaticText = class(StdCtrls.TStaticText) private procedure DrawFocusRect; procedure WMSetFocus(var Message: TWMSetFocus); message WM_SETFOCUS; procedure WMKillFocus(var Message: TWMSetFocus); message WM_KILLFOCUS; procedure WMPaint(var Message: TWMPaint); message WM_PAINT; end; // all the components of that form will inherited from my modified // TStaticText TDataBrowseForm = class(TForm) Label1: TStaticText; Label2: TStaticText; private { Private declarations } public { Public declarations } end; .... implementation .... procedure TStaticText.DrawFocusRect; var DC: HDC; begin DC := GetDC(Handle); try Windows.DrawFocusRect(DC, Rect(1, 1, Width-1, Height -1)); finally ReleaseDC(Handle, DC); end; end; procedure TStaticText.WMKillFocus(var Message: TWMSetFocus); begin inherited; DrawFocusRect; end; procedure TStaticText.WMPaint(var Message: TWMPaint); begin inherited; if Focused then DrawFocusRect; end; procedure TStaticText.WMSetFocus(var Message: TWMSetFocus); begin inherited; DrawFocusRect; end;