Back to Devexpress

Example: TcxCustomGridView.OnStartDrag

vcl-171186-expressquantumgrid-examples-example-tcxcustomgridview-onstartdrag.md

latest6.1 KB
Original Source

Example: TcxCustomGridView.OnStartDrag

  • Jul 29, 2021
  • 3 minutes to read

This example shows how to create a custom drag object for drag-and-drop. The drag object will provide a drag image to display while dragging records of the tvItems View. If a user starts dragging within the View with the multi-selection feature enabled (the OptionsSelection.MultiSelect property is set to True ), the drag image will display information on the number of selected records:

If the multi-selection feature is disabled, the drag image will look as follows:

To provide a custom drag object, you need to:

The following is an example of implementing a custom drag object when dragging from the tvItems View. The OnStartDrag event handler just creates a drag object of the TMyDragControlObject class and assigns it to the DragObject parameter.

delphi
procedure TForm1.tvItemsStartDrag(Sender: TObject;
  var DragObject: TDragObject);
begin
  DragObject := TMyDragControlObject.Create(Sender as TcxGridSite);
end;
cpp
void __fastcall TForm1::tvItemsStartDrag(TObject *Sender, TDragObject *&DragObject) {
  DragObject = new TMyDragControlObject((TcxGridSite*)Sender);
}

The TMyDragControlObject class overrides the GetDragImages method inherited from TDragControlObject. This method should return a list containing the image to be displayed while dragging. The drag image is created by the private CreateImageList method, which will display information regarding the selected/focused records of the View in the form shown above.

delphi
TMyDragControlObject = class(TDragControlObjectEx)
  private
    FImageList: TImageList;
    procedure CreateImageList;
  protected
    function GetDragImages: TDragImageList; override;
  public
    constructor Create(AControl: TControl); override;
    destructor Destroy; override;
  end;
//...
{ TMyDragControlObject }
constructor TMyDragControlObject.Create(AControl: TControl);
begin
  inherited Create(AControl);
end;
procedure TMyDragControlObject.CreateImageList;
var
  AView: TcxCustomGridTableView;
  s: string;
  ABmp: TBitmap;
begin
  if (FImageList = nil) and (Control is TcxGridSite)then
  begin
    //Get the current View
    AView := TcxCustomGridTableView(TcxGridSite(Control).GridView);
    //Prepare the text to display within the drag image
    if AView.OptionsSelection.MultiSelect then
      s := ' Dragging ' + IntToStr(AView.Controller.SelectedRecordCount) + ' selected records from the ' + AView.Name + ' View'
    else
      s := ' Dragging the focused record from the ' + AView.Name + ' View';
    //Create the drag image
    ABmp := TBitmap.Create();
    try
      with ABmp.Canvas do
      begin
        ABmp.Width := TextWidth(s);
        ABmp.Height := TextHeight(s);
        TextOut(0, 0, s);
      end;
      FImageList := TImageList.CreateSize(ABmp.Width, ABmp.Height);
      FImageList.AddMasked(ABmp, clNone);
    finally
      ABmp.Free();
    end;
  end;
end;
destructor TMyDragControlObject.Destroy;
begin
  if FImageList <> nil then
    FImageList.Free;
  inherited;
end;
function TMyDragControlObject.GetDragImages: TDragImageList;
begin
  if FImageList = nil then
    CreateImageList();
  Result := FImageList;
end;
cpp
class TMyDragControlObject: public TDragControlObjectEx {
  private:
    TImageList * FImageList;
    void CreateImageList();
  protected:
    TDragImageList * __fastcall GetDragImages();
  public:
    __fastcall TMyDragControlObject(TControl * AControl);
    __fastcall ~TMyDragControlObject();
};
//...
void TMyDragControlObject::CreateImageList() {
  String s;
  TcxGridSite * ASite;
  if ((!FImageList) && (ASite = dynamic_cast<TcxGridSite*>(Control))) {
    //Get the current View
    TcxCustomGridTableView * AView =
      (TcxCustomGridTableView*)ASite->GridView;
    //Prepare the text to display within the drag image
    if (AView->OptionsSelection->MultiSelect)
      s = " Dragging " +
IntToStr(AView->Controller->SelectedRecordCount) + " selected records from the " + AView->Name + " View";
    else
      s = " Dragging the focused record from the " +
AView->Name + " View";
    //Create the drag image
    Graphics::TBitmap * ABmp = new Graphics::TBitmap();
    try {
      ABmp->Width = ABmp->Canvas->TextWidth(s);
      ABmp->Height = ABmp->Canvas->TextHeight(s);
      ABmp->Canvas->TextOut(0, 0, s);
      FImageList = new TImageList(ABmp->Width, ABmp->Height);
      FImageList->AddMasked(ABmp, clNone);
    }
    __finally {
      delete ABmp;
    }
  }
}
TDragImageList * __fastcall TMyDragControlObject::GetDragImages() {
  if (!FImageList)
    CreateImageList();
  return FImageList;
}
__fastcall TMyDragControlObject::TMyDragControlObject(TControl * AControl) : TDragControlObjectEx(AControl) {
  FImageList = NULL;
}
__fastcall TMyDragControlObject::~TMyDragControlObject() {
  if (FImageList)
    delete FImageList;
}