Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can a control be notified when its parent receives and loses focus in Delphi?

As the title says, I'd like a component (say, a label) to be notified when it's parent (say, a panel) receives and loses focus. I wandered a bit in Delphi source, in hope of using TControl.Notify, but it's only used to notify child controls of some property changes like font and color. Any suggestions?

like image 347
iMan Biglari Avatar asked Sep 15 '12 12:09

iMan Biglari


1 Answers

Whenever the active control in an application changes, a CM_FOCUSCHANGED message is broadcast to all controls. Simply intercept it, and act accordingly.

Also, I assumed that by when it's parent (say, a panel) receives and loses focus you mean whenever a (nested) child control on that parent/panel receives or loses focus.

type
  TLabel = class(StdCtrls.TLabel)
  private
    function HasCommonParent(AControl: TWinControl): Boolean;
    procedure CMFocusChanged(var Message: TCMFocusChanged);
      message CM_FOCUSCHANGED;
  end;

procedure TLabel.CMFocusChanged(var Message: TCMFocusChanged);
const
  FontStyles: array[Boolean] of TFontStyles = ([], [fsBold]);
begin
  inherited;
  Font.Style := FontStyles[HasCommonParent(Message.Sender)];
end;

function TLabel.HasCommonParent(AControl: TWinControl): Boolean;
begin
  Result := False;
  while AControl <> nil do
  begin
    if AControl = Parent then
    begin
      Result := True;
      Break;
    end;
    AControl := AControl.Parent;
  end;
end;

If you don't like to subclass TJvGradientHeader, then it is possible to design this generically by the use of Screen.OnActiveControlChange:

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
    procedure FormDestroy(Sender: TObject);
  private
    FHeaders: TList;
    procedure ActiveControlChanged(Sender: TObject);
  end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  FHeaders := TList.Create;
  FHeaders.Add(Label1);
  FHeaders.Add(Label2);
  Screen.OnActiveControlChange := ActiveControlChanged;
end;

procedure TForm1.FormDestroy(Sender: TObject);
begin
  FHeaders.Free;
end;

function HasCommonParent(AControl: TWinControl; AMatch: TControl): Boolean;
begin
  Result := False;
  while AControl <> nil do
  begin
    if AControl = AMatch.Parent then
    begin
      Result := True;
      Break;
    end;
    AControl := AControl.Parent;
  end;
end;

procedure TForm1.ActiveControlChanged(Sender: TObject);
const
  FontStyles: array[Boolean] of TFontStyles = ([], [fsBold]);
var
  I: Integer;
begin
  for I := 0 to FHeaders.Count - 1 do
    TLabel(FHeaders[I]).Font.Style :=
      FontStyles[HasCommonParent(Screen.ActiveControl, TLabel(FHeaders[I]))];
end;

Note that I chose TLabel to demonstrate this works also for TControl derivatives.

like image 152
NGLN Avatar answered Sep 26 '22 19:09

NGLN