Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Delphi: Общие вопросы > TPageControl и кнопка close


Автор: ZBugz 19.8.2010, 16:38
Всем привет. Искал, не нашел, если что, дайте ссылку...
Как сделать кнопку закрытия вкладки у TPageControl ?
Хочу свою, компоненты не устраивают (видел только те, кто скины использует).

Автор: fantomba 19.8.2010, 17:14
как пример:
Код

procedure TForm1.Button1Click(Sender: TObject);
begin
pagecontrol1.Pages[1].TabVisible:=false;
end;

Автор: ZBugz 20.8.2010, 08:11
Не, мне вот такую кнопочку надо smile

Автор: zhefran 20.8.2010, 17:41
Для Delphi 7 есть бесплатный компонент PageExtControl - смотри пририкрепленный файл.
Для 2010:
Код

type
  TFormMain = class(TForm)
    {...}
  private
    FCloseButtonsRect: array of TRect;
    FCloseButtonMouseDownIndex: Integer;
    FCloseButtonShowPushed: Boolean;
    {...}
  end;

{...}

procedure TFormMain.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  PageControlCloseButton.TabWidth := 150;
  PageControlCloseButton.OwnerDraw := True;

  //should be done on every change of the page count
  SetLength(FCloseButtonsRect, PageControlCloseButton.PageCount);
  FCloseButtonMouseDownIndex := -1;

  for I := 0 to Length(FCloseButtonsRect) - 1 do
  begin
    FCloseButtonsRect[I] := Rect(0, 0, 0, 0);
  end;
end;

procedure TFormMain.PageControlCloseButtonDrawTab(Control: TCustomTabControl;
  TabIndex: Integer; const Rect: TRect; Active: Boolean);
var
  CloseBtnSize: Integer;
  PageControl: TPageControl;
  TabCaption: TPoint;
  CloseBtnRect: TRect;
  CloseBtnDrawState: Cardinal;
  CloseBtnDrawDetails: TThemedElementDetails;
begin
  PageControl := Control as TPageControl;

  if InRange(TabIndex, 0, Length(FCloseButtonsRect) - 1) then
  begin
    CloseBtnSize := 14;
    TabCaption.Y := Rect.Top + 3;

    if Active then
    begin
      CloseBtnRect.Top := Rect.Top + 4;
      CloseBtnRect.Right := Rect.Right - 5;
      TabCaption.X := Rect.Left + 6;
    end
    else
    begin
      CloseBtnRect.Top := Rect.Top + 3;
      CloseBtnRect.Right := Rect.Right - 5;
      TabCaption.X := Rect.Left + 3;
    end;

    CloseBtnRect.Bottom := CloseBtnRect.Top + CloseBtnSize;
    CloseBtnRect.Left := CloseBtnRect.Right - CloseBtnSize;
    FCloseButtonsRect[TabIndex] := CloseBtnRect;

    PageControl.Canvas.FillRect(Rect);
    PageControl.Canvas.TextOut(TabCaption.X, TabCaption.Y, PageControl.Pages[TabIndex].Caption);

    if not UseThemes then
    begin
      if (FCloseButtonMouseDownIndex = TabIndex) and FCloseButtonShowPushed then
        CloseBtnDrawState := DFCS_CAPTIONCLOSE + DFCS_PUSHED
      else
        CloseBtnDrawState := DFCS_CAPTIONCLOSE;

      Windows.DrawFrameControl(PageControl.Canvas.Handle,
        FCloseButtonsRect[TabIndex], DFC_CAPTION, CloseBtnDrawState);
    end
    else
    begin
      Dec(FCloseButtonsRect[TabIndex].Left);

      if (FCloseButtonMouseDownIndex = TabIndex) and FCloseButtonShowPushed then
        CloseBtnDrawDetails := ThemeServices.GetElementDetails(twCloseButtonPushed)
      else
        CloseBtnDrawDetails := ThemeServices.GetElementDetails(twCloseButtonNormal);

      ThemeServices.DrawElement(PageControl.Canvas.Handle, CloseBtnDrawDetails,
        FCloseButtonsRect[TabIndex]);
    end;
  end;
end;

procedure TFormMain.PageControlCloseButtonMouseDown(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  I: Integer;
  PageControl: TPageControl;
begin
  PageControl := Sender as TPageControl;

  if Button = mbLeft then
  begin
    for I := 0 to Length(FCloseButtonsRect) - 1 do
    begin
      if PtInRect(FCloseButtonsRect[I], Point(X, Y)) then
      begin
        FCloseButtonMouseDownIndex := I;
        FCloseButtonShowPushed := True;
        PageControl.Repaint;
      end;
    end;
  end;
end;

procedure TFormMain.PageControlCloseButtonMouseMove(Sender: TObject;
  Shift: TShiftState; X, Y: Integer);
var
  PageControl: TPageControl;
  Inside: Boolean;
begin
  PageControl := Sender as TPageControl;

  if (ssLeft in Shift) and (FCloseButtonMouseDownIndex >= 0) then
  begin
    Inside := PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y));

    if FCloseButtonShowPushed <> Inside then
    begin
      FCloseButtonShowPushed := Inside;
      PageControl.Repaint;
    end;
  end;
end;

procedure TFormMain.PageControlCloseButtonMouseLeave(Sender: TObject);
var
  PageControl: TPageControl;
begin
  PageControl := Sender as TPageControl;
  FCloseButtonShowPushed := False;
  PageControl.Repaint;
end;

procedure TFormMain.PageControlCloseButtonMouseUp(Sender: TObject;
  Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
var
  PageControl: TPageControl;
begin
  PageControl := Sender as TPageControl;

  if (Button = mbLeft) and (FCloseButtonMouseDownIndex >= 0) then
  begin
    if PtInRect(FCloseButtonsRect[FCloseButtonMouseDownIndex], Point(X, Y)) then
    begin
      ShowMessage('Button ' + IntToStr(FCloseButtonMouseDownIndex + 1) + ' pressed!');

      FCloseButtonMouseDownIndex := -1;
      PageControl.Repaint;
    end;
  end;
end;


Чесно говоря работоспособность кода для 2010 не проверял.

Автор: ZBugz 21.8.2010, 09:24
Спасибо, я его видел. Тока там еще кучу юнитов хочет от других компонетов, котрых незнаю где искать .

Автор: Akella 21.8.2010, 12:01
а так пробовал?
http://www.google.com/search?ie=UTF-8&oe=UTF-8&sourceid=navclient&gfns=1&q=TPageControl+%D0%B8+%D0%BA%D0%BD%D0%BE%D0%BF%D0%BA%D0%B0+close

Автор: ZBugz 24.8.2010, 14:38
Усе не то вообще :( 
Че , нихто не знам ?  smile 

Автор: zhefran 27.8.2010, 12:50
Цитата(ZBugz @ 21.8.2010,  09:24)
Спасибо, я его видел. Тока там еще кучу юнитов хочет от других компонетов, котрых незнаю где искать .

В папку с проектом нужно вкинуть PageExtControl.res - у меня так работает.

Автор: ZBugz 28.8.2010, 09:00
На 2010 это не помогает :( Лана, я из Alpha Skins стащил, но хотелось бы самому конечно...

Автор: x128 28.8.2010, 10:28
Цитата(ZBugz @  28.8.2010,  09:00 Найти цитируемый пост)
но хотелось бы самому конечно...

Установи OwnerDraw:=true и в OnDrawTab рисуй свою кнопку.

Автор: ZBugz 30.8.2010, 08:18
Цитата(x128 @ 28.8.2010,  10:28)
Цитата(ZBugz @  28.8.2010,  09:00 Найти цитируемый пост)
но хотелось бы самому конечно...

Установи OwnerDraw:=true и в OnDrawTab рисуй свою кнопку.

Знать бы как smile Я в этом ниче не понимаю... 

Автор: Akella 30.8.2010, 09:35
А этот пример смотрел?
http://dumpz.ru/showthread.php?t=52677

Автор: AnalogXP 30.8.2010, 15:50
А кажись темы поддерживать нельзя?

Автор: ZBugz 31.8.2010, 11:47
Цитата(Akella @ 30.8.2010,  09:35)
А этот пример смотрел?
http://dumpz.ru/showthread.php?t=52677

Пашет smile Но скины убивают кнопку  smile 
Пример прилагаю, для тех кто будет делать без скинов smile 

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)