TPageControl/TTabSheet Color

 [1;37m?i HU?|r??|?  [32mdelphi  [37mQ?? ?j
?i -?? [32m yeskw [37m ?oa ?j [m
http://members.truepath.com/delphi/tips/tip85_tpagecontrolttabsheetco...

TPageControl/TTabSheet Color

05/18/1999

This tip is comment and will be further processed in the morning with details on all aspects of the functions used. I wanted to post it now to keep from being too far behind, thnaks for your patience.

For this tip to work you will need to set the TPageControl.OwnerDraw to TRUE.

Example 1

{...}

type
  tmpTabControl = class(TCustomTabControl);

{...}

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    procedure PageControl1DrawTab(
      Control: TCustomTabControl;
      TabIndex: Integer;
      const Rect: TRect;
      Active: Boolean);
    procedure PageControl1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

{...}

{ we will handle the drawing all through the OnDrawTab
  event.  Since we are handling the drawing we will
  have to handle the text as well as any images }
procedure TForm1.PageControl1DrawTab(
  Control: TCustomTabControl;
  TabIndex: Integer;
  const Rect: TRect;
  Active: Boolean);
var
  { a variable trect is needed for the WIN32 API call
    DrawText() }
  tmpRect : TRect;
begin

  { change color per tab }
  case TabIndex of
    0 : PageControl1.Canvas.Brush.Color := clRed;
    1 : PageControl1.Canvas.Brush.Color := clWhite;
    2 : PageControl1.Canvas.Brush.Color := clBlue;
  end;

  { fill the tab rect color }
  tmpRect := Rect;
  PageControl1.Canvas.FillRect(tmpRect);

  { draw our text using DrawText so we can center it
    vertical and horizontal }
  DrawText(PageControl1.Canvas.Handle,
           pChar(PageControl1.Pages[TabIndex].Caption),
           -1,
           tmpRect,
           DT_CENTER or
           DT_SINGLELINE or
           DT_VCENTER);

  { if the tab is active then color the tabsheet }
  if Active then
  begin
    { get our pagecontrol display area using our
      typecast to our TCustomTabControl class }
    tmpRect := tmpTabControl(Control).DisplayRect;

    { resize our rect area to go closer to the borders
      because the displayrect is the area of the
      tabsheet which is a few pixels within the border
      of the pagecontrol }
    InflateRect(tmpRect,2,2);
    OffsetRect(tmpRect,-1,-1);

    { since we do not have direct access to the
      pagecontrol canvas we will be using the WIN32 API
      call to color the rect over the pagecontrol.  to
      get the handle to the canvas area we will use the
      WIN32 API call GetWindowDc() }
    Windows.FillRect(GetWindowDc(PageControl1.Handle),
                     tmpRect,
                     PageControl1.Canvas.Brush.Handle);
  end;
end;

procedure TForm1.PageControl1Change(Sender: TObject);
begin

  { force the pagecontrol to repaint on every tab change }
  PageControl1.Repaint;
end;

{...}

--
 [1;36m? -?:?Eyeskw ? Jun  8 13:54:33 -????E[FROM: dns.hiperfect.com] [m
 [m [1;34m? ?:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m
--
 [m [1;34m? ??:?Ei??aoo? bbs.yeskw.com?E[FROM: dns.hiperfect.com] [m