-
Notifications
You must be signed in to change notification settings - Fork 1
/
DragDropHelpers.h
271 lines (239 loc) · 8.25 KB
/
DragDropHelpers.h
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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
// THIS CODE AND INFORMATION IS PROVIDED "AS IS" WITHOUT WARRANTY OF
// ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING BUT NOT LIMITED TO
// THE IMPLIED WARRANTIES OF MERCHANTABILITY AND/OR FITNESS FOR A
// PARTICULAR PURPOSE.
//
// Copyright (c) Microsoft Corporation. All rights reserved
#pragma once
#include <strsafe.h>
#include <commoncontrols.h>
// declare a static CLIPFORMAT and pass that that by ref as the first param
__inline CLIPFORMAT GetClipboardFormat(CLIPFORMAT *pcf, PCWSTR pszForamt)
{
if (*pcf == 0)
{
*pcf = (CLIPFORMAT)RegisterClipboardFormat(pszForamt);
}
return *pcf;
}
__inline HRESULT SetBlob(IDataObject *pdtobj, CLIPFORMAT cf, const void *pvBlob, UINT cbBlob)
{
void *pv = GlobalAlloc(GPTR, cbBlob);
HRESULT hr = pv ? S_OK : E_OUTOFMEMORY;
if (SUCCEEDED(hr))
{
CopyMemory(pv, pvBlob, cbBlob);
FORMATETC fmte = {cf, NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
// The STGMEDIUM structure is used to define how to handle a global memory transfer.
// This structure includes a flag, tymed, which indicates the medium
// to be used, and a union comprising pointers and a handle for getting whichever
// medium is specified in tymed.
STGMEDIUM medium = {};
medium.tymed = TYMED_HGLOBAL;
medium.hGlobal = pv;
hr = pdtobj->SetData(&fmte, &medium, TRUE);
if (FAILED(hr))
{
GlobalFree(pv);
}
}
return hr;
}
__inline void SetDropTip(IDataObject *pdtobj, DROPIMAGETYPE type, PCWSTR pszMsg, PCWSTR pszInsert)
{
DROPDESCRIPTION dd = { type };
StringCchCopyW(dd.szMessage, ARRAYSIZE(dd.szMessage), pszMsg);
StringCchCopyW(dd.szInsert, ARRAYSIZE(dd.szInsert), pszInsert ? pszInsert : L"");
static CLIPFORMAT s_cfDropDescription = 0;
SetBlob(pdtobj, GetClipboardFormat(&s_cfDropDescription, CFSTR_DROPDESCRIPTION), &dd, sizeof(dd));
}
__inline void ClearDropTip(IDataObject *pdtobj)
{
SetDropTip(pdtobj, DROPIMAGE_INVALID, L"", NULL);
}
// helper to convert a data object with HIDA format or folder into a shell item
// note: if the data object contains more than one item this function will fail
// if you want to operate on the full selection use SHCreateShellItemArrayFromDataObject
HRESULT CreateItemFromObject(IUnknown *punk, REFIID riid, void **ppv)
{
*ppv = NULL;
PIDLIST_ABSOLUTE pidl;
HRESULT hr = SHGetIDListFromObject(punk, &pidl);
if (SUCCEEDED(hr))
{
hr = SHCreateItemFromIDList(pidl, riid, ppv);
ILFree(pidl);
}
else
{
// perhaps the input is from IE and if so we can construct an item from the URL
IDataObject *pdo;
hr = punk->QueryInterface(IID_PPV_ARGS(&pdo));
if (SUCCEEDED(hr))
{
static CLIPFORMAT g_cfURL = 0;
FORMATETC fmte = {GetClipboardFormat(&g_cfURL, CFSTR_SHELLURL), NULL, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
STGMEDIUM medium;
hr = pdo->GetData(&fmte, &medium);
if (SUCCEEDED(hr))
{
PCSTR pszURL = (PCSTR)GlobalLock(medium.hGlobal);
if (pszURL)
{
WCHAR szURL[2048];
SHAnsiToUnicode(pszURL, szURL, ARRAYSIZE(szURL));
hr = SHCreateItemFromParsingName(szURL, NULL, riid, ppv);
GlobalUnlock(medium.hGlobal);
}
ReleaseStgMedium(&medium);
}
pdo->Release();
}
}
return hr;
}
// encapsulation of the shell drag drop presentation and Drop handling functionality
// this provides the following features 1) drag images, 2) drop tips,
// 3) ints OLE and registers drop target, 4) conversion of the data object item into shell items
//
// to use this
// 1) have the object that represents the main window of your app derive
// from CDragDropHelper exposing it as public.
// "class CAppClass : public CDragDropHelper"
// 2) add IDropTarget to the QueryInterface() implementation of your class
// that is a COM object itself
// 3) in your WM_INITDIALOG handling call
// InitializeDragDropHelper(_hdlg, DROPIMAGE_LINK, NULL) passing
// the dialog window and drop tip template and type
// 4) in your WM_DESTROY handler add a call to TerminateDragDropHelper(). note not
// doing this will lead to a leak of your object so this is important
// 5) add the delaration and implementation of OnDrop() to your class, this
// gets called when the drop happens
class CDragDropHelper : public IDropTarget
{
public:
CDragDropHelper() : _pdth(NULL), _pdtobj(NULL), _hrOleInit(OleInitialize(0)), _hwndRegistered(NULL), _dropImageType(DROPIMAGE_LABEL), _pszDropTipTemplate(NULL)
{
CoCreateInstance(CLSID_DragDropHelper, NULL, CLSCTX_INPROC, IID_PPV_ARGS(&_pdth));
}
~CDragDropHelper()
{
SafeRelease(&_pdth);
if (SUCCEEDED(_hrOleInit))
{
OleUninitialize();
}
}
void SetDropTipTemplate(PCWSTR pszDropTipTemplate)
{
_pszDropTipTemplate = pszDropTipTemplate;
}
void InitializeDragDropHelper(HWND hwnd, DROPIMAGETYPE dropImageType, PCWSTR pszDropTipTemplate)
{
if (SUCCEEDED(_hrOleInit))
{
_dropImageType = dropImageType;
_pszDropTipTemplate = pszDropTipTemplate;
if (SUCCEEDED(RegisterDragDrop(hwnd, this)))
{
_hwndRegistered = hwnd;
}
}
}
void TerminateDragDropHelper()
{
if (_hwndRegistered)
{
RevokeDragDrop(_hwndRegistered);
_hwndRegistered = NULL;
}
}
HRESULT GetDragDropHelper(REFIID riid, void **ppv)
{
*ppv = NULL;
return _pdth ? _pdth->QueryInterface(riid, ppv) : E_NOINTERFACE;
}
// direct access to the data object, if you don't want to use the shell item array
IDataObject *GetDataObject()
{
return _pdtobj;
}
// IDropTarget
IFACEMETHODIMP DragEnter(IDataObject *pdtobj, DWORD /* grfKeyState */, POINTL pt, DWORD *pdwEffect)
{
// leave *pdwEffect unchanged, we support all operations
if (_pdth)
{
POINT ptT = { pt.x, pt.y };
_pdth->DragEnter(_hwndRegistered, pdtobj, &ptT, *pdwEffect);
}
SetInterface(&_pdtobj, pdtobj);
IShellItem *psi;
HRESULT hr = CreateItemFromObject(pdtobj, IID_PPV_ARGS(&psi));
if (SUCCEEDED(hr))
{
PWSTR pszName;
hr = psi->GetDisplayName(SIGDN_NORMALDISPLAY, &pszName);
if (SUCCEEDED(hr))
{
SetDropTip(pdtobj, _dropImageType, _pszDropTipTemplate ? _pszDropTipTemplate : L"%1", pszName);//pszName
CoTaskMemFree(pszName);
}
psi->Release();
}
return S_OK;
}
IFACEMETHODIMP DragOver(DWORD /* grfKeyState */, POINTL pt, DWORD *pdwEffect)
{
// leave *pdwEffect unchanged, we support all operations
if (_pdth)
{
POINT ptT = { pt.x, pt.y };
_pdth->DragOver(&ptT, *pdwEffect);
}
return S_OK;
}
IFACEMETHODIMP DragLeave()
{
if (_pdth)
{
_pdth->DragLeave();
}
ClearDropTip(_pdtobj);
SafeRelease(&_pdtobj);
return S_OK;
}
IFACEMETHODIMP Drop(IDataObject *pdtobj, DWORD grfKeyState, POINTL pt, DWORD *pdwEffect)
{
if (_pdth)
{
POINT ptT = { pt.x, pt.y };
_pdth->Drop(pdtobj, &ptT, *pdwEffect);
}
IShellItemArray *psia;
HRESULT hr = SHCreateShellItemArrayFromDataObject(_pdtobj, IID_PPV_ARGS(&psia));
if (SUCCEEDED(hr))
{
OnDrop(psia, grfKeyState);
psia->Release();
}
else
{
OnDropError(_pdtobj);
}
return S_OK;
}
private:
// client provides
virtual HRESULT OnDrop(IShellItemArray *psia, DWORD grfKeyState) = 0;
virtual HRESULT OnDropError(IDataObject * /* pdtobj */)
{
return S_OK;
}
IDropTargetHelper *_pdth;
IDataObject *_pdtobj;
DROPIMAGETYPE _dropImageType;
PCWSTR _pszDropTipTemplate;
HWND _hwndRegistered;
HRESULT _hrOleInit;
};