当消息处理程序不调用inherited时会发生什么?

zig*_*zig 7 delphi

我刚刚注意到我们的一个非常(非常)旧的自定义控件(不是由我创建的)有这个WM_SIZE消息处理程序(我在TPanel这里用于演示):

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

procedure TPanel.WMSize(var Message: TWMSize);
begin
  DoSomethingWhenResized;
end;
Run Code Online (Sandbox Code Playgroud)

inherited不会被调用.在DoSomethingWhenResized创建其在控制的油漆过程中使用的高速缓存的梯度位图.

每个东西"看起来"并且表现得很好,但我只是想知道是否因为没有先调用inherited处理程序会出错?

J..*_*... 8

当然,如果您不调用,inherited则会丢失祖先控件中实现的行为.这是否是一个问题只有你可以决定.VCL源显示了这些祖先正在做的事情.在你的例子中,要处理的链的第一个祖先WM_SIZE就是TWinControl这样:

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;
Run Code Online (Sandbox Code Playgroud)

这里WMSize调用inherited,但是(目前)没有上面的祖先对象TWinControl实现这个,所以上面是你没有调用所缺少的inherited.如果您DoSomethingWhenResized管理控件边界,大小调整和子控件的组件对齐(或者如果您不需要它来执行此操作)那么您就可以了.但是,如果您发现控件未正确处理这些内容,那么您可能会怀疑在实现DoSomethingWHenResized中错过了一项或多项责任.