Link to home
Start Free TrialLog in
Avatar of lirtua
lirtua

asked on

Borland C++ Builder control accept dropped files

Borland C++ Builder control accept dropped files

Q:  How do you make a control accept a file which is dragged over it?
Example:   *.txt file is dropped onto an application and the file is opened by the application.

I would like to have an edit box accept a *.txt file which is dropped onto it.  In addition, I would like an image box accept a *.bmp file which is dropped onto it.

I do not need help with opening these file types.  What I need is how to have the control recognize that a file is being dropped, and how do I get the filename, path, etc.
Avatar of AlexVirochovsky
AlexVirochovsky

To deal file dropping
on a form, you have to do the following:

1) in the header file of the form, initialize the handling of the
WM_DROPFILES message, specifying the handling funtion.
Example:

class TForm1 : public TForm
{
private:
void virtual __fastcall WMDropFiles(TWMDropFiles &message);
public:
__fastcall TForm1(TComponent* Owner);
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER(WM_DROPFILES,TWMDropFiles,WMDropFiles);
END_MESSAGE_MAP (TForm);
};

2) in your source code insert the function which should handle the
file dropping. Example:

void __fastcall TForm1::WMDropFiles (TWMDropFiles &message)
{
...
}
Avatar of lirtua

ASKER

The code you supplied compiles and the application runs.  However, when dragging a file over the application, the nodrop icon is displayed.

Also, I cannot find information about the struct TWMDropFiles although I see its definition in messages.hpp.

How do I get the filename & path?
>>How do I get the filename & path?

void __fastcall TForm1::WMDropFiles (TWMDropFiles &message)
 
{
 UINT FileCount = DragQueryFile((HDROP) Msg.Drop, 0xFFFFFFFF, NULL,
 0);
 AnsiString FileName;
 AnsiString FileExtension;
 
 int FileLength;
 for(UINT ii=0; ii < FileCount; ii++)
 {
   FileName.SetLength(MAX_PATH);
 FileLength = DragQueryFile((HDROP)Msg.Drop, ii,
 FileName.c_str(), FileName.Length());
 FileName.SetLength(FileLength);
 ProcessFile(FileLength) // <------ Whatever you want it to be...
 } // end for
 DragFinish((HDROP) Msg.Drop);
 }
Avatar of lirtua

ASKER

Thank you for the information on this.  The code you have supplied is compiling without error.  I appreciate that.

I think were getting close.  However, the form still will not accept a dropped file.  When dragging a file over the form during runtime, the nodrop icon is displayed.

With this final detail, i believe all will be well in the file dropping realm.
ASKER CERTIFIED SOLUTION
Avatar of AlexVirochovsky
AlexVirochovsky

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of lirtua

ASKER

Fantastic Alex - thanks for the help!