How can I change the orientation of the label on a TPageControl?

Mark W picture Mark W · Apr 20, 2009 · Viewed 7k times · Source

I'm new to Delphi (again - I used Delphi back in 1994). I now have Delphi 2009 Pro.

Coming from Java, I find the object inheritance very obscure.

My users want tabbed pages with the tabs on the left. But, the TPageControl doesn't allow the tab label direction or orientation to be changed. They want the words on the tabs to read top to bottom with the letters rotated so they are in a "normal" orientation. With the tabs on the left the labels read from the bottom up with the letters rotated 90 deg. to the left and there is a tendency to tilt your head to the left to read the tabs. I found several enhancements to the standard TPageControl VCL that add images, text and color changes for hover and active, but nothing that allows the manipulation of font direction or orientation on the tabs.

Page Control Tabls should look something like:

P
a
g
e
1

P
a
g
e
2

P
a
g
e
3

And so on...

Answer

ulrichb picture ulrichb · Apr 20, 2009

1.) set the TPageControl properties:

TabPosition := tpLeft;
OwnerDraw := True;
TabWidth := 180;    //set to any adequate value because
                    // TPageControl doesn't have a measure event handler 

2.) use the following OnDrawTab code:

procedure TForm1.PageControl1DrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  I: Integer;
  PageControl: TPageControl;
  TextFormat: TTextFormat;
  Text: string;
  TextRect: TRect;
begin
  PageControl := Control as TPageControl;

  Text := PageControl.Pages[TabIndex].Caption;

  for I := Length(Text) - 1 downto 1 do
  begin
    Text := Copy(Text, 1, I) + sLineBreak + Copy(Text, I+1, MaxInt);
  end;

  TextRect := Rect;
  TextRect.Left := TextRect.Left + 5;
  TextRect.Top := TextRect.Top + 3;

  TextFormat := [tfCenter];

  PageControl.Canvas.TextRect(
    TextRect,
    Text,
    TextFormat
    );
end;

3.) compile, start and enjoy !