Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What happens when message handler does not call inherited?

Tags:

delphi

I have just noticed that one of our very (very) old custom controls (not created by me) has this WM_SIZE message handler (I used a TPanel here for the demo):

TPanel = class(ExtCtrls.TPanel)
private
  procedure WMSize(var Message: TWMSize); message WM_SIZE;
end;

procedure TPanel.WMSize(var Message: TWMSize);
begin
  DoSomethingWhenResized;
end;

The inherited is not being called. the DoSomethingWhenResized creates a cached gradient bitmap which is used in the paint process of the control.

Every thing "looks" and behaves fine, but I just wonder if anything could go wrong by not calling the inherited handler first?

like image 698
zig Avatar asked Aug 28 '17 12:08

zig


1 Answers

Naturally, if you don't call inherited then you lose the behaviour that is implemented in the ancestor controls. Whether or not this is a problem for you is something only you can decide. The VCL source shows you exactly what those ancestors are doing. In your example the first ancestor up the chain to handle WM_SIZE is TWinControl which does this :

procedure TWinControl.WMSize(var Message: TWMSize);
var
  LList: TList;
begin
  UpdateBounds;
  UpdateExplicitBounds;
  inherited;

  LList := nil;
  if (Parent <> nil) and (Parent.FAlignControlList <> nil) then
    LList := Parent.FAlignControlList
  else if FAlignControlList <> nil then
    LList := FAlignControlList;

  if LList <> nil then
  begin
    if LList.IndexOf(Self) = -1 then
      LList.Add(Self);
  end
  else
  begin
    Realign;
    if not (csLoading in ComponentState) then
      Resize;
  end;
end;

Here WMSize calls inherited, but there are (currently) no ancestor objects above TWinControl that implement this, so the above is what you are missing by not calling inherited. If your DoSomethingWhenResized manages your control bounds, sizing, and component alignment of child controls (or if you don't need it to do this) then you're fine. If you find, however, that the control is not handling these things properly then you might suspect that DoSomethingWHenResized has missed one or more of these responsibilities in its implementation.

like image 169
J... Avatar answered Oct 29 '22 02:10

J...