Jimmy Blundell
Jimmy Blundell

Reputation: 43

Is there a way to conditionally check if a window is using a scroll bar?

In the application I am working on, 2 buttons are drawn onto the screen relative to a CListBox (let's call it myListBox) that sits directly to their left.

The buttons are placed according to values retrieved from myListBox.GetClientRect(). However, when scroll bars are present in myListBox, these buttons are incorrectly placed, since GetClientRect doesn't account for things such as scroll bars.

I'm curious if there is a conditional way in which I can check if a list box is currently using a scroll bar, if that makes sense. That way I can apply the difference to the function that moves my buttons so that I can achieve proper placement whether or not a scroll bar is present.

I tried GetWindowRect, but I suppose the window encapsulating the list box is much larger than the list box itself (in hindsight this is obvious).

if (scrollbarinfo.rgstate[0] == (STATE_SYSTEM_INVISIBLE || 
STATE_SYSTEM_UNAVAILABLE))
{
    visible = false;
}

Upvotes: 1

Views: 345

Answers (2)

Jovibor
Jovibor

Reputation: 789

You can use CWnd::GetScrollBarInfo function and check rgstate member of its SCROLLBARINFO retrieved struct.

SCROLLBARINFO sbi { };
LONG lScrollBar = OBJID_VSCROLL; //can also be OBJID_HSCROLL or OBJID_CLIENT
GetScrollBarInfo(lScrollBar, &sbi);

if (sbi.rgstate[0] & STATE_SYSTEM_INVISIBLE)
{
    //Scroll bar is not visible at the moment.
}
else if (sbi.rgstate[0] & STATE_SYSTEM_UNAVAILABLE)
{
    //Scrol bar is disabled, but might be visible.
}

Upvotes: 1

Jimmy Blundell
Jimmy Blundell

Reputation: 43

For reference to anyone else who might see this post, this is what worked for me given my original code, taking into consideration what @Jovibor said:

int info = scrollbarinfo.rgstate[0];
if ((info & (STATE_SYSTEM_INVISIBLE | STATE_SYSTEM_UNAVAILABLE)) != 0)
{
visible = false;
}

Upvotes: 0

Related Questions