Use MFC to implement file drag and drop (get file path)

Source: Internet
Author: User

1. Use MFC Appwizard to create an SDI-based new project named drag. In addition to the first step, use the default settings for all other projects.

2. dragview. h file

     CListBox  m_View_List;
3. dragview. cpp File

---- Use classwizard to add the wm_create message response, and then add the following code in the oncreate function:

Dragacceptfiles (); // register yourself with file manager so that you can accept droped file (s) crect rect (,); m_view_list.create (ws_visible | ws_vscroll, rect, this, idc_view_list); // create a ListBox

4. Resource. h

---- Modify the resource. h file and assign a value to the ListBox ID (idc_view_list ).

---- Example: # define idc_view_list 1000

5. dragview. cpp File

---- Use classwizard to add the wm_size message response, and then add the following code to the onsize function:

---- M_view_list.movewindow (0, 0, CX, CY); // keep the region occupied by ListBox as large as the customer Zone

6. dragview. cpp File

---- Use classwizard to add the wm_dropfiles message response, and then add the following code in the ondropfiles function:

Void cmydragview: ondropfiles (hdrop hdropinfo) {// todo: add your message handler code here and/or call default int dropcount = dragqueryfile (hdropinfo,-1, null, 0 ); // obtain the number of dragged files for (INT I = 0; I <dropcount; I ++) {// get the bytes occupied by the I-th drag file name int namesize = dragqueryfile (hdropinfo, I, null, 0); handle hheap = getprocessheap (); // allocate a buffer based on the number of bytes // char * pname = new char [++ namesize]; char * pname = (lpstr) heapalloc (hheap, heap_zero_memory, namesize ++ );
If (pname = NULL) {MessageBox ("An error occurred while allocating a temporary space to the file name! "," Error message ", mb_iconerror); return;} dragqueryfile (hdropinfo, I, pname, namesize); // copy the file name to the buffer m_view_list.addstring (pname ); // Add the file name to ListBox. // Delete [] pname; // release the buffer heapfree (hheap, heap_zero_memory, pname );}
Dragfinish (hdropinfo); // The memory is released after the drag and drop operation is complete.
 CView::OnDropFiles(hDropInfo);}

---- At this point, the file is dragged to the ListBox in the view. You can compile and run the file.

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.