-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathBrowserHelperObject.cpp
217 lines (183 loc) · 5.23 KB
/
BrowserHelperObject.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
/*
* BrowserHelperObject.cpp: Manages general/background tasks for Explorer.
*/
#include "stdafx.h"
#include "framework.h"
#include "resource.h"
#include "ClassicExplorer_i.h"
#include "dllmain.h"
#include <commoncontrols.h>
#include "util.h"
#include "shell_undoc.h"
#include "BrowserHelperObject.h"
void BrowserHelperObject::Cleanup()
{
m_pSite = NULL;
m_parentWindow = NULL;
//UnhookWindowsHookEx(m_hHook);
}
HRESULT BrowserHelperObject::_DoUpdateWatermark(HWND listView)
{
HRESULT hr = S_OK;
int resourceId = 0;
bool shouldUse = false;
CComPtr<IShellView> pView;
m_pShellBrowser->QueryActiveShellView(&pView);
CComPtr<IFolderType> pType;
pView->QueryInterface(IID_IFolderType, (void **)&pType);
FOLDERTYPEID curFolderType;
pType->GetFolderType(&curFolderType);
if (curFolderType == FOLDERTYPEID_Pictures)
{
resourceId = IDB_BG_PICTURES_BLUE;
shouldUse = true;
}
else if (curFolderType == FOLDERTYPEID_Music)
{
resourceId = IDB_BG_MUSIC_BLUE;
shouldUse = true;
}
else if (curFolderType == FOLDERTYPEID_Videos)
{
resourceId = IDB_BG_VIDEOS_BLUE;
shouldUse = true;
}
if (shouldUse)
{
HBITMAP bitmap = LoadBitmapW(
_AtlBaseModule.GetResourceInstance(),
MAKEINTRESOURCEW(resourceId)
);
LVBKIMAGEW bkImage = { 0 };
bkImage.ulFlags = LVBKIF_TYPE_WATERMARK;
bkImage.xOffsetPercent = 100;
bkImage.yOffsetPercent = 100;
bkImage.hbm = bitmap;
if (!SendMessageW(listView, LVM_SETBKIMAGEW, NULL, (LPARAM)&bkImage))
{
DeleteObject(bitmap);
WCHAR buffer[128];
swprintf_s(buffer, L"Failed to apply background image on HWND: %x", listView);
MessageBoxW(NULL, buffer, L"Fuck you", MB_OK);
}
}
else
{
// Remove the background
SendMessageW(listView, LVM_SETBKIMAGEW, NULL, NULL);
}
return hr;
}
HRESULT BrowserHelperObject::UpdateWatermark()
{
bool found = false;
EnumChildWindows(m_parentWindow, [](HWND hWnd, LPARAM lParam) -> BOOL CALLBACK {
BrowserHelperObject *pThis = (BrowserHelperObject *)lParam;
WCHAR className[MAX_PATH];
if (GetClassName(hWnd, (LPWSTR)&className, MAX_PATH) && (_wcsicmp(className, L"SysListView32") == 0))
{
HWND parent = GetParent(hWnd);
if (parent)
{
WCHAR className2[MAX_PATH];
GetClassNameW(parent, (LPWSTR)&className2, MAX_PATH);
if (wcscmp(className2, L"SHELLDLL_DefView") != 0)
{
// not the right ListView
return TRUE;
}
}
pThis->_DoUpdateWatermark(hWnd);
return FALSE;
}
return TRUE;
}, (LPARAM)this);
if (!found)
{
// hmm
}
return S_OK;
}
LRESULT CALLBACK BrowserHelperObject::s_HookProc(int nCode, WPARAM wParam, LPARAM lParam)
{
if (nCode == HCBT_CREATEWND)
{
HWND hWnd = (HWND)wParam;
CBT_CREATEWNDW *pCreate = (CBT_CREATEWNDW *)lParam;
}
return CallNextHookEx(NULL, nCode, wParam, lParam);
}
//================================================================================================================
// implement IObjectWithSite:
//
/*
* SetSite: Responsible for installation or removal of the toolbar band from a location provided
* by the Shell.
*
* This function is additionally responsible for obtaining the shell control APIs and creating
* the actual toolbar control window.
*/
STDMETHODIMP BrowserHelperObject::SetSite(IUnknown *pUnkSite)
{
IObjectWithSiteImpl<BrowserHelperObject>::SetSite(pUnkSite);
// If pUnkSite is not NULL, then the site is being set.
// Otherwise, the site is being removed.
if (pUnkSite) // hook:
{
CComQIPtr<IServiceProvider> pProvider = pUnkSite;
// Set Windows hooks for additional things:
//m_hHook = SetWindowsHookExW(WH_CBT, s_HookProc, NULL, GetCurrentThreadId());
if (pProvider)
{
CComPtr<IShellBrowser> pShellBrowser;
pProvider->QueryService(SID_SShellBrowser, IID_IShellBrowser, (void **)&m_pShellBrowser);
pProvider->QueryService(SID_SWebBrowserApp, IID_IWebBrowser2, (void **)&m_pWebBrowser);
if (m_pWebBrowser)
{
if (m_dwEventCookie == 0xFEFEFEFE)
{
DispEventAdvise(m_pWebBrowser, &DIID_DWebBrowserEvents2);
}
}
HWND parentWindow = NULL;
if (SUCCEEDED(m_pShellBrowser->GetWindow(&parentWindow)))
{
m_parentWindow = GetAncestor(parentWindow, GA_ROOT);
}
else
{
MessageBox(NULL, NULL, L"could not get parent window", MB_OK);
}
}
}
else // unhook:
{
Cleanup();
}
return S_OK;
}
//================================================================================================================
// handle DWebBrowserEvents2:
//
STDMETHODIMP BrowserHelperObject::OnNavigateComplete(IDispatch *pDisp, VARIANT *url)
{
UpdateWatermark();
return S_OK;
}
/**
* OnQuit: Called when the user attempts to quit the Shell browser.
*
* This detaches the event listener we installed in order to listen for navigation
* events.
*
* Copied from Open-Shell implementation here:
* https://github.com/Open-Shell/Open-Shell-Menu/blob/master/Src/ClassicExplorer/ExplorerBand.cpp#L2280-L2285
*/
STDMETHODIMP BrowserHelperObject::OnQuit()
{
if (m_pWebBrowser && m_dwEventCookie != 0xFEFEFEFE)
{
return DispEventUnadvise(m_pWebBrowser, &DIID_DWebBrowserEvents2);
}
return S_OK;
}