-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
303 lines (226 loc) · 8.11 KB
/
main.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
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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
//
// MAIN.CPP
//
// DragSource
//
// Freeware written by J Brown 2004
//
// www.catch22.net
//
#define STRICT
#include <windows.h>
#include "resource.h"
#define APPNAME "IDropSource"
#pragma comment(linker, "/OPT:NOWIN98")
HWND hwndMain;
HWND hwndEdit;
HINSTANCE hInstance;
WNDPROC OldEditWndProc;
HRESULT CreateDropSource(IDropSource **ppDropSource);
HRESULT CreateDataObject(FORMATETC *fmtetc, STGMEDIUM *stgmeds, UINT count, IDataObject **ppDataObject);
//
// Is the mouse cursor within the edit control's selected text?
//
// Return TRUE/FALSE
//
BOOL MouseInSelection(HWND hwndEdit, LPARAM MouseMsgParam) {
DWORD nSelStart;
DWORD nSelEnd;
// get the selection inside the edit control
SendMessage(hwndEdit, EM_GETSEL, (WPARAM) &nSelStart, (LPARAM) &nSelEnd);
if (nSelStart != nSelEnd) {
DWORD nCurPos;
// Get the cursor position the mouse has clicked on
nCurPos = SendMessage(hwndEdit, EM_CHARFROMPOS, 0, MouseMsgParam);
nCurPos = LOWORD(nCurPos);
// Did the mouse click inside the active selection?
return (nCurPos >= nSelStart && nCurPos < nSelEnd) ? TRUE : FALSE;
}
return FALSE;
}
//
// Remove any selection from the edit control
//
void ClearSelection(HWND hwndEdit) {
SendMessage(hwndEdit, EM_SETSEL, -1, -1);
}
//
// Copy selected text to a HGLOBAL and return it
//
HGLOBAL CopySelection(HWND hwndEdit) {
DWORD nSelStart, nSelEnd;
DWORD nSelLength, nEditLength;
HGLOBAL hMem;
char *ptr;
char *tmp;
SendMessage(hwndEdit, EM_GETSEL, (WPARAM) &nSelStart, (LPARAM) &nSelEnd);
nSelLength = nSelEnd - nSelStart;
// get the entire contents of the control
nEditLength = SendMessage(hwndEdit, EM_GETLIMITTEXT, 0, 0);
tmp = (char *) malloc(nEditLength);
SendMessage(hwndEdit, WM_GETTEXT, nEditLength, (LPARAM) tmp);
hMem = GlobalAlloc(GHND, nSelLength + 1);
ptr = (char *) GlobalLock(hMem);
// copy the selected text and nul-terminate
memcpy(ptr, tmp + nSelStart, nSelLength);
ptr[nSelLength] = '\0';
GlobalUnlock(hMem);
free(tmp);
return hMem;
}
//
// Subclass window-procedure for EDIT control
//
LRESULT CALLBACK EditWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
static BOOL fMouseDown = FALSE;
static BOOL fDidDragDrop = FALSE;
switch (msg) {
case WM_KEYDOWN:
// when ESCAPE is pressed clear the current selection
if (wParam == VK_ESCAPE)
ClearSelection(hwnd);
break;
case WM_LBUTTONDOWN:
case WM_LBUTTONDBLCLK:
// if the mouse is pressed when it is over a selection,
// then start a drag-drop as soon as the mouse moves next
if (MouseInSelection(hwndEdit, lParam)) {
fMouseDown = TRUE;
fDidDragDrop = FALSE;
SetCapture(hwnd);
return 0;
}
break;
case WM_SETCURSOR:
// set the mouse cursor to an ARROW when it intersects the
// current selection, or the default IBEAM otherwise
if ((HWND) wParam == hwnd) {
POINT pt;
GetCursorPos(&pt);
ScreenToClient(hwndEdit, &pt);
if (MouseInSelection(hwndEdit, MAKELPARAM(pt.x, pt.y))) {
SetCursor(LoadCursor(0, (IDC_ARROW)));
} else {
SetCursor(LoadCursor(0, (IDC_IBEAM)));
}
}
return TRUE;
case WM_MOUSEMOVE:
// if the mouse is held down then start a drag-drop
if (fMouseDown) {
IDataObject *pDataObject;
IDropSource *pDropSource;
DWORD dwEffect;
DWORD dwResult;
FORMATETC fmtetc = {CF_TEXT, 0, DVASPECT_CONTENT, -1, TYMED_HGLOBAL};
STGMEDIUM stgmed = {TYMED_HGLOBAL, {0}, 0};
// transfer the current selection into the IDataObject
stgmed.hGlobal = CopySelection(hwndEdit);
// Create IDataObject and IDropSource COM objects
CreateDropSource(&pDropSource);
CreateDataObject(&fmtetc, &stgmed, 1, &pDataObject);
//
// ** ** ** The drag-drop operation starts here! ** ** **
//
dwResult = DoDragDrop(pDataObject, pDropSource, DROPEFFECT_COPY | DROPEFFECT_MOVE, &dwEffect);
// success!
if (dwResult == DRAGDROP_S_DROP) {
if (dwEffect & DROPEFFECT_MOVE) {
// remove selection from edit control
}
}
// cancelled
else if (dwResult == DRAGDROP_S_CANCEL) {
}
pDataObject->Release();
pDropSource->Release();
ReleaseCapture();
fMouseDown = FALSE;
fDidDragDrop = TRUE;
}
break;
case WM_LBUTTONUP:
// stop drag-drop from happening when the mouse is released.
if (fMouseDown) {
ReleaseCapture();
fMouseDown = FALSE;
if (fDidDragDrop == FALSE)
ClearSelection(hwnd);
}
break;
}
return CallWindowProc(OldEditWndProc, hwnd, msg, wParam, lParam);
}
//
// Main Window message handler
//
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_CREATE:
// create a child-window EDIT control
hwndEdit = CreateWindowEx(WS_EX_CLIENTEDGE, "EDIT", "",
WS_CHILD | WS_VISIBLE | ES_MULTILINE | ES_WANTRETURN | WS_VSCROLL,
0, 0, 0, 0, hwnd, 0, hInstance, 0);
// fixed-width font
SendMessage(hwndEdit, WM_SETFONT, (WPARAM) GetStockObject(ANSI_FIXED_FONT), 0);
// subclass the edit control so we can add drag+drop support to it
OldEditWndProc = (WNDPROC) SetWindowLongPtr(hwndEdit, GWLP_WNDPROC,
reinterpret_cast<LONG_PTR>(EditWndProc));
SetFocus(hwndEdit);
return TRUE;
case WM_COMMAND:
// react to menu messages
switch (LOWORD(wParam)) {
case IDM_FILE_EXIT:
CloseWindow(hwnd);
return 0;
case IDM_FILE_ABOUT:
MessageBox(hwnd, "IDropSource Test Application\r\n\r\n"
"Copyright(c) 2004 by Catch22 Productions\t\r\n"
"Written by J Brown.\r\n\r\n"
"Homepage at www.catch22.net", APPNAME, MB_ICONINFORMATION);
return 0;
}
break;
case WM_CLOSE:
// shut program down
DestroyWindow(hwnd);
PostQuitMessage(0);
return 0;
case WM_SIZE:
// resize editbox to fit in main window
MoveWindow(hwndEdit, 0, 0, LOWORD(lParam), HIWORD(lParam), TRUE);
return 0;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
void InitMainWnd() {
WNDCLASSEX wc = {sizeof(wc)};
wc.lpfnWndProc = WndProc;
wc.lpszClassName = APPNAME;
wc.lpszMenuName = MAKEINTRESOURCE(IDR_MENU1);
wc.hInstance = hInstance;
RegisterClassEx(&wc);
}
void CreateMainWnd() {
hwndMain = CreateWindowEx(0, APPNAME, APPNAME,
WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 512, 200, 0, 0,
hInstance, 0);
}
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR lpCmdLine, int nShowCmd) {
MSG msg;
hInstance = hInst;
// This program requires COM
OleInitialize(0);
InitMainWnd();
CreateMainWnd();
// message-pump
while (GetMessage(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
// Shutdown COM
OleUninitialize();
return 0;
}