3,245
社区成员
发帖
与我相关
我的任务
分享
#include <exdispid.h>
#include "browser.h"
/**
* @brief Html显示视
*/
class CHtmlShowView:
public CWindowImpl<CHtmlShowView, CAxWindow>,
public CWebBrowser2<CHtmlShowView>
{
public:
HWND m_parentWnd;
CEdit *m_URL;
CMultiPaneStatusBarCtrl *m_StatusBar;
CListViewCtrl *m_PostListView;
CListViewCtrl *m_HeaderListView;
CRichEditCtrl *m_PostRowDataShow;
BOOL m_isAnalyer;
_bstr_t m_requestURL; //请求URL
string m_domain; //请求域
DECLARE_WND_SUPERCLASS(NULL, CAxWindow::GetWndClassName())
CHtmlShowView(void):m_parentWnd(NULL),m_URL(NULL),m_StatusBar(NULL),m_isAnalyer(FALSE)
{
AtlAxWinInit();
m_requestURL = "";
m_domain = "";
}
~CHtmlShowView(void){}
BOOL PreTranslateMessage(MSG* pMsg)
{
if((pMsg->message < WM_KEYFIRST || pMsg->message > WM_KEYLAST) &&
(pMsg->message < WM_MOUSEFIRST || pMsg->message > WM_MOUSELAST))
return FALSE;
return (BOOL)SendMessage(WM_FORWARDMSG, 0, (LPARAM)pMsg);
}
BEGIN_MSG_MAP(CHtmlShowView)
CHAIN_MSG_MAP(CWebBrowser2<CHtmlShowView>)
END_MSG_MAP()
BOOL StartHtml(LPCTSTR url)
{
Stop();
m_isAnalyer = TRUE;
CComPtr<IWebBrowser2> webBrowser2;
m_requestURL = "";
m_domain = "";
HRESULT hr=this->QueryControl(__uuidof(IWebBrowser2),(void **)&webBrowser2);
if(SUCCEEDED(hr))
webBrowser2->Navigate(_bstr_t(url),NULL,NULL,NULL,NULL);
return SUCCEEDED(hr);
}
//重载IE事件
BOOL OnNewWindow2(IDispatch** ppDisp)
{
return TRUE;
}
void OnDocumentComplete(IDispatch* pDisp, const CComBSTR& szURL)
{
IUnknown *pUnk = NULL;
HRESULT hr=this->QueryControl(__uuidof(IUnknown),(void **)&pUnk);
ATLASSERT(pUnk);
LPDISPATCH lpWBDisp = NULL;
hr = pUnk->QueryInterface(IID_IDispatch, (void**)&lpWBDisp);
ATLASSERT(SUCCEEDED(hr));
if (pDisp == lpWBDisp )
{
m_requestURL = "";
m_domain = "";
::PostMessage(m_parentWnd,MSG_SHOWTREE,(WPARAM)pDisp,(LPARAM)&szURL);
if(m_isAnalyer)
{
m_isAnalyer = FALSE;
::PostMessage(m_parentWnd,MSG_ANALYSER,(WPARAM)pDisp,(LPARAM)&szURL);
}
}
lpWBDisp->Release();
}
string GetDomainUrl(LPCSTR lpUrl)
{
string domain;
LPCSTR lpStart = ::StrStrI(lpUrl,"http://");
if(lpStart!=NULL)
{
lpStart+=7;
LPCSTR lpEnd = ::StrStrI(lpStart,"/");
if(lpEnd != NULL)
{
lpStart = ::StrStrI(lpStart,".");
if(lpStart != NULL && lpEnd != NULL)
{
if(lpStart < lpEnd)
{
domain.append((char *)lpStart,lpEnd-lpStart);
}
}
}
}
return domain;
}
BOOL OnBeforeNavigate2(IDispatch* pDisp, const CComBSTR& szURL, DWORD dwFlags, const CComBSTR& szTargetFrameName, string &postedData, const CComBSTR& szHeaders)
{
return FALSE;
}
void OnNavigateComplete2(IDispatch* pDisp, const CComBSTR& szURL)
{
ATLASSERT(m_URL!=NULL);
m_requestURL = _bstr_t(GetLocationURL());
m_URL->SetWindowText(m_requestURL.operator char *());
}
void OnStatusTextChange(const CComBSTR szText)
{
ATLASSERT(m_StatusBar!=NULL);
m_StatusBar->SetPaneText(ID_DEFAULT_PANE,_bstr_t(szText));
}
void OnProgressChange(long nProgress, long nProgressMax)
{
ATLASSERT(m_StatusBar!=NULL);
CString szText;
if (nProgressMax>0 && nProgress >0)
{
int percentage=(nProgress*100)/nProgressMax;
if(percentage >100)
percentage=100;
else if(percentage<0)
percentage=0;
szText.Format(_T("%d%%"),percentage);
}
m_StatusBar->SetPaneText(IDR_PROGRESS,szText);
}
};
void CXXHtmlView::OnDocumentComplete(LPCTSTR lpszURL)
{
CHtmlView::OnDocumentComplete(lpszURL);
// The document is not complete until all of its data has been retrieved
if(GetReadyState() != READYSTATE_COMPLETE) return;
// Do something useful
}