7

我正在尝试模拟 Chrome 的标签拖动功能。我希望用户能够将选项卡拖动到选项卡条中的新位置或将其拖放到应用程序外部以创建新窗口。在应用程序中拖动很容易,但我如何检测用户何时在我的应用程序之外的某个地方掉线?

本质上,我希望实现“撕下”标签。

4

1 回答 1

8

由于鼠标在拖动操作期间被捕获,因此在处理程序中检测拖动操作何时完成没有问题OnEndDrag,即使它在应用程序的任何形式之外。您可以通过测试“目标”对象来判断放置是否被接受,如果放置不被接受,您可以通过测试鼠标位置来判断它是否在应用程序之外。

但是,这种方法仍然存在问题。您无法通过按“Esc”键来判断拖动是否被取消。还有一个问题是无法在表单外将拖动光标设置为“接受”,因为OnDragOver那里不会调用任何控件。

您可以通过使用您创建的拖动对象更改拖动操作的行为来克服这些问题。下面是一个例子:

unit Unit1;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs, Vcl.ComCtrls;

type
  TForm1 = class(TForm)
    PageControl1: TPageControl;
    TabSheet1: TTabSheet;
    TabSheet2: TTabSheet;
    TabSheet3: TTabSheet;
    procedure FormCreate(Sender: TObject);
    procedure PageControl1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PageControl1StartDrag(Sender: TObject;
      var DragObject: TDragObject);
    procedure PageControl1EndDrag(Sender, Target: TObject; X, Y: Integer);
    procedure PageControl1DragOver(Sender, Source: TObject; X, Y: Integer;
      State: TDragState; var Accept: Boolean);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  PageControl1.DragMode := dmManual;
end;


type
  TDragFloatSheet = class(TDragControlObjectEx)
  private
    class var
      FDragSheet: TTabSheet;
      FDragPos: TPoint;
      FCancelled: Boolean;
  protected
    procedure WndProc(var Msg: TMessage); override;
  end;

procedure TDragFloatSheet.WndProc(var Msg: TMessage);
begin
  if (Msg.Msg = CN_KEYDOWN) and (Msg.WParam = VK_ESCAPE) then
    FCancelled := True;
  FDragPos := DragPos;
  inherited;
  if (Msg.Msg = WM_MOUSEMOVE) and
      (not Assigned(FindVCLWindow(SmallPointToPoint(TWMMouse(Msg).Pos)))) then
    Winapi.Windows.SetCursor(Screen.Cursors[GetDragCursor(True, 0, 0)]);
end;

//-------------------

procedure TForm1.PageControl1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
begin
  TDragFloatSheet.FDragSheet :=
      (Sender as TPageControl).Pages[TPageControl(Sender).IndexOfTabAt(X, Y)];
  PageControl1.BeginDrag(False);
end;

procedure TForm1.PageControl1StartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  DragObject := TDragFloatSheet.Create(Sender as TPageControl);
end;

procedure TForm1.PageControl1DragOver(Sender, Source: TObject; X, Y: Integer;
  State: TDragState; var Accept: Boolean);
var
  TargetSheet: TTabSheet;
begin
  TargetSheet :=
      (Sender as TPageControl).Pages[TPageControl(Sender).IndexOfTabAt(X, Y)];
  Accept := Assigned(TargetSheet) and (TargetSheet <> TDragFloatSheet.FDragSheet);
end;

procedure TForm1.PageControl1EndDrag(Sender, Target: TObject; X, Y: Integer);
begin
  if Assigned(Target) then begin

    // normal processing, f.i. find the target tab as in OnDragOver
    // and switch positions with TDragFloatSheet.FDragSheet

  end else begin
    if not TDragFloatSheet.FCancelled then begin
      if not Assigned(FindVCLWindow(TDragFloatSheet.FDragPos)) then begin

        // drop TDragFloatSheet.FDragSheet at TDragFloatSheet.FDragPos

      end;
    end;
  end;
end;

end.
于 2012-10-18T22:49:51.873 回答