如何获取TWinControl的实际背景颜色?

问题描述 投票:0回答:1

在表单上,我有几个相互放置的控件,例如:

TForm/TPageControl/TTabSheet/TGroupBox...

我正在尝试获取最顶层控件的实际背景颜色。考虑到每个控件可以具有不同的 ParentColor 属性值,并且(嵌套)控件的确切数量在设计时未知,我该如何做到这一点?

我尝试了Windows API GetBkColor函数,但它没有返回实际的颜色。

我尝试为此编写递归函数,但它不起作用,因为 TWinControl 的 ParentColor 属性受到保护:

  function GetColor(C: TWinControl): TColor;
  begin
    if C.ParentColor {compiling error is here} and Assigned(C.Parent) then
    begin
      Result := GetColor(C.Parent)
    end
    else
      //Result := C.Color; //compiling error is here...
      Result := C.Brush.Color;
  end;

事情可能会变得更糟,因为 ParentBackround 属性对于每个控件可能有不同的值......

那么,如何获取最顶层TWinControl的实际背景颜色?

更新: 看来,只需查询 Brush.Color 就可以了(感谢 Sherlock70)。它总是有效吗?或者,也许我需要添加额外的检查?

delphi
1个回答
0
投票

TWinControl 没有公开的 Color 属性。确切地说,查看 Brush 属性 Brush.Color。对于(不是那么多)进一步阅读,请参阅:https://docwiki.embarcadero.com/Libraries/Athens/en/Vcl.Controls.TWinControl.Brush

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.