Override SetEnabled and handling message CM_ENABLEDCHANGED

There is a TFrame

descendant class as follows:

TCustomHistoryFrame = class(TFrame)
  tbMainFunction: TToolBar;
  // there more, of course, but that is irrelevant to the question
end;

      

I noticed that when I set a property of Enabled

this frame to False

, its component tbMainFunction

will not (visually) be disabled.

My first idea was to override the virtual method TControl.SetEnabled

. Looking at its implementation, I saw that it executes a control message CM_ENABLEDCHANGED

when the value is actually different.

I'm not sure how to properly apply the frame state Enabled

to the toolbar.
What would be the general way? Since this question will be mostly opinion based, let me rephrase it:

What advantages and disadvantages are there for either overriding SetEnabled

or processing CM_ENABLEDCHANGED

?


Things I thought about myself:

  • override SetEnabled

    :
    • I would have to double-check if the new value is different from the old value. This will be redundancy. (Which wouldn't have a significant impact on performance, but - call me a hair sprinter - smells to me.)
  • processing CM_ENABLEDCHANGED

    :
    • How can I keep the legacy code for this post? There are versions for this post (at least) in TControl

      and TWinControl

      . Will they be executed if I process the message in my class TCustomHistoryFrame

      ?
+3


source to share


1 answer


Processing CM_ENABLEDCHANGED

is the right decision. Such messages are CM_...

specifically designed to allow descendant classes to respond to changes to properties declared in base classes.

For example:

TCustomHistoryFrame = class(TFrame)
  tbMainFunction: TToolBar;
private
  procedure CMEnabledChanged(var Message: TMessage); message CM_ENABLEDCHANGED;
end;

procedure TCustomHistoryFrame.CMEnabledChanged(var Message: TMessage);
begin
  inherited;
  tbMainFunction.Enabled := Enabled;
end;

      



As an alternative:

TCustomHistoryFrame = class(TFrame)
  tbMainFunction: TToolBar;
protected
  procedure WndProc(var Message: TMessage); override;
end;

procedure TCustomHistoryFrame.WndProc(var Message: TMessage);
begin
  inherited;
  if Message.Msg = CM_ENABLEDCHANGED then
    tbMainFunction.Enabled := Enabled;
end;

      

+5


source







All Articles