C ++ Builder supports file drag and drop

Source: Internet
Author: User

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 );

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.