一、关于滚动条的一些说明
1.当要显示的内容不大于显示区域,这时候不需要显示滚动条。
2.滚动条长度/显示区域的长度= 显示区域的长度/所有显示内容的长度
二、duilib中的CScrollBarUI
CScrollBarUI::SetScrollRange(int nRange);
CScrollBarUI::SetScrollPos(int nPos);
- nRange:所有显示内容的长度-显示区域的长度
- nPos:在所有内容能够被完全显示的情况下,列表当前显示的第一个item在整个显示区域中的相对位置。
三、纵向滚动条例子
#define LISTITEM_HEIGHT 28 //板块柱状图列表每项的高度
//数据总条数
int data_count = m_vecData.size();//m_vecData为所有需要显示的内容
//需要显示的条数
int list_visible_count = m_pList->GetHeight() /LISTITEM_HEIGHT;//m_pList->GetHeight()为显示区域的长度
if (m_pBlockBarRankListScrollbar && m_pBlockBarRankListScrollContainer)
{
if (data_count > list_visible_count)
{
int nRange = (data_count - list_visible_count) * LISTITEM_HEIGHT;
int nPos = m_nBeginIndex * LISTITEM_HEIGHT;//m_nBeginIndex为list当前显示的第一条数据在m_vecData中的索引
m_pListScrollbar->SetScrollRange(nRange);//m_pListScrollbar为滚动条控件
m_pListScrollbar->SetScrollPos(nPos);
m_pListScrollContainer->SetVisible(true);
}
else
{
m_pListScrollbar->SetScrollRange(0);
m_pListScrollbar->SetScrollPos(0);
m_pListScrollContainer->SetVisible(false);
}
}
参考:
Duilib 源码分析之 CScrollBarUI 篇_duilib scrollbar_Jinchx_NightSong的博客-CSDN博客