You are here: Home » Builder » [ En ] » Drag & Drop Files

Drag & Drop Files

10 February |

How to enabled drag&drop from explorer to your window and retrieve a list of dropped files?

1. Enable your window (Form) to accept dropped files (DragAcceptFiles )
2. Add a message handler for WM_DROPFILES
3. Use DragQueryFile ( on WM_DROPFILES) to get the dropped files count and their full path.

[cpp]
//—————————————————————————
void __fastcall TForm1::FormCreate(TObject *Sender)
{
//enable drag&drop files
DragAcceptFiles(this->Handle,true);
}
//—————————————————————————
[/cpp]

[cpp]
class TForm1 : public TForm
{
__published: // IDE-managed Components

void __fastcall FormCreate(TObject *Sender);

private: // User declarations

void __fastcall WMDropFile(TWMDropFiles &Msg);

public: // User declarations

__fastcall TForm1(TComponent* Owner);

BEGIN_MESSAGE_MAP
//add message handler for WM_DROPFILES
VCL_MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDropFile)
END_MESSAGE_MAP(TComponent)

};
[/cpp]

[cpp]
//—————————————————————————
void __fastcall TForm1::WMDropFile(TWMDropFiles &Msg)
{
char filePath[MAX_PATH];
TStringList *FilesDroped=new TStringList();

//get dropped files count
int cnt=::DragQueryFile((HDROP)Msg.Drop,0xFFFFFFFF,NULL,0);

//get dropped files path
for(int i=0;i {
::DragQueryFile((HDROP)Msg.Drop, i,filePath,MAX_PATH);
FilesDroped->Add(filePath);
}

ShowMessage(FilesDroped->Text);

delete FilesDroped;
}
[/cpp]



You like this post? Be the first of my friends to give me a beer! click here. Thanks!
Filed under

Builder, [ En ]

| Tags:

Comments are closed.