A Scrollable Dialog Class (CScrollDialog)
Environment: VC6 SP4, NT4 SP3, Win98
MFC's CDialog class is a powerful and flexible class but it has its shortcomings. If we wish to allow the user to resize the dialog and still retain access to all controls within the dialog, then we must either resize/displace the controls within the dialog, or implement any scrolling functionality ourselves.
There are already a good number of articles in CodeGuru which address the first option. This article essentially presents a solution to the second option, the scrollable dialog option. The class presented here is intended to serve as a starting point for those of you who should ever need to implement scrollability in a dialog; it can be used without modification, or it can be tailored to your needs.
To implement a CScrollDialog, simply do the following:
- Derive your dialog from CScrollDialog rather than from CDialog.
- Add the header definition at the top of your dialog's header file (ie. #include "ScrollDialog.h")
- Size your dialog template in the resource editor to its ideal dimensions.
That's it! Upon creation, the ScrollDialog stores its initial dimensions for later reference. Anything smaller than these will enable the corresponding scrollbar(s).
The code was written with MFC version 4.22, and compiles cleanly with Warning level 4. Unicode compatibility is unknown.
Last but not least, I must mention that the window gripper implementation was drawn from code snippets found in the CodeGuru site. Enjoy.

Comments
Can I do Like this..?
Posted by Legacy on 12/29/2003 12:00amOriginally posted by: Babul
In this demo , Dialog is acting like a ScrollView , I mean whole Dialog contents are Scrolling..
I want to make some part (assume that width of 100 pixel From left Side ) Constant and remaining part of the Dialog to be Scroll.
How can it be possible..?
ReplyThanks
scroll in dialog-based program
Posted by Legacy on 06/25/2003 12:00amOriginally posted by: Yuki
Pls help, I am very new for programming.
I am writing a dialog-based program for Pocket PC (Win CE 3.0) using MFC. I am able to get the scrollbar for one of the child dialog embedded in the overlapped dialog. However, the scrollbar is not scrolling at all (the window is not moving when I scroll!!!). What did I do wrong? Here is my code for the WM_VSCROLL:
void CScrollDlg::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar)
{
int nDelta;
m_nScrollPos=GetScrollPos(SB_VERT);
SCROLLINFO si;
si.cbSize = sizeof(SCROLLINFO);
si.fMask = SIF_ALL;
si.nMin = 0;
si.nMax = nMaxPos;
si.nPage = si.nMax/10;
si.nPos = 0;
SetScrollInfo(SB_VERT, &si, TRUE);
switch (nSBCode)
{
case SB_TOP:
nDelta = -m_nScrollPos;
break;
case SB_BOTTOM:
nDelta = si.nMax-m_nScrollPos;
break;
case SB_LINEDOWN:
nDelta = 1;
break;
case SB_LINEUP:
nDelta = -1;
break;
case SB_PAGEDOWN:
nDelta = 100;
break;
case SB_THUMBPOSITION:
nDelta = nPos - m_nScrollPos;
break;
case SB_PAGEUP:
nDelta = -100;
break;
default:
nDelta = 0;
return;
}
m_nScrollPos += nDelta;
SetScrollPos(SB_VERT,m_nScrollPos,TRUE);
ScrollWindow(0,-nDelta, NULL, NULL);
CDialog::OnVScroll(nSBCode, nPos, pScrollBar);
}
Thanks, in advance.
ReplyDialog bigger than screen - answers included
Posted by Legacy on 06/06/2003 12:00amOriginally posted by: redCPU4GHZ
Hi folks:
First I wanted to say what a great article you have
Felix!!
For those that have dialogs bigger than their screen and
are having trouble getting to be able to see the part off
the screen, the secret is to insure to size of the dialog
that initially appears fits on the screen.
Here are the steps to accomplish this:
1. Add a message handler for WM_SHOWWINDOW
2. In that handler add the following code - note that I
am modifying the height parameters only. It should be
obvious the same thing can be done for the width.
CRect rectEntireScreen;
HWND hWndEntireScreen = NULL;
int nHeightOfEntireScreen = 0,nHeightOfMyDlg = 0;
int nAdjHeight = 0;
int nMaxDesiredHeight = 0;
CRect rectMyDlg;
hWndEntireScreen = ::GetDesktopWindow();
BOOL bSuccess = ::GetWindowRect(hWndEntireScreen,&rectEntireScreen);
if (bSuccess != FALSE)
{
nHeightOfEntireScreen = rectEntireScreen.Height();
GetWindowRect(&rectMyDlg);
nHeightOfMyDlg = rectMyDlg.Height();
nAdjHeight = min(nHeightOfEntireScreen,nHeightOfMyDlg);
//In my case, I want the height to be no more than 90% of the entire screen
nMaxDesiredHeight = (int)(0.9 * (double)nHeightOfEntireScreen);
if (nAdjHeight >= nMaxDesiredHeight)
{
nAdjHeight = nMaxDesiredHeight;
}
MoveWindow(rectMyDlg.left,rectMyDlg.top,
rectMyDlg.Width(),nAdjHeight);
}
3. When your dialog comes up it will fit on top the
screen and now you can scroll down to the bottom of
the dialog.
Answer to person who wants to add 200 controls on 8 pages:
ReplyIt is definitely possible and you will not meet any
CDialog limitations. Use my code above and you'll be fine.
how can i resize the dialog box...plzz help me...
Posted by Legacy on 03/31/2003 12:00amOriginally posted by: Garimella
i am using 60 contols in one dialog box ...so i using ScrollDialog class...here i modified dialog templates(co ordinates) using resource editor..but it looks bad ..it's fit to screen also ..so i want to minimize the dialog box when i am calling this dialog box ...
if i mentioned less coordinates in resource file i did't use scrollbar functionality ...
i tried lot of ways ..but not success...
ReplyCScrollDialog issue with smaller screens
Posted by Legacy on 01/13/2003 12:00amOriginally posted by: Ed A
ReplyGreat Job - Thanks
Posted by Legacy on 07/29/2002 12:00amOriginally posted by: Roy Motley
Great Job - Thanks
ReplyVery handy... great work..
Posted by Legacy on 05/02/2002 12:00amOriginally posted by: Jitu Deshpande
Yhanks a lot for giving such a nice and handy code for scrolling dialog boxes. Thanks to Patrix Dughi also for suggesting a very important change as i also faced the same problem...
Thanks
ReplyThanks, but I'm in trouble...
Posted by Legacy on 01/15/2002 12:00amOriginally posted by: toni
I implemented dialog derived from CScrollDialog, on tab control.
So, mouse event is not captured on edit box of dialog .
Other controls(combo box or button) are run well.
Pls. comment it.
ReplyScroll Dialog invariable to the dialog size
Posted by Legacy on 12/14/2001 12:00amOriginally posted by: Praveen
ReplyCDialog
Posted by Legacy on 06/01/2001 12:00amOriginally posted by: Aajay
ReplyLoading, Please Wait ...