Use MFC to drag and drop files to the editing box

Source: Internet
Author: User
Not long ago, after reading the article "Using MFC to implement file drag-and-drop into the edit box" published by Huang xiangming of Nanjing Naval Command Institute, I was very interested in the last question, I checked the msdn help of vc and found that DragAcceptFiles () is also a member function of the CWnd class. In this case, I want to use DragAcceptFiles () as long as it is a derived class of the CWnd class () function to register itself with the windows File Manager as the recipient of the dragged file, that is, to respond to the Message WM_DROPFILES, so I use appwizard to generate a SDI-based project file Drag, in addition, a dialog box CDragDialog is added. Then, I use classwizard to check whether the CWnd Derived classes such as CMainFrame, CDragView, and CDragDialog have WM_DROPFILES messages. The result is that the CDragDialog class does not exist, however, when setting the Extended Style of dialog, you can see the accept File option, which makes me more and more strange. Why can't I figure out what is going on? Later I did not think about it any more. Since I cannot use classwizard to add a response to the WM_DROPFILES message, why not add it manually? After being manually added, compilation, debugging, and running will not cause any problems. Although the program was written, I still have no clear reason.

Huang xiangming explained the implementation based on CFormView and dialog respectively. Next I will briefly introduce the implementation based on cview and another implementation based on dialog. In order to facilitate the demonstration of program running results, the edit box is not used in the program, but The listbox is used to accept the dragged file name, which also facilitates the demonstration of the drag of multiple files.

1. Drag and Drop a file to the listbox in cview

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,
Make yourself accept droped file (s)
CRectrect (,); 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 (, cx, cy); // keep the area occupied by listbox as large as the customer area

6. DragView. cpp File

Use classwizard to add the WM_DROPFILES message response, and then add the following code in the OnDropFiles function:

Void CDragView: OnDropFiles (HDROP hDropInfo)
{
Int DropCount = DragQueryFile (hDropInfo,-1, NULL, 0 );
// Obtain the number of dragged files
For (int I = 0; I <DropCount; I ++)
{
Int NameSize = DragQueryFile (hDropInfo, I, NULL, 0 );
// Obtain the bytes occupied by the I-th drag file name
HANDLE hHeap = GetProcessHeap ();
Char * pName = (LPSTR) HeapAlloc
(HHeap, HEAP_ZERO_MEMORY,
NameSize ++); // allocate a buffer based on the number of bytes
If (pName = NULL)
{
MessageBox ("An error occurred while allocating temporary storage space to file names! ",
"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 for display
  
HeapFree (hHeap, HEAP_ZERO_MEMORY, pName );
// Release the buffer
}
CView: OnDropFiles (hDropInfo );
}

This completes the drag-and-drop of the file to the listbox in the view. You can compile and run the file.
2. Drag and Drop the file to the listbox in the dialog box.

1. Add a dialog box in the Drag project generated above with the ID IDD_DRAG_DIALOG, generate a new class CDragDialog, and select the Accept File option in Extend Style.

2. Add a listbox control in the dialog box. The ID is IDC_DIALOG_LIST and sort style is removed.

3. manually add the WM_DROPFILES message response, which is similar to the custom message, but it is simpler because it is not required here.

You define WM_DROPFILES;

(1) Declare the message processing function in the DragDialog. h file:

Afx_msg void OnDropFiles (HDROP hDropInfo );

(2) In the message ing of DragDialog. cpp, point out the message processing entry:
ON_MESSAGE (WM_DROPFILES, OnDropFiles)
(It can also be macro ON_WM_DROPFILES ());

(3) Finally define the message processing function, which is similar to Step 1 in cview:
Void CDragDialog: OnDropFiles (HDROP hDropInfo)
{
Int DropCount = DragQueryFile (hDropInfo,-1, NULL, 0 );
// Obtain the number of dragged files
For (int I = 0; I <DropCount; I ++)
{
Int NameSize = DragQueryFile (hDropInfo, I, NULL, 0 );
// Obtain the bytes occupied by the I-th drag file name
HANDLE hHeap = GetProcessHeap ();
Char * pName = (LPSTR) HeapAlloc (hHeap, HEAP_ZERO_MEMORY,
NameSize ++); // allocate a buffer based on the number of bytes
If (pName = NULL)
{
MessageBox ("An error occurred while allocating temporary storage space to file names! ",
"Error message", MB_ICONERROR );
Return;
}
DragQueryFile (hDropInfo, I, pName, NameSize );
// Copy the file name to the buffer
M_Dialog_List.AddString (pName );
// Add the file name to listbox for display
  
HeapFree (hHeap, HEAP_ZERO_MEMORY, pName );
// Release the buffer
}
CDialog: OnDropFiles (hDropInfo );
}

4. Modify the menu from the resource and add a menu "test" to pop up the dialog box we just added. The menu ID is ID_DRAG_TEST;
5. Add the dialog box header file to MainFrm. cpp and add the ID_DRAG_TEST response function with classwizard;

# Include "DragDialog. h"
  
......
Void CMainFrame: OnDragTest ()
{
CDragDialog dlg;
Dlg. DoModal ();
}

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.