Thread: Scroll Bars

  1. #1
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382

    Scroll Bars

    I wish to use a scroll bar as a slider. I've worked out how to get a scrollbar on the screen but I can't figure out how to set its maximum and minimum values or even get it to stay where I move it.

    Can anyone help?
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  2. #2
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Wow, that's two perfectly good threads totally ignored. I must have offended someone pretty high up.

    I refuse to beleive that nobody on this board knows how to acheive what I'm looking for.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

  3. #3
    Yes, my avatar is stolen anonytmouse's Avatar
    Join Date
    Dec 2002
    Posts
    2,544
    You can set the range of a scroll bar using SetScrollInfo. You respond to movement by handling the WM_HSCROLL or WM_VSCROLL messages depending on whether the scroll bar is horizontal or vertical. This is slightly complicated, as you have to manually update the position. There is an example at MSDN:
    Code:
        case WM_HSCROLL:
        {
             SCROLLINFO si     = { 0 };
             int        oldPos = 0;
    
             si.cbSize = sizeof (si);
             si.fMask  = SIF_ALL;
             GetScrollInfo (hwnd, SB_HORZ, &si);
    
             // Save the position for comparison later on
             oldPos = si.nPos;
    
             switch (LOWORD (wParam))
             {
                 // user clicked left arrow
                 case SB_LINELEFT: 
                     si.nPos -= 1;
                     break;
                  
                 // user clicked right arrow
                 case SB_LINERIGHT: 
                     si.nPos += 1;
                     break;
                  
                 // user clicked the scroll bar shaft left of the scroll box
                 case SB_PAGELEFT:
                     si.nPos -= si.nPage;
                     break;
                  
                 // user clicked the scroll bar shaft right of the scroll box
                 case SB_PAGERIGHT:
                     si.nPos += si.nPage;
                     break;
                  
                 // user dragged the scroll box
                 case SB_THUMBTRACK: 
                     si.nPos = si.nTrackPos;
                     break;
                  
                 default:
                     break;
             }
    
             // Set the position and then retrieve it.  Due to adjustments
             //   by Windows it may not be the same as the value set.
             si.fMask = SIF_POS;
             SetScrollInfo (hwnd, SB_HORZ, &si, TRUE);
             GetScrollInfo (hwnd, SB_HORZ, &si);
             
             if (si.nPos != oldPos)
             {
                 // If the position has changed, use the new value
             }
    
             return 0;
        }
    However, instead of using a scroll bar, I would strongly suggest you use a trackbar control.

    As far as I know, you haven't offended anybody, some questions just slip by.

  4. #4
    Registered User samGwilliam's Avatar
    Join Date
    Feb 2002
    Location
    Newport
    Posts
    382
    Thanks, but in my haste I've written my own slider control.
    Current Setup: Win 10 with Code::Blocks 17.12 (GNU GCC)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Scroll Bars Are Scrolling Me Crazy
    By Iyouboushi in forum C# Programming
    Replies: 6
    Last Post: 03-03-2006, 01:43 PM
  2. Scroll bars
    By bigdan43 in forum C++ Programming
    Replies: 5
    Last Post: 04-19-2005, 12:05 PM
  3. Scroll Bars and Focus
    By Thantos in forum Windows Programming
    Replies: 1
    Last Post: 08-21-2003, 11:57 AM
  4. Horizontal Scroll Bars with CListBox
    By Malek in forum Windows Programming
    Replies: 1
    Last Post: 04-10-2003, 09:58 PM
  5. Scroll Bars
    By bc17 in forum Windows Programming
    Replies: 2
    Last Post: 02-21-2003, 03:15 PM