把PageControl的OwnerDraw设为TRUE
然后处理PageControl的OnDrawTab和OnMouseDown事件如下
procedure TForm1.PageControl1DrawTab(Control: TCustomTabControl;
TabIndex: Integer;
const Rect: TRect;
Active: Boolean);
var
TextRect, BtnRect : TRect;
l : Integer;
Text :string;
begin
BtnRect := Rect;
l := (BtnRect.Bottom - BtnRect.Top) div 4;
Inc(btnRect.Top, l);
Dec(btnRect.Bottom, l);
Dec(BtnRect.Right,l);
BtnRect.Left := BtnRect.Right - (BtnRect.Bottom - BtnRect.Top);
TextRect := Rect;
TextRect.Right := BtnRect.Left;
InflateRect(TextRect, -2, 0);
Text := TPageControl(Control).Pages[TabIndex].Caption;
DrawText(Control.Canvas.Handle, PChar(Text), -1, TextRect, DT_LEFT or DT_VCENTER or DT_SINGLELINE);
DrawFrameControl(Control.Canvas.Handle, BtnRect, DFC_CAPTION, DFCS_CAPTIONCLOSE);
end;
procedure TForm1.PageControl1MouseDown(Sender: TObject;
Button: TMouseButton;
Shift: TShiftState;
X, Y: Integer);
var
TabRect,BtnRect : TRect;
l : Integer;
pg : TPageControl;
begin
pg := TPageControl(Sender);
TabRect := pg.TabRect(pg.ActivePageIndex);
BtnRect := TabRect;
l := (BtnRect.Bottom - BtnRect.Top) div 4;
Inc(btnRect.Top, l);
Dec(btnRect.Bottom, l);
Dec(BtnRect.Right,l);
BtnRect.Left := BtnRect.Right - (BtnRect.Bottom - BtnRect.Top);
if PtInRect(BtnRect, Point(X, Y))and
(Application.MessageBox(PChar(Format('你真的决定关闭标签"%s"吗',[pg.ActivePage.Caption])),'', MB_OKCANCEL) = MROK) then
begin
pg.ActivePage.Free();
end;
//
end;