1. Declare the handler function and the message map in the form's header file. h, for example:
[CPP]View PlainCopy
- Class TForm1: Public tform
- {
- ...
- void __fastcall handledropfiles (tmessage &Owner;
- ...
- Begin_message_map
- Message_handler (Wm_dropfiles,tmessage,handledropfiles)
- End_message_map (Tform)
- };
2. In the form's implementation file. CPP, add the code
In the Formcreate event response:
[CPP]View PlainCopy
- void __fastcall tform1::formcreate (tobject *sender)
- {
- ...
- DragAcceptFiles (Handle,true);
- ...
- }
The most important processing function is handledropfiles:
[CPP]View PlainCopy
- void __fastcall tform1::handledropfiles (tmessage &msg)
- {
- Char Filename[_max_path];
- int i, Sum;
- //Get the number of dragged files, this function is determined by the second parameter
- Sum = Dragqueryfile (hdrop (msg). WParam), 0xFFFFFFFF, NULL, 0);
- if (Sum > 0)
- {
- //Here as long as the first file
- Dragqueryfile (hdrop (msg. WParam), 0, FileName, _max_path);
- //Display the file path to the text control on the form
- Txtpath->text = ansistring (FileName);
- }
- //Releases the memory space that the application opens up to pass the file name
- Dragfinish ( hdrop (msg. WParam));
- }
Drag the file onto the form and get its full path "implementation under C + + Builder" goes