需求: 建立好能用的CListUI, 填入超过一页的内容后,将鼠标落到CListUI上, 滚动鼠标中键,CListUI可以一行行滚动。
但是得不到滚动条的Notify.
去研究了一下DuiLib滚动通知的实现,改了3句代码,搞定~
工程下载点 : srcGetListUiScrollNotify_2014_1023_2206.rar
效果:
void CMainDlg::Notify(TNotifyUI & msg)
{
std::wstring strTemp = L"";
SIZE szScrollRange;
SIZE szScrollPos;
CListUI* pListInfo = NULL;
if (msg.sType == DUI_MSGTYPE_SCROLL)
{
pListInfo = static_cast<CListUI *>(m_PaintManager.FindControl(L"List_info"));
_ASSERT_EXPR((NULL != pListInfo), L"cant' find pListInfo");
szScrollRange = pListInfo->GetScrollRange();
szScrollPos = pListInfo->GetScrollPos();
strTemp = ns_helper::StringFormatV(L"szScrollRange(%d, %d), szScrollPos(%d, %d)\r\n",
szScrollRange.cx,
szScrollRange.cy,
szScrollPos.cx,
szScrollPos.cy);
OutputDebugStringW(strTemp.c_str());
}
/** run result
szScrollRange(0, 94), szScrollPos(0, 20)
szScrollRange(0, 94), szScrollPos(0, 40)
szScrollRange(0, 94), szScrollPos(0, 60)
szScrollRange(0, 94), szScrollPos(0, 80)
szScrollRange(0, 94), szScrollPos(0, 94)
szScrollRange(0, 94), szScrollPos(0, 94)
szScrollRange(0, 94), szScrollPos(0, 94)
*/
__super::Notify(msg);
}
原生实现是: 只有鼠标点击滚动条进行滚动时,才会触发 m_pPaintManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
发现只要滚动,都会调用 CScrollBarUI::SetScrollPos
修改如下:
BOOL CScrollBarUI::DoEvent(TEventUI& event)
{
/// ...
if( event.Type == UIEVENT_BUTTONDOWN || event.Type == UIEVENT_DBLCLICK )
{
if( !IsEnabled() )
return TRUE;
/// ...
/// 这个片段中会调用 SetScrollPos, 下面这句挪到 SetScrollPos中判断
// if( m_pPaintManager != NULL && m_pOwner == NULL ) m_pPaintManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
return TRUE;
}
if( event.Type == UIEVENT_TIMER && event.wParam == DEFAULT_TIMERID )
{
/// ...
/// 这个片段中会调用 SetScrollPos, 下面这句挪到 SetScrollPos中判断
// if( m_pPaintManager != NULL && m_pOwner == NULL ) m_pPaintManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
return TRUE;
}
void CScrollBarUI::SetScrollPos(int nPos)
{
if( m_nScrollPos == nPos ) return;
m_nScrollPos = nPos;
if( m_nScrollPos < 0 ) m_nScrollPos = 0;
if( m_nScrollPos > m_nRange ) m_nScrollPos = m_nRange;
SetPos(m_rcItem);
/// m_pOwner 不为空
if (NULL != m_pPaintManager)
{
m_pPaintManager->SendNotify(this, DUI_MSGTYPE_SCROLL);
}
}