Thread: scrollbar

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    479

    scrollbar

    ive looked everywhere for a good explanation on scrollbars
    the one at www.gametutorials.com isnt that impressive, and i've read the info at MSDN

    can you tell me what i need to do to scroll a simple window?

  2. #2
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    I suppose what many people would ask is, what kind of application are you gearing this towards? Are you going to be using MFC or just the straight API? Do you just want simple scrollbars or something more complex? If you can answer those it would help people give you a better answer.

  3. #3
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    no MFC just straight API and a simple scrollbar for a simple window

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    You might be able to get away with somethign simple by using the CreateWindowEx function and then specifying WS_EX_LEFTSCROLLBAR as one of the dwExStyle Params.

    Or you can use WS_EX_RIGHTSCROLLBAR as well...
    Last edited by dpro; 12-20-2004 at 07:02 PM. Reason: made a silly error...

  5. #5
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    i know of that but it's not gonna scroll for me i need to add some extra stuff which i know some of like scrollwindow() function and so
    but i dont know how this works, i need an explaining on some things that make the window scroll

  6. #6
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    what du i need to be able to scroll?

  7. #7
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    It is a pretty big case to cover, but I can try.
    First, there are two ways of handling the windows-scrollbar.

    Number 1:
    First of all, to add the scrollbars to your window, add "WS_HSCROLL or "WS_VSCROLL" to the 3.rd parameter to the CreateWindow() function. The 3.rd parameter is as known, the place where all WindowStyles are placed. "WS=WindowStyle".
    H=HorizontalScroll, V=Vertical scroll.


    Windows takes care of the mouse-input to the scrollbars, so you dont neet to bother about that. But if you want to have a keyboard-interface, I take that later. You will have to make that yourself. But its very simple.

    The magic you probably look for are in way1, two functions:
    "SetScrollRange()" and "SetScrollPos()". As the name says, ScrollRange sets the "range of the Scrollbar", a number(Integer), for how long the scrollbar should be able to scroll.
    The "SetScrollPos()" sets the ScrollBar Thumb and scrollposition to a number in the scroll-range. For example:
    SetScrollRange(hwnd,SB_VERT,0,100,TRUE);
    SetScrollPos(hwnd,SB_VERT,50,TRUE);

    This means that i :Sets the scrollrange to one of the scrollbars in to the window which my HWND is pointing to. Further, I specify in parameter 2 that it is the Vertical scrollbar I'm messing with.
    I set the minimun scrollrange to zero and the maximum to 100.
    Then, I Sets the Scroll-position and Scrollbar thumb to number "50" of "100", with function two.
    So after this, I would have got a Scrollbar with its thumb in the middle.

    And ofcaurse, Windows also gives you thease functions:
    "GetScrollRange()" and "SetScrollRange()". I wont be explaining how they work, as I guess you got the point

    Let me guess, you now think something similar to this "Wow, this is all great, but how can I actually make the scrollbar move when i click on it?
    Here comes the answer:
    When you click on them with the mouse, Windows sends your WindowsProcedure either a WM_VSCROLL or a WM_HSCROLL message. You trap thease messages as you do with everything else, like this:
    ...
    case WM_VSCROLL:
    *do something to the scrollbar*
    return0;
    ...
    What should you call when the message appears?
    First, you need to check the "low word of wParam", like this
    switch(LOWORD(wParam)) {
    case something:
    break;
    case somethingelse:
    break;
    }

    But instead of writing something and something else, insert the possible notifactioncodes.
    They are
    SB_LINEUP
    SB_LINELEFT
    SB_LINEDOWN
    SB_LINERIGHT
    SB_PAGEUP
    SB_PAGELEFT
    SB_PAGEDOWN
    SB_PAGERIGHT
    SB_THUMBPOSITION
    SB_TOP
    SB_BOTTOM
    SB_RIGHT
    SB_ENDSCROLL
    I wont be telling more 'bout thease, as the names says it all.
    If you wanna know more though, scearch it up in MSDN.

    Important: Windows wont change the contents of the window, or the scrollbar, that must you make yourself.

    Now, how to move the scrollbar?
    In your windowsprocedure, add a int varaible named scroll for instance.
    Then, on the WM_VSCROLL message, simply do something like this:
    ...
    case WM_VSCROLL:
    switch(LOWORD(wParam)) {
    case SB_LINEUP:
    scroll+=1; // This is similar to "scroll = scroll+1;"
    }
    SetScrollPos(use your "scroll integer here");
    return 0;
    ...



    Great, now you know how to administrate the scrollbar. Your last task is to update the contents according to what position the thumb is in.. Which is plain maths in your WM_PAINT part.

    Here is a example application, if you need a example:
    http://amalfi.dis.unina.it/~carlosan...2/sysmets2.htm

    EDIT: I try to cover the other scrolling method tomorrow

  8. #8
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    Here is an example program that displays vertically scrolling the window. Hopefully it will get you started.


    Happy coding!

  9. #9
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    The example program andyhunter come up with, uses the other way of Scrollbar managing. Contact me if you would like to know more about it

  10. #10
    yes, I'm registered!!! algi's Avatar
    Join Date
    Nov 2004
    Location
    Ipswich
    Posts
    161

    Arrow

    A really good in depth tutorial for win32 and scrollbars is here

  11. #11
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    thx alot all of you ,hey the autoscroll is so easy why dont you use that?

  12. #12
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    It depends what you want...
    When using autoscrolls, you do little work yourself..
    If you are like me, you want to have as much power as possible at your hand, which means as few windows-side-messing as possible.. hmm...I sound like a Linux programmer

  13. #13
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    what does this mean?
    Code:
    case WM_SIZE:
    			cxClient = LOWORD(lParam);
    			cyClient = HIWORD(lParam);

  14. #14
    Handy Andy andyhunter's Avatar
    Join Date
    Dec 2004
    Posts
    540
    During the Size message that the window proc recieves whenever the window is resized, the new size of the window is passed via lParam.

    The code simply splits up lParam into its parts and retrieves the new width of the window cxClient and the new height cyClient.

    It then uses that information to fill out the scroll information structure in the code immediately following.
    Last edited by andyhunter; 12-24-2004 at 08:17 PM.

  15. #15
    Registered User
    Join Date
    Dec 2001
    Posts
    479
    my compilers cannot use LOWORD i have both msvc6 and dev++
    can i set the words manually?
    maybe with MAKEWORD();

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C win32 API - Scrollbar won't work
    By Matt-Stevens in forum Windows Programming
    Replies: 2
    Last Post: 08-03-2006, 09:05 PM
  2. Scrollbar Control Problem
    By Vicious in forum Windows Programming
    Replies: 0
    Last Post: 11-20-2005, 04:53 PM
  3. edit control scrollbar colour
    By underthesun in forum Windows Programming
    Replies: 1
    Last Post: 02-19-2005, 06:32 AM
  4. My first scrollbar
    By IAmRegistered in forum Windows Programming
    Replies: 5
    Last Post: 07-18-2003, 09:28 AM
  5. sending messages to a scrollbar
    By stormbringer in forum Windows Programming
    Replies: 1
    Last Post: 05-16-2003, 12:32 PM