Recent

Author Topic: [Solved] Drag and drop an image file onto a page with more than one TPanel  (Read 1315 times)

Paul1986

  • Newbie
  • Posts: 4
I have a multi page application that can have several TPanels on a page.  The panels display images that can be merged in a variety of ways.
I'd like to add a drag and drop option  so a user can drop an image file onto one of the TPanels.  AllowDropFiles is enabled for the form and I figured out a way of identifying the page the file was dropped onto but I can't figure how to identify the intended TPanel.

To identify the page concerned I used the TPageControl.ActivePage property that seems to be reliable.  What I want to know is the name/id of the panel on which the image was dropped.  I've tried the focus events but that didn't work.  Do I have to start tracking the mouse?
« Last Edit: February 03, 2021, 12:50:05 pm by Paul1986 »

jamie

  • Hero Member
  • *****
  • Posts: 6988
Re: Drag and drop an image file onto a page with more than one TPanel
« Reply #1 on: February 02, 2021, 11:07:55 pm »
TheControl := MyPageorTabSheet.ControlAtPos(Point(X,Y), Flase {Don't allow disabled});


If TheControl is TPanel then.....

The only true wisdom is knowing you know nothing

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Drag and drop an image file onto a page with more than one TPanel
« Reply #2 on: February 03, 2021, 12:17:22 am »
I was thinking about something on that line; the problem is that OnDropFiles doesn't tell you where the files where dropped so you'd have to keep track of the mouse position with OnMouseMove (or the current control with OnMouseEnter/ONMouseLeave) handlers. But then you'll have to track for all controls (or at lest all interested control) and the coordinates (if using OnMouseMove) are then control-relative.

Sure, no big deal, but a litttle tedious and a bit error prone ...

So to answer the OP's question: yes, I think you'll have to track the mouse ... :(
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Paul1986

  • Newbie
  • Posts: 4
Re: Drag and drop an image file onto a page with more than one TPanel
« Reply #3 on: February 03, 2021, 07:05:17 am »
Thanks for the help.  Based on the above, and another post about ControlAtPos I found in a 2019 thread, I have this piece of code that seems to work:
Code: Pascal  [Select][+][-]
  1.        mPos := ScreenToClient(Mouse.CursorPos);
  2.         aControl := ImagePages.ActivePage.ControlAtPos(mPos, [capfRecursive,capfAllowWinControls]);
  3.         if (aControl = nil) then
  4.         begin
  5.           Form1.MemoRecord.Lines.Add('Hmm - nil/null control');
  6.         end else
  7.         begin
  8.           if (aControl is TPanel) then
  9.           begin
  10.             Form1.MemoRecord.Lines.Add('Control is ' + aControl.GetNamePath);
  11.             Form1.MemoRecord.Lines.Add('Mouse X: ' + IntToStr(mPos.x) + ', Y: ' + IntToStr(mPos.y));
  12.           end else
  13.           begin
  14.             Form1.MemoRecord.Lines.Add('Not a TPanel control');
  15.           end;
  16.         end;        
       

When debugging and stopping at a breakpoint, I was getting some negative positions for the mouse location so I wonder it that is a side effect of the debugger.  So I added some debug lines to record the mouse position and the numbers look about right.

Thanks again for your help.  :)  This thread can be closed/solved thanks.                       

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: Drag and drop an image file onto a page with more than one TPanel
« Reply #4 on: February 03, 2021, 10:38:25 am »
Thanks again for your help.  :)  This thread can be closed/solved thanks.

It's a nice solution that you found :)
To mark as solved, edit your first post and add [SOLVED] at the front of the "Subject" line.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Paul1986

  • Newbie
  • Posts: 4
Re: Drag and drop an image file onto a page with more than one TPanel
« Reply #5 on: February 03, 2021, 12:52:37 pm »
...
To mark as solved, edit your first post and add [SOLVED] at the front of the "Subject" line.

Thanks,  I should have known that but its been a while since I was last here.

 

TinyPortal © 2005-2018