Handling The ScrollBar
Posted
by Petr Stejskal
on July 28th, 1999
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:00amOriginally 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
ReplyScroll CListView with Background Map
Posted by Legacy on 02/21/2003 12:00amOriginally 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
ReplyHow to extend the scrollbar?
Posted by Legacy on 08/19/2002 12:00amOriginally posted by: Sankar
ReplyAny solution to change the whole bk-color of scrollbar?
Posted by Legacy on 12/11/2001 12:00amOriginally 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:00amOriginally 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
Replythank you.
^^
How are the size of the arrows determined
Posted by Legacy on 04/16/2001 12:00amOriginally 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
ReplyCan't get max value in scroll bar
Posted by Legacy on 04/12/2001 12:00amOriginally 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
ReplyProblem using a scroll bar
Posted by Legacy on 04/12/2001 12:00amOriginally 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 ...
Replywhen you have more than one scrollbar in same dialog
Posted by Legacy on 03/21/2001 12:00amOriginally 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
Replyhttp://codeguru.earthweb.com/controls/scrollbar_handling.shtml
Posted by Legacy on 01/16/2001 12:00amOriginally posted by: marius
<b>TEST1</b>
Reply<<b>>TEST1<</b>>
<><b>>TEST1<></b>>
<><b>TEST1<></b>
<><<b>TEST1<><</b>
<<<b>TEST1<<</b>
><<<b>TEST1><<</b>
><<<b>TEST1><<</b>
Loading, Please Wait ...