Jump to content
sjordi

Create a bitmap of selected control?

Recommended Posts

Hi,

I'm implementing a drag&drop process in a TListView and I'd like to animate the selected Item (or group item).
For this I think we would need to create a bitmap of the selected item to be able then to move it around in the drag process.

Is there any way to actually determine the coordinates, width, height of the Item, and make a bitmap of it on the fly?
Thanks for any clue about that.

Steve

Share this post


Link to post
8 hours ago, sjordi said:

Is there any way to actually determine the coordinates, width, height of the Item, and make a bitmap of it on the fly?

Yes, even if I don't remember how I proceed. I will look for in my ('old') sources code tests. 

I remember there are some private properties but, but these memories are polluted by more recent tests on VCL.TControlList (sorry)

As soon as I find these sources, I come back.

Edited by Serge_G

Share this post


Link to post

Gday,

 

From the Delphi drag and drop example:

 

      DragImage := Control.MakeScreenshot;
      Svc.BeginDragDrop(Self, DragData, DragImage);

 

DragImage is a TBitmap

Control is a TControl descendent

 

Regards,

Pat

Share this post


Link to post

I don't found the source I had in mind, but ...

 

If there are no headers or footers

procedure TForm1.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
begin
if AItem.Purpose=TListItemPurpose.None then
  begin
   memo1.Lines.Clear;
   memo1.Lines.Add('Item width '+(Listview1.width).ToString);
   memo1.Lines.Add('real Item width '+(Listview1.width-Listview1.ItemSpaces.Left-ListView1.ItemSpaces.Right).ToString);
   memo1.Lines.Add('Item Height '+Listview1.ItemAppearance.ItemHeight.toString);
   memo1.Lines.Add('real Item height '+(Listview1.ItemAppearance.Itemheight-Listview1.ItemSpaces.top-ListView1.ItemSpaces.bottom).ToString);
   memo1.Lines.Add('cursor '+Listview1.ScrollViewPos.ToString);
   memo1.Lines.Add(' Item toppos in list '+(AItem.index*Listview1.ItemAppearance.ItemHeight-Listview1.ScrollViewPos).ToString);
   memo1.Lines.Add(' real Item toppos in list '+(AItem.index*Listview1.ItemAppearance.ItemHeight-       Listview1.ScrollViewPos+Listview1.ItemSpaces.top).ToString);
  end;
end;

With this, it's easy to make a bitmap extracted from ListView1.MakeScreenShot like this

procedure TForm1.ListView1ItemClick(const Sender: TObject;
  const AItem: TListViewItem);
var aBitmap : TBitmap;
    rect : Trect;
begin
if AItem.Purpose=TListItemPurpose.None then
  begin
    Rect:=TRect.Create(TPoint.Zero);
    Rect.Left:=Trunc(ListView1.ItemSpaces.Left);
    Rect.Top:=trunc(AItem.index*Listview1.ItemAppearance.ItemHeight-Listview1.ScrollViewPos+Listview1.ItemSpaces.top);
    Rect.Right:=Rect.Left+Trunc(Listview1.width-ListView1.ItemSpaces.Right);
    Rect.Bottom:=Rect.Top+Trunc(Listview1.ItemAppearance.Itemheight-ListView1.ItemSpaces.bottom);
    aBitmap:=TBitmap.Create;
    try
    aBitmap.Width:=Rect.Width;
    abitmap.Height:=Rect.Height;
    aBitmap.CopyFromBitmap(Listview1.MakeScreenshot,Rect,0,0);
    aBitmap.SaveToFile('itemtest.bmp');
    finally
      abitmap.Free;
    end;
  end;
end;

result : Ok, I just forget to discount Scrollbar width, but that's the idea

 

Capture.PNG

  • Thanks 1

Share this post


Link to post

Thanks both. I'll try the demo and your code Serge.
Hopefully works as expected...
I'll post the results back

Share this post


Link to post

Note : in case of headers/footers a loop in the items till item index shall certainly do the job.

My question : what event will you used to detect drag mode ?

Share this post


Link to post

Ah, I knew I faced this GetItemRect somewhere ! It was when I compare the new VCL TControlList to FMX.TListView.

In fact, I was deploring the lack of the method (not lack but private one) GetItemRect (I found in FMX.TListview) in the VCL.TControlList 

On 10/13/2021 at 7:38 AM, Serge_G said:

I will look for in my ('old') sources code tests. 

Was not so old finally, but other context . (webinaire sur TControlList partie sur le dragdrop)

 

I quickly check and "joy" got the same results

Quote

ItemObj
   left 46
   top 260
   right 246
   bottom 294
GetItemRect
   left 46
   top 260
   right 246
   bottom 294

 

16 hours ago, sjordi said:

I didn't try it yet on mobile devices though.

No mousemove event will fire, I think. And tracking the movement of the finger  should be a real challenge

Share this post


Link to post

Yes with fingers, that might be another challenge.
I hope to have some testing time over the week-end and let you know

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×