Click here to Skip to main content
13,351,050 members (55,439 online)
Click here to Skip to main content
Add your own
alternative version


18 bookmarked
Posted 23 Mar 2008

Scroll Window

, 23 Mar 2008
Rate this:
Please Sign up or sign in to vote.
A scroll window that surpasses the 16-bit limit and can be hosted by a dialog.


CScrollView comes in handy whenever you need to provide scrolling capability to your view. However, there are several limitations. First, CScrollView is derived from CView, so it is designed to work with the document-view architecture. If you attempt to nest a CScrollView inside a normal CWnd or a CDialog, you will get into a big heap of trouble. If your application requires simple and lightweight scrolling support, CScrollView becomes awkward and costly. Second, the size of the scrolling region is bound to the signed 16-bit limit. Your application may require painting on a large logical area. A map editor or a graphics design application is a good example for such a situation. CScrollWindow was created to address the above problems.


Basing on the MFC source code of CScrollView, I made some modifications to achieve my targets. To avoid creating bugs, I tried to make as few changes as possible. However, I know that I am just as imperfect as CScrollWindow, so bugs may still exist.

Unlike CScrollView, my class CScrollWindow is derived from CWnd so that it can be put inside any other CWnd. If you want it on a view, nest it inside a CView or any derived class. In this way, you can still make CScrollWindow play nicely with the document-view architecture, of cource with more effort than if you use CScrollView. However, flexibility is what you get in exchange for the effort.

CScrollView uses functions LDtoDP and DPtoLP to convert points from device unit to logical unit and vice versa. These functions accept only 16-bit values. Therefore, you get errors when your total width or height is greater than 32767. To avoid these errors, I do not use these functions, which means that CScrollWindow does not support mapping modes. The only supported unit of length is pixel. If you want to support other units of measure or even zooming, you have to implement it on your own, given the background that CScrollWindow provides to you.

One important point is that CScrollView uses 16-bit position data provided by WM_HSCROLL and WM_VSCROLL. CScrollWindow instead calls GetScrollInfo to get 32-bit position data.

Using the Code

Although CScrollWindow works with any CWnd and derivatives, including CDialog, I embed it in a CFrameWnd in the demo project in this article. You would typically add a member of the type CScrollWindow or derivatives in your CFrameWnd.

CScrollWindow m_ScrollWindow;

In the OnCreate of your CFrameWnd, create the scroll window and set the scroll sizes.

if (!m_ScrollWindow.Create(NULL, NULL, AFX_WS_DEFAULT_VIEW,
    CRect(0, 0, 0, 0), this, AFX_IDW_PANE_FIRST, NULL))
    TRACE0("Failed to create scroll window\n");
    return -1;
m_ScrollWindow.SetScrollSizes(CSize(65536, 65536));

Running the Demo

The demo creates a scroll window with a large total size. Scroll around using the scroll bars to verify that you can view any part of the total area.


March 23, 2008

  • Uploaded the first version

September 12, 2015

  • Changed the project name from ScrollWindowTest to ScrollWindowSample
  • Converted the project format so that it can be built in Visual Studio 2008
  • Removed the SystemInformation class, thereby simplifying the code
  • Modified the drawing code in the demo project
  • Revised the tip


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Tu (Le Hong)
No Biography provided

You may also be interested in...

Comments and Discussions

QuestionHave you consider to post this as a tip? Pin
Nelek11-Sep-15 22:43
protectorNelek11-Sep-15 22:43 
AnswerRe: Have you consider to post this as a tip? Pin
Tu (Le Hong)13-Sep-15 2:55
memberTu (Le Hong)13-Sep-15 2:55 
GeneralRe: Have you consider to post this as a tip? Pin
Nelek13-Sep-15 23:04
protectorNelek13-Sep-15 23:04 
GeneralMy vote of 2 Pin
RDSchaefer16-Apr-12 15:32
memberRDSchaefer16-Apr-12 15:32 
GeneralThis class CScrollWindow was exactly what I was looking for Pin
Blair Whitney19-May-08 16:46
memberBlair Whitney19-May-08 16:46 
This class CScrollWindow was exactly what I was looking for. The MFC application I am adding to cannot use CScrollView - is not MDI and does not support child windows of the main frame of type WS_CHILD -> instead child windows must be WS_POPUP. I wanted a CWnd WS_POPOP child window that I could scroll, but did not want to write all the scrolling code myself. Thank you very much for publishing this source code, it saved me some time and is very useful.
GeneralSome suggestions Pin
Sam Levy24-Mar-08 13:11
memberSam Levy24-Mar-08 13:11 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.180111.1 | Last Updated 23 Mar 2008
Article Copyright 2008 by Tu (Le Hong)
Everything else Copyright © CodeProject, 1999-2018
Layout: fixed | fluid