⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 fixedhtmlview.cpp

📁 用VC写的浏览器
💻 CPP
📖 第 1 页 / 共 2 页
字号:
	//
	VARIANT* vtPostedData = V_VARIANTREF(PostData);
	BOOL array = FALSE;
	if (V_VT(vtPostedData) & VT_ARRAY)
	{
		// must be a vector of bytes
		ASSERT(vtPostedData->parray->cDims == 1 && vtPostedData->parray->cbElements == 1);
		vtPostedData->vt |= VT_UI1;
		COleSafeArray safe(vtPostedData);
		DWORD dwSize = safe.GetOneDimSize();
		array = dwSize>0?TRUE:FALSE;
	}
	// make real parameters out of the notification
	DWORD nFlags = V_I4(Flags);
	// notify the user's class
	OnBeforeNavigate2(m_strUrl, nFlags, "", array, "", Cancel);

	}catch(...){}
}

void CFixedHtmlView::DocumentComplete(LPDISPATCH pDisp, VARIANT* URL)
{
	try
	{
		//	UNUSED_ALWAYS(pDisp);
		ASSERT(V_VT(URL) == VT_BSTR);
		//is download complete
		if (m_pDisp && m_pDisp == pDisp)
		{
			// if the LPDISPATCH are same, that means
			// it is the final DocumentComplete. Reset glpDisp
			TRACE("Document is done downloading");
			m_pDisp = NULL;
		}
		m_strUrl = V_BSTR(URL);
		OnDocumentComplete(m_strUrl);
	}catch(...){}
}

/////////////////////////////////////////////////////////////////////////////
// CFixedHtmlView Events

void CFixedHtmlView::OnProgressChange(long lProgress, long lProgressMax)
{
	// user will override to handle this notification
	UNUSED_ALWAYS(lProgress);
	UNUSED_ALWAYS(lProgressMax);
}

void CFixedHtmlView::OnCommandStateChange(long lCommand, BOOL bEnable)
{
	// user will override to handle this notification
	UNUSED_ALWAYS(lCommand);
	UNUSED_ALWAYS(bEnable);
}

void CFixedHtmlView::OnTitleChange(LPCTSTR lpszText)
{
	// user will override to handle this notification
	UNUSED_ALWAYS(lpszText);
}

void CFixedHtmlView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* bCancel)
{
	// default to continuing
	*bCancel = FALSE;
	// user will override to handle this notification
	UNUSED_ALWAYS(ppDisp);
}

void CFixedHtmlView::OnDocumentComplete(LPCTSTR lpszURL)
{
	// user will override to handle this notification
	UNUSED_ALWAYS(lpszURL);
}

void CFixedHtmlView::OnNavigateComplete2(LPCTSTR lpszURL)
{
	// user will override to handle this notification
	UNUSED_ALWAYS(lpszURL);
}

void CFixedHtmlView::OnBeforeNavigate2(LPCTSTR lpszURL, DWORD nFlags,
	LPCTSTR lpszTargetFrameName, BOOL baPostData,
	LPCTSTR lpszHeaders, BOOL* bCancel)
{
	// default to continuing
	*bCancel = FALSE;

	// user will override to handle this notification
	UNUSED_ALWAYS(lpszURL);
	UNUSED_ALWAYS(nFlags);
	UNUSED_ALWAYS(lpszTargetFrameName);
	UNUSED_ALWAYS(baPostData);
	UNUSED_ALWAYS(lpszHeaders);
}

void CFixedHtmlView::OnStatusTextChange(LPCTSTR pszText)
{
}

BOOL CFixedHtmlView::Create(LPCTSTR lpszClassName, LPCTSTR lpszWindowName, DWORD dwStyle, const RECT& rect, CWnd* pParentWnd, UINT nID, CCreateContext* pContext) 
{
	if (!CView::Create(lpszClassName, lpszWindowName,dwStyle, rect, pParentWnd,  nID, pContext))
		return FALSE;

	RECT rectClient;
	if(g_bMax==1)
	{
		::GetClientRect(pmf->m_hWndMDIClient, &rectClient);
		rectClient.top -= 2;
		rectClient.left -= 2;
		rectClient.bottom += 2;
		rectClient.right += 2;
	}
	else
	{
		rectClient.top = rect.top - 2;
		rectClient.left = rect.left - 2;
		rectClient.bottom = rect.bottom + 2;
		rectClient.right = rect.right + 2;
	}

	// create the control window
	// AFX_IDW_PANE_FIRST is a safe but arbitrary ID
	if (!m_wndBrowser.CreateControl(CLSID_WebBrowser, lpszWindowName,
				WS_VISIBLE | WS_CHILD , rectClient, this, IDC_BROWSER_CONTROL))
	{
		DestroyWindow();
		return FALSE;
	}

	LPUNKNOWN lpUnk = m_wndBrowser.GetControlUnknown();
	HRESULT hr = lpUnk->QueryInterface(IID_IWebBrowser2, (void**) &m_pBrowserApp);
	if (!SUCCEEDED(hr))
	{
		m_pBrowserApp = NULL;
		m_wndBrowser.DestroyWindow();
		DestroyWindow();
		return FALSE;
	}

	return TRUE;
}

BOOL CFixedHtmlView::PreTranslateMessage(MSG* pMsg)
{
	ASSERT(pMsg != NULL);
	ASSERT_VALID(this);
	ASSERT(m_hWnd != NULL);

	try
	{
	if(pMsg == NULL || m_hWnd == NULL)
		return TRUE;
	//do key
	if (pMsg->wParam==VK_RETURN)
	{
		//initialization value must be WM_KEYUP
		static int nCharState=WM_KEYUP;
		//when keyup, ctrl key maybe have release, so remember when keydown
		static BOOL bPressCtrl=0;
		if(pMsg->message == WM_KEYDOWN)
		{
			nCharState = WM_KEYDOWN;
			bPressCtrl = PRESS_CTRL;
			//
			QueryPerformanceCounter(&m_tLastClick);
			m_bRClick = FALSE;
			m_bLClick = TRUE;
		}
		else if(pMsg->message == WM_CHAR)
			nCharState = WM_CHAR;
		else if(pMsg->message == WM_KEYUP)
		{
			//the right order is down,char,up, but sometime maybe no char, add one
			if (nCharState==WM_KEYDOWN && !bPressCtrl)
				pMsg->message = WM_CHAR;
			//
			nCharState = WM_KEYUP;
		}
	}
 	if (pMsg->message == WM_KEYDOWN)
	{
		switch(pMsg->wParam)
		{
		case VK_LEFT:
			if (PRESS_CTRL && GetFocusType()==0)
			{
				pmf->PostMessage(WM_COMMAND, ID_TAB_PRE, 0);
				return TRUE;
			}
			break;

		case VK_RIGHT:
			if (PRESS_CTRL && GetFocusType()==0)
			{
				pmf->PostMessage(WM_COMMAND, ID_TAB_NEXT, 0);
				return TRUE;
			}
			break;
		}
	}
	//do mouse
	else if (pMsg->message == WM_RBUTTONUP)
	{
		if (pmf->m_bNotShowRightMenu)
		{
			pmf->m_bNotShowRightMenu = FALSE;
			return TRUE;
		}
		if (pmf->m_nLeftRight && LOWORD(pMsg->wParam)==MK_LBUTTON)
		{
			m_tLastRClick.LowPart = 0;//??
			((CGreenBrowserView*)this)->MouseAction(pmf->m_nLeftRight);
			return TRUE;
		}
	}
	else if (pMsg->message == WM_LBUTTONUP)
	{
		//长时间按下链接后松开,新开的链接会被过滤
		QueryPerformanceCounter(&m_tLastClick);
		m_bLClick = TRUE;
		((CGreenBrowserView*)this)->m_strOpenNewUrl.Empty();
		if (pmf->m_nRightLeft && LOWORD(pMsg->wParam)==MK_RBUTTON)
		{
			m_tLastClick.LowPart = 0;//??
			((CGreenBrowserView*)this)->MouseAction(pmf->m_nRightLeft);
			if (!pmf->m_bNotShowRightMenu)
			{
				((CGreenBrowserView*)this)->m_bIsCapture = FALSE;
				ReleaseCapture();
				pmf->m_bNotShowRightMenu = TRUE;
			}
			return TRUE;
		}
	}
	else if(pMsg->message==WM_MOUSEWHEEL)
	{
		if (pmf->m_bWheelTab && LOWORD(pMsg->wParam)==MK_RBUTTON)
		{
			short zDelta = HIWORD(pMsg->wParam);
			if ( zDelta > 0)
				((CGreenBrowserView*)this)->MouseAction(ID_TAB_PRE);
			else
				((CGreenBrowserView*)this)->MouseAction(ID_TAB_NEXT);
			//
			if (!pmf->m_bNotShowRightMenu)
			{
				((CGreenBrowserView*)this)->m_bIsCapture = FALSE;
				ReleaseCapture();
				pmf->m_bNotShowRightMenu = TRUE;
			}
			return TRUE;
		}
	}
	//这里不要用 up 消息,因为down时光标就变了
	else if(pMsg->message == WM_MBUTTONDOWN && pmf->m_nMButtonDown)//??
	{
		if ( ((CGreenBrowserView*)this)->MouseAction(pmf->m_nMButtonDown))
		{
			if (pmf->m_nMButtonDown>=ID_OPEN_LINK && pmf->m_nMButtonDown<=ID_OPEN_LINK_DEACTIVE)
			{
				//因为模拟的是 shift+click 
				QueryPerformanceCounter(&m_tLastClick);
				m_bLClick = TRUE;
			}
			return TRUE;
		}
	}
	else if(pMsg->message == WM_LBUTTONDBLCLK && pmf->m_nLDbClick)
	{
		((CGreenBrowserView*)this)->MouseAction(pmf->m_nLDbClick);
		return TRUE;
	}
	}catch(...){}

	if (CFormView::PreTranslateMessage(pMsg))
		return TRUE;

	return FALSE;
}


BOOL CFixedHtmlView::PreCreateWindow(CREATESTRUCT& cs)
{
	// TODO: Modify the Window class or styles here by modifying
	//  the CREATESTRUCT cs

    cs.lpszClass = AfxRegisterWndClass(
       CS_DBLCLKS,                       // if you need double-clicks
       NULL,                             // no cursor (use default)
       NULL,                             // no background brush
       NULL); // app icon
    ASSERT(cs.lpszClass);
 
	if( !CFormView::PreCreateWindow(cs) )
		return FALSE;

	return TRUE;
}

void CFixedHtmlView::OnParentNotify(UINT message, LPARAM lParam) 
{
	CFormView::OnParentNotify(message, lParam);
	
	// TODO: Add your message handler code here
	try{
	if(message == WM_DESTROY)
	{
		CFrameWnd* pFrame = GetParentFrame();
		if (pFrame!=NULL && pFrame!=pmf)
		{
			m_bToClose = TRUE;
			pFrame->PostMessage(WM_CLOSE);
		}
	}
	}catch(...){}
}

int CFixedHtmlView::SetHistory(LPCSTR strCurUrl)
{
	if(!m_bIniTravelLog)
	{
		//not initialize at startup
		if(strcmp(strCurUrl, "about:blank")==0)
			return 0;

		//load ITravelLogStg for IE 5.5
		m_pITravelLog = NULL;
		IServiceProvider* pISP = NULL;
		LPDISPATCH pDisp = GetApplication();
		if(pDisp!=NULL)
		{
			HRESULT hr = pDisp->QueryInterface(IID_IServiceProvider, (void**)&pISP);
			if(SUCCEEDED(hr) && pISP!=NULL) 
			{
				hr = pISP->QueryService(SID_STravelLogCursor , IID_ITravelLogStg, (void**)&m_pITravelLog);
				if(FAILED(hr))
					m_pITravelLog=NULL;
				pISP->Release();
			}
			pDisp->Release();
		}
		m_bIniTravelLog = TRUE;
		if(m_pITravelLog!=NULL)
			return 0;
	}

	short p = m_nHistoryLength;
	int seth = 0;
	LPDISPATCH pDisp = NULL;
	IHTMLDocument2 *pHTMLDoc = NULL;
	IHTMLWindow2 *pHTMLWnd = NULL;
	IOmHistory  *pHistory = NULL;

	try{
		//get history length
		pDisp = GetHtmlDocument();
		if( pDisp )
		{
			if(SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDoc)))
			{
				if(SUCCEEDED(pHTMLDoc->get_parentWindow( &pHTMLWnd )))
				{
					if(SUCCEEDED(pHTMLWnd->get_history( &pHistory )))
					{
						if(pHistory->get_length(&p)==S_OK)
						{
							if(p>0 && p!=m_nHistoryLength)
							{
								if(m_strLastTitle.IsEmpty())
									m_strLastTitle = m_strLastURL;
								if(p>m_nHistoryLength)
								{
									m_astrHistory.SetAtGrow(p-1, m_strLastTitle);
									m_astrHistoryURL.SetAtGrow(p-1, m_strLastURL);
								}
								else
								{
									m_astrHistory.SetAt(p-1, m_strLastTitle);
									m_astrHistory.SetSize(p);
									if(m_nHistoryLength - p >MAX_HISTORY)
										m_astrHistory.FreeExtra();

									m_astrHistoryURL.SetAt(p-1, m_strLastURL);
									m_astrHistoryURL.SetSize(p);
									if(m_nHistoryLength - p >MAX_HISTORY)
										m_astrHistoryURL.FreeExtra();
								}
								m_nCurHistory = p;
								seth = 1;
							}
							else if(p==0)
								m_nCurHistory = 0;
							//adjust cur history
							m_nCurHistory -= m_nBackSteps;
							if(!m_bBack)
								m_nCurHistory = 0;
							else if(!m_bForward && seth && m_nBackSteps>=0)
								m_nCurHistory = p;
							else if(m_nCurHistory>=0 && m_nCurHistory<p && strCurUrl == m_astrHistoryURL.GetAt(m_nCurHistory))
								m_nCurHistory = m_nCurHistory;
							else if(m_nCurHistory-1>=0 && strCurUrl == m_astrHistoryURL.GetAt(m_nCurHistory-1))
								m_nCurHistory --;
							else if(m_nCurHistory-2>=0 && strCurUrl  == m_astrHistoryURL.GetAt(m_nCurHistory-2))
								m_nCurHistory -=2;
							else if(m_nCurHistory+1<p && strCurUrl == m_astrHistoryURL.GetAt(m_nCurHistory+1))
								m_nCurHistory ++;
							else if(m_nCurHistory+2<p && strCurUrl  == m_astrHistoryURL.GetAt(m_nCurHistory+2))
								m_nCurHistory +=2;

							if(m_bForward && m_nCurHistory>=p-1)
								m_nCurHistory = p-2;
							if(m_bBack && m_nCurHistory==0)
								m_nCurHistory =1;
								
							m_nHistoryLength = p;
						}
					}
				}
			}
		}
	}catch(...){}

	try{
		if(pHistory != NULL)
			pHistory->Release();
		if(pHTMLWnd != NULL)
			pHTMLWnd->Release();
		if(pHTMLDoc != NULL)
			pHTMLDoc->Release();
		if(pDisp != NULL)
			pDisp->Release();
	}catch(...){}
	
	m_nBackSteps = 0;
	return seth;
}

/////////////////////////////////////////////////////////////////////////////
// Pre-startup code

/*
#ifdef AFX_INIT_SEG
#pragma code_seg(AFX_INIT_SEG)
#endif
*/

//return value means 0: not edit 1:single line 2:mult-line
int CFixedHtmlView::GetFocusType()
{
	int nFocusType=0;
	LPDISPATCH pDisp = NULL;
    IHTMLDocument2 *pHTMLDoc=NULL;
	IHTMLElement *focus=NULL;
	IHTMLTextElement *text=NULL;
	IHTMLTextAreaElement *textarea=NULL;

	try
	{
		pDisp = GetHtmlDocument();
		if( pDisp )
		{
			if(SUCCEEDED(pDisp->QueryInterface(IID_IHTMLDocument2, (void**)&pHTMLDoc)))
			{
				if (SUCCEEDED(pHTMLDoc->get_activeElement(&focus)))
				{
					if (SUCCEEDED(focus->QueryInterface(IID_IHTMLInputTextElement,(void**)&text)))
						nFocusType = 1;
					else if (SUCCEEDED(focus->QueryInterface(IID_IHTMLTextAreaElement,(void**)&textarea)))
						nFocusType = 2;
				}
			}
		}
	}catch(...){}
	//
	try{
		RELEASE(text)
		RELEASE(textarea)
		RELEASE(focus)
		RELEASE(pHTMLDoc)
		RELEASE(pDisp)
	}catch(...){}
	//
	return nFocusType;
}

BOOL CFixedHtmlView::GetBusy()
{
	VARIANT_BOOL result;
	try{
	m_pBrowserApp->get_Busy(&result);
	}catch(...){}
	return result;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -