如何更改 TTabSheets 的背景颜色?

发布于 2024-11-05 02:12:39 字数 276 浏览 3 评论 0原文

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

Im Designing a Form with Delphi 2009, and Im trying to figure out the TPageControl element. Im trying to make separate dialogs for each tab. I can make the TTabSheets, and I can place my elements on the TabSheets, but my problem is that they are barley visible, as the default background for a TTabSheet appears to be white. Ive tried to place a panel on the TabSheet, but for whatever reason, the panel always appears behind the TabSheet. So my question: Is there any way to change the color of a tab sheet to the standard windows beige, or is their a way to place a TPanel on the tab page, accomplishing the same goal?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

不乱于心 2024-11-12 02:12:39

将样式属性设置为 tsFlatButtons
背景〜颜色〜将恢复为美丽的clBtnFace

Set the style property to tsFlatButtons
The background ~colour~ will revert to beautiful clBtnFace

半寸时光 2024-11-12 02:12:39

选项卡的标准 Windows 颜色是白色。该标准是在 XP 主题推出时应运而生的。如果用户切换回 Windows Classic,他们将看到灰色背景。 [你的意思是灰色而不是米色,不是吗?米色真的很卑鄙!]

选项卡内的面板永远不能位于页面后面,因为它位于页面内部。实际发生的情况是,面板被透明绘制,以便标准选项卡颜色占主导地位。

The standard Windows colour for a tab sheet is white. That standard came into being when XP themes were introduced. If a user switches back to Windows Classic then they will get a grey background. [You do mean grey rather than beige don't you? Beige would be truly vile!]

A panel inside a tab sheet can never be behind the page since it is inside the page. What is actually happening is that the panel is being drawn transparently so that the standard tab sheet colour prevails.

只想待在家 2024-11-12 02:12:39

在您的表单中的界面中使用此单位:

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.

Use this unit in your Form at the interface:

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.
吃素的狼 2024-11-12 02:12:39

不太喜欢这两种解决方案,这就是我正在做的事情来阻止您遇到的问题。您不需要牺牲 Windows 主题来使其工作:

只需检查:

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

并设置表单的颜色,然后再将其显示在选项卡上。

(就我个人而言,我从来不喜欢这个解决方案,但这就是我在开始之前编写的程序的主要部分是如何编程的,因此为了使它在我的计算机异或终端服务器上看起来不像垃圾,这就是我必须做的)


ThemeServices 是在主题.pas中

Not liking either solution much, this is what I'm doing to thwart the problem you're having. You don't need to sacrifice windows themes to make it work:

just check:

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

and set the color of the form before you show it on your tabs.

(personally, I never liked this solution, but it's how a major part of the program I work on was programmed before I started, so to make it not look like crap on my compuer XOR terminal servers that's what I had to do)


ThemeServices is in themes.pas

ぽ尐不点ル 2024-11-12 02:12:39

如果您希望将 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);

If you wish to preserve the PageControl Style property as tsTabs then you'll need to hack the TTabSheet class ...

Just above your form's type declaration add the following ...

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

Then in the unit's implementation section ...

  var brushBtnFace: HBrush;

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

And finally create and destroy your brush in the unit's initialization and finalization sections ...

  initialization
    brushBtnFace := CreateSolidBrush(GetSysColor(COLOR_BTNFACE));
  finalization
    DeleteObject(brushBtnFace);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文