-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathWinRefineShlExt.cpp
More file actions
521 lines (381 loc) · 10.2 KB
/
WinRefineShlExt.cpp
File metadata and controls
521 lines (381 loc) · 10.2 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
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
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
// WinRefineShlExt.cpp : Implementation of CWinRefineShlExt
#include "stdafx.h"
#include "WinRefine.h"
#include "WinRefineShlExt.h"
#include "Util.h"
#include "Player.cpp"
/////////////////////////////////////////////////////////////////////////////
// CWinRefineShlExt
#define MyKey CString(_T("Software\\CrystalSoft\\WinRefine"))
struct PassThread
{
CWinRefineShlExt* pnt;
CComBSTR* FileName;
};
CWinRefineShlExt::CWinRefineShlExt()
{
Thumb = NULL;
}
CString CWinRefineShlExt::GetFileExt(CString m_sFilename)
{
m_sFilename = m_sFilename.Right(m_sFilename.GetLength() - (m_sFilename.ReverseFind('.') + 1));
m_sFilename.MakeLower();
return m_sFilename;
}
CString CWinRefineShlExt::GetFileName(CString m_sFilename)
{
return m_sFilename.Mid(m_sFilename.ReverseFind('\\') + 1);
}
typedef HRESULT (__stdcall* PsGetPropertyKeyFromName)(PCWSTR pszName, PROPERTYKEY* ppropkey);
HRESULT __stdcall GetPropertyKeyFromName(PCWSTR pszName, PROPERTYKEY* ppropkey)
{
HRESULT Hr = E_NOTIMPL;
HMODULE PropSys = LoadLibrary(_T("propsys.dll"));
if (PropSys)
{
PsGetPropertyKeyFromName f = (PsGetPropertyKeyFromName)GetProcAddress(PropSys, "PSGetPropertyKeyFromName");
if (f)
{
Hr = (f)(pszName, ppropkey);
}
FreeLibrary(PropSys);
}
return Hr;
}
CString* GetValueFromPropertyName(CString FilePath, CString PropertyName)
{
CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
LPMALLOC pMalloc;
CString* Return = new CString[2];
Return[0].Empty();
Return[1].Empty();
if (FAILED(SHGetMalloc(&pMalloc)))
{
return Return;
}
CString Path = FilePath.Left(FilePath.ReverseFind('\\') + 1);
CString Filename = FilePath.Mid(FilePath.ReverseFind('\\') + 1);
IPropertyUI* pID = NULL;
HRESULT Hr = CoCreateInstance(CLSID_PropertiesUI, NULL, CLSCTX_INPROC, IID_IPropertyUI, (void**)&pID);
if (SUCCEEDED(Hr))
{
IShellFolder* DesktopFolder = NULL;
if (SUCCEEDED(SHGetDesktopFolder(&DesktopFolder)))
{
LPITEMIDLIST PIDL = NULL;
if (SUCCEEDED(DesktopFolder->ParseDisplayName(NULL, NULL, Path.GetBuffer(), NULL, &PIDL, NULL)))
{
Path.ReleaseBuffer();
IShellFolder2* FileFolder = NULL;
if (SUCCEEDED(DesktopFolder->BindToObject(PIDL, NULL, IID_IShellFolder2, (void**)(&FileFolder))))
{
::CoTaskMemFree(PIDL);
PIDL = NULL;
if (SUCCEEDED(FileFolder->ParseDisplayName(NULL, NULL, Filename.GetBuffer(), NULL, &PIDL, NULL)))
{
Filename.ReleaseBuffer();
PROPERTYKEY PropKey;
ULONG Eated = 0;
HRESULT Hr = pID->ParsePropertyName(PropertyName, &PropKey.fmtid, &PropKey.pid, &Eated);
if (!SUCCEEDED(Hr))
{
Hr = GetPropertyKeyFromName(PropertyName, &PropKey);
}
if (SUCCEEDED(Hr))
{
pID->GetDisplayName(PropKey.fmtid, PropKey.pid, PUIFNF_DEFAULT, Return[0].GetBuffer(1024), 1024);
Return[0].ReleaseBuffer();
SHELLDETAILS ShellDetails;
int Column = -1;
TCHAR* temp = new TCHAR[1024];
for (int i = 0; true; i++)
{
if (SUCCEEDED(FileFolder->GetDetailsOf(NULL, i, &ShellDetails)))
{
*temp = 0;
::StrRetToStr(&ShellDetails.str, NULL, &temp);
if (!wcscmp(temp, Return[0]))
{
Column = i;
break;
}
}
}
if (Column >= 0)
{
if (SUCCEEDED(FileFolder->GetDetailsOf(PIDL, Column, &ShellDetails)))
{
*temp = 0;
::StrRetToStr(&ShellDetails.str, NULL, &temp);
Return[1] = temp;
}
}
pMalloc->Free(temp);
}
FileFolder->Release();
}
}
}
DesktopFolder->Release();
}
}
pMalloc->Release();
CoUninitialize();
return Return;
}
HRESULT CWinRefineShlExt::GetInfoTipFromFile(CWinRefineShlExt* pnt, wchar_t* file, LPTSTR pszText, int cchTextMax)
{
HRESULT fRet = S_FALSE;
*pszText = 0;
CString FileName = CString(file).Mid(CString(file).ReverseFind('\\') + 1);
CString FinalTip;
FinalTip.Empty();
DWORD propLen = 1024;
wchar_t* propToRead;
LPMALLOC pMalloc;
if (FAILED(SHGetMalloc(&pMalloc)))
{
return E_FAIL;
}
ULONG cbText = (propLen + 1) * sizeof(wchar_t);
propToRead = (wchar_t*)pMalloc->Alloc(cbText);
pMalloc->Release();
if (propToRead == NULL)
{
return E_OUTOFMEMORY;
}
IQueryAssociations* pQA = NULL;
if (SUCCEEDED(AssocCreate(CLSID_QueryAssociations, IID_IQueryAssociations, (void**)&pQA)) && SUCCEEDED(pQA->Init(NULL, file, NULL, NULL)))
{
*propToRead = 0;
if (!SUCCEEDED(pQA->GetString(NULL, ASSOCSTR_INFOTIP, NULL, propToRead, &propLen)))
{
CRegKey Reg;
if (Reg.Open(HKEY_CLASSES_ROOT, _T("*\0"), KEY_QUERY_VALUE) == ERROR_SUCCESS)
{
DWORD dwcbNeeded = 1024;
Reg.QueryStringValue(_T("InfoTip\0"), propToRead, &dwcbNeeded);
Reg.Close();
}
}
if (*propToRead)
{
if (!wcsncmp(propToRead, _T("prop:"), 5))
{
propToRead = propToRead + 5;
if (wcslen(FileName) > 15)
{
FinalTip.Format(_T("%s\r\n"), FileName);
}
int len = wcslen(propToRead);
for (int i = 0, z = 0; i < len; i++)
{
if ((propToRead[i] == ';') || ((propToRead[i] != ';') && ((i) == (len-1)) && i++))
{
CString Prop = CString(propToRead+z).Left(i - z);
z = i+1;
CString* Value = GetValueFromPropertyName(file, Prop);
if (!Value[1].IsEmpty())
{
FinalTip.Format(_T("%s%s: %s\r\n"), FinalTip, Value[0], Value[1]);
}
}
}
}
}
pQA->Release();
}
if (!FinalTip.IsEmpty())
{
FinalTip.Delete(FinalTip.GetLength()-2, 2);
lstrcpyn(pszText, FinalTip, cchTextMax);
return S_OK;
}
pMalloc->Free(propToRead);
return E_FAIL;
}
void CWinRefineShlExt::ShowDialog(CPoint Point, CString FileName, HWND Parent)
{
Thumb = new CThumbDialog(Point, FileName, Parent);
Thumb->Create(IDD_THUMB, CWnd::FromHandle(Parent));
}
void CWinRefineShlExt::CloseDialog()
{
Thumb->DestroyWindow();
delete Thumb;
Thumb = NULL;
}
bool PositionChanged(CPoint Point)
{
CPoint Point2;
GetCursorPos(&Point2);
int diff = 20;
if ((((Point2.x - Point.x) >= diff) || ((Point2.x - Point.x) <= -diff)) || (((Point2.y - Point.y) >= diff) || ((Point2.y - Point.y) <= -diff)))
{
return true;
}
return false;
}
//C:\project\Sourcecode\MyProg\Visual C++\Pubblici\WinRefine\ReleaseUMinSize
DWORD WINAPI CWinRefineShlExt::GeneralPreviewThread(LPVOID params)
{
PassThread* Ps = (PassThread*)params;
CRegKey Reg;
DWORD Latency = 0, Volume = 1000;
if (Reg.Open(HKEY_CURRENT_USER, MyKey, KEY_READ) == ERROR_SUCCESS)
{
Reg.QueryDWORDValue(_T("Latency\0"), Latency);
Reg.QueryDWORDValue(_T("Volume\0"), Volume);
Reg.Close();
}
Sleep(Latency);
CComBSTR m_sFilename = *Ps->FileName;
int Type = Ps->pnt->IsValidAudioVideo(m_sFilename) ? 0 : 1;
CPoint Point;
GetCursorPos(&Point);
HWND ListCtrl = WindowFromPoint(Point);
HWND Parent = GetMainWindow(ListCtrl);
bool ok = false;
MidiPlayer Pl(m_sFilename, _T("icon"));
if (!Type)
{
Pl.open();
Pl.setvolume(Volume);
ok = Pl.play();
}
else
{
Ps->pnt->ShowDialog(Point, m_sFilename.m_str, Parent);
ok = true;
}
if (ok)
{
while (!PositionChanged(Point))
{
Sleep(100);
}
if (!Type)
{
Pl.stop();
Pl.close();
}
else
{
Ps->pnt->CloseDialog();
}
}
Ps->pnt->Release();
return TRUE;
}
bool CWinRefineShlExt::CanGo(HWND ListCtrl, int Sel2)
{
return ((GetListSelectedItem(ListCtrl) != -1) && GetListSelectedItem(ListCtrl) == Sel2) ? false : true;
}
bool CWinRefineShlExt::IsValidAudioVideo(CComBSTR m_sFilename)
{
CString ext = GetFileExt(m_sFilename.m_str);
wchar_t Extensions[1024] = _T("mp3|wav|avi|mpg|mpeg|wma|mpa|vob|wmv|asf");
CRegKey Reg;
if (Reg.Open(HKEY_CURRENT_USER, MyKey, KEY_READ) == ERROR_SUCCESS)
{
DWORD dwcbNeeded = 1024;
Reg.QueryStringValue(_T("AudioVideo\0"), Extensions, &dwcbNeeded);
Reg.Close();
}
if (wcsstr(Extensions, ext))
{
return true;
}
return false;
}
bool CWinRefineShlExt::IsValidImage(CComBSTR m_sFilename)
{
CString ext = GetFileExt(m_sFilename.m_str);
wchar_t Extensions[1024] = _T("png|gif|bmp|jpg|jpeg|tiff|tif|psd|tga");
CRegKey Reg;
if (Reg.Open(HKEY_CURRENT_USER, MyKey, KEY_READ) == ERROR_SUCCESS)
{
DWORD dwcbNeeded = 1024;
Reg.QueryStringValue(_T("Image\0"), Extensions, &dwcbNeeded);
Reg.Close();
}
if (wcsstr(Extensions, ext))
{
return true;
}
return false;
}
void CWinRefineShlExt::PerformPreview(CComBSTR FileName)
{
PassThread* Ps = new PassThread;
Ps->pnt = this;
Ps->FileName = new CComBSTR(m_sFilename);
if (IsValidAudioVideo(m_sFilename) || IsValidImage(m_sFilename))
{
AddRef();
SHCreateThread(GeneralPreviewThread, Ps, CTF_COINIT | CTF_PROCESS_REF, NULL);
}
}
STDMETHODIMP CWinRefineShlExt::GetClassID(LPCLSID csid)
{
return E_NOTIMPL;
}
STDMETHODIMP CWinRefineShlExt::Load(LPCOLESTR wszFilename, DWORD dwMode)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); //Init MFC
UNREFERENCED_PARAMETER(dwMode);
m_sFilename = wszFilename;
PerformPreview(m_sFilename);
return S_OK;
}
STDMETHODIMP CWinRefineShlExt::GetInfoTip(DWORD dwFlags, LPWSTR* ppwszTip)
{
AFX_MANAGE_STATE(AfxGetStaticModuleState()); //Init MFC
UNREFERENCED_PARAMETER(dwFlags);
LPMALLOC pMalloc;
if (FAILED(SHGetMalloc(&pMalloc)))
{
return E_FAIL;
}
ULONG cbText = (1025) * sizeof(LPWSTR);
*ppwszTip = (LPWSTR)pMalloc->Alloc(cbText);
pMalloc->Release();
if (*ppwszTip == NULL)
{
return E_OUTOFMEMORY;
}
if (!SUCCEEDED(GetInfoTipFromFile(this, m_sFilename.m_str, *ppwszTip, 1024)))
{
wcscpy(*ppwszTip, m_sFilename.m_str);
}
else
{
if (IsValidImage(m_sFilename))
{
CRegKey Reg;
DWORD MinWidth = 100, MinHeigth = 100, Tooltip = 0;
if (Reg.Open(HKEY_CURRENT_USER, MyKey, KEY_READ) == ERROR_SUCCESS)
{
Reg.QueryDWORDValue(_T("MinWidth\0"), MinWidth);
Reg.QueryDWORDValue(_T("MinHeight\0"), MinHeigth);
Reg.QueryDWORDValue(_T("Tooltip\0"), Tooltip);
Reg.Close();
}
FREE_IMAGE_FORMAT FIFormat = FreeImage_GetFileType(CStringA(m_sFilename));
FIBITMAP* FBmp = FreeImage_Load(FIFormat, CStringA(m_sFilename), BMP_DEFAULT);
if (FBmp)
{
int Width = FreeImage_GetWidth(FBmp);
if (!((FreeImage_GetWidth(FBmp) < MinWidth) || (FreeImage_GetHeight(FBmp) < MinHeigth)))
{
if (!Tooltip)
{
*ppwszTip = 0;
}
}
FreeImage_Unload(FBmp);
}
}
}
return S_OK;
}