SHARE
Facebook X Pinterest WhatsApp

PageCounter Object for ScrollViews

Environment: The demo project was created with VC++ 6.0 SP1 When scrolling in a document that consists of quite a lot pages it might be helpful for the user to get an information which page of the document would be displayed if he/she >would release to slider of the scrollbar at a certain position. Microsoft […]

Written By
thumbnail
CodeGuru Staff
CodeGuru Staff
Apr 16, 1999
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Environment: The demo project was created with VC++ 6.0 SP1

When scrolling in a document that consists of quite a lot pages it might
be helpful for the user to get an information which page of the document would be
displayed if he/she >would release to slider of the scrollbar at a certain position.

Microsoft Word has such a feature. It’s a tooltip-like window that displays
the page number that corresponds to the current position of the slider
during scrolling.

I implemented a class named CScrollViewPageCounter that displays such a
page number in a window that looks like a tooltip-window.
If you have a look at the source code you’ll find out that it was quite easy to implement.
The code itself is self-explanatory…

HOW TO USE

To use the ScrollViewPageCounter-Object you have to perform the following
steps:

1. Add the files ScrollViewPageCounter.h and ScrollViewPageCounter.cpp to
your project.

2. Add a new member m_pScrollViewPageCounter to your scroll view class.

CScrollViewPageCounter*
m_pScrollViewPageCounter;

Of course your have to include the corresponding header file here as well:

 #include “ScrollViewPageCounter.h

3. Create the page counter object in CYourScrollView::OnInitialUpdate()

BOOL CYourScrollView::PreCreateWindow(CREATESTRUCT& cs)
{
 CScrollView::OnInitialUpdate();
 // Instantiate the counter object
 m_pScrollViewPageCounter = new CScrollViewPageCounter( this );
 }

4. Add a command handler for the message WM_VSCROLL:

 void CYourScrollView::OnVScroll(UINT SBCode, UINT nPos, CScrollBar* pScrollBar)
{
 if ( nSBCode == SB_THUMBTRACK )
 {
  // calculate page number (insert your algorithm here)
 UINT uiPageNumber = (UINT) (nPos / 10) + 1;
  // display page number
  m_pScrollViewPageCounter->DisplayPageNumber( uiPageNumber );
 }
 // when scrolling is finished we have to hide the page counter
 if ( nSBCode == SB_ENDSCROLL )
 m_pScrollViewPageCounter->Reset();
 CScrollView::OnVScroll(nSBCode, nPos, pScrollBar);
}

That’s it.

Now compile and enjoy…

Download demo project – 33 KB

Download source – 3 KB

Date Last Updated: April 16, 1999

Recommended for you...

How To Make Windows 11 Faster
Enrique Stone
Nov 30, 2022
Working with the Windows Registry in C#
Joydip Kanjilal
Jun 17, 2022
Using Multiple Programming Languages to Create an ASP.NET Website
Tariq Siddiqui
Jun 11, 2022
Finding a Microsoft Office version with .NET
Hannes DuPreez
May 20, 2022
CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2025 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.