Drag and Drop
Win32 subsystem facilitates files and object transfer between two applications using drag and drop. Drag and drop is a mouse operation. User clicks on some file(s) in file explorer or some OLE object in application like Word and moves mouse cursor to other application window with out realising left mouse button. This operation is called drag. Now releaseing left mouse button in the other application window is called drop operation. When drag and drop operations are made in conjunction, a drag-n-drop operation happens. This creates a visual effect and object seems moves between two windows and mouse cursor changes. An application should prepare its window to accept drop operations.
There are Win32 APIs to enble this operation and Windows sends event when drop operation is performed. drag-n-drop is mainly with two types -
- file/folder drag and drom from windows exploler or other file manager applications.
- OLE objects like simple text, rich edit text, html, bitmaps, pictures etc drag and drop from other aplications.
We are discussing file drag and drop events here. Before going the the demo aplication we should know the Win32 APIs and Windows attributes related to drag and drop.
WS_EX_ACCEPTFILES is the extended windows style to pass the parameter in CreateWindowEx() to give the capability of accepting dropped files from windows explorer or file manager applications. An active and visible window receives the file drop event (WM_DROPFILES) when this extended style is applied.
While create a dialog in resource editor, WS_EX_ACCEPTFILES can be applied by selecting "Accept files" checkbox.
Registers whether a window accepts dropped files nor not. DragAcceptFiles function call changes windows style/capability of WS_EX_ACCEPTFILES at runtime.
VOID DragAcceptFiles( HWND hWnd, BOOL fAccept );Parameters hWnd - Handle to the window fAccept- TRUE to accept dropped files or FALSE to discontinue accepting dropped files. Return value No return value. Remarks DragAcceptFiles() can be called with FALSE to disable file drop operation.
Window receives this event during drag-n-drop operation. Windows with WS_EX_ACCEPTFILES can only receives this event. Windows explorer and file manager applications are sender and our demo application is the receiver part of this message. File manager application detects the mouse drag even and calls PostMessage() with message id WM_DROPFILES and one or more file path information as a handle in wParam papameter.
PostMessage( (HWND) hWndControl, // handle to destination control (UINT) WM_DROPFILES, // message ID (WPARAM) wParam, // = (WPARAM) (HDROP) hDrop; (LPARAM) lParam // = 0; not used, must be zero );
ParametershWndControl - Handle to the target control msgID - WM_DROPFILES hDrop - A handle to an internal structure describing the dropped files. Pass this handle DragFinish, DragQueryFile, or DragQueryPoint to retrieve information about the dropped files. lParam - ust be zero.
Return valueAn application should return zero if it processes this message.
The HDROP handle is declared in Shellapi.h. You must include this header in your build to use WM_DROPFILES. For further discussion of how to use drag-and-drop to transfer Shell data, see Transferring Shell Dat
Retrieves the names of dropped files that result from a successful drag-and-drop operation.
UINT DragQueryFile( HDROP hDrop, UINT iFile, LPTSTR lpszFile, UINT cch );
- hDrop - Handle of the structure that contains the file names of the dropped files.
- iFile - Index of the file to query. If the value of this parameter is 0xFFFFFFFF, DragQueryFile returns a count of the files dropped. If the value of this parameter is between zero and the total number of files dropped, DragQueryFile copies the file name with the corresponding value to the buffer pointed to by the lpszFile parameter.
- lpszFile- The address of a buffer that receives the file name of a dropped file when the function returns. This file name is a null-terminated string. If this parameter is NULL, DragQueryFile returns the required size, in characters, of this buffer.
- cch - The size of the lpszFile buffer.
A nonzero value indicates a successful call.
- Returns number of dropped files - Pass iFile as 0xFFFFFFFF to know how many files are dropped to this window.
- Returns length of the file path - Pass lpszFile as NULL to index as iFile to know the size of the buffer needed.
- Returns file path string in lpszFile - Pass iFile index between 0 to count -1 and lpszFile as a pointer to a buffer and cch as the length of the buffer.
Releases memory that the system allocated for use in transferring file names to the application.
VOID DragFinish( HDROP hDrop );
- hDrop- Handle that describes dropped files. This handle is retrieved from the wParam parameter of the WM_DROPFILES message.
During drag from windows explorer
After files are dropped to dialog
About our authors: Team EQA
MFC hides WinMain in its framework and includes source file on WinMain(). This explains how framework calls global CWinApp::Initinstance() from entry WinMain.
This is constructed during global C++ objects are constructed and is already available when Windows calls the WinMain function, which is supplied by the ...
Define a custom Window class structure, Register the class name, CreateWindow, Show windows and write message get and dispatch loop statements. Define the Window CallBack procedure and write the handlers.
DispatchMessage() is a API which indirectly triggers the Window CallBack procedure. Message structure members from this function are passed to the CallBack procedure. CallBack procedure should implement event handlers depending on the need of the application.
LPARAM and WPARAM are the two parameters in Window CallBack procedure. They signifies parameters of various events. They are used in handing individual events.
We need to write WinMain and need to follow all these in a Win32 application. However we need not to write much if we are writing an application with MFC ...
Questions index C Questions C++ Questions Win32 MFC COM/DCOM DLL Questions
Compilers & Editors
Download Visual Studio Download XCode Download Visual Studio Code Android studio install sdk Eclipse installer Best C compilers IDEs
Development system setup
Windows media creation tool MSDN subscription Ubuntu virtualbox
New updated posts
Why learn C? Draw on printer Memory leaks