PRB: OnScroll Change Between Visual C++ 1.0 and 1.5

Last reviewed: July 22, 1997
Article ID: Q117608
1.00 1.50 WINDOWS kbprg kbprb

The information in this article applies to:

  • The Microsoft Foundation Classes (MFC) included with:

        - Microsoft Visual C++ for Windows, versions 1.0 and 1.5
    

SYMPTOMS

Programs overriding or calling CScrollView::OnScroll() work when compiled with Visual C++, version 1.0 (using the MFC, version 2.0), but work incorrectly when compiled with Visual C++, version 1.5 (using the MFC, version 2.5).

For example, the EX04C sample, in the book "Inside Visual C++" (written for Visual C++, version 1.0), calls OnScroll() when it receives arrow-key keystrokes, allowing the user to scroll through the document. This works correctly in Visual C++, version 1.0, but does not work well in Visual C++, version 1.5.

CAUSE

CScrollView::OnScroll() was an undocumented function in Visual C++, version 1.0. It was changed, and the new version was documented for Visual C++, version 1.5.

In Visual C++ , version 1.0, CScrollView::OnScroll() was defined as

   void CScrollView::OnScroll(int nBar, UINT nSBCode, UINT nPos)

where nBar specified SB_HORZ or SB_VERT to handle vertical or horizontal scrolling. The function was also listed under the "Implementation" section of the CScrollView class definition, which generally means that a function is internal and may change in the future.

Visual C++, version 1.5, defines a virtual OnScroll() for both CView and CScrollView. This is now a public and documented function:

   BOOL CScrollView::OnScroll(UINT nScrollCode, UINT nPos, BOOL bDoScroll)

Because the actual underlying parameter types match closely enough for the different versions of OnScroll(), code from Visual C++, version 1.0, can be compiled using Visual C++, version 1.5. However, the two functions differ enough (see the Visual C++ version 1.5 documentation of "OnScroll" for more details on how it works) that such programs generally do not work.

RESOLUTION

Change any calls to or derivations from OnScroll() to correctly pass the needed parameters.

For example, the sample code below (taken from "Inside Visual C++") calls OnScroll() when various arrow keys are pressed in a CScrollView-derived view:

    void CEx04cView::OnKeyDown( UINT nChar, UINT nRepCnt, UINT nFlags )
    {
     switch( nChar )
     {
      case VK_HOME:
        OnScroll( SB_VERT, SB_TOP, 0 );
        OnScroll( SB_HORZ, SB_TOP, 0 );
      break;
      case VK_END:
        OnScroll( SB_VERT, SB_BOTTOM, 0 );
      break;
      case VK_UP:
        OnScroll( SB_VERT, SB_LINEUP, 0 );
      break;
      case VK_DOWN:
        OnScroll( SB_VERT, SB_LINEDOWN, 0 );
      break;
      case VK_PRIOR:
         OnScroll( SB_VERT, SB_PAGEUP, 0 );
      break;
      case VK_NEXT:
        OnScroll( SB_VERT, SB_PAGEDOWN, 0 );
      break;
      case VK_LEFT:
        OnScroll( SB_HORZ, SB_PAGEUP, 0 );
      break;
      case VK_RIGHT:
        OnScroll( SB_HORZ, SB_PAGEDOWN, 0 );
      break;
      default:
        break;
     }
    }

If compiled using Visual C++, version 1.0, this code works correctly. If compiled using Visual C++, version 1.5, the user cannot scroll when the corresponding arrow key is pressed. A correct Visual C++ version 1.5 implementation of this code follows below:

    void CMyView::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
    #define MAKESCROLLWORD(v,h) ( (((BYTE)(short)v)<<8)|(BYTE)h )

     switch(nChar)
     {
      case VK_HOME:
        // High byte is vertical, low byte is horizontal
        // -1 does nothing
        OnScroll( MAKESCROLLWORD(SB_TOP,SB_TOP), 0 );
      break;

      case VK_END:
        OnScroll( MAKESCROLLWORD(SB_BOTTOM, SB_BOTTOM), 0 );
      break;

      case VK_UP:
        OnScroll( MAKESCROLLWORD(SB_LINEUP, -1), 0 );
      break;

      case VK_DOWN:
        OnScroll( MAKESCROLLWORD(SB_LINEDOWN, -1), 0);
      break;

      case VK_PRIOR:    // page up
        OnScroll( MAKESCROLLWORD(SB_PAGEUP, -1), 0 );
      break;

      case VK_NEXT:    // page down
        OnScroll( MAKESCROLLWORD(SB_PAGEDOWN, -1), 0);
      break;

      case VK_LEFT:
        OnScroll( MAKESCROLLWORD(-1,SB_LINEUP), 0 );
      break;

      case VK_RIGHT:
        OnScroll( MAKESCROLLWORD(-1,SB_LINEDOWN), 0 );
      break;
     }

     CScrollView::OnKeyDown(nChar, nRepCnt, nFlags);
    }


Additional reference words: 1.00 1.50 2.00 2.50
KBCategory: kbprg kbprb
KBSubcategory: MfcUI
Keywords : kb16bitonly
Technology : kbMfc


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: July 22, 1997
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.