First, call DragAcceptFiles (this, true) to register your program during FormCreate so that your program can accept the DragDrop of the file. Then, process the WM_DROPFILES message and obtain the DropDrag message. Call the following function to obtain the relevant parameters ::
UINT DragQueryFile (
HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
);
BOOL DragQueryPoint (
HDROP hDrop,
LPPOINT lppt
);
Finally, use the following function to accept the DragDrop action.
VOID DragFinish (
Hddrop
);
III.
Add the following content to the header file:
Private: // User declarations
Void _ fastcall AcceptFiles (TMessage & Msg );
Public: // User declarations
_ Fastcall TForm1 (TComponent * Owner );
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER (WM_DROPFILES, TMessage, AcceptFiles)
END_MESSAGE_MAP (TForm)
A piece of code to help you:
//---------------------------------------------------------------------------
_ Fastcall TForm1: TForm1 (TComponent * Owner)
: TForm (Owner)
{
DragAcceptFiles (this-> Handle, true );
}
//---------------------------------------------------------------------------
Void _ fastcall TForm1: AcceptFiles (TMessage & Msg)
{
Const int m_nmaxfills enamelen = 255;
Int I, m_nCount;
Char m_sFileName [m_nMaxFileNameLen];
M_nCount = DragQueryFile (HANDLE) Msg. WParam, 0 xffffffff, m_sFileName, m_nMaxFileNameLen );
For (I = 0; I <m_nCount; I ++)
{
DragQueryFile (HANDLE) Msg. WParam, I, m_sFileName, m_nMaxFileNameLen );
MessageBox (this-> Handle, m_sFileName, "Drop File", MB_ OK );
}
DragFinish (HANDLE) Msg. WParam );
}
4.
Add Message Processing ing to the form class to be dragged and dropped, as shown below:
BEGIN_MESSAGE_MAP
MESSAGE_HANDLER (WM_DROPFILES, TWMDropFiles, WMDropFiles)
END_MESSAGE_MAP (TForm );
Enter the message processing function in the corresponding cpp file, as shown below:
Void _ fastcall TForm1: WMDropFiles (TWMDropFiles & message)
{
UINT filecount = DragQueryFile (HDROP) message. Drop, 0 xFFFFFFFF, NULL, 0); // query the number of drag-and-Drop files
For (int n = 0; n <= filecount-1; n ++)
{
String filename;
Filename. SetLength (MAX_PATH );
Int length = DragQueryFile (HDROP) message. Drop, n, filename. c_str (), MAX_PATH );
Filename. SetLength (length );
// Replace your processing code here
}
DragFinish (HDROP) message. Drop );
}
Add the following code to the OnCreate event of the Form class,
DragAcceptFiles (Handle, true );