delphi – 如何更改主题TabSheet标题的文本颜色?

delphi – 如何更改主题TabSheet标题的文本颜色?,第1张

概述美好的一天! 我需要在TPageControl中更改某些TabSheet的标题文本颜色.照片上有这样的东西 我知道如何使用OnDrawTab完成.但如果我启用了OwnerDraw,Windows XP Theme的装饰就会消失.这就是为什么我尝试手动绘制这个装饰.这就是我试图这样做的方式: procedure TForm1.PageControl1DrawTab(Control: TCustom 美好的一天!

我需要在TPageControl中更改某些TabSheet的标题的文本颜色.照片上有这样的东西

我知道如何使用OnDrawTab完成.但如果我启用了OwnerDraw,Windows XP theme的装饰就会消失.这就是为什么我尝试手动绘制这个装饰.这就是我试图这样做的方式:

procedure TForm1.PageControl1DrawTab(Control: TCustomTabControl;  TabIndex: Integer; const Rect: TRect; Active: Boolean);var  FRect: TRect;  Text: string;begin  FRect := Control.TabRect(TabIndex);  if Active then    themeServices.DrawElement(Control.Canvas.Handle,themeServices.GetElementDetails(ttTabItemHot),FRect)  else    themeServices.DrawElement(Control.Canvas.Handle,themeServices.GetElementDetails(ttTabItemnormal),FRect);  Text := PageControl1.Pages[TabIndex].Caption;  Control.Canvas.Brush.Style := bsClear;  if not Active then    FRect.top := FRect.top + 4;  DrawText(Control.Canvas.Handle,PChar(Text),Length(Text),FRect,DT_SINGLEliNE or DT_CENTER or DT_VCENTER);end;

我得到了这个

(左 – OwnerDraw版本,右 – 默认绘制)

正如您所看到的,TabSheets有一些不透支的边框.我不能透支这个边界.

如何正确绘制选项卡的背景(如右侧的PageControl)?

解决方法 可能的解决方案是覆盖 TPageControl的Paintwindow方法而不是使用ownerdraw,这样您就可以控制选项卡的每个可视方面.

检查这个基本样本.

type  TPageControl = class(Vcl.ComCtrls.TPageControl)  private    FcolorTextTab: Tcolor;    procedure  DrawTab(LCanvas: TCanvas; Index: Integer);    procedure  DoDraw(DC: HDC; DrawTabs: Boolean);    procedure SetcolorTextTab(const Value: Tcolor);  protected    procedure Paintwindow(DC: HDC); overrIDe;  published    property  colorTextTab : Tcolor read FcolorTextTab write SetcolorTextTab;  end;  TForm1 = class(TForm)    PageControl1: TPageControl;    TabSheet1: TTabSheet;    TabSheet2: TTabSheet;    CheckBox1: TCheckBox;    button2: Tbutton;    button3: Tbutton;    button4: Tbutton;    TabSheet3: TTabSheet;    TabSheet4: TTabSheet;    TabSheet5: TTabSheet;    TabSheet6: TTabSheet;    procedure FormCreate(Sender: TObject);  private    { Private declarations }  public    { Public declarations }  end;var  Form1: TForm1;implementation{$R *.dfm}uses Math,themes,Types;type  TCustomTabControlClass = class(TCustomTabControl);procedure Angletextout2(Canvas: TCanvas; Angle: Integer; X,Y: Integer; const Text: string);var  NewFontHandle,oldFontHandle: hFont;  LogRec: TLogFont;begin  Getobject(Canvas.Font.Handle,SizeOf(LogRec),Addr(LogRec));  LogRec.lfEscapement := Angle * 10;  LogRec.lfOrIEntation := LogRec.lfEscapement;  NewFontHandle := CreateFontIndirect(LogRec);  oldFontHandle := SelectObject(Canvas.Handle,NewFontHandle);  SetBkMode(Canvas.Handle,transparent);  Canvas.textout(X,Y,Text);  NewFontHandle := SelectObject(Canvas.Handle,oldFontHandle);  DeleteObject(NewFontHandle);end;{ TPageControl }procedure TPageControl.DrawTab(LCanvas: TCanvas; Index: Integer);var  LDetails    : TthemedElementDetails;  limageIndex : Integer;  LthemedTab  : TthemedTab;  liconRect   : TRect;  R,LayoutR  : TRect;  limageW,limageH,DxImage : Integer;  Ltextx,LTextY: Integer;  LTextcolor    : Tcolor;    //draw the text in the tab    procedure DrawControlText(const S: string; var R: TRect; Flags: Cardinal);    var      @R_403_3468@: T@R_403_3468@Flags;    begin      LCanvas.Font       := Font;      @R_403_3468@         := T@R_403_3468@Flags(Flags);      LCanvas.Font.color := LTextcolor;      StyleServices.DrawText(LCanvas.Handle,LDetails,S,R,@R_403_3468@,LCanvas.Font.color);    end;begin  //get the size of tab image (icon)  if (Images <> nil) and (Index < Images.Count) then  begin    limageW := Images.WIDth;    limageH := Images.Height;    DxImage := 3;  end  else  begin    limageW := 0;    limageH := 0;    DxImage := 0;  end;  R := TabRect(Index);  //check the left position of the tab.  if R.left < 0 then Exit;  //adjust the size of the tab to draw  if Tabposition in [tptop,tpBottom] then  begin    if Index = TabIndex then      InflateRect(R,2);  end  else  if Index = TabIndex then    Dec(R.left,2)  else    Dec(R.Right,2);  LCanvas.Font.Assign(Font);  LayoutR := R;  LthemedTab := ttTabDontCare;  //Get the type of the active tab to draw  case Tabposition of    tptop:      begin        if Index = TabIndex then          LthemedTab := ttTabItemSelected        else        {        if (Index = HottabIndex) and MouseInControl then          LthemedTab := ttTabItemHot        else        }          LthemedTab := ttTabItemnormal;      end;    tpleft:      begin        if Index = TabIndex then          LthemedTab := ttTabItemleftEdgeSelected        else        {        if (Index = HottabIndex) and MouseInControl then          LthemedTab := ttTabItemleftEdgeHot        else        }          LthemedTab := ttTabItemleftEdgenormal;      end;    tpBottom:      begin        if Index = TabIndex then          LthemedTab := ttTabItemBothEdgeSelected        else        {        if (Index = HottabIndex) and MouseInControl then          LthemedTab := ttTabItemBothEdgeHot        else        }          LthemedTab := ttTabItemBothEdgenormal;      end;    tpRight:      begin        if Index = TabIndex then          LthemedTab := ttTabItemRightEdgeSelected        else        {        if (Index = HottabIndex) and MouseInControl then          LthemedTab := ttTabItemRightEdgeHot        else        }          LthemedTab := ttTabItemRightEdgenormal;      end;  end;  //draw the tab  if StyleServices.Available then  begin    LDetails := StyleServices.GetElementDetails(LthemedTab);//necesary for DrawControlText and draw the icon    StyleServices.DrawElement(LCanvas.Handle,R);  end;  //get the index of the image (icon)  if Self is TCustomTabControl then    limageIndex := TCustomTabControlClass(Self).GetimageIndex(Index)  else    limageIndex := Index;  //draw the image  if (Images <> nil) and (limageIndex >= 0) and (limageIndex < Images.Count) then  begin    liconRect := LayoutR;    case Tabposition of      tptop,tpBottom:        begin          liconRect.left := liconRect.left + DxImage;          liconRect.Right := liconRect.left + limageW;          LayoutR.left := liconRect.Right;          liconRect.top := liconRect.top + (liconRect.Bottom - liconRect.top) div 2 - limageH div 2;          if (Tabposition = tptop) and (Index = TabIndex) then            OffsetRect(liconRect,-1)          else          if (Tabposition = tpBottom) and (Index = TabIndex) then            OffsetRect(liconRect,1);        end;      tpleft:        begin          liconRect.Bottom := liconRect.Bottom - DxImage;          liconRect.top := liconRect.Bottom - limageH;          LayoutR.Bottom := liconRect.top;          liconRect.left := liconRect.left + (liconRect.Right - liconRect.left) div 2 - limageW div 2;        end;      tpRight:        begin          liconRect.top := liconRect.top + DxImage;          liconRect.Bottom := liconRect.top + limageH;          LayoutR.top := liconRect.Bottom;          liconRect.left := liconRect.left + (liconRect.Right - liconRect.left) div 2 - limageW div 2;        end;    end;    if StyleServices.Available then      StyleServices.DrawIcon(LCanvas.Handle,liconRect,Images.Handle,limageIndex);  end;  //draw the text of the tab  if StyleServices.Available then  begin    //StyleServices.GetElementcolor(LDetails,ecTextcolor,LTextcolor);    LTextcolor:=FcolorTextTab;    if (Tabposition = tptop) and (Index = TabIndex) then      OffsetRect(LayoutR,-1)    else    if (Tabposition = tpBottom) and (Index = TabIndex) then      OffsetRect(LayoutR,1);    if Tabposition = tpleft then    begin      Ltextx := LayoutR.left + (LayoutR.Right - LayoutR.left) div 2 - LCanvas.TextHeight(Tabs[Index]) div 2;      LTextY := LayoutR.top + (LayoutR.Bottom - LayoutR.top) div 2 + LCanvas.TextWIDth(Tabs[Index]) div 2;      LCanvas.Font.color:=LTextcolor;      Angletextout2(LCanvas,90,Ltextx,LTextY,Tabs[Index]);    end    else    if Tabposition = tpRight then    begin      Ltextx := LayoutR.left + (LayoutR.Right - LayoutR.left) div 2 + LCanvas.TextHeight(Tabs[Index]) div 2;      LTextY := LayoutR.top + (LayoutR.Bottom - LayoutR.top) div 2 - LCanvas.TextWIDth(Tabs[Index]) div 2;      LCanvas.Font.color:=LTextcolor;      Angletextout2(LCanvas,-90,Tabs[Index]);    end    else     DrawControlText(Tabs[Index],LayoutR,DT_VCENTER or DT_CENTER or DT_SINGLEliNE  or DT_NOCliP);  end;end;procedure TPageControl.DoDraw(DC: HDC; DrawTabs: Boolean);var  Details: TthemedElementDetails;  R: TRect;  lindex,Selindex: Integer;begin  Details := StyleServices.GetElementDetails(ttTabItemnormal);  Selindex := TabIndex;  try    Canvas.Handle := DC;    if DrawTabs then      for lindex := 0 to Tabs.Count - 1 do        if lindex <> Selindex then         DrawTab(Canvas,lindex);    if Selindex < 0 then      R := Rect(0,WIDth,Height)    else    begin      R := TabRect(Selindex);      R.left := 0;      R.top := R.Bottom;      R.Right := WIDth;      R.Bottom := Height;    end;    StyleServices.DrawElement(DC,StyleServices.GetElementDetails(ttPane),R);    if (Selindex >= 0) and DrawTabs then      DrawTab(Canvas,Selindex);  finally    Canvas.Handle := 0;  end;end;procedure TPageControl.Paintwindow(DC: HDC);begin DoDraw(DC,True); //inherited;end;procedure TPageControl.SetcolorTextTab(const Value: Tcolor);begin  FcolorTextTab := Value;end;procedure TForm1.FormCreate(Sender: TObject);begin  PageControl1.colorTextTab:=clGreen;end;

这就是结果.

总结

以上是内存溢出为你收集整理的delphi – 如何更改主题TabSheet标题的文本颜色?全部内容,希望文章能够帮你解决delphi – 如何更改主题TabSheet标题的文本颜色?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/langs/1273927.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-08
下一篇 2022-06-08

发表评论

登录后才能评论

评论列表(0条)

保存