Single-file drag-and-drop of MFC

Source: Internet
Author: User
Tags textout

In fact, the implementation principle is very simple, that is, let the program respond to and process WM_DROPFILES messages. Three functions are used:


VOID DragAcceptFiles (
HWND hWnd,
BOOL fAccept
); UINT DragQueryFile (
HDROP hDrop,
UINT iFile,
LPTSTR lpszFile,
UINT cch
); VOID DragFinish (
Hddrop
);
First, we create a single-document program for MFC named DragFiles

Add a function call at the end of the OnCreate function of the generated CDragFilesView class


[Cpp]
DragAcceptFiles (TRUE); // supports drag and drop of Files

DragAcceptFiles (TRUE); // This is the description of DragAcceptFiles in MSDN.


DragAcceptFiles Function

--------------------------------------------------------------------------------


Registers whether a window accepts dropped files.

// Register a window and whether to drag and drop files

Syntax

VOID DragAcceptFiles (
HWND hWnd,
BOOL fAccept
); Parameters

HWnd
The identifier of the window that is registering whether it will accept dropped files.
// The Window handle that must support Drag and Drop Operations
FAccept
A value that indicates if the window identified by the hWnd parameter accepts dropped files. This value is TRUE to accept dropped files or FALSE to discontinue accepting dropped files.
A boolean value indicating whether drag and drop are supported
Return Value

No return value.

Remarks

An application that calldragacceptfiles with the fAccept parameter set to TRUE has identified itself as able to process the WM_DROPFILES message from File Manager.

If an application calls the DragAcceptFiles function and sets the fAccept parameter to TRUE, it specifies that it can process WM_DROPFILES messages from the File Manager.

Function Information

Minimum DLL Version shell32.dll version 4.0 or later
Custom Implementation No
Header shellapi. h
Import library shell32.lib
Minimum operating systems Windows NT 3.1, Windows 95
We can see that the shellapi. h header file needs to be included when calling this API function, and the lib file needs to be imported.
# Pragma comment (lib, "shell32.lib ")
However, the function we called above only has one parameter, because the function we called was encapsulated by MFC and is a member function of the CWnd class. Included in atlwin. h, the underlying layer still calls the above API function.
Because documents are covered on the main form, you must use the document class to process WM_DROPFILES messages.
Next we will add the message processing function WM_DROPFILES to the document class:
[Cpp]
Void CDragFilesView: OnDropFiles (HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT nFileNum = DragQueryFile (hDropInfo, 0 xFFFFFFFF, NULL, 0); // Number of drag files
TCHAR strFileName [MAX_PATH]; // defines a buffer and the name of the received file.
For (int I = 0; I <nFileNum; I ++) // supports dragging multiple files at the same time
{
DragQueryFile (hDropInfo, I, strFileName, MAX_PATH); // obtain the dragged file name.
M_vetFileNames.push_back (strFileName); // m_vetFileNames is a member variable in the document class. std: vector <CString> saves the file name.
}
DragFinish (hDropInfo); // release hDrop and destroy the memory block opened by the system to store the drag-and-drop file name.
 
Invalidate (TRUE); // makes the window invalid and redraws
CView: OnDropFiles (hDropInfo );
}

Void CDragFilesView: OnDropFiles (HDROP hDropInfo)
{
// TODO: Add your message handler code here and/or call default
UINT nFileNum = DragQueryFile (hDropInfo, 0 xFFFFFFFF, NULL, 0); // Number of drag files
TCHAR strFileName [MAX_PATH]; // defines a buffer and the name of the received file.
For (int I = 0; I <nFileNum; I ++) // supports dragging multiple files at the same time
{
DragQueryFile (hDropInfo, I, strFileName, MAX_PATH); // obtain the dragged file name.
M_vetFileNames.push_back (strFileName); // m_vetFileNames is a member variable in the document class. std: vector <CString> saves the file name.
}
DragFinish (hDropInfo); // release hDrop and destroy the memory block opened by the system to store the drag-and-drop file name.

Invalidate (TRUE); // makes the window invalid and redraws
CView: OnDropFiles (hDropInfo );
} When you start the drag operation, the operating system will allocate a piece of memory to store the information of the drag file, and pass the address of the block memory to the OnDropFiles function through an HDROP handle.
Then we call DragQueryFile. The prototype of this function is:
UINT DragQueryFile (
HDROP hDrop, // structure identifier that contains the name of the drag-and-drop File
UINT iFile, // drag the file index. If it is 0 xFFFFFFFF, the function returns the number of files to be dragged. Otherwise, the file name is copied to the lpszFile Buffer Based on the index.
LPTSTR lpszFile, // buffer for receiving file names. If it is NULL, the function returns the size required by the buffer.
UINT cch // lpszFile buffer size
); If iFile sets 0xFFFFFFFF, the number of drag files is returned; otherwise, the number of characters in the copy file name is returned. // to verify the drag effect, output the file name to the document. // process [cpp] view plaincopyprint in OnDrow? Void CDragFilesView: OnDraw (CDC * pDC) [cpp] view plaincopyprint? {[Cpp] view plaincopyprint? CDragFilesDoc * pDoc = GetDocument (); ASSERT_VALID (pDoc); if (! PDoc) return; // TODO: add the code std: vector <CString >:: iterator pos; int y = 0; TEXTMETRIC tm to the local data; // system font information structure: pDC-> GetTextMetrics (& tm); // obtain the system font information. To obtain the font height, for (pos = m_vetFileNames.begin (); pos! = M_vetFileNames.end (); pos ++) // output the file name {pDC-> TextOut (0, y, * pos); y + = tm. tmHeight ;}} CDragFilesDoc * pDoc = GetDocument ();
ASSERT_VALID (pDoc );
If (! PDoc)
Return;

// TODO: add the drawing code for the local data here
Std: vector <CString >:: iterator pos;
Int y = 0;
TEXTMETRIC tm; // system font information structure
PDC-> GetTextMetrics (& tm); // obtain the system font information to obtain the font height.
For (pos = m_vetFileNames.begin (); pos! = M_vetFileNames.end (); pos ++) // output the file name
{
PDC-> TextOut (0, y, * pos );
Y + = tm. tmHeight;
}
} Execution result:




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.