📄 htmlviewwin.h
字号:
/*
Copyright (c) 1999, kSet Lab
Author: Konstantin Bukreev
E-mail: konstantin@mail.primorye.ru
Created: 23.08.99 14:33:41
Version: 1.0.0
html view window
*/
#ifndef _HtmlViewWin_7715d762_5967_11d3_9284_0080adb811c5
#define _HtmlViewWin_7715d762_5967_11d3_9284_0080adb811c5
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000
#pragma warning (disable : 4244)
class HTMLViewWin :
public CWindowImpl <HTMLViewWin, CAxWindow>
{
public:
enum exStyleEnum {exFlat, ex3D, exBorder};
private:
CComPtr<IHTMLDocument2> m_pDoc;
CComPtr<IHTMLElement> m_pBody;
CComPtr<IHTMLBodyElement> m_pBodyElement;
const CComBSTR afterBegin;
const CComBSTR beforeEnd;
CComBSTR m_url;
CComBSTR m_file;
RECT m_rect;
public:
HTMLViewWin() : afterBegin(L"AfterBegin"), beforeEnd(L"BeforeEnd"), m_url(L""), m_file(L"")
{
memset(&m_rect, 0, sizeof RECT);
}
~HTMLViewWin ()
{
if (m_hWnd)
DestroyWindow();
}
DECLARE_WND_SUPERCLASS (0, _T("STATIC"))
BEGIN_MSG_MAP(HTMLView)
MESSAGE_HANDLER(WM_CREATE, OnCreate)
MESSAGE_HANDLER(WM_DESTROY, OnDestroy)
END_MSG_MAP()
public:
STDMETHOD(Create1)(HWND hWndParent, exStyleEnum eVal, LONG Left, LONG Top, LONG Width, LONG Height)
{
RECT rect = {Left, Top, Left + Width, Top + Height};
m_rect = rect;
HWND res = CWindowImpl <HTMLViewWin, CAxWindow>::Create ((HWND)hWndParent, rect, 0, WS_CHILD | WS_VISIBLE | (eVal & exBorder ? WS_BORDER : 0), (eVal & ex3D ? WS_EX_CLIENTEDGE : 0));
if (!res)
return E_FAIL;
return S_OK;
}
STDMETHOD(Resize)(short Left, short Top, short Width, short Height)
{
RECT rect = {Left, Top, Width, Height};
m_rect = rect;
if (HelperSetPos())
return S_OK;
return E_FAIL;
}
STDMETHOD(Clear)()
{
HRESULT hr = E_FAIL;
BSTR txt = SysAllocString(L"");
__try {hr = m_pBody->put_innerHTML(txt);}
__finally {SysFreeString(txt);}
return hr;
}
STDMETHOD(insertToFront)(BSTR html)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->insertAdjacentHTML(afterBegin, html);}
__except(1) {}
return hr;
}
STDMETHOD(insertToBack)(BSTR html)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->insertAdjacentHTML(beforeEnd, html);}
__except(1) {}
return hr;
}
STDMETHOD(get_HTML)(BSTR *pVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->get_innerHTML (pVal);}
__except(1) {}
return hr;
}
STDMETHOD(put_HTML)(BSTR newVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->put_innerHTML (newVal);}
__except(1) {}
return hr;
}
STDMETHOD(get_Text)(BSTR *pVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->get_innerText (pVal);}
__except(1) {}
return hr;
}
STDMETHOD(put_Text)(BSTR newVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBody->put_innerText (newVal);}
__except(1) {}
return hr;
}
STDMETHOD(get_Top)(short *pVal)
{
if (!pVal) return E_POINTER;
(*pVal) = m_rect.top;
return S_OK;
}
STDMETHOD(put_Top)(short newVal)
{
short old_value = m_rect.top;
m_rect.top = newVal;
if (HelperSetPos())
return S_OK;
m_rect.top = old_value;
return E_FAIL;
}
STDMETHOD(get_Left)(short *pVal)
{
if (!pVal) return E_POINTER;
(*pVal) = m_rect.left;
return S_OK;
}
STDMETHOD(put_Left)(short newVal)
{
short old_value = m_rect.left;
m_rect.left = newVal;
if (HelperSetPos())
return S_OK;
m_rect.left = old_value;
return E_FAIL;
}
STDMETHOD(get_Width)(short *pVal)
{
if (!pVal) return E_POINTER;
(*pVal) = m_rect.right;
return S_OK;
}
STDMETHOD(put_Width)(short newVal)
{
short old_value = m_rect.right;
m_rect.right = newVal;
if (HelperSetPos())
return S_OK;
m_rect.right = old_value;
return E_FAIL;
}
STDMETHOD(get_Height)(short *pVal)
{
if (!pVal) return E_POINTER;
(*pVal) = m_rect.bottom;
return S_OK;
}
STDMETHOD(put_Height)(short newVal)
{
short old_value = m_rect.bottom;
m_rect.bottom = newVal;
if (HelperSetPos())
return S_OK;
m_rect.bottom = old_value;
return E_FAIL;
}
STDMETHOD(get_fgColor)(VARIANT *pVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pDoc->get_fgColor (pVal);}
__except(1) {}
return hr;
}
STDMETHOD(put_fgColor)(VARIANT newVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pDoc->put_fgColor (newVal);}
__except(1) {}
return hr;
}
STDMETHOD(get_bgColor)(VARIANT *pVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pDoc->get_bgColor (pVal);}
__except(1) {}
return hr;
}
STDMETHOD(put_bgColor)(VARIANT newVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pDoc->put_bgColor (newVal);}
__except(1) {}
return hr;
}
STDMETHOD(get_Background)(BSTR *pVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBodyElement->get_background (pVal);}
__except(1) {}
return hr;
}
STDMETHOD(put_Background)(BSTR newVal)
{
HRESULT hr = E_FAIL;
__try {hr = m_pBodyElement->put_background (newVal);}
__except(1) {}
return hr;
}
STDMETHOD(get_URL)(BSTR *pVal)
{
if (!pVal) return E_POINTER;
*pVal = m_url.Copy();
return S_OK;
}
STDMETHOD(put_URL)(BSTR newVal)
{
CComPtr<IMoniker> pMnk;
CComPtr<IPersistMoniker> pPM;
HRESULT hr = CreateURLMoniker(0, newVal, &pMnk);
if (FAILED(hr)) return hr;
hr = m_pDoc.QueryInterface (&pPM);
if (FAILED(hr)) return hr;
hr = pPM->Load (true, pMnk, 0, 0);
if (FAILED(hr)) return hr;
m_url = newVal;
m_file = L"";
return hr;
}
STDMETHOD (get_File)(BSTR *pVal)
{
if (!pVal) return E_POINTER;
*pVal = m_file.Copy();
return S_OK;
}
STDMETHOD(put_File)(BSTR newVal)
{
CComPtr<IPersistFile> m_pF;
HRESULT hr = m_pDoc.QueryInterface (&m_pF);
if (FAILED(hr)) return hr;
hr = m_pF->Load (newVal, 0);
if (FAILED(hr)) return hr;
m_file = newVal;
m_url = L"";
return hr;
}
LRESULT OnCreate(UINT, WPARAM, LPARAM, BOOL&)
{
CComPtr<IUnknown> pUnk;
return HelperCreateDoc(pUnk);
}
LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
{
__try {m_pBody.Release ();} __except(1) {}
__try {m_pDoc.Release ();} __except(1) {}
return 0;
}
private:
//need for used SEH (thru warning 4509)
LRESULT HelperCreateDoc (CComPtr<IUnknown>& pUnk)
{
HRESULT hr = E_FAIL;
__try
{
CreateControlEx(L"{25336920-03F9-11cf-8FD0-00AA00686F13}", 0, 0, &pUnk);
pUnk.QueryInterface(&m_pDoc);
hr = m_pDoc->get_body(&m_pBody);
m_pBody.QueryInterface(&m_pBodyElement);
}
__except (1) {}
if (FAILED(hr) || !m_pBody)
return -1;
return 0;
}
bool HelperSetPos ()
{
return (MoveWindow (m_rect.left, m_rect.top, m_rect.right, m_rect.bottom) != 0);
}
};
#pragma warning (default : 4244)
#endif //_HtmlViewWin_7715d762_5967_11d3_9284_0080adb811c5
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -