Raz-Soft » Blog Archive » Drag & Drop Files Buy Cheap Software
Sorting a ListView »
« Running Scared

Drag & Drop Files

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.

C++:
  1. //---------------------------------------------------------------------------
  2. void __fastcall TForm1::FormCreate(TObject *Sender)
  3. {
  4.     //enable drag&drop files
  5.     DragAcceptFiles(this->Handle,true);
  6. }
  7. //---------------------------------------------------------------------------

C++:
  1. class TForm1 : public TForm
  2. {
  3. __published:    // IDE-managed Components   
  4.  
  5.     void __fastcall FormCreate(TObject *Sender);
  6.  
  7. private:    // User declarations
  8.  
  9.     void __fastcall WMDropFile(TWMDropFiles &Msg);
  10.  
  11. public:  // User declarations
  12.  
  13.     __fastcall TForm1(TComponent* Owner);
  14.  
  15. BEGIN_MESSAGE_MAP
  16.        //add message handler for WM_DROPFILES
  17.     VCL_MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WMDropFile)
  18. END_MESSAGE_MAP(TComponent)
  19.  
  20. };

C++:
  1. //---------------------------------------------------------------------------
  2. void __fastcall TForm1::WMDropFile(TWMDropFiles &Msg)
  3. {
  4.  char filePath[MAX_PATH];
  5.  TStringList *FilesDroped=new TStringList();
  6.  
  7.  //get dropped files count
  8.  int cnt=::DragQueryFile((HDROP)Msg.Drop,0xFFFFFFFF,NULL,0);
  9.  
  10.  //get dropped files path
  11.  for(int i=0;i<cnt;i++)
  12.   {
  13.       ::DragQueryFile((HDROP)Msg.Drop, i,filePath,MAX_PATH);
  14.       FilesDroped->Add(filePath);
  15.   }
  16.  
  17.  ShowMessage(FilesDroped->Text);
  18.  
  19. delete FilesDroped;
  20. }



related things:

Author: Raz | On February 10th, 2007 | C++ Builder, [ En ] | No Comments

Q: So what can I do next?
A: You can Buy me a Beer or Coffee. You can say Hi! or peek on the related stuff. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet

Leave a Reply

You can use these tags: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <code> <em> <i> <strike> <strong> and all YM emotions