views:

46

answers:

1

My code involves standard Scroll Bar control and it happens that I need to change its value programmatically in some cases. I do this using SetScrollInfo function, as in this example:

void setScrollBarValue( HWND scrollBar, int value )
{
    SCROLLINFO si = { sizeof( SCROLLINFO ); }
    si.fMask = SIF_POS;
    si.nPos = value;
    ::SetScrollInfo( scrollBar, SB_CTL, &si, true /* redraw */ );
}

This appears to work fine (the thumb of the scrollbar moves around) but it fails to notify the rest of the application of the new scrollbar value. For instance, an edit control which uses the scroll bar (much like in the Windows notepad application) fails to scroll around because it doesn't get notified about the new scrollbar value.

In case it matters: the scrollbar I'm modifying is not in the same process as the above setScrollBarValue function.

Does anybody know how to achieve this?

Edit: I found out how to do this with default window scrollbars (those of type SB_VERT or SB_HORZ). I can send the WM_HSCROLL and WM_VSCROLL to the window like this:

::SendMessage( windowContainingScrollBar,
               WM_HSCROLL,
               MAKEWPARAM( SB_THUMBPOSITION, si.nPos ), NULL );

However, in my case the scroll bar has a window handle of its own (it has the type SB_CTL). This means that I don't know the orientation of the scroll bar (so I cannot tell whether to send WM_HSCROLL or WM_VSCROLL) and I don't know what window to send the message to.

+1  A: 

Try sending the WM_VSCROLL message after calling SetScrollInfo().

Stefan
I looked into this, but my scrollbar is a distinct window (hence I'm passing `SB_CTL`); which window would I send `WM_VSCROLL` to? And what would the WPARAM argument look like? I guess it should be `SB_THUMBPOSITION` but how can I know the position of the thumb after scrolling? The same as I set `si.nPos` to in my example?
Frerich Raabe
Send the message to the control itself. If it doesn't handle it, it will automatically get passed on to its parent windows. Or you could just send it directly to its parent window.About the parameters: use GetScrollInfo() to get the params - it's required to call GetScrollInfo() after SetScrollInfo() since the control can adjust the params.
Stefan
@Stefan: Thanks about the GetScrollInfo() hint! I got it working now (except that I don't know how to determine the orientation of a `SB_CTL` scrollbar, but that's a separate SO question). :-)
Frerich Raabe