none
How to conditionally check whether a window's scroll bar is currently visible or not? RRS feed

  • Question

  • I am working on an mfc application that draws two buttons on the screen relative to the dimensions of the CListBox that sits directly to the left of it. Their positions are determined by coordinates retrieved from the GetClientRect() function. However, when the CListBox contains enough data, a scroll bar will become visible, and GetClientRect() will not take the scroll bar's size into account. Therefore, my buttons end up being shifted over to the wrong place. 

    I am aware there is an rgstate in the SCROLLBARINFO that has information regarding the current state of the scrollbar. I haven't been successful thus far in checking this state to determine if the scroll bar is currently visible or not. My understanding of this is pretty shallow so it's likely an error on my part. 

    My conditional part of my code looks something like:

    bool visible = true;
    int info = scrollbarinfo.rgstate[0];
    if (info == ((STATE_SYSTEM_INVISIBLE || STATE_SYSTEM_UNAVAILABLE)) 
    {
    visible = false;
    }
    My understanding on how this works must be way off. Any guidance would be much appreciated!


    Tuesday, June 18, 2019 3:01 PM

Answers

All replies

  • Try (info & (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN) ) != 0


    • Edited by RLWA32 Tuesday, June 18, 2019 3:13 PM
    • Marked as answer by Jimmy Blundell Tuesday, June 18, 2019 7:16 PM
    Tuesday, June 18, 2019 3:12 PM
  • How exactly does just putting (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN) let the program know I'm referencing a particular scroll bar in a particular list box?
    Tuesday, June 18, 2019 3:42 PM
  • GetScrollBarInfo() takes HWND as the first argument - a handle to the window associated with the scrollbar.

    -Seetharam

    Tuesday, June 18, 2019 3:58 PM
  • This GetScrollBarInfo() is apart of the CWnd class, and only takes 2 parameters. The same as the GetScrollBarInfo() you're referring to, but withouth the HWND parameter
    Tuesday, June 18, 2019 4:04 PM
  • How exactly does just putting (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN) let the program know I'm referencing a particular scroll bar in a particular list box?

    I assumed that you had already called GetScrollBarInfo.
    Tuesday, June 18, 2019 4:11 PM
  • I see, my bad. That being said, maybe it would better help me if I know what in particular is wrong with my original attempt? Unless my approach is wrong altogether, I assumed that checking that scrollbarinfo.rgstate[0] was STATE_SYSTEM_INVISIBLE or UNAVAILABLE or OFFSCREEN would be enough, but so far no combination of those works. Am I not understanding how this works correctly?

    SCROLLBARINFO scrollbarinfo;
    bool visible = true;
    scrollbarinfo.cbSize = sizeof(scrollbarinfo);
    if (m_lstFT.GetScrollBarInfo(OBJID_VSCROLL, &scrollbarinfo)) 
    {
         if (scrollbarinfo.rgstate[0] == (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN))
         {
         visible = false;
         }
    }

    I tried updating it like above, but am I still missing the point here?

    Tuesday, June 18, 2019 4:16 PM
  • I see, my bad. That being said, maybe it would better help me if I know what in particular is wrong with my original attempt? Unless my approach is wrong altogether, I assumed that checking that scrollbarinfo.rgstate[0] was STATE_SYSTEM_INVISIBLE or UNAVAILABLE or OFFSCREEN would be enough, but so far no combination of those works. Am I not understanding how this works correctly?

    SCROLLBARINFO scrollbarinfo;
    bool visible = true;
    scrollbarinfo.cbSize = sizeof(scrollbarinfo);
    if (m_lstFT.GetScrollBarInfo(OBJID_VSCROLL, &scrollbarinfo)) 
    {
         if (scrollbarinfo.rgstate[0] == (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_OFFSCREEN))
         {
         visible = false;
         }
    }

    I tried updating it like above, but am I still missing the point here?

    Yes, you missed my point.  Note that in my suggested code it does a bitwise test using & instead of checking for equality using ==.  Give the suggested code a try.

    • Edited by RLWA32 Tuesday, June 18, 2019 4:24 PM
    Tuesday, June 18, 2019 4:24 PM
  • That does work, thank you. Last question if you don't mind: why not just check if rgstate[0] != 0 instead of checking INVISIBLE and OFFSCREEN as well?
    Tuesday, June 18, 2019 5:31 PM
  • That does work, thank you. Last question if you don't mind: why not just check if rgstate[0] != 0 instead of checking INVISIBLE and OFFSCREEN as well?

    Because rgstate[0] != 0 would return true if any of the other bit flags unrelated to visibility were set.
    Tuesday, June 18, 2019 5:36 PM
  • I get it now. I had never heard of bitwise operations before this so I was lost there for awhile but I understand why it's working now. Thank you so much!
    Tuesday, June 18, 2019 7:16 PM