Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Open a file by dropping it over the form

Status
Not open for further replies.

SLider3

Programmer
Jun 17, 2003
56
PT
I want to open a file when the user drop it over the form or over a secific object. Can anyone help me?
 
You can have any component that has a HANDLE (or Handle in Borland) parameter accept dragged files.

To do this, call the Microsoft function DragAcceptFiles(Handle, true). To disable, set true to false. You can find more on this information in the Microsoft help files Win32 Programmer's Reference shipped with Borland.

After you set the window up, you need to handle the actual event. The event is called WM_DROPFILES. Catch this event with the provided Borland macros in your class definition for that windowed control. Note, if you want to use this for something like a TEdit, you must create a derived component TDerivedEdit (Or whatever), and then add the following code to it's class.

void __fastcall OnDrop(TMessage *Message);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES, TMessage, OnDrop)
END_MESSAGE_MAP(TEdit)

Now, go declare OnDrop and look at Message.wparam, this is the HANDLE of the file being dropped.
Use that handle and the function DragQueryFile(Handle, Index, StringPtr, BufferSize);

Index is the index of the file that was dropped, there could be more than 1. StringPtr is a pointer to a string that will receive the name and path of the file at Index. BufferSize is the size of the StringPtr that you've allocated. Make it MAX_PATH or something big.

You can also use DragQueryPoint(Handle, PointPtr). This will provide you with the actual client coordinates in that windowed object where the file was dropped. Useful if you have a listbox and want to know which list item it was dropped on.

Good luck,
Chris
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top