Advertisement
HwapX

Advanced PageControl OwnerDraw

Feb 24th, 2014
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Delphi 4.62 KB | None | 0 0
  1. unit Unit3;
  2.  
  3. interface
  4.  
  5. uses
  6.   Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  7.   Dialogs, ComCtrls, GraphUtil, StdCtrls;
  8.  
  9. const
  10.   TCM_ADJUSTRECT = $1328;
  11. type
  12.  
  13.   TTabSheet = class(comctrls.TTabSheet)
  14.     procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  15.   end;
  16.  
  17.   TPageControl = class(comctrls.TPageControl)
  18.   protected
  19.     procedure CNDrawitem(var Message: TWMDrawItem); message CN_DRAWITEM;
  20.     procedure WMEraseBkGnd(var Msg: TWMEraseBkGnd); message WM_ERASEBKGND;
  21.     procedure TCMAdjustRect(var Msg: TMessage); message TCM_ADJUSTRECT;
  22.   end;
  23.  
  24.   TForm3 = class(TForm)
  25.     PageControl1: TPageControl;
  26.     TabSheet1: TTabSheet;
  27.     TabSheet2: TTabSheet;
  28.     TabSheet3: TTabSheet;
  29.     TabSheet4: TTabSheet;
  30.     Button1: TButton;
  31.     procedure PageControl1DrawTab(Control: TCustomTabControl; TabIndex: Integer;
  32.       const Rect: TRect; Active: Boolean);
  33.   private
  34.     { Private declarations }
  35.   public
  36.     { Public declarations }
  37.   end;
  38.  
  39. var
  40.   Form3: TForm3;
  41.  
  42. implementation
  43.  
  44. {$R *.dfm}
  45.  
  46. procedure TPageControl.CNDrawitem(var Message: TWMDrawItem);
  47. var
  48.   Color: TColor;
  49.   Rect: TRect;
  50.   Rgn: HRGN;
  51. begin
  52.   if Assigned(OnDrawTab) then
  53.   begin
  54.     // magic numbers corresponding to where the OS draw the borders
  55.     Rect := Message.DrawItemStruct.rcItem;
  56.     if Bool(Message.DrawItemStruct.itemState and ODS_SELECTED) then begin
  57.       Inc(Rect.Left, 2);
  58.   //    Inc(Rect.Top, 1);
  59.       Dec(Rect.Right, 2);
  60.       Dec(Rect.Bottom, 3);
  61.     end else begin
  62.       Dec(Rect.Left, 2);
  63.       Dec(Rect.Top, 2);
  64.       Inc(Rect.Right, 2);
  65.       Inc(Rect.Bottom);
  66.     end;
  67.  
  68.     {if Rect.Right > Left + Width - 50 then
  69.       Rect.Right := Left + Width - 50; }
  70.     if Rect.Right > Left + Width - 44 then
  71.       Rgn := CreateRectRgn(Rect.Left, Rect.Top, Left + Width - 44, Rect.Bottom + 10)
  72.     else
  73.       Rgn := 0;//CreateRectRgn(Rect.Left-1, Rect.Top, Rect.Right, Rect.Bottom);
  74.  
  75.     SelectClipRgn(Self.Canvas.Handle, Rgn);
  76.  
  77.     OnDrawTab(Self, Message.DrawItemStruct.itemID, Rect, Bool(Message.DrawItemStruct.itemState and ODS_SELECTED));
  78.  
  79.     DeleteObject(Rgn);
  80.  
  81.     // we want to clip the DC so that the borders to be drawn are out of region
  82.     Rgn := CreateRectRgn(0, 0, 0, 0);
  83.     SelectClipRgn(Message.DrawItemStruct.hDC, Rgn);
  84.     DeleteObject(Rgn);
  85.  
  86.     Message.Result := 1;
  87.   end;
  88.   //inherited;
  89. end;
  90.  
  91. procedure TForm3.PageControl1DrawTab(Control: TCustomTabControl;
  92.   TabIndex: Integer; const Rect: TRect; Active: Boolean);
  93. var
  94.   R: TRect;
  95. begin
  96.   Control.Canvas.Font.Color := clBlack;
  97.   Control.Canvas.Font.Style := [];
  98.   R := Rect;
  99.   Inc(R.Bottom, 2);
  100.   Inc(R.Left, -2);
  101.   Control.Canvas.Brush.Style := bsClear;
  102.   if Active then
  103.     GradientFillCanvas(Control.Canvas, clWhite, clLtGray, R, gdVertical)
  104.   else
  105.     GradientFillCanvas(Control.Canvas, clLtGray, clGray, R, gdVertical);
  106.  
  107.   //Control.Canvas.RoundRect(R, 10, 10);
  108.   Control.Canvas.Rectangle(R);
  109.   R := Rect;
  110.   Inc(R.Left, 6);
  111.   DrawText(Control.Canvas.Handle, TPageControl(Control).Pages[TabIndex].Caption, -1, R, DT_SINGLELINE or DT_VCENTER);
  112.   //Control.Canvas.FillRect(Rect);
  113.   R.Left := R.Right - 20;
  114.   R.Top  := R.Top + 4;
  115.   R.Bottom := R.Top + 16;
  116.   R.Right := R.Right - 4;
  117.   //Control.Canvas.FillRect(R);
  118.   GradientFillCanvas(Control.Canvas, clRed, $AA, R, gdVertical);
  119.   Control.Canvas.Rectangle(R);
  120.   Control.Canvas.Font.Color := clWhite;
  121.   Control.Canvas.Font.Style := [fsBold];
  122.   DrawText(Control.Canvas.Handle, 'X', 1, R, DT_SINGLELINE or DT_CENTER or DT_VCENTER);
  123. end;
  124.  
  125. procedure TPageControl.TCMAdjustRect(var Msg: TMessage);
  126. begin
  127.   inherited;
  128.   if Msg.WParam = 0 then
  129.     InflateRect(PRect(Msg.LParam)^, 4, 4)
  130.   else
  131.     InflateRect(PRect(Msg.LParam)^, -4, -4);
  132. end;
  133.  
  134. procedure TPageControl.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
  135. begin
  136.   Brush.Color := TPageControl(Parent).Color;
  137.   Windows.FillRect(Msg.dc, ClientRect, Brush.Handle);
  138.   //Rectangle(Msg.dc, ClientRect.Left, ClientRect.Top, ClientRect.Right, ClientRect.Bottom);
  139.   Msg.Result := 1;
  140. end;
  141.  
  142. { TTabSheet }
  143.  
  144. procedure TTabSheet.WMEraseBkGnd(var Msg: TWMEraseBkGnd);
  145. var
  146.   C: TCAnvas;
  147. begin
  148.   C:= TCanvas.Create;
  149.   C.Handle := Msg.DC;
  150.  
  151.   C.Refresh;
  152.   C.Brush.Color := $777777;
  153.   C.Rectangle(ClientRect.Left, ClientRect.Top - 20, ClientRect.Right, ClientRect.Bottom);
  154.   {Brush.Color := clltgray;
  155.   Brush.Style := bsSolid;
  156.   SetDCBrushColor(Msg.dc, clRed);
  157.   SetDCPenColor(Msg.dc, clRed);
  158.   Windows.FillRect(Msg.dc, ClientRect, Brush.Handle);    }
  159.   //Windows.Rectangle(Msg.dc, ClientRect.Left, ClientRect.Top, ClientRect.Right, ClientRect.Bottom);
  160.   Msg.Result := 1;
  161.   C.Free;
  162. end;
  163.  
  164. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement