class IE
{
public:
static void Back(const TCHAR* tszSubWindowText);
static void Navigate(const TCHAR* tszSubWindowText, const TCHAR* tszDestUrl);
private:
IE()
{}
// Find Explorer_Server Window - subfunction of _Findwindow()
static HWND _Find_Explorer_Server(HWND hParentWnd, HWND hChildWnd);
// Find IE Window
static HWND _FindWindow(const TCHAR* tszSubWindowText);
static HWND _FindWindowWithSubWindowText(const TCHAR* tszSubWindowText);
private:
static HWND m_hWnd;
};// class IE
HWND IE::_Find_Explorer_Server(HWND hParentWnd, HWND hChildWnd)
{
if (hParentWnd == 0 || hChildWnd == 0) return 0;
TCHAR tszClassName[1024] = {0, };
::GetClassName(hChildWnd, tszClassName, 1024);
if (tstring(tszClassName) == _T("Internet Explorer_Server")) return hChildWnd;
HWND hRetWnd = _Find_Explorer_Server(hChildWnd, ::FindWindowEx(hChildWnd, 0, 0, 0));
if (hRetWnd != 0) return hRetWnd;
return _Find_Explorer_Server(hParentWnd, ::FindWindowEx(hParentWnd, hChildWnd, 0, 0));
}
HWND IE::_FindWindow(const TCHAR* tszSubWindowText)
{
HWND hWnd = _FindWindowWithSubWindowText(tszSubWindowText);
return _Find_Explorer_Server(hWnd, ::FindWindowEx(hWnd, 0, 0, 0));
}
HWND IE::_FindWindowWithSubWindowText(const TCHAR* tszSubWindowText)
{
HWND hWnd = 0;
while (hWnd = ::FindWindowEx(0, hWnd, _T("IEFrame"), 0))
{
TCHAR tszWindowText[1024] = {0, };
::GetWindowText(hWnd, tszWindowText, 1024);
size_t pos = tstring(tszWindowText).find(tszSubWindowText);
if (pos != tstring::npos) return hWnd;
}
return 0;
}
void IE::Back(const TCHAR* tszSubWindowText)
{
m_hWnd = _FindWindowWithSubWindowText(tszSubWindowText);
if (m_hWnd)
{
::SetForegroundWindow(m_hWnd);
m_hWnd = _FindWindow(tszSubWindowText);
HRESULT hr;
if (FAILED(hr = CoInitialize(NULL))) return;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
::SendMessageTimeout(m_hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes);
CComPtr<IHTMLDocument2> spDoc;
CComPtr<IHTMLWindow2> spWnd2;
CComPtr<IServiceProvider> spServiceProv;
hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (void**)&spDoc);
if (SUCCEEDED(hr))
{
hr = spDoc->get_parentWindow((IHTMLWindow2**)&spWnd2);
if (SUCCEEDED(hr))
{
hr = spWnd2->QueryInterface(IID_IServiceProvider, (void**)&spServiceProv);
if (SUCCEEDED(hr))
{
IWebBrowser2* pWebBrowser2 = 0;
hr = spServiceProv->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&pWebBrowser2);
hr = pWebBrowser2->GoBack();
pWebBrowser2->Release();
spServiceProv.Release();
}
spWnd2.Release();
}
spDoc.Release();
}
CoUninitialize();
}
}
void IE::Navigate(const TCHAR* tszSubWindowText, const TCHAR* tszDestUrl)
{
m_hWnd = _FindWindowWithSubWindowText(tszSubWindowText);
if (m_hWnd)
{
::SetForegroundWindow(m_hWnd);
m_hWnd = _FindWindow(tszSubWindowText);
HRESULT hr;
if (FAILED(hr = CoInitialize(NULL))) return;
LRESULT lRes;
UINT nMsg = ::RegisterWindowMessage(_T("WM_HTML_GETOBJECT"));
::SendMessageTimeout(m_hWnd, nMsg, 0L, 0L, SMTO_ABORTIFHUNG, 1000, (DWORD*)&lRes);
CComPtr<IHTMLDocument2> spDoc;
CComPtr<IHTMLWindow2> spWnd2;
CComPtr<IServiceProvider> spServiceProv;
hr = ::ObjectFromLresult(lRes, IID_IHTMLDocument2, 0, (void**)&spDoc);
if (SUCCEEDED(hr))
{
hr = spDoc->get_parentWindow((IHTMLWindow2**)&spWnd2);
if (SUCCEEDED(hr))
{
hr = spWnd2->QueryInterface(IID_IServiceProvider, (void**)&spServiceProv);
if (SUCCEEDED(hr))
{
IWebBrowser2* pWebBrowser2 = 0;
hr = spServiceProv->QueryService(IID_IWebBrowserApp, IID_IWebBrowser2, (void**)&pWebBrowser2);
CComVariant v;
CComBSTR url(tszDestUrl);
hr = pWebBrowser2->Navigate(url, &v, &v, &v, &v);
pWebBrowser2->Release();
spServiceProv.Release();
}
spWnd2.Release();
}
spDoc.Release();
}
CoUninitialize();
}
}