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 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



Comments

  • nPos only goes up to 32K

    Posted by wadigzon on 03/04/2004 10:00pm

    you've gotta use something like these in order to handle positions beyond 32K at ::OnVScroll(...) function.
    
    SCROLLINFO si;
    ZeroMemory(&si, sizeof(SCROLLINFO));
    si.cbSize = sizeof(SCROLLINFO);
    // Call GetScrollInfo to get current tracking 
    // position in si.nTrackPos
    if (!GetScrollInfo(SB_VERT, &si) )
    {
     return ; // GetScrollInfo failed
    }
    
    // this is the Page number
    UINT uiPageNumber = (UINT)(si.nTrackPos/10)+1;

    Reply
  • Thank you.

    Posted by Legacy on 12/08/2003 12:00am

    Originally posted by: NANA

    Great. This is exactly I was looking for. I had to integrate this to CListCtrl and it works great. Thank you.

    Reply
  • It looks nicer like this:

    Posted by Legacy on 06/21/1999 12:00am

    Originally posted by: Bruno Vais

    I think it looks nicer if the yellow thing moves aside the mouse. So delete the "s_bFirstCallDuringScrolling" member.
    Also, during scrolling the main view loses focus (of course). To avoid showing that dumb grey caption, at the end of your "DisplayPageNumber" function call
    "AfxGetMainWnd()->SendMessage(WM_NCACTIVATE,TRUE,0);".

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • On-demand Event Event Date: May 18, 2015 While the idea of using facial and/or gesture recognition to create a modern, intuitive game seems attractive, some developers may want to leverage Unity 3D as a way to accelerate their development efforts. There are many different ways in which Intel and Unity Technologies have been working together to help speed the development of games with the Intel&reag; RealSense™ SDK (software developer kit). Check out this webcast to join a panel of experts as they …

  • On-Demand eSeminar DevOps and Cloud are all the rage in IT, but the two terms relating process and computing aren't mutually exclusive. Join us to see how your movement into cloud changes the way you develop, deploy, test and manage, and how DevOps can actually be a good thing when coupled with cloud.

Most Popular Programming Stories

More for Developers

RSS Feeds

Thanks for your registration, follow us on our social networks to keep up-to-date