Board index » cppbuilder » Drag & drog

Drag & drog

We can drag a item of VCL component to another, but
how to drag a item of windows into our program?
(I want to drag a bitmap file to my program.)
 thanks.
 

Re:Drag & drog


Here is an example of drag and drop from another application:
In your main form, you need to add the following in the FormDeclaration:
class MyMainForm : public TForm
{

........
.......
private:
  void __fastcall DropFilesHandler(TWMDropFiles &aMsg);
........

  // The message map table.
  //
  BEGIN_MESSAGE_MAP
//    MESSAGE_HANDLER(WM_GETMINMAXINFO, TWMGetMinMaxInfo,OnGetMinMaxInfo)
    MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, DropFilesHandler)
  END_MESSAGE_MAP(TForm)

Quote
};

Then in the main form file:
Add:

void __fastcall MyMainForm ::DropFilesHandler(TWMDropFiles &aMsg)
{
  char FileName[MAXPATH];
  int  FileCount;

  FileCount = DragQueryFile((HDROP) aMsg.Drop, 0xFFFFFFFF, NULL, MAXPATH);

  if (FileCount)
  {
    DragQueryFile((HDROP) aMsg.Drop, 0, FileName, MAXPATH); // get the
dropped file
    // do smething with data in FileName...
    // or do something with all the items dropped (replace the 0 above with
a variable
    // and use a for-loop
    .............
    .............
  }
  DragFinish((HDROP) aMsg.Drop);

Quote
}

HTH Pete
GenHelp: The Component Writers Help Authoring Tool
http://www.frasersoft.co.uk/genhelp
Quote
"goodguy" <good...@hello.com.tw> wrote in message news:3a62c835$2_2@dnews...
> We can drag a item of VCL component to another, but
> how to drag a item of windows into our program?
> (I want to drag a bitmap file to my program.)
>  thanks.

Re:Drag & drog


Quote
> how to drag a item of windows into our program?
> (I want to drag a bitmap file to my program.)
>  thanks.

A simple way is to accept dropped files. This is from Wang Weijun:

Two step:

1. Add a line
  DragAcceptFiles(Handle, true);
   in TForm1::Create

2. In unit1.h, add these lines in class TForm1 declaration

 void __fastcall WhenDropFile(TWMDropFiles& Message);
  BEGIN_MESSAGE_MAP
   MESSAGE_HANDLER(WM_DROPFILES, TWMDropFiles, WmDropFiles)
  END_MESSAGE_MAP(TForm)

3. Implement the WhenDropFile() function like this

       char buffer[ENOUGH_LONG];
    HDROP hDrop = (HDROP)Message.Drop;
    DragQueryFile(hDrop, 0, buffer, sizeof(buffer));

   now buffer contains the first filename. if you want to frag more than one file,
    DragQueryFile(hDrop, -1, NULL, NULL) returns how many

For more elaborate drag and drop, you could use a component. See Anders'
free one at http://www.melander.dk/

+=====================================================+
| Jonathan Arnold (mailto:jdarn...@buddydog.org)      |
|         Comprehensive C++Builder link site:         |
| http://www.buddydog.org/C++Builder/c++builder.html  |
+=====================================================+

Other Threads