Drag and drop into TPanel [Edit]

Dear Embarcadero,

It seems it is possible to create a drag and drop for a TPanel. However, I can't get it working.
When the user drags a file into a TPanel, I want to extract its filename.

But right now, I'm getting the following error: "E2316 'Drop' is not a member of 'TMessage'".
This is right because somehow I need to convert TMessage to TWMDropFiles.
But don't know how I could do this.

Is there somebody who can help me with this?

Kind regards,

Will Dento


My code:

*.cpp*
{code}
__fastcall TForm5::TForm5(TComponent* Owner)
	: TForm(Owner)
{
	oldProc = dragAndDrop->WindowProc;
	dragAndDrop->WindowProc = newProc;
	DragAcceptFiles(dragAndDrop->Handle, true);
}

void __fastcall TForm5::newProc(TMessage &message)
{
	if (message.Msg == WM_DROPFILES)
	{
		String FileName;
		FileName.SetLength(MAX_PATH);

		// The following code gets the FileName of the dropped file
		DragQueryFile((HDROP)message.Drop, 0, FileName.c_str(), MAX_PATH));
		ShowMessage(FileName);
	}
	oldProc(message);
}
{code}

*.h*
{code}
public:
TWndMethod oldProc;
TWndMethod originalPanelWindowProc;
void __fastcall newProc(TMessage& message);
{code}

Edited by: Will Dento on Feb 28, 2014 8:27 AM
0
Will
2/28/2014 4:29:05 PM
embarcadero.cppbuilder.using 1848 articles. 1 followers. Follow

4 Replies
733 Views

Similar Articles

[PageSpeed] 36

Hi Will

Will Dento wrote:

> Dear Embarcadero,
> 
> It seems it is possible to create a drag and drop for a TPanel. However, I can't get it working.
> When the user drags a file into a TPanel, I want to extract its filename.
> 
> But right now, I'm getting the following error: "E2316 'Drop' is not a member of 'TMessage'".
> This is right because somehow I need to convert TMessage to TWMDropFiles.
> But don't know how I could do this.
> 
> Is there somebody who can help me with this?

Sure thing, try this:

{code}
void __fastcall newProc(TMessage &message)
{
   if (message.Msg == WM_DROPFILES)
   {
      String FileName;
      FileName.SetLength(MAX_PATH);

      // The following code gets the FileName of the dropped file
      DragQueryFile( (HDROP)(message.WParam), 0, FileName.c_str(), MAX_PATH);
      ShowMessage(FileName);
   }
   oldProc(message);
}
{code}

p.s. be aware that now aday using wchar_t filenames can be longer then 
MAX_PATH I always use MAX_PATH*2.


Best regards
Asger-P
http://asger-p.dk/software
0
Asger
2/28/2014 4:57:34 PM
Hi Asger,

You are a life saver!
Everything works as it should be, so I'm very thankfull for that. Thanks Asger.

For everyone who wants to use a drag & drop on a TPanel ( or a other component ), here is the full code.

*.cpp*
{code}
// Drag & drop files into a Tpanel.
// Thanks to Asger Joergensen.


__fastcall Tstart::Tstart(TComponent* Owner)
	: TForm(Owner)
{
	originalPanelWindowProc = dragAndDropPanel->WindowProc;
	dragAndDropPanel->WindowProc = PanelWindowProc;

	DragAcceptFiles(dragAndDropPanel->Handle,true) ;
}

void __fastcall Tstart::PanelWindowProc(TMessage &message)
 {
	if (message.Msg == WM_DROPFILES)
	{
		int filecount, length, i;
		filecount = DragQueryFile ((HDROP)(message.WParam), 0xFFFFFFFF, NULL, 0);
		if (filecount > 1)
		{
			ShowMessage("You can drop only one file file at a time!");
		}
		else
		{
			String FileName;
			FileName.SetLength(MAX_PATH);
			length =  DragQueryFile( (HDROP)(message.WParam), 0, FileName.c_str(), FileName.Length());
			FileName.SetLength(length);
			ShowMessage(FileName);

			DragFinish ((HDROP)(message.WParam));
		}
	}

	originalPanelWindowProc(message);
}
{code}

*.h*
{code}
public:		// User declarations
	TWndMethod originalPanelWindowProc;

	void __fastcall PanelWindowProc (TMessage &message);
	__fastcall Tstart(TComponent* Owner);
{code}
0
Will
2/28/2014 6:54:35 PM
Will wrote:

> But right now, I'm getting the following error: "E2316 'Drop' is not a
> member of 'TMessage'".

That is correct.

> This is right because somehow I need to convert TMessage to
> TWMDropFiles.

You can use a type-cast for that, eg:

{code}
DragQueryFile( ((TWMDropFiles *)&message)->Drop, ... );
{code}

Or:

{code}
DragQueryFile( reinterpret_cast<TWMDropFiles&>(message).Drop, ... );
{code}

> DragAcceptFiles(dragAndDrop->Handle, true);

You have to watch out when using the TWinControl::Handle property.  It is 
NOT persistent for the lifetime of the TWinControl.  It can (and DOES) change 
dynamically.  Every time it changes, you have to unregister and then re-register 
the drag&drop.  You will have to handle the CM_RECREATEWND message to account 
for that.

Alternatively, derive a new component from TPanel and override its virtual 
CreateWnd() and DestroyWnd() methods to handle that.

> DragQueryFile((HDROP)message.Drop, 0, FileName.c_str(), MAX_PATH));

Multiple files can be dragged at one time, but you are only querying the 
first filename.

Also, you are not resizing the String to the actual length of the filename. 
 DragQueryFile() returns how many characters were actually copied:

{code}
String FileName;
FileName.SetLength(MAX_PATH);
UINT len = DragQueryFile((HDROP)message.Drop, 0, FileName.c_str(), MAX_PATH+1);
if (len > 0)
{
    FileName.SetLength(len);
    // use FileName as needed...
}
{code}

Or:

{code}
TCHAR szFileName[MAX_PATH+1];
UINT len = DragQueryFile((HDROP)message.Drop, 0, szFileName, MAX_PATH+1);
if (len > 0)
{
    String FileName(szFileName, len);
    // use FileName as needed...
}
{code}

Or, you can ask DragQueryFile() for the actual length before then sizing 
the String:

{code}
UINT len = DragQueryFile((HDROP)message.Drop, 0, NULL, 0);
if (len > 0)
{
    String FileName;
    FileName.SetLength(len);
    DragQueryFile((HDROP)message.Drop, 0, FileName.c_str(), len+1);
    // use FileName as needed...
}
{code}

--
Remy Lebeau (TeamB)
0
Remy
2/28/2014 11:07:06 PM
Hi Remy,

Thanks for the explanation. Your alternative code seems to work too.
So thanks for the help guys.
0
Will
3/1/2014 10:08:59 PM
Reply:

Similar Artilces:

superreview granted: [Bug 421817] Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation : [Attachment 309761] remove dragstart, dragleave and drop events for now, change
Johnny Stenback (:jst) <jst@mozilla.org> has granted Neil Deakin <enndeakin@gmail.com>'s request for superreview: Bug 421817: Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation https://bugzilla.mozilla.org/show_bug.cgi?id=421817 Attachment 309761: remove dragstart, dragleave and drop events for now, change the NS_DRAGDROP_DROP constant to refer to the dragdrop event and not the drop event. https://bugzilla.mozilla.org/attachment.cgi?id=309761&action=edit ------- Additional Comments from Johnny Stenback (:jst) <jst@mozill...

superreview requested: [Bug 421817] Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation : [Attachment 309761] remove dragstart, dragleave and drop events for now, chan #3
Neil Deakin <enndeakin@gmail.com> has asked Johnny Stenback (:jst) <jst@mozilla.org> for superreview: Bug 421817: Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation https://bugzilla.mozilla.org/show_bug.cgi?id=421817 Attachment 309761: remove dragstart, dragleave and drop events for now, change the NS_DRAGDROP_DROP constant to refer to the dragdrop event and not the drop event. https://bugzilla.mozilla.org/attachment.cgi?id=309761&action=edit ...

superreview requested: [Bug 421817] Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation : [Attachment 309761] remove dragstart, dragleave and drop events for now, chan #2
Neil Deakin <enndeakin@gmail.com> has asked for superreview: Bug 421817: Don't use HTML5 drag-and-drop event names for non-HTML5 drag-and-drop implementation https://bugzilla.mozilla.org/show_bug.cgi?id=421817 Attachment 309761: remove dragstart, dragleave and drop events for now, change the NS_DRAGDROP_DROP constant to refer to the dragdrop event and not the drop event. https://bugzilla.mozilla.org/attachment.cgi?id=309761&action=edit ...

Using AJAX Drag Panel for Drag Drop
 Hi Guys,Need some help regarding ASP.net Ajax control toolkit DragPanel. Is there any way that i can accomplish drag and drop. The scene goes like thisI have to create new controls by dragging and dropping.There is a left  panel from where i pick a control  lets just say an HTMLInputTextThen i drop it on left panelOn Dropping, it should create a new control in panel and add it to panel like OnDropIntoPanel = {new ObjHtmlInputText ; LeftPanel.Controls.Add(ObjHtmlInputText). The Ondrop will fire a server side subroutine that will return me an object oh Html that will be added t...

Arrange order from drop down listbox using drag and drop
Hi everyone, I want to arrange the order from my list box. Can i drag and drop? Is there a way to do it or i need a different control to perform this action. thanks, HenryToronto Trade show displays | Toronto Printing Solutions | Toronto Printing What do u mean ? are u loading ur listbox dynamically ? if yes, then u can sort them before binding the database to the listbox.Bilal Hadiar, MCP, MCTS, MCPD, MCTMicrosoft MVP - Telerik MVP I am sorry!! i explain it wrong! Actually, i want a "listbox" that can be drag to arrange the data in the list. thanks, henryToro...

Using the drag panel extender to make a drag and drop shopping cart
Hi all,I'm trying to make a product catalogue which will allow me to drag pictures of items into a panel which represents a customer's cart. Problem is, as far as I can work out, using the toolkit drag panel extender, there's no way to do the following:On drag start - Create an opaque copy of the products image to represent the item being draggedOn Drop - Call a method that adds a product to the cart by its id             - hide the opaque picture Does anyone know if this or even part of this can be done using the ajax toolkit?...

superreview granted: [Bug 372322] Attachments dropped into the wrong folder when using Drag and Drop
David Bienvenu <bienvenu@nventure.com> has granted Scott MacGregor <mscott@mozilla.org>'s request for superreview: Bug 372322: Attachments dropped into the wrong folder when using Drag and Drop https://bugzilla.mozilla.org/show_bug.cgi?id=372322 Attachment 256971: the fix https://bugzilla.mozilla.org/attachment.cgi?id=256971&action=edit ...

superreview requested: [Bug 372322] Attachments dropped into the wrong folder when using Drag and Drop
Scott MacGregor <mscott@mozilla.org> has asked David Bienvenu <bienvenu@nventure.com> for superreview: Bug 372322: Attachments dropped into the wrong folder when using Drag and Drop https://bugzilla.mozilla.org/show_bug.cgi?id=372322 Attachment 256971: the fix https://bugzilla.mozilla.org/attachment.cgi?id=256971&action=edit ------- Additional Comments from Scott MacGregor <mscott@mozilla.org> I've verified this works on the Mac too. See the previous comment for a description of the patch. ...

TListBox drag and drop [Edit]
I'm looking for some code to implement a simple drag and drop in TListBox to allow a user to reorder the list, one item at a time (no block moves). The samples I've found on the internet seem to work with a VCL app, but not FM. Dave Edited by: Dave Fitzpatrick on Mar 17, 2013 10:29 AM ...

How do people know they can drag and drop a drag and drop control?
Hi,My question is one of design and usage.  I created a drag an droppable "page menu" for a small number of items that change dynamically depending on the page a user is on.  My question is, how do I communicate that this control can be moved around on the page?  Do others depend on their users discovering this on their own?  I considered putting it in the title - ie "Page Menu (drag and drop)" but co-workers didn't care for that approach much more than I did.  I considered  hover-over text, but I'm not sure how to...

Use crDrag and not crMultDrag for Drag operation using Ctrl+Drag
When dragging an item from a tree component, the DragCursor = crDrag unless the control key is down, in which case the drag cursor used is crMultDrag. How can I keep it to crDrag regardless of which keys are down? Thanks. Raymond ...

Use crDrag and not crMultDrag for Drag operation using Ctrl+Drag
When dragging an item from a tree component, the DragCursor = crDrag unless the control key is down, in which case the drag cursor used is crMultDrag. How can I keep it to crDrag regardless of which keys are down? Thanks. Raymond Raymond Kennington wrote: > When dragging an item from a tree component, the DragCursor = crDrag > unless the control key is down, in which case the drag cursor used is > crMultDrag. How can I keep it to crDrag regardless of which keys are > down? I cannot reproduce the behaviour you are describing with a TTreeview in Delphi XE2 (on Win7 6...

Drag Drop using Listviews
I want to drag data from a listview and drop it in a datawindow. How would I go about doing this. Any help would be appreciated. Thanks Bo Pritchard bopritchard@mindspring.com ...

Drag Drop using Ajax
Hi, Can anybody worked on Drag Drop functionality.I have found help for Atlas but I need using latest  Ajax Library.Please help me out . Thanks, Satish Varma     Hi Satish Varma, Take a look at this link it will show you how to implement drag and drop :  http://www.asp.net/learn/videos/view.aspx?tabid=63&id=90 Regards,Dennis van de Laarhttp://dennisv.net/Don't forget to mark the most helpful reply/replies as Answer. ...

Web resources about - Drag and drop into TPanel [Edit] - embarcadero.cppbuilder.using

Lazarus Free Pascal - Google Bøger
Søgning Billeder Maps Play YouTube Gmail Drev Kalender Mere Oversæt Blogger Fotos Docs Endnu mere » Account Options Log ind Avanceret bogsøgning ...

Resources last updated: 12/5/2015 3:53:28 AM