A simple method for realizing file drag and drop

zhaozj2021-02-11  183

A simple method for document drag and drop Hubei Xiangfan City Telecom Bureau computer center Zhang Hongzheng ---- Reading Comrade Nanjing Navy Command College Huang Xiangming "Drag and drop to the edit box" with MFC implementation documents " Various, and also discovering the shortcomings of this method: 1. Self-prepared class, the procedures are complex, maintained; 2, the file must be dragged on the editor control in the dialog box, drag the file name Accepted, and the standard Windows program can be accepted by dragging the file to the dialog; 3, only one drag and drop file can be accepted. This method you want to introduce this method only needs to call a Windows API function DragQueryFile to realize the drag and drop operation of the file, and completely overcome the above 3 points. Let's first introduce this function. ---- DragQueryFile prototype: uint Dragqueryfile (HDROP HDROP,

Uint ifile,

LPTSTR LPSZFILE,

UINT CCH)

---- HDROP is a handle to drag the file structure; ---- ifile is the serial number of the hosted file starting with 0 because it may be dragged multiple files at a time. When this parameter is set to 0xffffff, the function returns the number of drag files; ---- lpszfile is a buffer to the file name; ---- CCH is the size of the file name buffer, the file name The number of characters. ---- After the parameters of the function are clarified, the programming is a very simple thing. The following is given, and a dialog box will pop up after running, you can drag any number of files or directories to the above, after release the mouse, the program first displays the number of drag and drop files, and then drag and drop the file name Displayed in a List box control. Specific steps are as follows:

Establish a dialog-based engineering DROP, then other options are all default. Select the Accept Files property of Extended Styles in the IDD_DROP_DIALOG dialog. Add a list box control idc_list1 to the IDD_DROP_DIALOG dialog. Add a member variable m_list to the CDROPDLG class in ClassWizard, type CLISTBOX. Add a function of processing Windows message WM_DROPFILES to the CDROPDLG class, accept the default name IdropFiles given by the system, then enter the following code: void cdropdlg :: Ondropfiles (HDrop HDropinfo)

{

Char * LPSZFILENAME = New Char [512], CFILECOUNT [10];

INT NFILECUNT, I;

NfileCount = :: DragQueryFile

(HDropInfo, 0xfffffffff, NULL, 512);

:: AfxMessageBox (ITOA (NfileCount, CfileCount, 10);

For (i = 0; i

---- Note :: Dragfinish (HDropInfo); statement is essential, it is used to release Windows to process the memory to handle the file. ---- Run this program You can see that the file that dragged as long as you enter the boundary of the dialog, the mouse will now be " ", release the mouse on any position on the dialog, and the files dragged will be accepted. It is completely solved "Drag and drop to the edit box" with the MFC implementation file "must be dragged to the shortage of the edit box; and dragging is achieved by calling the Windows API function, so the program is very strong, there is little maintenance. The program is passed in IndoWS98, VC 5.0.

转载请注明原文地址:https://www.9cbs.com/read-4023.html

New Post(0)