I am trying to cater for smaller displays as I know that some users have these devices. And the layout of my dialog is such that some of the content is not visible.
Unfortunately, it is not practical for me to supply a copy of my exact dialog resources. Suffice to say that I have dragged a vertical scroll bar on to the dialog and mapped it to a variable.
I have added the following code:
void CChristianLifeMinistryEditorDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int nDelta;
int nMaxPos = 100; // Maximum scroll position, adjust as needed
switch (nSBCode)
{
case SB_LINEUP:
nDelta = -10; // Scroll up by 10 units
break;
case SB_LINEDOWN:
nDelta = 10; // Scroll down by 10 units
break;
case SB_PAGEUP:
nDelta = -50; // Scroll up by a page (adjust as needed)
break;
case SB_PAGEDOWN:
nDelta = 50; // Scroll down by a page (adjust as needed)
break;
case SB_THUMBTRACK:
nDelta = nPos - m_nScrollPos;
break;
default:
nDelta = 0;
}
// Calculate the new scroll position
int nNewPos = m_nScrollPos + nDelta;
nNewPos = max(0, min(nNewPos, nMaxPos));
// If the position has changed, scroll the window
if (nNewPos != m_nScrollPos)
{
ScrollPartialArea(0, m_nScrollPos - nNewPos);
m_nScrollPos = nNewPos;
SetScrollPos(SB_VERT, m_nScrollPos, TRUE);
}
__super::OnVScroll(nSBCode, nPos, pScrollBar);
}
// Function to scroll a part of the dialog
void CChristianLifeMinistryEditorDlg::ScrollPartialArea(int dx, int dy)
{
// Define the scroll rectangle (portion of the client area to scroll)
CRect rctScroll;
CRect rctStatus;
m_verticalScroll.GetWindowRect(rctScroll);
m_StatusBar.GetWindowRect(rctStatus);
ScreenToClient(rctScroll);
ScreenToClient(rctStatus);
const CRect scrollRect(0, 0,
rctScroll.left - 2, rctStatus.top - 2);
// Define the clipping rectangle (NULL means no clipping)
CRect* prcClip = nullptr;
// Region and rectangle for update region
CRgn updateRgn;
updateRgn.CreateRectRgn(0, 0, 0, 0);
CRect rcUpdate;
// Flags for scrolling
const UINT flags = SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE;
// Perform the scrolling
const int result = ScrollWindowEx(
dx,
dy,
&scrollRect,
prcClip,
&updateRgn,
&rcUpdate,
flags
);
// Check the result (0 if an error occurred, otherwise positive value)
if (result == 0)
{
// Handle error (optional)
}
// Clean up
updateRgn.DeleteObject();
}
When I run the program, I find that I can scroll down fine:
- Before:
- After:
But, when I then scroll back to the top:
I have now lost all the original controls.
How do I address that?