-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdrop_target.d
More file actions
192 lines (164 loc) · 4.65 KB
/
drop_target.d
File metadata and controls
192 lines (164 loc) · 4.65 KB
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
/++
Win32 Drag and Drop demo
CF_TEXT drop_target
++/
module drop_target;
import std.string;
import win32.objidl;
import win32.ole2;
import win32.winbase;
import win32.windef;
import win32.winuser;
import win32.wtypes;
import win32.shellapi;
import utils;
import debugLog;
private string _text;
string dropGetText()
{
return _text.length ? _text : "";
}
void dropSetText(string s)
{
_text = s;
}
void dropSetText(char *s)
{
_text = cPtrToString(s);
}
class DropTarget : ComObject, IDropTarget
{
private:
HWND m_hWnd;
bool m_fAllowDrop;
IDataObject _DataObject;
public:
this(HWND hwnd)
{
outLog("DropTarget:this");
m_hWnd = hwnd;
m_fAllowDrop = false;
}
void RegisterDropTarget()
{
outLog("DropTarget:RegisterDropTarget");
HRESULT result;
if ((result = RegisterDragDrop(m_hWnd, this)) != S_OK) {
if (result == DRAGDROP_E_INVALIDHWND)
outLog("DRAGDROP_E_INVALIDHWND");
else if (result == DRAGDROP_E_ALREADYREGISTERED)
outLog("DRAGDROP_E_ALREADYREGISTERED");
else if (result == E_OUTOFMEMORY)
outLog("E_OUTOFMEMORY");
else
outLog("Undefined");
}
}
void RevokeDropTarget()
{
outLog("DropTarget:RevokeDropTarget");
HRESULT result;
if (RevokeDragDrop(m_hWnd) != S_OK) {
if (result == DRAGDROP_E_NOTREGISTERED)
outLog("DRAGDROP_E_NOTREGISTERED");
else if (result == DRAGDROP_E_INVALIDHWND)
outLog("DRAGDROP_E_INVALIDHWND");
else if (result == E_OUTOFMEMORY)
outLog("E_OUTOFMEMORY");
else
outLog("Undefined");
}
}
// IDropTarget interfaces
extern (Windows)
override HRESULT QueryInterface(GUID* riid, void** ppv)
{
if (*riid == IID_IDropTarget)
{
*ppv = cast(void*)cast(IUnknown)this;
AddRef();
return S_OK;
}
return super.QueryInterface(riid, ppv);
}
// ドロップされると呼ばれる
extern (Windows)
HRESULT DragEnter(IDataObject pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
outLog("DropTarget:DragEnter");
_DataObject = pDataObject;
return DragOver(grfKeyState, pt, pdwEffect);
}
//ドロップ状態でマウスのポインタが移動すると呼ばれる POINTL に注意
extern (Windows)
HRESULT DragOver(DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
outLog("DropTarget:DragOver");
if (QueryFormat(CF_TEXT)) {
// *pdwEffect = DropEffect(grfKeyState, pt, DROPEFFECT.DROPEFFECT_COPY);
*pdwEffect = DROPEFFECT.DROPEFFECT_COPY;
} else {
*pdwEffect = DROPEFFECT.DROPEFFECT_NONE;
}
return S_OK;
}
//ドロップ状態でマウスのポインタが Window の外に出ると呼ばれる
extern (Windows)
HRESULT DragLeave()
{
outLog("DropTarget:DragLeave");
return S_OK;
}
//ドロップされると呼ばれる
extern (Windows)
HRESULT Drop(IDataObject pDataObject, DWORD grfKeyState, POINTL pt, DWORD* pdwEffect)
{
outLog("DropTarget:Drop");
FORMATETC text_fmt = { CF_TEXT, null, DVASPECT.DVASPECT_CONTENT, -1, TYMED.TYMED_HGLOBAL };
STGMEDIUM medium;
if (pDataObject.GetData(&text_fmt, &medium) == S_OK) {
char *buf = cast(char*)GlobalLock(medium.hGlobal);
dropSetText(buf);
GlobalUnlock(medium.hGlobal);
ReleaseStgMedium(&medium);
*pdwEffect = DROPEFFECT.DROPEFFECT_COPY;
}
else {
*pdwEffect = DROPEFFECT.DROPEFFECT_NONE;
}
InvalidateRect(m_hWnd, NULL, TRUE);
return S_OK;
}
private:
private bool QueryFormat(CLIPFORMAT cfFormat)
{
FORMATETC fmt;
fmt.cfFormat = cfFormat;
fmt.ptd = null;
fmt.dwAspect = DVASPECT.DVASPECT_CONTENT;
fmt.lindex = -1;
fmt.tymed = TYMED.TYMED_HGLOBAL;
return _DataObject.QueryGetData(&fmt) == S_OK ? true : false;
}
private DWORD DropEffect(DWORD grfKeyState, POINTL pt, DWORD dwAllowed)
{
DWORD dwEffect = 0;
if (grfKeyState & MK_CONTROL)
{
dwEffect = dwAllowed & DROPEFFECT.DROPEFFECT_COPY;
}
else if (grfKeyState & MK_SHIFT)
{
dwEffect = dwAllowed & DROPEFFECT.DROPEFFECT_MOVE;
}
if (dwEffect == 0)
{
if (dwAllowed & DROPEFFECT.DROPEFFECT_COPY)
dwEffect = DROPEFFECT.DROPEFFECT_COPY;
if (dwAllowed & DROPEFFECT.DROPEFFECT_MOVE)
dwEffect = DROPEFFECT.DROPEFFECT_MOVE;
}
return dwEffect;
}
}
//eof