-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwin32_dnd.cpp
83 lines (72 loc) · 3.28 KB
/
win32_dnd.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
#include <Windows.h>
// from dropsource.cpp
extern HRESULT CreateDropSource(IDropSource **ppDropSource);
// from dataobject.cpp
extern HRESULT CreateDataObject(FORMATETC *fmtetc, STGMEDIUM *stgmeds, UINT count, IDataObject **ppDataObject);
// Drag one or more files to another window
// If you call CreateWindowEx with WS_EX_ACCEPTFILES, then do the following to prevent dropping onto yourself:
// DragAcceptFiles(your_window_handle, FALSE);
// Win32DragDrop(paths, num_paths);
// DragAcceptFiles(your_window_handle, TRUE);
void Win32DragDrop(const wchar_t **paths, int num_paths)
{
IDataObject *pDataObject = NULL;
IDropSource *pDropSource = NULL;
HGLOBAL hgDrop = NULL;
DROPFILES* pDrop = NULL;
SIZE_T uBuffSize = 0;
SIZE_T uTotalWideChars = 0;
FORMATETC etc = { CF_HDROP, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL };
// Initialize OLE for using DoDragDrop function
if (SUCCEEDED(OleInitialize(NULL)))
{
// Get the total number of wide characters needed
for (int i = 0; i < num_paths; i++)
{
uTotalWideChars += lstrlenW(paths[i]) + 1;
}
// DROPFILES struct + null terminated wide strings + final L'\0'
uBuffSize = sizeof(DROPFILES) + sizeof(wchar_t) * (uTotalWideChars + 1);
// Allocate memory from the heap for the DROPFILES struct.
if (hgDrop = GlobalAlloc(GHND | GMEM_SHARE, uBuffSize))
{
if (pDrop = (DROPFILES *)GlobalLock(hgDrop))
{
// Fill in the DROPFILES struct.
pDrop->pFiles = sizeof(DROPFILES);
// Indicate it contains Unicode strings.
pDrop->fWide = TRUE;
// Copy all the filenames into memory after the end of the DROPFILES struct.
wchar_t *iter = (wchar_t *)(LPBYTE(pDrop) + sizeof(DROPFILES));
for (int i = 0; i < num_paths; i++)
{
int pathlen = lstrlenW(paths[i]);
CopyMemory(iter, paths[i], pathlen * sizeof(wchar_t));
iter += pathlen;
*iter++ = L'\0';
}
*iter++ = L'\0';
GlobalUnlock(hgDrop);
STGMEDIUM stg = {};
stg.tymed = CF_HDROP;
stg.hGlobal = hgDrop;
CreateDropSource(&pDropSource);
CreateDataObject(&etc, &stg, 1, &pDataObject);
if (pDropSource && pDataObject)
{
// do the drag/drop operation
// DROPEFFECT options from MSDN:
// DROPEFFECT_COPY: Drop results in a copy. The original data is untouched by the drag source.
// DROPEFFECT_MOVE: Drag source should remove the data.
// DROPEFFECT_LINK: Drag source should create a link to the original data.
DWORD dwEffect = 0;
DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY, &dwEffect);
}
if (pDropSource) { pDropSource->Release(); pDropSource = NULL; }
if (pDataObject) { pDataObject->Release(); pDataObject = NULL; }
}
GlobalFree(hgDrop); hgDrop = NULL;
}
OleUninitialize();
}
}