IWebBrowser2 사용 시 현재 포커스가 있는 Element를 얻어오는 코드 조각
HRESULT ClassName::GetActiveElement(IHTMLElement** element0)
HRESULT ClassName::GetActiveElement(IHTMLElement** element0)
{
if (!element0) return E_INVALIDARG;
HRESULT hr = E_FAIL;
if (*element0) *element0 = NULL;
CComQIPtr<IDispatch> dispatch;
hr = (*this)->get_Document(&dispatch);
if (SUCCEEDED(hr) && dispatch)
{
CComQIPtr<IHTMLDocument2> document2(dispatch);
dispatch.Release();
while (document2)
{
CComQIPtr<IHTMLElement> element;
hr = document2->get_activeElement(&element);
document2.Release();
if (SUCCEEDED(hr))
{
CComBSTR tagname;
element->get_tagName(&tagname);
tagname.ToUpper();
if (tagname == _T("FRAME") || tagname == _T("IFRAME"))
{
CComQIPtr<IWebBrowser2> webBrowser(element);
if (webBrowser)
{
hr = webBrowser->get_Document(&dispatch);
dispatch->QueryInterface(IID_IHTMLDocument2, (void**)&document2);
dispatch.Release();
}
}
else
{
*element0 = element.Detach();
}
}
}
}
return hr;
}