Click here to Skip to main content
15,877,915 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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 :
C++
//########### 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);
}
Posted
Updated 1-Jun-15 1:00am
v2
Comments
Kornfeld Eliyahu Peter 1-Jun-15 6:58am    
Please format your code - it is hard to understand what do you want...

the scrollbars don't belong to you, they belong to the browser ...

Programmatically scrolling WebBrowser control from Visual C/C++[^]
 
Share this answer
 
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);

}
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900