Handling The ScrollBar

Here is a simple example illustrating how to programmatically handle the scroll bar (and it's messages).

1. First set up the dimensions of the control:

  SCROLLINFO ScrollInfo;
  ScrollInfo.cbSize = sizeof(ScrollInfo);     // size of this structure
  ScrollInfo.fMask = SIF_ALL;                 // parameters to set
  ScrollInfo.nMin = 0;                        // minimum scrolling position
  ScrollInfo.nMax = 100;                      // maximum scrolling position
  ScrollInfo.nPage = 40;                      // the page size of the scroll box
  ScrollInfo.nPos = 50;                       // initial position of the scroll box
  ScrollInfo.nTrackPos = 0;                   // immediate position of a scroll box that the user is dragging
  m_MyScrollBar.SetScrollInfo(&ScrollInfo);
2. Now override the WM_VSCROLL or WM_HSCROLL (for a horizontal control) function in Your ScrollBar class:

void CMyScrollBar::VScroll(UINT nSBCode, UINT nPos) 
{
  SCROLLINFO ScrollInfo;
  GetScrollInfo(&ScrollInfo);  // get information about the scroll

  switch(nSBCode)
  {
    case SB_BOTTOM:         //Scrolls to the lower right. 
      break;

    case SB_ENDSCROLL:      //Ends scroll. 
      break;

    case SB_LINEDOWN:       //Scrolls one line down. 
      SetScrollPos(GetScrollPos() + 1);
      break;

    case SB_LINEUP:         //Scrolls one line up. 
      SetScrollPos(GetScrollPos() - 1);
      break;

    case SB_PAGEDOWN:       //Scrolls one page down. 
      SetScrollPos(GetScrollPos() + ScrollInfo.nPage);
      break;

    case SB_PAGEUP:         //Scrolls one page up. 
      SetScrollPos(GetScrollPos() - ScrollInfo.nPage);
      break;

    case SB_THUMBPOSITION:  //The user has dragged the scroll box (thumb) and released the mouse button. The nPos parameter indicates the position of the scroll box at the end of the drag operation. 
      break;

    case SB_THUMBTRACK:     //The user is dragging the scroll box. This message is sent repeatedly until the user releases the mouse button. The nPos parameter indicates the position that the scroll box has been dragged to. 
      SetScrollPos(nPos);
      break;

    case SB_TOP:            //Scrolls to the upper left. 
      break;
  }
}
3.That's it! With this code snippet, you can easily add a scrollbar control to your application and insert your own application specific logic to handle the different messages sent to your application when the user scrolls your document.



Comments

  • Win32 Console App

    Posted by Legacy on 03/10/2003 12:00am

    Originally posted by: AirTurK

    Hey everyone. I was programming in Visual c++ and I am using a win32 console application to display my output. However when I have a lot of output I do not get a scrollbar and I am unable to see previous output since it automatically scrolls down. How do I get a scroll bar on my win32 consol application?? Can someone please email me if you know. thank you.

    airturkoz@hotmail.com

    Reply
  • Scroll CListView with Background Map

    Posted by Legacy on 02/21/2003 12:00am

    Originally posted by: Yannick Bessette

    Hi all,

    I have a problem when trying to scroll a ListView with a Background bitmap. I am trying to make the scroll bar, shift the icons ( done automatically by the call CListView::OnHScroll(...)) AND the background map at the same time while keeping the icons at the same position with respect to the map.

    My problem is that when I scroll, I get the amount of pixel I want to shift the map by, but that doesn't necessarily correspond to the exact position of the thumb in the scroll bar due to integer round up.

    For example, my map is 577x121 pixels and the limit of the scroll bar is 153. When I scroll right, I want the offset (amount of pixels the map will be shifted by) to be 20 pixels right on the x axis, then the thumb should be located at 20/577 of the scroll bar limit ( 153 ). But since this calculation will get a decimal value (153*20/577=5.3033), it will be round up to the closest integer(5). That introduces a small difference (20/577 = 0.035 vs 5/153 =0.032) in the ratio of the Map coordinates vs scrollbar and after a while, the icons will be slighly shifted away from the map and they won't keep their correct coordinates with respect to the map.

    Do you have any way I could handle this kind of problem?

    Thank you

    Yannick

    Reply
  • How to extend the scrollbar?

    Posted by Legacy on 08/19/2002 12:00am

    Originally posted by: Sankar

    We are facing problem while using the scrollbar. we have to show more than three MB bytes on the screen. But maximum length of the Scrollbar is 32767. How to extend the scrollbar?. Or any other option is available for displaying large amount of data in single view?
    

    Reply
  • Any solution to change the whole bk-color of scrollbar?

    Posted by Legacy on 12/11/2001 12:00am

    Originally posted by: KentSi

    Refer to the document about scrollbar control and the Flat scroll bar in msdn, we can't change background color of it but the shaft color.
    When browsing in internet, some sites made the scrollbar's color changed in IE. What kind of scrollbar is it? Could I use it in Win32 programming as a control?

    Thanks

    Reply
  • [question] one more vertical scrollbars

    Posted by Legacy on 05/21/2001 12:00am

    Originally posted by: Tommy

    I hope someone can help me to make a dialog which have several vertical scroll bars on the same page I can understand to use one scrollbar but i am unsure to work with several scrollbars..
    anyway, I need some help from you


    thank you.
    ^^

    Reply
  • How are the size of the arrows determined

    Posted by Legacy on 04/16/2001 12:00am

    Originally posted by: John

    I need to create a new scrollbar class that reacts in the following manner. When a user clicks the middle mouse button somewhere on the scrollbar, the thumb will automatically scroll to that position. I've managed to
    come up with a solution but this solution requires that I know the client area of the scrollbar. I can obtain this but need to condsider the size of the arrows in order for my calculations to work. Could anyone please tell me how big the scrollbar arrows are - a percentage of the whole client area?

    Thanks.

    John

    Reply
  • Can't get max value in scroll bar

    Posted by Legacy on 04/12/2001 12:00am

    Originally posted by: Carl

    I'm using three scroll bars in a COM component, and they're relatively easy to identify from the lParam parameter to the OnHScroll handler function (lParam is the m_hWnd of the control that generated the message). What's really bizarre is that no matter what I set as the maximum value in range (using SCROLLINFO and SetScrollInfo), I can only get a maximum position value that's 2 less than the maximum I provided to set the range. What's going on here? Arrow clicks, track clicks (page), and dragging all work fine for 0..(n-2) when I define a range of min=0 and max=n.

    -- Carl

    Reply
  • Problem using a scroll bar

    Posted by Legacy on 04/12/2001 12:00am

    Originally posted by: Peter Weiss

    Ok, but if, for example, my scroll bar is IDC_SPEED; How do i get IDC_SPEED to be srollable? Mine doesn�t do anything but blinking yet; Where do i have to implement WM_HSCROLL?

    Please help me anyone ...

    Reply
  • when you have more than one scrollbar in same dialog

    Posted by Legacy on 03/21/2001 12:00am

    Originally posted by: NL

    I hope someone can help me out with this

    If I have 2 or more horizontal scrollbars in the same
    dialog box, in the function OnHScroll(...),
    how do i identify exactly which scrollbar is
    being clicked on by the user?

    I know that the OnHScroll(..) is sufficient to move
    either scrollbar back and forth, but I the individual scrollbar affects other variables in the dialog
    differently, so that's why I need to identify which
    one is being dragged or clicked on.

    Thanks

    Reply
  • just test

    Posted by Legacy on 01/16/2001 12:00am

    Originally posted by: marius again

    <b>TEST1</b>
    <<b>>TEST1<</b>>
    <><b>>TEST1<></b>>
    <><b>TEST1<></b>
    <><<b>TEST1<><</b>
    <<<b>TEST1<<</b>
    ><<<b>TEST1><<</b>
    ><<<b>TEST1><<</b>

    Reply
  • Loading, Please Wait ...

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

Top White Papers and Webcasts

  • Live Event Date: December 11, 2014 @ 1:00 p.m. ET / 10:00 a.m. PT Market pressures to move more quickly and develop innovative applications are forcing organizations to rethink how they develop and release applications. The combination of public clouds and physical back-end infrastructures are a means to get applications out faster. However, these hybrid solutions complicate DevOps adoption, with application delivery pipelines that span across complex hybrid cloud and non-cloud environments. Check out this …

  • On-demand Event Event Date: October 29, 2014 It's well understood how critical version control is for code. However, its importance to DevOps isn't always recognized. The 2014 DevOps Survey of Practice shows that one of the key predictors of DevOps success is putting all production environment artifacts into version control. In this webcast, Gene Kim discusses these survey findings and shares woeful tales of artifact management gone wrong! Gene also shares examples of how high-performing DevOps …

Most Popular Programming Stories

More for Developers

RSS Feeds