如何更改TTabSheets的背景颜色?

时间:2011-05-05 21:35:01

标签: delphi tpagecontrol

我正在使用Delphi 2009设计表单,我试图弄清楚TPageControl元素。我试图为每个标签创建单独的对话框。我可以制作TTabSheets,我可以将我的元素放在TabSheets上,但我的问题是它们是大麦可见的,因为TTabSheet的默认背景看起来是白色的。我试图在TabSheet上放置一个面板,但无论出于何种原因,面板总是出现在TabSheet后面。所以我的问题是:有没有办法将标签页的颜色更改为标准的米色窗口,或者是一种在标签页上放置TPanel,实现相同目标的方法?

5 个答案:

答案 0 :(得分:23)

将style属性设置为tsFlatButtons
background~color~将恢复美丽的clBtnFace

答案 1 :(得分:6)

标签页的标准Windows颜色为白色。当XP主题被引入时,该标准应运而生。如果用户切换回Windows Classic,那么他们将获得灰色背景。 [你的意思是灰色而不是米色不是吗?米色会真的很卑鄙!]

标签页内的面板永远不会在页面后面,因为它在页面内。实际发生的是面板被透明地绘制,以便标准标签页颜色占优势。

答案 2 :(得分:1)

在界面的表单中使用此单元:

unit MSCtrlsStyleHook;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls, Vcl.ExtCtrls, Vcl.Themes,
  Winapi.CommCtrl;

type
  TTabSheet = class(Vcl.ComCtrls.TTabSheet)
  private
    procedure WMEraseBkgnd(var Message: TWMEraseBkgnd); message WM_ERASEBKGND;
  end;

  TPageControl = class(Vcl.ComCtrls.TPageControl)
  private
    procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT;
  end;

implementation

{ TPageControl }

procedure TPageControl.TCMAdjustRect(var Msg: TMessage);
begin
  inherited;
  if Msg.WParam = 0 then
    InflateRect(PRect(Msg.LParam)^, 3, 3)
  else
    InflateRect(PRect(Msg.LParam)^, -3, -3);
end;

{ TTabSheet }

procedure TTabSheet.WMEraseBkgnd(var Message: TWMEraseBkgnd);
var
  LRect  : TRect;
  LCanvas: TCanvas;
begin
  if (PageControl <> nil) and StyleServices.Enabled and
     ((PageControl.Style = tsTabs) or TStyleManager.IsCustomStyleActive) then
  begin
    //Get the bounds of the Tabsheet
    GetWindowRect(Handle, LRect);
    OffsetRect(LRect, -LRect.Left, -LRect.Top);
    //create a TCanvas for erase the background, using the DC of the message
    LCanvas := TCanvas.Create;
    try
      LCanvas.Handle := Message.DC;
      LCanvas.Brush.Color:= $fafafa;// Color You need;
      LCanvas.FillRect(LRect);
    finally
      LCanvas.Handle := 0;
      LCanvas.Free;
    end;

    Message.Result := 1;
  end
  else
    inherited;
end;

end.

答案 3 :(得分:0)

如果您希望将PageControl Style属性保留为tsTabs,则需要修改TTabSheet类...

在表单的类型声明上方,添加以下内容...

  TTabSheet = class(ComCtrls.TTabSheet)
  protected
    procedure PaintWindow(DC: HDC); override;
  end;

然后在该单位的实施部分...

  var brushBtnFace: HBrush;

  procedure TTabSheet.PaintWindow(DC: HDC);
  var
    rec: TRect;
  begin
    rec := ClientRect;
    windows.FillRect(DC, rec, brushBtnFace);
  end;

最后在单元的初始化和完成部分中创建并销毁画笔...

  initialization
    brushBtnFace := CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  finalization
    DeleteObject(brushBtnFace);

答案 4 :(得分:-1)

不喜欢任何一种解决方案,这就是我正在采取的措施来阻止你遇到的问题。您不需要牺牲Windows主题来使其工作:

请检查:

if ThemeServices.ThemesEnabled then
  FormBGColor := clBtnHighlight
else
  FormBGColor := clBtnFace;

并在您在标签上显示之前设置表单的颜色。

(就个人而言,我从来不喜欢这个解决方案,但这是我开始工作的程序的主要部分是如何在我开始之前进行编程的,所以在我的compuer XOR终端服务器上看起来不像废话就是我必须要做的事情)


ThemeServices位于themes.pas