📄 uihtmlview.cpp
字号:
//*******************************************************************************
// COPYRIGHT NOTES
// ---------------
// You may use this source code, compile or redistribute it as part of your application
// for free. You cannot redistribute it as a part of a software development
// library without the agreement of the author. If the sources are
// distributed along with the application, you should leave the original
// copyright notes in the source code without any changes.
// This code can be used WITHOUT ANY WARRANTIES at your own risk.
//
// For the latest updates to this code, check this site:
// http://www.masmex.com
// after Sept 2000
//
// Copyright(C) 2000 Philip Oldaker <email: philip@masmex.com>
//*******************************************************************************
// HtmlMsgView.cpp : implementation file
//
#include "stdafx.h"
#include "UIHtmlView.h"
#include "UIMessages.h"
#include <atlbase.h>
#include <mshtml.h>
#include "UIres.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUIHtmlView
IMPLEMENT_DYNAMIC(CUIHtmlView, CHtmlView)
CUIHtmlView::CUIHtmlView()
{
//{{AFX_DATA_INIT(CUIHtmlView)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
m_pHTMLDocument2 = NULL;
m_hNotifyWnd = NULL;
m_bSetCursor = false;
}
CUIHtmlView::~CUIHtmlView()
{
ReleaseDocument();
}
BOOL CUIHtmlView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Add your specialized code here and/or call the base class
cs.lpszClass = AfxRegisterWndClass(
CS_DBLCLKS,
NULL,
NULL,
NULL);
ASSERT(cs.lpszClass);
BOOL bRet = CHtmlView::PreCreateWindow(cs);
cs.dwExStyle |= WS_EX_CLIENTEDGE;
// cs.style |= WS_BORDER;
return bRet;
}
void CUIHtmlView::ReleaseDocument()
{
if (m_pHTMLDocument2)
{
m_pHTMLDocument2->Release();
m_pHTMLDocument2 = NULL;
}
}
void CUIHtmlView::DocumentReady()
{
m_bSetCursor = false;
}
bool CUIHtmlView::ExecScript(LPCTSTR pszScript,LPCTSTR pszLang,_variant_t *pvt)
{
bool bRet = false;
CWaitCursor w;
IHTMLWindow2 *pW2=NULL;
IHTMLDocument2 *pDoc = GetHTMLDocument();
if (pDoc == NULL)
return bRet;
HRESULT hr = pDoc->get_parentWindow(&pW2);
if (SUCCEEDED(hr))
{
if (pszLang == NULL)
pszLang = _T("JScript");
_variant_t v;
hr = pW2->execScript(_bstr_t(pszScript),_bstr_t(pszLang),&v);
if (pvt)
*pvt = v;
pW2->Release();
bRet= true;
}
return bRet;
}
CString CUIHtmlView::GetBodyText()
{
IHTMLElement *pElem=NULL;
GetHTMLDocument()->get_body(&pElem);
_bstr_t bstText;
BSTR bsText;
pElem->get_innerText(&bsText);
pElem->Release();
bstText = bsText;
return (LPCTSTR)bstText;
}
CString CUIHtmlView::GetElementValue(LPCTSTR pszElemID)
{
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
BSTR bsText;
_bstr_t bstText;
if (pElem)
{
IHTMLInputTextElement *pInputElem=NULL;
HRESULT hr = pElem->QueryInterface(IID_IHTMLInputTextElement,(LPVOID*)&pInputElem);
if (SUCCEEDED(hr))
{
pInputElem->get_value(&bsText);
bstText= bsText;
pInputElem->Release();
pInputElem = NULL;
}
pElem->Release();
}
return (LPCTSTR)bstText;
}
CString CUIHtmlView::GetElementText(LPCTSTR pszElemID)
{
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
if (pElem == NULL)
return _T("");
BSTR bsText;
_bstr_t bstText;
pElem->get_innerText(&bsText);
bstText = bsText;
pElem->Release();
pElem = NULL;
return (LPCTSTR)bstText;
}
CString CUIHtmlView::GetElementHTML(LPCTSTR pszElemID)
{
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
BSTR bsText;
_bstr_t bstText;
if (pElem)
{
pElem->get_innerHTML(&bsText);
bstText = bsText;
pElem->Release();
pElem = NULL;
}
return (LPCTSTR)bstText;
}
bool CUIHtmlView::SetElementValue(LPCTSTR pszElemID,LPCTSTR pszText)
{
bool bRet=false;
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
if (pElem)
{
IHTMLInputTextElement *pInputElem=NULL;
HRESULT hr = pElem->QueryInterface(IID_IHTMLInputTextElement,(LPVOID*)&pInputElem);
if (SUCCEEDED(hr))
{
pInputElem->put_value(_bstr_t(pszText));
pInputElem->Release();
pInputElem = NULL;
bRet = true;
}
pElem->Release();
pElem = NULL;
}
return bRet;
}
bool CUIHtmlView::SetElementText(LPCTSTR pszElemID,LPCTSTR pszText)
{
bool bRet=false;
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
if (pElem == NULL)
return bRet;
pElem->put_innerText(_bstr_t(pszText));
pElem->Release();
pElem = NULL;
bRet= true;
return bRet;
}
bool CUIHtmlView::SetElementHTML(LPCTSTR pszElemID,LPCTSTR pszText)
{
bool bRet=false;
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
if (pElem == NULL)
return bRet;
pElem->put_innerHTML(_bstr_t(pszText));
pElem->Release();
pElem = NULL;
bRet= true;
return bRet;
}
bool CUIHtmlView::SetImageSource(LPCTSTR pszElemID,LPCTSTR pszText)
{
bool bRet=false;
IHTMLElement *pElem=NULL;
GetElement(pszElemID,&pElem);
if (pElem == NULL)
return bRet;
IHTMLImgElement *pImgElem=NULL;
HRESULT hr = pElem->QueryInterface(IID_IHTMLImgElement,(LPVOID*)&pImgElem);
pElem->Release();
if (SUCCEEDED(hr))
{
pImgElem->put_src(_bstr_t(pszText));
pImgElem->Release();
bRet = true;
}
return bRet;
}
bool CUIHtmlView::GetOptionString(LPCTSTR pszElemID,CString &sText,CString &sValue)
{
IHTMLElement *pElement = NULL;
GetElement(pszElemID,&pElement);
bool bRet=false;
if (pElement == NULL)
return bRet;
IHTMLSelectElement *pSelElem=NULL;
HRESULT hr = pElement->QueryInterface(IID_IHTMLSelectElement,(LPVOID*)&pSelElem);
pElement->Release();
pElement = NULL;
if (FAILED(hr))
return bRet;
long nSelIndex=-1;
pSelElem->get_selectedIndex(&nSelIndex);
if (nSelIndex == -1)
{
pSelElem->Release();
return bRet;
}
IDispatch *pDisp=NULL;
_variant_t vtName(nSelIndex);
_variant_t vtIndex;
pSelElem->item(vtName,vtIndex,&pDisp);
IHTMLOptionElement *pOptElem=NULL;
hr = pDisp->QueryInterface(IID_IHTMLOptionElement,(LPVOID*)&pOptElem);
if (SUCCEEDED(hr))
{
_bstr_t bstValue;
BSTR bsValue;
pOptElem->get_value(&bsValue);
bstValue = bsValue;
sValue = (LPCTSTR)bstValue;
BSTR bsText;
_bstr_t bstText;
pOptElem->get_text(&bsText);
bstText = bsText;
sText = (LPCTSTR)bstText;
pOptElem->Release();
bRet=true;
}
if (pSelElem)
pSelElem->Release();
return bRet;
}
bool CUIHtmlView::SetOptionString(LPCTSTR pszElemID,LPCTSTR pszText)
{
IHTMLElement *pElement = NULL;
GetElement(pszElemID,&pElement);
bool bRet=false;
if (pElement == NULL)
return bRet;
IHTMLSelectElement *pSelElem=NULL;
IDispatch *pDisp=NULL;
HRESULT hr = pElement->QueryInterface(IID_IHTMLSelectElement,(LPVOID*)&pSelElem);
if (FAILED(hr))
goto SOS_CleanUp;
{
long nLength=0;
pSelElem->get_length(&nLength);
for(long i=0;i < nLength;i++)
{
_variant_t vtName(i);
_variant_t vtIndex;
pSelElem->item(vtName,vtIndex,&pDisp);
if (pDisp == NULL)
continue;
IHTMLOptionElement *pOptElem=NULL;
hr = pDisp->QueryInterface(IID_IHTMLOptionElement,(LPVOID*)&pOptElem);
pDisp->Release();
pDisp = NULL;
if (SUCCEEDED(hr))
{
_bstr_t bstValue;
_bstr_t bstText;
BSTR bsValue;
BSTR bsText;
pOptElem->get_value(&bsValue);
pOptElem->get_text(&bsText);
bstValue = bsValue;
bstText = bsText;
pOptElem->Release();
if (_tcsicmp((LPCTSTR)bstText,pszText) == 0)
{
pSelElem->put_selectedIndex(i);
bRet=true;
break;
}
}
}
}
SOS_CleanUp:
if (pElement)
pElement->Release();
if (pSelElem)
pSelElem->Release();
return bRet;
}
bool CUIHtmlView::AddOptionString(LPCTSTR pszElemID,LPCTSTR pszText,LPCTSTR pszValue,bool bSelect)
{
IHTMLElement *pElement = NULL;
GetElement(pszElemID,&pElement);
bool bRet=false;
if (pElement == NULL)
return bRet;
IHTMLSelectElement *pSelElem=NULL;
HRESULT hr = pElement->QueryInterface(IID_IHTMLSelectElement,(LPVOID*)&pSelElem);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -