☆TabControlのDrag&Drop

TabControlのDrag&Dropのサンプルです。(自コントロール内の処理としています)
TabControlのTabsは、TStringsなので移動はとても簡単です。

unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls;

type
  TForm1 = class(TForm)
    TabControl1: TTabControl;
    procedure TabControl1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
    procedure TabControl1DragDrop(Sender, Source: TObject; X, Y: Integer);
    procedure TabControl1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure FormCreate(Sender: TObject);
  private
    { Private 宣言 }
  public
    { Public 宣言 }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  // タブ名の設定
  for I := 1 to 10 do
    TabControl1.Tabs.Add(IntToStr(I));
end;

procedure TForm1.TabControl1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
begin
  // ちょっとややこしいですけど、自分自身の間でしか許可しない設定です。 
  Accept := ((Sender is TTabControl) and (Source is TTabControl)) and
    ((Sender as TTabControl) = TabControl1) and 
    (TabControl1.TabIndex <> TabControl1.IndexOfTabAt(X,Y));
end;

procedure TForm1.TabControl1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  if (Button = mbLeft) then
    TabControl1.BeginDrag(False, 5);
end;

procedure TForm1.TabControl1DragDrop(Sender, Source: TObject; X, Y: Integer);
var
  DstIndex, SrcIndex: Integer;
begin
  SrcIndex := TabControl1.IndexOfTabAt(X, Y);
  DstIndex := TabControl1.TabIndex;
  TabControl1.Tabs.Move(DstIndex,SrcIndex);
end;

end.

|