是否可以更改TTabSheet选项卡的颜色

时间:2012-02-18 11:57:52

标签: delphi lazarus ttabsheet

我正在跑Lazarus 0.9.30.2。 我有一个TForm,其中有一个TPageControl。在TPageControl中有一系列TTabSheets(大约30个)。我想要做的是对标签进行颜色编码,因此前10个为红色,后10个为蓝色,后10个为绿色。我已经看到内联网上的代码片段,当您单击它们并导航到它们(以突出显示活动选项卡)时,它会更改选项卡表颜色(包括选项卡本身),但我想要做的是如上所述对它们进行着色。标签页首先加载。

有办法吗?

enter image description here

1 个答案:

答案 0 :(得分:4)

如果这足以让您获得一些棘手的解决方案 仅在禁用主题的Windows上工作 ,请尝试以下操作:

取消选中Use manifest file to enable themes (Windows only)项目设置对话框中的Project / Project Options ...选项,然后将以下代码粘贴到具有页面控制功能的设备中。它使用插入的类,因此它只能在粘贴此代码的单元中起作用。

uses
  ComCtrls, Windows, LCLType;

type
  TPageControl = class(ComCtrls.TPageControl)
  private
    procedure CNDrawItem(var Message: TWMDrawItem); message WM_DRAWITEM;
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end; 

implementation

procedure TPageControl.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  with Params do
  begin
    if not (csDesigning in ComponentState) then
      Style := Style or TCS_OWNERDRAWFIXED;
  end;
end;

procedure TPageControl.CNDrawItem(var Message: TWMDrawItem);
var
  BrushHandle: HBRUSH;
  BrushColor: COLORREF;
begin
  with Message.DrawItemStruct^ do
  begin
    case itemID of
      0: BrushColor := RGB(235, 24, 33);
      1: BrushColor := RGB(247, 200, 34);
      2: BrushColor := RGB(178, 229, 26);
    else
      BrushColor := ColorToRGB(clBtnFace);
    end;
    BrushHandle := CreateSolidBrush(BrushColor);
    FillRect(hDC, rcItem, BrushHandle);
    SetBkMode(hDC, TRANSPARENT);
    DrawTextEx(hDC, PChar(Page[itemID].Caption), -1, rcItem, DT_CENTER or 
      DT_VCENTER or DT_SINGLELINE, nil);
  end;
  Message.Result := 1;
end;

这是它的样子(丑陋:)

enter image description here

相关问题