Skip to content

Commit

Permalink
修正容器滚动条相关问题 By 冰下海
Browse files Browse the repository at this point in the history
修正CHorizontalLayoutUI、CVerticalLayoutUI
初始时容器有hscrollbar属性时,没超出容器大小也会有横向滚动条;
修正CHorizontalLayoutUI中,有vscrollbar属性时,纵向滚动条按照子控件中高度最大的计算
修正CVerticalLayoutUI中,有hscrollbar属性时,横向滚动条按照子控件中宽度最大的计算
  • Loading branch information
Troy committed Aug 15, 2015
1 parent 1184e88 commit 1371ab4
Show file tree
Hide file tree
Showing 14 changed files with 122 additions and 42 deletions.
6 changes: 3 additions & 3 deletions Demos/duidemo/duidemo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE /*hPrevInstance*/, LPSTR /*l
if( pFrame == NULL ) return 0;
pFrame->Create(NULL, _T("duilibÀý×ÓÑÝʾ"), UI_WNDSTYLE_FRAME, 0L, 0, 0, 800, 572);
pFrame->CenterWindow();
::ShowWindow(*pFrame, SW_SHOW);

CPaintManagerUI::MessageLoop();
pFrame->ShowModal();
//::ShowWindow(*pFrame, SW_SHOW);
//CPaintManagerUI::MessageLoop();

::CoUninitialize();
return 0;
Expand Down
Binary file modified DuiLib.suo
Binary file not shown.
93 changes: 91 additions & 2 deletions DuiLib/Core/UIContainer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ namespace DuiLib
m_bMouseChildEnabled(true),
m_pVerticalScrollBar(NULL),
m_pHorizontalScrollBar(NULL),
m_bScrollProcess(false),
m_bHScrollProcess(false),
m_bVScrollProcess(false),
m_nScrollStepSize(0)
{
::ZeroMemory(&m_rcInset, sizeof(m_rcInset));
Expand Down Expand Up @@ -784,7 +785,7 @@ namespace DuiLib

void CContainerUI::ProcessScrollBar(RECT rc, int cxRequired, int cyRequired)
{
if( m_pHorizontalScrollBar != NULL && m_pHorizontalScrollBar->IsVisible() ) {
/*if( m_pHorizontalScrollBar != NULL && m_pHorizontalScrollBar->IsVisible() ) {
RECT rcScrollBarPos = { rc.left, rc.bottom, rc.right, rc.bottom + m_pHorizontalScrollBar->GetFixedHeight()};
m_pHorizontalScrollBar->SetPos(rcScrollBarPos);
}
Expand Down Expand Up @@ -827,6 +828,94 @@ namespace DuiLib
SetPos(m_rcItem);
}
}
}*/
// heliangbao
while (m_pHorizontalScrollBar)
{
if (cxRequired > rc.right - rc.left && !m_pHorizontalScrollBar->IsVisible())
{
m_pHorizontalScrollBar->SetVisible(true);
m_pHorizontalScrollBar->SetScrollRange(cxRequired - (rc.right - rc.left));
m_pHorizontalScrollBar->SetScrollPos(0);
m_bHScrollProcess = true;
SetPos(m_rcItem);
m_bHScrollProcess = false;
break;
}

// No scrollbar required
if (!m_pHorizontalScrollBar->IsVisible()) break;

// Scroll not needed anymore?
int cxScroll = cxRequired - (rc.right - rc.left);
if (cxScroll <= 0 && !m_bHScrollProcess)
{
m_pHorizontalScrollBar->SetVisible(false);
m_pHorizontalScrollBar->SetScrollPos(0);
m_pHorizontalScrollBar->SetScrollRange(0);
SetPos(m_rcItem);
}
else
{
RECT rcScrollBarPos = { rc.left, rc.bottom, rc.right, rc.bottom + m_pHorizontalScrollBar->GetFixedHeight() };
m_pHorizontalScrollBar->SetPos(rcScrollBarPos);

if (m_pHorizontalScrollBar->GetScrollRange() != cxScroll) {
int iScrollPos = m_pHorizontalScrollBar->GetScrollPos();
m_pHorizontalScrollBar->SetScrollRange(::abs(cxScroll));
if (m_pHorizontalScrollBar->GetScrollRange() == 0) {
m_pHorizontalScrollBar->SetVisible(false);
m_pHorizontalScrollBar->SetScrollPos(0);
}
if (iScrollPos > m_pHorizontalScrollBar->GetScrollPos()) {
SetPos(m_rcItem);
}
}
}
break;
}

while (m_pVerticalScrollBar && !m_bHScrollProcess)
{
if (cyRequired > rc.bottom - rc.top && !m_pVerticalScrollBar->IsVisible()) {
m_pVerticalScrollBar->SetVisible(true);
m_pVerticalScrollBar->SetScrollRange(cyRequired - (rc.bottom - rc.top));
m_pVerticalScrollBar->SetScrollPos(0);
m_bVScrollProcess = true;
SetPos(m_rcItem);
m_bVScrollProcess = false;
break;
}

// No scrollbar required
if (!m_pVerticalScrollBar->IsVisible()) break;

// Scroll not needed anymore?
int cyScroll = cyRequired - (rc.bottom - rc.top);
if (cyScroll <= 0 && !m_bVScrollProcess) {
m_pVerticalScrollBar->SetVisible(false);
m_pVerticalScrollBar->SetScrollPos(0);
m_pVerticalScrollBar->SetScrollRange(0);
SetPos(m_rcItem);
}
else
{
RECT rcScrollBarPos = { rc.right, rc.top, rc.right + m_pVerticalScrollBar->GetFixedWidth(), rc.bottom };
m_pVerticalScrollBar->SetPos(rcScrollBarPos);

if (m_pVerticalScrollBar->GetScrollRange() != cyScroll) {
int iScrollPos = m_pVerticalScrollBar->GetScrollPos();
m_pVerticalScrollBar->SetScrollRange(::abs(cyScroll));
if (m_pVerticalScrollBar->GetScrollRange() == 0) {
m_pVerticalScrollBar->SetVisible(false);
m_pVerticalScrollBar->SetScrollPos(0);
}
if (iScrollPos > m_pVerticalScrollBar->GetScrollPos()) {
SetPos(m_rcItem);
}
}
}
break;
}
}

Expand Down
3 changes: 2 additions & 1 deletion DuiLib/Core/UIContainer.h
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ class UILIB_API CContainerUI : public CControlUI, public IContainerUI
bool m_bAutoDestroy;
bool m_bDelayedDestroy;
bool m_bMouseChildEnabled;
bool m_bScrollProcess; // 防止SetPos循环调用
bool m_bHScrollProcess; // 防止SetPos循环调用
bool m_bVScrollProcess; // 防止SetPos循环调用
int m_nScrollStepSize;

CScrollBarUI* m_pVerticalScrollBar;
Expand Down
51 changes: 19 additions & 32 deletions DuiLib/Layout/UIHorizontalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace DuiLib

void CHorizontalLayoutUI::SetPos(RECT rc, bool bNeedInvalidate)
{
// heliangbao
CControlUI::SetPos(rc, bNeedInvalidate);
rc = m_rcItem;

Expand All @@ -36,19 +37,18 @@ namespace DuiLib
rc.top += m_rcInset.top;
rc.right -= m_rcInset.right;
rc.bottom -= m_rcInset.bottom;
if (m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible()) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
if (m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible()) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();

if( m_items.GetSize() == 0) {
ProcessScrollBar(rc, 0, 0);
return;
}

if( m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible() ) rc.right -= m_pVerticalScrollBar->GetFixedWidth();
if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();

// Determine the width of elements that are sizeable
SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();
//if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
//szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();

int nAdjustables = 0;
int cxFixed = 0;
Expand All @@ -70,11 +70,17 @@ namespace DuiLib
}
cxFixed += (nEstimateNum - 1) * m_iChildPadding;

int cxExpand = 0;
// Place elements
int cxNeeded = 0;
int cyNeeded = 0;
int cxExpand = 0;
if( nAdjustables > 0 ) cxExpand = MAX(0, (szAvailable.cx - cxFixed) / nAdjustables);
// Position the elements
SIZE szRemaining = szAvailable;
int iPosY = rc.top;
if (m_pVerticalScrollBar && m_pVerticalScrollBar->IsVisible()) {
iPosY -= m_pVerticalScrollBar->GetScrollPos();
}
int iPosX = rc.left;
if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() ) {
iPosX -= m_pHorizontalScrollBar->GetScrollPos();
Expand All @@ -88,6 +94,7 @@ namespace DuiLib
SetFloatPos(it2);
continue;
}

RECT rcPadding = pControl->GetPadding();
szRemaining.cx -= rcPadding.left;
SIZE sz = pControl->EstimateSize(szRemaining);
Expand All @@ -104,49 +111,29 @@ namespace DuiLib
else {
if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();

// cxFixedRemaining -= sz.cx + rcPadding.left + rcPadding.right ;
}

// cxFixedRemaining -= m_iChildPadding;

sz.cy = pControl->GetFixedHeight();
if( sz.cy == 0 ) sz.cy = rc.bottom - rc.top - rcPadding.top - rcPadding.bottom;
if( sz.cy == 0 ) sz.cy = szAvailable.cy - rcPadding.top - rcPadding.bottom;
if( sz.cy < 0 ) sz.cy = 0;
if( sz.cy < pControl->GetMinHeight() ) sz.cy = pControl->GetMinHeight();
if( sz.cy > pControl->GetMaxHeight() ) sz.cy = pControl->GetMaxHeight();

RECT rcCtrl = { iPosX + rcPadding.left, rc.top + rcPadding.top, iPosX + sz.cx + rcPadding.left , rc.top + rcPadding.top + sz.cy};
RECT rcCtrl = { iPosX + rcPadding.left, iPosY + rcPadding.top, iPosX + rcPadding.left + sz.cx, iPosY + rcPadding.top + sz.cy };
pControl->SetPos(rcCtrl);

iPosX += sz.cx + m_iChildPadding + rcPadding.left + rcPadding.right;
cxNeeded += sz.cx + rcPadding.left + rcPadding.right;
cyNeeded = ((sz.cy + rcPadding.top + rcPadding.bottom) > cyNeeded) ? (sz.cy + rcPadding.top + rcPadding.bottom) : cyNeeded;
szRemaining.cx -= sz.cx + m_iChildPadding + rcPadding.right;
}
cxNeeded += (nEstimateNum - 1) * m_iChildPadding;
//reddrain
if( m_pHorizontalScrollBar != NULL ) {
if( cxNeeded > rc.right - rc.left ) {
if( m_pHorizontalScrollBar->IsVisible() ) {
m_pHorizontalScrollBar->SetScrollRange(cxNeeded - (rc.right - rc.left));
}
else {
m_pHorizontalScrollBar->SetVisible(true);
m_pHorizontalScrollBar->SetScrollRange(cxNeeded - (rc.right - rc.left));
m_pHorizontalScrollBar->SetScrollPos(0);
rc.bottom -= m_pHorizontalScrollBar->GetFixedHeight();
}
}
else {
if( m_pHorizontalScrollBar->IsVisible() ) {
m_pHorizontalScrollBar->SetVisible(false);
m_pHorizontalScrollBar->SetScrollRange(0);
m_pHorizontalScrollBar->SetScrollPos(0);
rc.bottom += m_pHorizontalScrollBar->GetFixedHeight();
}
}
}

ProcessScrollBar(rc, cxNeeded, 0);
// Process the scrollbar
ProcessScrollBar(rc, cxNeeded, cyNeeded);
}

void CHorizontalLayoutUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
Expand Down
11 changes: 7 additions & 4 deletions DuiLib/Layout/UIVerticalLayout.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ namespace DuiLib

void CVerticalLayoutUI::SetPos(RECT rc, bool bNeedInvalidate)
{
// heliangbao
CControlUI::SetPos(rc, bNeedInvalidate);
rc = m_rcItem;

Expand All @@ -46,8 +47,8 @@ namespace DuiLib

// Determine the minimum size
SIZE szAvailable = { rc.right - rc.left, rc.bottom - rc.top };
if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();
//if( m_pHorizontalScrollBar && m_pHorizontalScrollBar->IsVisible() )
//szAvailable.cx += m_pHorizontalScrollBar->GetScrollRange();

int nAdjustables = 0;
int cyFixed = 0;
Expand All @@ -70,6 +71,7 @@ namespace DuiLib
cyFixed += (nEstimateNum - 1) * m_iChildPadding;

// Place elements
int cxNeeded = 0;
int cyNeeded = 0;
int cyExpand = 0;
if( nAdjustables > 0 ) cyExpand = MAX(0, (szAvailable.cy - cyFixed) / nAdjustables);
Expand Down Expand Up @@ -120,17 +122,18 @@ namespace DuiLib
if( sz.cx < pControl->GetMinWidth() ) sz.cx = pControl->GetMinWidth();
if( sz.cx > pControl->GetMaxWidth() ) sz.cx = pControl->GetMaxWidth();

RECT rcCtrl = { iPosX + rcPadding.left, iPosY + rcPadding.top, iPosX + rcPadding.left + sz.cx, iPosY + sz.cy + rcPadding.top };
RECT rcCtrl = { iPosX + rcPadding.left, iPosY + rcPadding.top, iPosX + rcPadding.left + sz.cx, iPosY + rcPadding.top + sz.cy };
pControl->SetPos(rcCtrl);

iPosY += sz.cy + m_iChildPadding + rcPadding.top + rcPadding.bottom;
cxNeeded = ((sz.cx + rcPadding.left + rcPadding.right) > cxNeeded) ? (sz.cx + rcPadding.left + rcPadding.right) : cxNeeded;
cyNeeded += sz.cy + rcPadding.top + rcPadding.bottom;
szRemaining.cy -= sz.cy + m_iChildPadding + rcPadding.bottom;
}
cyNeeded += (nEstimateNum - 1) * m_iChildPadding;

// Process the scrollbar
ProcessScrollBar(rc, 0, cyNeeded);
ProcessScrollBar(rc, cxNeeded, cyNeeded);
}

void CVerticalLayoutUI::DoPostPaint(HDC hDC, const RECT& rcPaint)
Expand Down
Binary file added Lib/DuiLibA_d.lib
Binary file not shown.
Binary file added Lib/DuiLib_d.lib
Binary file not shown.
Binary file added Temp/duidemo/Debug/duidemo.res
Binary file not shown.
Binary file added Temp/duidemo/Debug/duidemo_d.exe.embed.manifest.res
Binary file not shown.
Binary file added Temp/duidemo/Debug/duidemo_d_manifest.rc
Binary file not shown.
Binary file added Temp/duidemo/DebugA/duidemo.res
Binary file not shown.
Binary file added Temp/duidemo/DebugA/duidemo_d.exe.embed.manifest.res
Binary file not shown.
Binary file added Temp/duidemo/DebugA/duidemo_d_manifest.rc
Binary file not shown.

0 comments on commit 1371ab4

Please sign in to comment.