Delphi中TTabSheet能否自定义设置不同颜色以改变选项卡外观?
- 内容介绍
- 文章标签
- 相关推荐
本文共计442个文字,预计阅读时间需要2分钟。
我在使用Lazarus 0.9.30.2,有一个TForm,其中包含一个TPageControl。在TPageControl中有30个TTabSheet标签页。我想要对标签页进行颜色编码,前10个为红色,接下来10个为蓝色,再接下来10个为绿色。我已经...
我正在跑Lazarus 0.9.30.2.我有一个TForm,其中有一个TPageControl.在TPageControl中有一系列TTabSheets(大约30个).我想要做的是对标签进行颜色编码,因此前10个为红色,后10个为蓝色,后10个为绿色.我已经看到内联网上的代码片段,当您单击它们并导航到它们(以突出显示活动选项卡)时,它会更改选项卡表颜色(包括选项卡本身),但我想要做的是如上所述对它们进行着色.标签页首先加载.
有办法做到这一点吗?
如果只有在禁用主题的 Windows上才能获得有点棘手的解决方案,那么请尝试以下方法:从项目/项目选项…项目设置对话框中取消选中使用清单文件以启用主题(仅限Windows)选项,并将以下代码粘贴到具有页面控制的单元中.它使用插入的类,因此它只能在粘贴此代码的单元中使用.
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;
这是它的样子(丑陋:)
本文共计442个文字,预计阅读时间需要2分钟。
我在使用Lazarus 0.9.30.2,有一个TForm,其中包含一个TPageControl。在TPageControl中有30个TTabSheet标签页。我想要对标签页进行颜色编码,前10个为红色,接下来10个为蓝色,再接下来10个为绿色。我已经...
我正在跑Lazarus 0.9.30.2.我有一个TForm,其中有一个TPageControl.在TPageControl中有一系列TTabSheets(大约30个).我想要做的是对标签进行颜色编码,因此前10个为红色,后10个为蓝色,后10个为绿色.我已经看到内联网上的代码片段,当您单击它们并导航到它们(以突出显示活动选项卡)时,它会更改选项卡表颜色(包括选项卡本身),但我想要做的是如上所述对它们进行着色.标签页首先加载.
有办法做到这一点吗?
如果只有在禁用主题的 Windows上才能获得有点棘手的解决方案,那么请尝试以下方法:从项目/项目选项…项目设置对话框中取消选中使用清单文件以启用主题(仅限Windows)选项,并将以下代码粘贴到具有页面控制的单元中.它使用插入的类,因此它只能在粘贴此代码的单元中使用.
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;
这是它的样子(丑陋:)

