如何在Microsoft Visual C ++中的WebBrowser Microsoft ActiveX控件中滚动水平和垂直条 [英] How to scroll horizontal and Vertical bar in WebBrowser Microsoft ActiveX control in Microsoft Visual C++

查看:81
本文介绍了如何在Microsoft Visual C ++中的WebBrowser Microsoft ActiveX控件中滚动水平和垂直条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经创建了一个CMyHtmlView类并继承自CHtmlView类来实现WebBrowser Microsoft ActiveX控件,它运行正常。



现在我遇到滚动条的问题,我无法获得此视图的滚动条的句柄。



实际上我有两个窗格(LEFT PANE(普通视图)和右窗格(HTML视图)),我想在左视图滚动滚动右侧视图滚动。



我使用过很多函数,比如ScrollToPosition(),SetScrollSizes(CScrollView类)但是对Web浏览器滚动条没有任何影响。

我还添加了两个函数OnHScroll和OnVScroll,并在BEGIN_MESSAGE_MAP下添加了事件,但这些函数永远不会被调用。



请帮忙我如何控制Web浏览器的滚动条。



我的View类实现(Header和Source文件代码)如下所述:

I've created a CMyHtmlView class and inherit from CHtmlView class to implement the WebBrowser Microsoft ActiveX control and it is working fine.

Now I am facing problem with scroll bar that I am not able to get the handle of Scroll bar of this view.

Actually I have two panes (LEFT PANE (Normal View) and RIGHT PANE (HTML View)) and I want to scroll right side view on Scrolling of Left View scroll.

I have used many functions like ScrollToPosition(), SetScrollSizes(CScrollView class) but there is not any impact on Web browser Scroll bar.
I have also added two more functions OnHScroll and OnVScroll and have added events under BEGIN_MESSAGE_MAP, but these functions never get called.

Please help me that how to get control on scroll bar of web browser.

My View class implementation(Header and Source file code) is mentioned below :

//########### Header File Code  ####################
    // CMyHtmlView wrapper class
class CMyHtmlView : public CHtmlView
{

public:
	CMyHtmlView();           // protected constructor used by dynamic creation
	
	DECLARE_DYNCREATE(CMyHtmlView)

	virtual ~CMyHtmlView();

public:	

	void Navigate(LPCTSTR URL);
	
	HRESULT OnShowContextMenu(DWORD dwID, LPPOINT ppt, LPUNKNOWN pcmdtReserved, LPDISPATCH pdispReserved);

	afx_msg void OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

	afx_msg void OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar);

	CPoint GetScrollPosition() const;

protected:

	virtual void OnDraw(CDC* pDC);      // overridden to draw this view

	virtual void OnInitialUpdate();     // first time after construct


	DECLARE_MESSAGE_MAP()
};

    //########### Source File (.cpp) code  ####################
///////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "ctsmHTMLView.h"


#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

#ifndef __MAINFRM_H__
#include "MainFrm.h"
#endif //__MAINFRM_H__

#ifndef __CTSMDOC_H__
#include "ctsmDoc.h"
#endif //__CTSMDOC_H__


//////////////////////////////////////////////////////////////////////
// CMyHtmlView 

IMPLEMENT_DYNCREATE(CMyHtmlView, CHtmlView)

CMyHtmlView::CMyHtmlView()
{

}

CMyHtmlView::~CMyHtmlView()
{

}

BEGIN_MESSAGE_MAP(CMyHtmlView, CHtmlView)
	ON_WM_HSCROLL()

	ON_WM_VSCROLL()
END_MESSAGE_MAP()

void CMyHtmlView::Navigate(LPCTSTR URL)
{
	CHtmlView::Navigate(URL);
}

void CMyHtmlView::OnInitialUpdate()
{
	CScrollView::OnInitialUpdate();

	CSize sizeTotal;

	sizeTotal.cx = sizeTotal.cy = 100;

	SetScrollSizes(MM_TEXT, sizeTotal);

	CctsmDoc* pDoc = (CctsmDoc*)GetDocument();

	Navigate(LPCTSTR(pDoc->c_strHtmlTempName));	
}

void CMyHtmlView::OnDraw(CDC* pDC)
{
	CHtmlView::OnDraw(pDC);
}

HRESULT CMyHtmlView::OnShowContextMenu( DWORD dwID, LPPOINT ppt, LPUNKNOWN pcmdtReserved, LPDISPATCH pdispReserved )
{
	dwID;
	ppt;
	pcmdtReserved;
	pdispReserved;

	return S_OK;
}


CPoint CMyHtmlView::GetScrollPosition() const
{
	CPoint cpoint = CHtmlView::GetScrollPosition();

	POINT point = POINT(cpoint);

	int a = 10;

	return cpoint;
}

void CMyHtmlView::OnHScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	CctsmDoc* pDoc = (CctsmDoc*)GetDocument();

	ASSERT(pDoc);

	CWnd::OnHScroll(nSBCode, nPos, pScrollBar);

	CPoint ptScroll = GetScrollPosition();
        UpdateTrackRectOnScroll(ptScroll);

	pDoc->SetViewsScrollPrositions(ptScroll);

	CScrollView::OnHScroll(nSBCode, nPos, pScrollBar);
}

void CMyHtmlView::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
	CctsmDoc* pDoc = (CctsmDoc*)GetDocument();

	ASSERT(pDoc);

	CPoint ptScroll = GetScrollPosition();

	UpdateTrackRectOnScroll(ptScroll);

	pDoc->SetViewsScrollPrositions(ptScroll);

	CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}

推荐答案

滚动条不属于您,它们属于浏览器......



以编程方式从Visual C /滚动WebBrowser控件C ++ [ ^ ]
the scrollbars don't belong to you, they belong to the browser ...

Programmatically scrolling WebBrowser control from Visual C/C++[^]


嗨barneyman,



感谢您的建议。我只是根据我的要求修改了代码。



我创建了一个新的函数名称SetScrollPos,有两个参数nBar和nPos。 nBar包含水平或垂直条状态,nPos包含滚动坐标。





void CMyHtmlView :: SetScrollPos(int nBar,int nPos )

{

IDispatch * pDisp = this-> GetHtmlDocument();



HRESULT hr ;;



//获取文档界面

IHTMLDocument2 * pDocument = NULL;

hr = pDisp-> QueryInterface (IID_IHTMLDocument,(void **)&pDocument);

ASSERT(SUCCEEDED(hr));

ASSERT(pDocument);



IHTMLElement * pBody = NULL;

hr = pDocument-> get_body(&pBody);

ASSERT(SUCCEEDED(hr));

ASSERT(pBody);



//从身体我们可以获得element2接口,

//这允许我们滚动

IHTMLElement2 * pElement = NULL;

hr = pBody-> QueryInterface(IID_IHTMLElement2,(void **)&pElement);

ASSERT(SUCCEEDED(hr));

ASSERT(pElement);



//现在我们准备滚动

如果(nBar == SB_VERT)//从顶部向下滚动到第100个像素

pElement-> put_scrollTop(nPos);

else if(nBar == SB_HORZ)//从左向下滚动到第100个像素

pElement-> put_scrollLeft(nPos);



}
Hi barneyman,

Thanks for your suggestion it works. I just modified code according to my requirement.

I created a new function name as SetScrollPos with two parameters nBar and nPos. nBar contain horizontal or vertical bar status and nPos contain the coordinate of scroll.


void CMyHtmlView::SetScrollPos(int nBar, int nPos)
{
IDispatch *pDisp = this->GetHtmlDocument();

HRESULT hr;;

// get document interface
IHTMLDocument2 *pDocument = NULL;
hr = pDisp->QueryInterface(IID_IHTMLDocument, (void**)&pDocument);
ASSERT(SUCCEEDED(hr));
ASSERT(pDocument);

IHTMLElement *pBody = NULL;
hr = pDocument->get_body(&pBody);
ASSERT(SUCCEEDED(hr));
ASSERT(pBody);

// from body we can get element2 interface,
// which allows us to do scrolling
IHTMLElement2 *pElement = NULL;
hr = pBody->QueryInterface(IID_IHTMLElement2, (void**)&pElement);
ASSERT(SUCCEEDED(hr));
ASSERT(pElement);

// now we are ready to scroll
if (nBar == SB_VERT) // scroll down to 100th pixel from top
pElement->put_scrollTop(nPos);
else if (nBar == SB_HORZ) // scroll down to 100th pixel from left
pElement->put_scrollLeft(nPos);

}


这篇关于如何在Microsoft Visual C ++中的WebBrowser Microsoft ActiveX控件中滚动水平和垂直条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆