Jump to content
Mark Williams

TTabControl OwnerDraw Styled

Recommended Posts

I want to show a TabControl with tabs with different font styles. Basically, I want the font in some of the tabs to be shown in strikeout. The app uses a couple of different themes. I thought I'd use the StyleManager to achieve this with the following code:

 

 with (Control as TTabControl) do
    begin
      tabName := TTabControl(Control).Tabs[TabIndex];

	  if TabIndex=1 then
        Canvas.Font.Style:=[fsStrikeout];


      Pt := TabControl2.ScreenToClient(Mouse.CursorPos);
      TabUnderMouse := TabControl1.IndexOfTabAt(Pt.X, Pt.Y);

      if Active then
        Details := TStyleManager.ActiveStyle.GetElementDetails(ttTabItemSelected)
      else if (TabUnderMouse=TabIndex) then
        Details := TStyleManager.ActiveStyle.GetElementDetails(ttTabItemHot)
      else
        Details := TStyleManager.ActiveStyle.GetElementDetails(ttTabItemNormal);

      TStyleManager.ActiveStyle.DrawElement(Canvas.Handle, Details, Rect);

      TStyleManager.ActiveStyle.DrawText(Canvas.Handle, Details, TabName, Rect,
        DT_VCENTER or DT_CENTER, Canvas.Font.Color);
    end;

This sets the colour of the tab background perfectly depending on its state, but has no impact on the font which is always black. However, the font is always black whatever the state of the tab.

The color parameter in the DrawText function does not appear to do anything. I've tried setting the canvas font color before calling Drawtext to the stylefontcolor of the tab element in its various states. It has no effect, not does entering any color whatsoever in the color parameter.

Share this post


Link to post

Managed to work out a solution in the end. Sure there will be some issues I haven't considered, but seems to work ok. If anyone is interested here's the code:

procedure TForm3.TabControl2DrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
  var
    TabName:String;
    Pt : TPoint;
    R: TRect;
    TabUnderMouse:Integer;


begin
  Pt := TabControl2.ScreenToClient(Mouse.CursorPos);
  TabUnderMouse := TabControl1.IndexOfTabAt(Pt.X, Pt.Y);

  with (Control as TTabControl).canvas do
    begin
      R := Rect;

      if TabIndex=1 then
        Font.Style := [fsStrikeout];

      if (TabUnderMouse = TabIndex)and Active  then
        Font.Color := TStyleManager.ActiveStyle.GetStyleFontColor(sfTabTextActiveHot)
      else if Active then
        Font.Color := TStyleManager.ActiveStyle.GetStyleFontColor(sfTabTextActiveNormal)
      else if (TabUnderMouse = TabIndex) then
        Font.Color := TStyleManager.ActiveStyle.GetStyleFontColor(sfTabTextInActiveHot)
      else
        Font.Color := TStyleManager.ActiveStyle.GetStyleFontColor(sfTabTextInActiveNormal);

      tabName := TTabControl(Control).Tabs[TabIndex];

      Brush.Style := bsClear;

      DrawText(Handle, PChar(TabName), Length(TabName), R,  DT_SINGLELINE or DT_VCENTER or DT_CENTER)
  end;
end;

 

  • Like 1

Share this post


Link to post

I didn't know about tabcontrols.  Just started styles last month. Here's my stab at  it using 'Tabber' for the symbol of the TabControl Sender vs 'control' and an assignable TStylefont enum.

 

procedure TForm7.TabControl1DrawTab(Tabber: TCustomTabControl; TabIndex:
    Integer; const Rect: TRect; Active: Boolean);
var
    TabStr:String;
    Pt : TPoint;
    R: TRect;
    TabUnderMouse:Integer;
    JohnFontcolorStyle: TStyleFont;
begin
  with Tabber do
  begin
    Pt := ScreenToClient(Mouse.CursorPos);
    TabUnderMouse := IndexOfTabAt(Pt.X, Pt.Y);

    // Setting default font color to normal
    JohnFontcolorStyle := sfTabTextActiveNormal;

    if (TabUnderMouse = TabIndex) and Active then
      JohnFontcolorStyle := sfTabTextActiveHot;

    if Active then
      JohnFontcolorStyle := sfTabTextActiveNormal;

    if TabUnderMouse = TabIndex then
      JohnFontcolorStyle := sfTabTextInActiveHot;

    with Canvas do
    begin
      R := Rect;
      Font.Color := TStyleManager.ActiveStyle.GetStyleFontColor
        (JohnFontcolorStyle);

      if TabIndex = 0 then
        Font.Style := [fsStrikeout];

      TabStr := (Tabber as TTabControl).Tabs[TabIndex]; // ?

      Brush.Style := bsClear;

      DrawText(Handle, PChar(TabStr), Length(TabStr), R, DT_SINGLELINE or
        DT_VCENTER or DT_CENTER);
    end;

  end;

   

 

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×