Discussion:
[Lazarus] Drag (and show) images
Donald Ziesig via Lazarus
2018-04-11 22:02:36 UTC
Permalink
Hi All!

I am trying to write an app that allows me to drag images and show them
while they are being dragged (like the cards in Solitaire).

I have been able to drag images (showing only the drag cursor) and drop
them appropriately, but I haven't been able to find any recent
documentation showing the use of TDragImageList and TDragObject.

The only articles that I have found are for Delphi from 2011 (which
translates to Lazarus but doesn't work correctly) and some more recent
that don't show the dragged image while the drag is taking place (I have
that part working).

I was able to show a dragged image by adding code to the DragOver event,
but that seems to take more time than it should.

Can anyone point me in the right direction for this?

Thanks,

Don Ziesig


--
Graeme Geldenhuys via Lazarus
2018-04-11 22:40:02 UTC
Permalink
Post by Donald Ziesig via Lazarus
Can anyone point me in the right direction for this?
I've never really compared LCL's drag-n-drop implementation with fpGUI's
- but they might be similar in some way. Anyway, fpGUI' visual forms
designer - the fpGUI's UI Designer (<fpgui>/uidesigner/ directory)
does just that. You can drag components from the component palette to
the designer form, and it shows you the drag cursor and a image of the
rendered widget/component while you are dragging. Maybe that might give
you some ideas - or not.

The above is obviously different from dragging already placed widgets on
a form - because that is simply moving the widget.

Regards,
Graeme
--
fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal
http://fpgui.sourceforge.net/

My public PGP key: http://tinyurl.com/graeme-pgp
--
Werner Pamler via Lazarus
2018-04-11 23:18:39 UTC
Permalink
Post by Donald Ziesig via Lazarus
I am trying to write an app that allows me to drag images and show
them while they are being dragged (like the cards in Solitaire).
I have been able to drag images (showing only the drag cursor) and
drop them appropriately, but I haven't been able to find any recent
documentation showing the use of TDragImageList and TDragObject.
The only articles that I have found are for Delphi from 2011 (which
translates to Lazarus but doesn't work correctly) and some more recent
that don't show the dragged image while the drag is taking place (I
have that part working).
I was able to show a dragged image by adding code to the DragOver
event, but that seems to take more time than it should.
Can anyone point me in the right direction for this?
There is a sample in folder examples/dragimagelist of your Lazarus
installation - maybe it gets close to what you need?
--
Donald Ziesig via Lazarus
2018-04-13 00:40:57 UTC
Permalink
Post by Werner Pamler via Lazarus
Post by Donald Ziesig via Lazarus
I am trying to write an app that allows me to drag images and show
them while they are being dragged (like the cards in Solitaire).
I have been able to drag images (showing only the drag cursor) and
drop them appropriately, but I haven't been able to find any recent
documentation showing the use of TDragImageList and TDragObject.
The only articles that I have found are for Delphi from 2011 (which
translates to Lazarus but doesn't work correctly) and some more
recent that don't show the dragged image while the drag is taking
place (I have that part working).
I was able to show a dragged image by adding code to the DragOver
event, but that seems to take more time than it should.
Can anyone point me in the right direction for this?
There is a sample in folder examples/dragimagelist of your Lazarus
installation - maybe it gets close to what you need?
You're right.  It gets *close* but not quite there.  I can move the
control so long as the mouse is *not* over the control while it is
moving :-(. I will look at this closer in the morning and see if I can
work around it (or if there is something I am missing).

Thanks!

Don
Giuliano Colla via Lazarus
2018-04-13 08:45:42 UTC
Permalink
Post by Donald Ziesig via Lazarus
You're right.  It gets *close* but not quite there.  I can move the
control so long as the mouse is *not* over the control while it is
moving :-(. I will look at this closer in the morning and see if I can
work around it (or if there is something I am missing).
You might find useful, as a guideline, the code I'm using in one of my
application to drag a button so that it doesn't cover the useful part of
an image. Just using the OnMouseDown, OnMouseMove and OnMouseUp of your
TImage, and getting rid of the code you don't need, to perform an extra
action on OnMouseUp, you should be able to do the trick.
Post by Donald Ziesig via Lazarus
  TErrForm = class(TForm)
......
  protected
    ResetDragged: Boolean;
    CursorPos: TPoint;
    ObjectPos: TPoint;
....
TMouseButton;
  Shift: TShiftState; X: LongInt; Y: LongInt);
begin
  ResetDragged := True;
  CursorPos.X := Mouse.CursorPos.X;
  CursorPos.Y := Mouse.CursorPos.Y;
  with ResetBtn do begin;
    ObjectPos.X := Left;
    ObjectPos.Y := Top;
    end;
end;
procedure TErrForm.ResetBtnMouseMove(Sender: TObject; Shift: TShiftState;
  X: LongInt; Y: LongInt);
begin
  if ResetDragged then begin
    with ResetBtn do begin
      Left := ObjectPos.X + (Mouse.CursorPos.X - CursorPos.X);
      Top := ObjectPos.Y +  (Mouse.CursorPos.Y - CursorPos.Y);
      ResetPos.X := (Left * NormalGeometry.ClientWidth div
CurrImage.Width) + HorzScrollBar.Position;
      ResetPos.Y := (Top * NormalGeometry.ClientHeight div
CurrImage.Height) + VertScrollBar.Position;
      end;
    end;
end;
procedure TErrForm.ResetBtnMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X: LongInt; Y: LongInt);
begin
  if ResetDragged then begin
    ResetDragged := False;
    Paint;
    end;
  if (abs(Mouse.CursorPos.X - CursorPos.X) < 10) and
(abs(Mouse.CursorPos.Y - CursorPos.Y)< 10) then Close;
end;
Hope that it helps.

Giuliano
Donald Ziesig via Lazarus
2018-04-13 16:14:46 UTC
Permalink
Post by Giuliano Colla via Lazarus
Post by Donald Ziesig via Lazarus
You're right.  It gets *close* but not quite there.  I can move the
control so long as the mouse is *not* over the control while it is
moving :-(. I will look at this closer in the morning and see if I
can work around it (or if there is something I am missing).
You might find useful, as a guideline, the code I'm using in one of my
application to drag a button so that it doesn't cover the useful part
of an image. Just using the OnMouseDown, OnMouseMove and OnMouseUp of
your TImage, and getting rid of the code you don't need, to perform an
extra action on OnMouseUp, you should be able to do the trick.
Post by Donald Ziesig via Lazarus
  TErrForm = class(TForm)
......
  protected
    ResetDragged: Boolean;
    CursorPos: TPoint;
    ObjectPos: TPoint;
....
TMouseButton;
  Shift: TShiftState; X: LongInt; Y: LongInt);
begin
  ResetDragged := True;
  CursorPos.X := Mouse.CursorPos.X;
  CursorPos.Y := Mouse.CursorPos.Y;
  with ResetBtn do begin;
    ObjectPos.X := Left;
    ObjectPos.Y := Top;
    end;
end;
procedure TErrForm.ResetBtnMouseMove(Sender: TObject; Shift: TShiftState;
  X: LongInt; Y: LongInt);
begin
  if ResetDragged then begin
    with ResetBtn do begin
      Left := ObjectPos.X + (Mouse.CursorPos.X - CursorPos.X);
      Top := ObjectPos.Y +  (Mouse.CursorPos.Y - CursorPos.Y);
      ResetPos.X := (Left * NormalGeometry.ClientWidth div
CurrImage.Width) + HorzScrollBar.Position;
      ResetPos.Y := (Top * NormalGeometry.ClientHeight div
CurrImage.Height) + VertScrollBar.Position;
      end;
    end;
end;
procedure TErrForm.ResetBtnMouseUp(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X: LongInt; Y: LongInt);
begin
  if ResetDragged then begin
    ResetDragged := False;
    Paint;
    end;
  if (abs(Mouse.CursorPos.X - CursorPos.X) < 10) and
(abs(Mouse.CursorPos.Y - CursorPos.Y)< 10) then Close;
end;
Hope that it helps.
Giuliano
Hi Giuliano!

It works perfectly. 8-) The best part is that it doesn't use any poorly
documented library code and it doesn't hog the CPU either.

Many thanks,

Don

Continue reading on narkive:
Loading...