How to determine if a scrollbar for a CListCtrl is

2019-08-24 16:56发布

I have a class that is derived from a CListCtrl. I want the widths of all the columns to total the width of the display Window, so that I don't get the bottom scrollbar. I can get the width of standard scrollbars vai the GetSystemMetrics(SM_CXVSCROLL) call, but I don't know how to tell if the vertical scrollbar is active. I've tried to use:

auto pScrollbar = GetScrollBarCtrl(SB_VERT);
auto is_visible = pScrollbar && pScrollbar->IsWindowVisible();

But pScrollbar is always a nullptr. I've looked around and some ppl are saying that the scrollbars are not always windows and may be draw in by hand (ugh!) and may not be a Window at all. This will make my life more difficult. Ideas?

标签: mfc clistctrl
1条回答
来,给爷笑一个
2楼-- · 2019-08-24 17:05

From my linked question (How to stop the bottom scrollbar from a CListCtrl from displaying?), I was using:

void CMyListCtrl::ResizeLastColumn()
{
    LVCOLUMN column;
    column.mask = LVCF_WIDTH;
    LONG maxWidth = 0;
    for (int i = 0; i < lastColumnIndex; ++i)
    {
        GetColumn(i, &column);
        maxWidth += column.cx;
    }
    CRect wndRect;
    GetWindowRect(&wndRect);

    SetColumnWidth(lastColumnIndex, wndRect.Width() - maxWidth - 4);
}

to resize the columns to the width of the client area. Turns out, by using GetClientRect() instead, I don't have to subtract -4 or the vertical scrollbar width, making this no longer an issue.

查看更多
登录 后发表回答