-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUtil.cpp
501 lines (488 loc) · 16 KB
/
Util.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
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
#include "util.h"
//
extern HINSTANCE _hInst;
extern HWND _hDlg;
//
void ShowErrorMsg(HWND hWnd, const DWORD dwErrorCode, const char *szFunctionName) {
void *lpMsgBuf;
//
if (!FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER |
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
0L,
dwErrorCode,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
(char *)&lpMsgBuf,
0,
0L))
return;
MessageBox(hWnd, (const char *)lpMsgBuf, szFunctionName, MB_ICONSTOP | MB_OK);
LocalFree(lpMsgBuf);
};
//
void WaitCursor(const char bIsWait) {
HCURSOR hCur = 0L;
//
hCur = (HCURSOR)LoadImage(_hInst, (bIsWait) ? IDC_WAIT : IDC_ARROW,
IMAGE_CURSOR,
GetSystemMetrics(SM_CXSMICON),
GetSystemMetrics(SM_CYSMICON),
LR_DEFAULTCOLOR);
if (hCur)
SetCursor(hCur);
};
//
void DisableCloseBox(HWND hDlg) {
DWORD dwStyle = GetClassLongPtr(hDlg, GCL_STYLE);
dwStyle |= CS_NOCLOSE;
SetClassLongPtr(hDlg, GCL_STYLE, dwStyle);
};
//
/*********************************************************************/
/* ListView */
/*********************************************************************/
void ListViewDeleteItem(HWND hList, const short wItem) {
if (wItem == -1)
SendMessage(hList, LVM_DELETEALLITEMS, 0, 0);
else
SendMessage(hList, LVM_DELETEITEM, (WPARAM)wItem, 0);
};
//
void ListViewSetExtStyle(HWND hListView, const DWORD dwStyle) {
DWORD style;
//
style = (dwStyle) ? dwStyle :
LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT | LVS_EX_FLATSB;
SendMessage(hListView, LVM_SETEXTENDEDLISTVIEWSTYLE, 0, (LPARAM)style);
};
//
void ListViewSetSelectItem(HWND hListView, const short iItem) {
LVITEM lvItem;
short i;
//
lvItem.iItem = iItem;
lvItem.mask = LVIF_STATE;
lvItem.state = LVIS_SELECTED;
i = (short)SendMessage(hListView, LVM_SETITEMSTATE, iItem, (LPARAM)&lvItem);
};
//
void ListViewInsertColumnText(HWND hListView, const short wIdx,
const int wFmt, char *pszText, const BOOL bFinal) {
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HDC hDC = GetDC(_hDlg);
int fmt;
//
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth * 20;
ReleaseDC(_hDlg, hDC);
//
if (!wFmt)
fmt = LVCFMT_LEFT;
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH;
column.pszText = pszText;
column.fmt = fmt;
if (!bFinal)
column.cx = nWidth;
else
column.cx = nWidth * 100;
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(_hDlg, GetLastError(), "InitialListView");
};
//
void ListViewAddColumnImageText(HWND hWnd, const UINT nID, const short wIdx,
const int wFmt, char *pszText, const BOOL bFinal) {
LVCOLUMN column;
TEXTMETRIC text;
int nWidth;
HWND hListView = GetDlgItem(hWnd, nID);
HDC hDC = GetDC(hWnd);
int fmt;
//
GetTextMetrics(hDC, &text);
nWidth = text.tmAveCharWidth * 20;
ReleaseDC(hWnd, hDC);
//
if (!wFmt)
fmt = LVCFMT_LEFT;
column.mask = LVCF_TEXT | LVCF_FMT | LVCF_WIDTH | LVCF_IMAGE;
column.pszText = pszText;
column.fmt = fmt;
if (!bFinal)
column.cx = nWidth;
else
column.cx = 700;
if (SendMessage(hListView, LVM_INSERTCOLUMN, wIdx, (LPARAM)(LPLVCOLUMN)&column) == -1)
ShowErrorMsg(hWnd, GetLastError(), "InitialListView");
};
//
void ListViewInsertItemText(HWND hListView, const int iItem,
const int iSubItem, char *pszText) {
LVITEM item;
//
item.mask = LVIF_TEXT;
item.iItem = iItem;
item.iSubItem = iSubItem;
item.state = 0;
item.stateMask = 0;
item.iImage = 0;
item.lParam = 0;
item.pszText = pszText;
item.cchTextMax = (int)strlen(pszText);
if (!iSubItem)
SendMessage(hListView, LVM_INSERTITEM, 0, (LPARAM)&item);
else
SendMessage(hListView, LVM_SETITEMTEXT, iItem, (LPARAM)&item);
};
//
int ListViewGetItemSelect(HWND hListView) {
return (int)SendMessage(hListView, LVM_GETNEXTITEM, -1, MAKELPARAM((UINT)LVNI_SELECTED, 0));
};
//
int ListViewGetItemCount(HWND hListView) {
return (int)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
};
//
void ListViewGetItemText(HWND hListView, const int iSubItem, char *pszText) {
LVITEM item;
int idx = ListViewGetItemSelect(hListView);
//
if (idx == -1)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, idx, (LPARAM)&item);
};
//
void ListViewGetSpecItem(HWND hListView, const int iItem, const int iSubItem, char *pszText) {
LVITEM item;
//
if (iItem == -1)
return;
//
item.iSubItem = iSubItem;
item.pszText = pszText;
item.cchTextMax = 255;
SendMessage(hListView, LVM_GETITEMTEXT, iItem, (LPARAM)&item);
};
//
UINT IsListViewDBClkEvent(const UINT nID, NMHDR *pnmh) {
if (pnmh->idFrom == nID) {
if (pnmh->code == NM_DBLCLK)
return nID;
};
return 0;
};
//
UINT IsListViewClkEvent(const UINT nID, NMHDR *pnmh) {
if (pnmh->idFrom == nID) {
if (pnmh->code == NM_CLICK)
return NM_CLICK;
else if (pnmh->code == NM_RCLICK)
return NM_RCLICK;
};
return 0;
};
//
void ListViewRemoveAllItems(HWND hListView) {
short wCnt = (short)SendMessage(hListView, LVM_GETITEMCOUNT, 0, 0);
short wLoop;
//
for (wLoop = 0; wLoop < wCnt; wLoop++)
SendMessage(hListView, LVM_DELETEITEM, 0, 0);
SendMessage(hListView, LVM_DELETEALLITEMS, 0, 0);
};
//
void ListViewRemoveItem(HWND hDlg, const UINT nID, const int iItem) {
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETEITEM, (WPARAM)iItem, 0);
};
//
void ListViewRemoveColumn(HWND hDlg, const UINT nID, const int iCol) {
SendMessage(GetDlgItem(hDlg, nID), LVM_DELETECOLUMN, (WPARAM)iCol, 0);
};
//
BOOL UnicodeToAnsi(const wchar_t *Source, const short wLen, char *Destination, const short sLen) {
return WideCharToMultiByte(CP_ACP,
WC_COMPOSITECHECK,
Source,
wLen,
Destination,
sLen,
0L, 0L);
};
//
/*********************************************************************/
/* Unicode */
/*********************************************************************/
BOOL AnsiToUnicode(const char *Source, const short sLen, wchar_t *Destination, const short wLen) {
return MultiByteToWideChar(CP_ACP,
MB_PRECOMPOSED,
Source,
sLen,
Destination,
wLen);
};
//
/*********************************************************************/
/* TreeView */
/*********************************************************************/
HTREEITEM TreeViewInsertRootText(HWND hWnd, UINT nID,
HTREEITEM hParent, HTREEITEM hAfter,
char *pszText) {
TVINSERTSTRUCT tvInsert;
HWND hTree = GetDlgItem(hWnd, nID);
//
if (!hParent && !hAfter) {
tvInsert.hParent = 0L;
tvInsert.hInsertAfter = 0L;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = pszText;
return (HTREEITEM)SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)&tvInsert);
};
//
tvInsert.hParent = hParent;
tvInsert.hInsertAfter = hAfter;
tvInsert.item.mask = TVIF_TEXT;
tvInsert.item.pszText = pszText;
return (HTREEITEM)SendMessage(hTree, TVM_INSERTITEM, 0, (LPARAM)&tvInsert);
};
//
HTREEITEM TreeViewFindChild(HWND hTreeView, HTREEITEM hRoot,
const char *szBuf) {
HTREEITEM hChildItem = 0L;
HTREEITEM hParentItem = hRoot;
char szText[128] = {0};
TVITEM tvItem = {0};
int iLoop;
int nIndex;
//
tvItem.mask = TVIF_TEXT | TVIF_HANDLE;
tvItem.pszText = szText;
tvItem.cchTextMax = sizeof(char) * 127;
nIndex = (int)SendMessage(hTreeView, TVM_GETCOUNT, 0, 0);
RtlZeroMemory(szText, sizeof(char) * 128);
for (iLoop = 0; iLoop < nIndex; iLoop++) {
hChildItem = (HTREEITEM)SendMessage(hTreeView,
TVM_GETNEXTITEM,
(iLoop) ? TVGN_NEXT : TVGN_CHILD,
(iLoop) ? (LPARAM)hParentItem : (LPARAM)hRoot);
tvItem.hItem = hChildItem;
SendMessage(hTreeView, TVM_GETITEM, 0, (LPARAM)&tvItem);
if (*szText) {
if (!strcmp(szBuf, szText))
return hChildItem;
};
hParentItem = hChildItem;
};
return 0;
};
HBITMAP IconToBitmap(HICON hIcon) {
// Get the device context of the screen
HDC hDC = GetDC(NULL);
// Create a memory device context compatible with the screen
HDC hMemDC = CreateCompatibleDC(hDC);
// Get the size of the icon
ICONINFO iconInfo;
GetIconInfo(hIcon, &iconInfo);
int width = iconInfo.xHotspot * 2;
int height = iconInfo.yHotspot * 2;
// Create a compatible bitmap with the screen
HBITMAP hBitmap = CreateCompatibleBitmap(hDC, width, height);
// Select the bitmap into the memory device context
HBITMAP hOldBitmap = (HBITMAP)SelectObject(hMemDC, hBitmap);
// Fill the bitmap with a white background
RECT rect = { 0, 0, width, height };
FillRect(hMemDC, &rect, (HBRUSH)GetStockObject(WHITE_BRUSH));
// Draw the icon onto the bitmap
DrawIconEx(hMemDC, 0, 0, hIcon, width, height, 0, NULL, DI_NORMAL);
// Restore the old bitmap
SelectObject(hMemDC, hOldBitmap);
// Clean up
DeleteDC(hMemDC);
ReleaseDC(NULL, hDC);
DeleteObject(iconInfo.hbmColor);
DeleteObject(iconInfo.hbmMask);
return hBitmap;
}
//
HTREEITEM MakeDeviceRootTree(SP_CLASSIMAGELIST_DATA spImageData, const UINT nIdTree) {
TVINSERTSTRUCT tvStruct = {0};
HIMAGELIST hImageListTree = 0L;
HTREEITEM hTreeItem = 0L;
HBITMAP hBitmap = 0L;
int nIndex = MAX_PATH - 1;
char szName[MAX_PATH] = {0};
//
GetComputerName(szName, (LPDWORD)&nIndex);
SHFILEINFO sfi;
LPITEMIDLIST pidl;
SHGetSpecialFolderLocation(NULL, CSIDL_DRIVES, &pidl);
SHGetFileInfo((LPCSTR)pidl, 0, &sfi, sizeof(SHFILEINFO), SHGFI_ICON | SHGFI_PIDL | SHGFI_SMALLICON);
hBitmap = IconToBitmap(sfi.hIcon);
nIndex = ImageList_Add(spImageData.ImageList, hBitmap, 0L);
DestroyIcon(sfi.hIcon);
DeleteObject(hBitmap);
SendMessage(GetDlgItem(_hDlg, nIdTree), TVM_SETIMAGELIST,
TVSIL_NORMAL, (LPARAM)spImageData.ImageList);
if (ImageList_GetImageCount(spImageData.ImageList) != -1) {
tvStruct.hParent = 0L;
tvStruct.hInsertAfter = TVI_ROOT;
tvStruct.item.mask = TVIF_IMAGE | TVIF_TEXT | TVIF_SELECTEDIMAGE;
tvStruct.item.pszText = szName;
tvStruct.item.iImage = nIndex;
tvStruct.item.iSelectedImage = nIndex;
hTreeItem = (HTREEITEM)SendMessage(GetDlgItem(_hDlg, nIdTree),
TVM_INSERTITEM, 0, (LPARAM)&tvStruct);
return hTreeItem;
};
return 0;
};
//
UINT IsTreeViewClkEvent(const UINT nID, NMHDR *pnmh) {
if (pnmh->idFrom == nID) {
if (pnmh->code == NM_CLICK)
return NM_CLICK;
else if (pnmh->code == NM_RCLICK)
return NM_RCLICK;
};
return 0;
};
//
UINT IsTreeViewSelectChanged(const UINT nID, NMHDR *pnmh) {
if (pnmh->code == TVN_SELCHANGED && pnmh->idFrom == nID)
return TVN_SELCHANGED;
return 0;
};
//
void TreeViewExpand(HWND hTree, HTREEITEM hTreeItem, const BOOL bExpand) {
UINT flags = (bExpand) ? TVE_EXPAND : TVE_COLLAPSE;
//
SendMessage(hTree, TVM_EXPAND, (WPARAM)flags, (LPARAM)hTreeItem);
};
//
void TreeViewGetSelectText(HWND hTree, NMHDR *pnmh, TVITEM *pItem) {
NMTREEVIEW *pnmView = (NMTREEVIEW *)pnmh;
char pszText[MAX_PATH] = {0};
//
pItem->mask = TVIF_TEXT;
pItem->hItem = pnmView->itemNew.hItem;
do {
pItem->pszText = pszText;
pItem->cchTextMax = MAX_PATH - 1;
SendMessage(hTree, TVM_GETITEM, 0, (LPARAM)pItem);
if (strlen(pItem->pszText))
break;
} while (1);
};
//
void TreeViewRemoveAllNodes(HWND hDlg, const UINT nIdTree) {
HWND hTree = GetDlgItem(hDlg, nIdTree);
short wCnt = (short)SendMessage(hDlg, TVM_GETCOUNT, 0, 0);
short wLoop;
//
for (wLoop = 0; wLoop < wCnt; wLoop)
SendMessage(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
SendMessage(hTree, TVM_DELETEITEM, 0, (LPARAM)TVI_ROOT);
};
//
/*********************************************************************/
/* String */
/*********************************************************************/
void FindComma(char *szData) {
short wLen = (short)strlen(szData);
short wIdx;
short wLoop;
char szTmp[128] = {0};
//
for (wIdx = 0, wLoop = 0; wLoop < wLen; wLoop++) {
if (szData[wLoop] == ',')
szData[wLoop] = '.';
else if (szData[wLoop] == ' ')
continue;
szTmp[wIdx++] = szData[wLoop];
};
memcpy(szData, szTmp, wIdx * sizeof(char));
szData[wIdx] = 0;
};
//
void StrRight(char *szData, short wCount) {
short wLen = (short)strlen(szData) - wCount;
//
if (wCount < 0)
wCount = 0;
if (wCount >= (short)strlen(szData))
return;
//
memmove(szData, szData + wLen, wCount * sizeof(char));
szData[wCount] = 0;
};
//
void StrLTrim(char *szData) {
char *ptr = szData;
//
while (isspace(*ptr))
ptr = _tcsinc(ptr);
//
if (strcmp(ptr, szData)) {
short wLen = (short)(strlen(szData) - (ptr - szData));
memmove(szData, ptr, (wLen + 1)*sizeof(char));
};
};
//
void StrRTrim(char *szData) {
char *ptr = szData;
char *pTmp = 0L;
//
while (*ptr != 0) {
if (isspace(*ptr)) {
if (!pTmp)
pTmp = ptr;
} else
pTmp = 0L;
ptr = _tcsinc(ptr);
};
//
if (pTmp) {
*pTmp = 0;
memmove(szData, szData, strlen(szData) - strlen(pTmp));
};
};
//
void ConvertGUIDToString(const GUID guid, char *pData) {
char szData[30] = {0};
char szTmp[3] = {0};
short wLoop;
//
sprintf(pData, "%04X-%02X-%02X-", guid.Data1,
guid.Data2, guid.Data3);
for (wLoop = 0; wLoop < 8; wLoop++) {
if (wLoop == 2)
strcat(szData, "-");
sprintf(szTmp, "%02X", guid.Data4[wLoop]);
strcat(szData, szTmp);
};
memcpy(pData + strlen(pData), szData, strlen(szData));
};
//
/*********************************************************************/
/* ListView-Image */
/*********************************************************************/
void SetOverlayImage(HIMAGELIST hImage, const int iImage, const int iOverlay) {
ImageList_SetOverlayImage(hImage, iImage, iOverlay);
};
//
int GetImageCount(HIMAGELIST hImage) {
return ImageList_GetImageCount(hImage);
};
//
int ListViewSetImageList(HWND hList, const int nList, LPARAM lParam) {
WPARAM imageList = LVSIL_NORMAL;
//
if (nList)
imageList = nList;
return (int)SendMessage(hList, LVM_SETIMAGELIST,
imageList, lParam);
};