标签:
DuiLib学习笔记(二) 扩展CScrollbar属性
Duilib的滚动条滑块默认最小值为滚动条的高度(HScrollbar)或者宽度(VScrollbar)。并且这个值默认为16。当采用系统样式的滚动条,或者 Troy的源码(https://github.com/qdtroy/DuiLib_Ultimate )自带的样式时,是没有问题的,因为这两种样式默认高(宽)度都是16,当滑块最小时,也有16*16,背景图片(九宫格式)不会出拉伸BUG。但是,当自定义背景图片时,如果图片本身大小超过16*16,则会出现重影,本图中滚动条宽度设置为12,滑块背景图片为40*12。corner=”0,6,0,24”。或者滑块变得很小,本图中滚动条宽度设置为12,滑块背景图片为40*12。corner=”0,6,0,6”。
查看源码可以发现
m_rcThumb.top = rc.top; m_rcThumb.bottom = rc.top + m_cxyFixed.cy; if( m_nRange > 0 ) { int cxThumb = cx * (rc.right - rc.left) / (m_nRange + rc.right - rc.left); if( cxThumb < m_cxyFixed.cy ) cxThumb = m_cxyFixed.cy; m_rcThumb.left = m_nScrollPos * (cx - cxThumb) / m_nRange + m_rcButton1.right; m_rcThumb.right = m_rcThumb.left + cxThumb; if( m_rcThumb.right > m_rcButton2.left ) { m_rcThumb.left = m_rcButton2.left - cxThumb; m_rcThumb.right = m_rcButton2.left; } } else { m_rcThumb.left = m_rcButton1.right; m_rcThumb.right = m_rcButton2.left; }
,当滑块变得很小时,源码只是简单的设置滑块的宽度为m_cxyFixed.cy,即滚动条高度(HScrollbar)。因此,我们可以给它加一个属性ThumbMinSize,即设置一个滑块最小值,来替换m_cxyFixed.cy。
具体代码如下:
头文件UIScrollbar.h
protected: int m_nThumbMinSize; public: int GetThumbMinSize(); void SetThumbMinSize();
实现文件UIScrollbar.cpp
//构造函数里面初始化m_nThumbMinSize值为DEFAULT_SCROLLBAR_SIZE ,即16
CScrollBarUI::CScrollBarUI():……
{
m_nThumbMinSize = DEFAULT_SCROLLBAR_SIZE;
}
void CScrollBarUI::SetAttribute(LPCTSTR pstrName, LPCTSTR pstrValue) { ....... //源码 else if (_tcsicmp(pstrName, _T("thumbminsize")) == 0) SetThumbMinSize(_ttoi(pstrValue)); } int CScrollBarUI::GetThumbMinSize() { return m_nThumbMinSize; } void CScrollBarUI::SetThumbMinSize(int nSize) { m_nThumbMinSize = nSize; Invalidate(); } void CScrollBarUI::SetPos(RECT rc, bool bNeedInvalidate) { …… //源码 m_rcThumb.top = rc.top; m_rcThumb.bottom = rc.top + m_cxyFixed.cy; if( m_nRange > 0 ) { int cxThumb = cx * (rc.right - rc.left) / (m_nRange + rc.right - rc.left); /*这儿有改动,源码设置的滑块最小值为 滚动条的高度(HScrollbar)*/ if (cxThumb < m_nThumbMinSize) { if (m_nThumbMinSize < m_cxyFixed.cy) { cxThumb = m_cxyFixed.cy; } else cxThumb = m_nThumbMinSize; } m_rcThumb.left = m_nScrollPos * (cx - cxThumb) / m_nRange + m_rcButton1.right; m_rcThumb.right = m_rcThumb.left + cxThumb; if( m_rcThumb.right > m_rcButton2.left ) { m_rcThumb.left = m_rcButton2.left - cxThumb; m_rcThumb.right = m_rcButton2.left; } } …… //源码 //另外,下面的竖向滚动条也一样 }
完美实现!!!
标签:
原文地址:http://www.cnblogs.com/lyfh/p/5472400.html