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:

  1. Derive your dialog from CScrollDialog rather than from CDialog.
  2. Add the header definition at the top of your dialog's header file (ie. #include "ScrollDialog.h")
  3. 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.

Downloads

Download demo project - 20 KB

Download source - 4 KB



Comments

  • Can I do Like this..?

    Posted by Legacy on 12/29/2003 12:00am

    Originally 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..?
    Thanks

    Reply
  • scroll in dialog-based program

    Posted by Legacy on 06/25/2003 12:00am

    Originally 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.

    Reply
  • Dialog bigger than screen - answers included

    Posted by Legacy on 06/06/2003 12:00am

    Originally 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:
    It is definitely possible and you will not meet any
    CDialog limitations. Use my code above and you'll be fine.

    Reply
  • how can i resize the dialog box...plzz help me...

    Posted by Legacy on 03/31/2003 12:00am

    Originally 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...


    Reply
  • CScrollDialog issue with smaller screens

    Posted by Legacy on 01/13/2003 12:00am

    Originally posted by: Ed A

    I found an issue when the display is smaller than the dialog.  The initial call to getclientrect returns the screen size instead of the initial size of the dialog.
    
    

    BOOL CScrollDialog::OnInitDialog()
    {....
    // Get the initial dimensions of the dialog
    GetClientRect(&m_ClientRect);
    ....}

    I fixed this by adding a method to modify the 'target' dialog size after the OnInitDialog call.

    SetClientRect(int width, int height) { m_ClientRect.bottom = height; m_ClientRect.right = width; }

    I call this in onShowWindow if the dialog is bigger than the display, but I've used hardcoded values. I'm not sure how to get the size of the dialog BEFORE windows has made it fit on the display.

    Thanks for posting this, it saved me a buncha time.

    Reply
  • Great Job - Thanks

    Posted by Legacy on 07/29/2002 12:00am

    Originally posted by: Roy Motley

    Great Job - Thanks

    Reply
  • Very handy... great work..

    Posted by Legacy on 05/02/2002 12:00am

    Originally 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

    Reply
  • Thanks, but I'm in trouble...

    Posted by Legacy on 01/15/2002 12:00am

    Originally 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.

    Reply
  • Scroll Dialog invariable to the dialog size

    Posted by Legacy on 12/14/2001 12:00am

    Originally posted by: Praveen

    Is it possible to have the scrolling capability like a true view window. What I mean is that, If I dynamically add controls into the dialog (100s of them), can I scroll and view all the controls, currently it limits the max size to the screen (when maximised)
    
    

    Reply
  • CDialog

    Posted by Legacy on 06/01/2001 12:00am

    Originally posted by: Aajay

    Hi:
    
    

    I have opened a new project with CFormView as a base class & i have inserted Dialog box. I want the HScroll & VScroll functionality on this dialog box. If i choose CScroll as the derived class, DoModal will not work in order to open another Dialog box.

    I want this fuctionality in order to show a large number of outputs in the same window.

    And I want the second way of implementing the DoModal(), to open another dailog box.

    Ajay.

    Reply
  • Loading, Please Wait ...

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • This paper introduces IBM Java on the IBM PowerLinux 7R2 server and describes IBM's implementation of the Java platform, which includes IBM's Java Virtual Machine and development toolkit.

  • As mobile devices have pushed their way into the enterprise, they have brought cloud apps along with them. This app explosion means account passwords are multiplying, which exposes corporate data and leads to help desk calls from frustrated users. This paper will discover how IT can improve user productivity, gain visibility and control over SaaS and mobile apps, and stop password sprawl. Download this white paper to learn: How you can leverage your existing AD to manage app access. Key capabilities to …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds