【问题标题】:delphi group box caption color changedelphi 组框标题颜色变化
【发布时间】:2011-10-12 12:24:05
【问题描述】:

我正在使用 BDS 2006,想知道您是否可以使用项目中存在的 XPmanifest 更改 Group-box 和无线电组标题的颜色(因为它始终是蓝色的)。

【问题讨论】:

    标签: delphi colors caption groupbox


    【解决方案1】:

    唯一的方法是覆盖 TGroupBox 的 Paint 方法。

    检查这个使用 Interposer 类的示例

    type
      TGroupBox = class(StdCtrls.TGroupBox) //declare this before of your form definition
      public
        procedure Paint; override;
      end;
    
    uses
     Themes;
    
    { TGroupBox }
    
    procedure TGroupBox.Paint;
    var
      H: Integer;
      R: TRect;
      Flags: Longint;
      CaptionRect,
      OuterRect: TRect;
      Size: TSize;
      Box: TThemedButton;
      Details: TThemedElementDetails;
    begin
      with Canvas do
      begin
        Font := Self.Font;
    
        if ThemeControl(Self) then
        begin
          if Text <> '' then
          begin
            GetTextExtentPoint32(Handle, PChar(Text), Length(Text), Size);
            CaptionRect := Rect(0, 0, Size.cx, Size.cy);
            if not UseRightToLeftAlignment then
              OffsetRect(CaptionRect, 8, 0)
            else
              OffsetRect(CaptionRect, Width - 8 - CaptionRect.Right, 0);
          end
          else
            CaptionRect := Rect(0, 0, 0, 0);
    
          OuterRect := ClientRect;
          OuterRect.Top := (CaptionRect.Bottom - CaptionRect.Top) div 2;
          with CaptionRect do
            ExcludeClipRect(Handle, Left, Top, Right, Bottom);
          if Enabled then
            Box := tbGroupBoxNormal
          else
            Box := tbGroupBoxDisabled;
          Details := ThemeServices.GetElementDetails(Box);
          //Draw the themed frame
          ThemeServices.DrawElement(Handle, Details, OuterRect);    
          SelectClipRgn(Handle, 0);
          if Text <> '' then
          begin
             H := TextHeight('0');
             if not UseRightToLeftAlignment then
               R := Rect(8, 0, 0, H)
             else
               R := Rect(R.Right - Canvas.TextWidth(Text) - 8, 0, 0, H);
             Flags := DrawTextBiDiModeFlags(DT_SINGLELINE);
             //Now using the Windows.DrawText 
             DrawText(Handle, PChar(Text), Length(Text), R, Flags or DT_CALCRECT);
             Brush.Color := Color;//background color of the caption
             Font.Color := clRed;//the color of the caption.
             DrawText(Handle, PChar(Text), Length(Text), R, Flags);
          end;
        end
        else
        inherited;   //if the control is not themed then use the original paint method.
      end;
    end;
    

    【讨论】:

    • 我找不到 ThemeControl 函数,我想是这个函数 ThemeControl(AControl: TControl): Boolean;
    • @ruzz 我在主题单元内找不到声明(我使用 BDS 2006)
    • 如果 Delphi 2006 中不存在该函数,则不要写 if ThemeControl(Self) then,而是写这个 if ThemeServices.ThemesEnabled then
    • i.stack.imgur.com/q3XM0.jpg 这是我得到的,但我希望文本的背景与我的表单背景相同(在这种情况下是图像)
    • 您必须使用与表单颜色相同的值设置TGroupBoxcolor 属性。如果这不起作用,您可以在Brush.Color := Color; 这一行中设置背景颜色(请阅读代码中的 cmets)。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多