Microsoft KB Archive/219282

= BUG: Calling ScrollToPosition in CFormView Derived Class Moves the Scroll Bars but Not the Window =

Article ID: 219282

Article Last Modified on 11/21/2006

-

APPLIES TO

 Microsoft Foundation Class Library 4.2, when used with:  Microsoft Windows CE Embedded Toolkit for Visual C++ 5.0

 Microsoft Windows CE Toolkit for Visual C++ 6.0 

-



This article was previously published under Q219282



SYMPTOMS
If you create an MFC Windows CE application that has its view derived from CFormView, and call CFormView::ScrollToPosition, the function will move the scroll bars but not the window. A good time to call this function is when a control on the CFormView's dialog is outside of the view, and the user presses Tab. The new control should come into view by calling CFormView::ScrollToPosition, but unfortunately it does not.



CAUSE
This is caused by the implementation of ::ScrollWindowEx not moving the window correctly. ::ScrollWindowEx is eventually called when CFormView::ScrollToPosition is called, and is part of the Windows CE API.



RESOLUTION
In order to get the CFormView window to move correctly, each child control on the dialog of the CFormView is moved to a new position to make it look like the window has been moved.

The following external function must be defined, because it is used by the helper functions added to the CFormView derived class.

extern HWND WINAPI   wce_GetNextWindow(HWND hWnd, UINT nDirection); The following two functions are helper functions that will move each control on the dialog of the CFormView derived class to a new position.

void CMyFormViewView::ScrollToPos(POINT pt) {   ASSERT(m_nMapMode == MM_TEXT); ASSERT(m_nMapMode > 0);    // not allowed for shrink to fit if (m_nMapMode != MM_TEXT) {       CWindowDC dc(NULL); dc.SetMapMode(m_nMapMode); dc.LPtoDP((LPPOINT)&pt); }
 * 1) if defined(_WIN32_WCE)
 * 1) else
 * 1) endif

// now in device coordinates - limit if out of range int xMax = GetScrollLimit(SB_HORZ); int yMax = GetScrollLimit(SB_VERT); if (pt.x < 0) pt.x = 0; else if (pt.x > xMax) pt.x = xMax; if (pt.y < 0) pt.y = 0; else if (pt.y > yMax) pt.y = yMax;

ScrollToDevicePos(pt); }

void CMyFormViewView::ScrollToDevicePos(POINT ptDev) {   ASSERT(ptDev.x >= 0); ASSERT(ptDev.y >= 0);

// Note: ScrollToDevicePosition can and is used to scroll out-of-range // areas as far as CScrollView is concerned -- specifically in    //  the print-preview code. Since OnScrollBy makes sure the range is   //  valid, ScrollToDevicePosition does not vector through OnScrollBy.

int xOrig = GetScrollPos(SB_HORZ); SetScrollPos(SB_HORZ, ptDev.x); int yOrig = GetScrollPos(SB_VERT); SetScrollPos(SB_VERT, ptDev.y);

HWND hWndChild = ::GetWindow(m_hWnd, GW_CHILD); if (hWndChild != NULL) {       for (hWndChild != NULL;            hWndChild = ::wce_GetNextWindow(hWndChild, GW_HWNDNEXT)) {           CRect rect; ::GetWindowRect(hWndChild, &rect); ScreenToClient(&rect); ::SetWindowPos(hWndChild, NULL, rect.left-(ptDev.x-xOrig), rect.top-(ptDev.y-yOrig),               0, 0, SWP_NOSIZE|SWP_NOACTIVATE|SWP_NOZORDER); }   } }

The function ScrollToPos can be called when the tab key is pressed. The following implementation for the override of CFormView::PreTranslateMessage calls this function in order to move all the controls, so that it looks like the window was moved.

BOOL CMyFormViewView::PreTranslateMessage(MSG* pMsg) {   if(pMsg->message == WM_KEYUP) {       if(pMsg->wParam == VK_TAB) {           CWnd* pWnd = GetFocus; CRect rcChild, rcParent, rcTemp; pWnd->GetWindowRect(&rcChild); GetWindowRect(&rcParent);

//Get document size. Assume mapping mode is MM_TEXT and ViewPort //and Window origins have not been set so we can also assume LP==DP. CSize size = GetTotalSize;

// Determine if scroll bars are visible // and subtract their size from the view. if (size.cx > rcParent.right - rcParent.left) rcParent.bottom -= GetSystemMetrics(SM_CYHSCROLL); if (size.cy > rcParent.bottom - rcParent.top) rcParent.right -= GetSystemMetrics(SM_CXVSCROLL);

// Succeed only if no part of the child appears in the parent. rcTemp.IntersectRect(rcChild, rcParent);

if (rcTemp != rcChild) {               POINT pt;

pt.x = GetDeviceScrollPosition.x;               pt.y = GetDeviceScrollPosition.y;

if (rcChild.top < rcParent.top) {                   TRACE(_T("control is above the client area\n")); pt.y -= rcParent.top - rcChild.top + GetSystemMetrics(SM_CXEDGE); }               else if (rcChild.bottom > rcParent.bottom) {                   TRACE(_T("control is below the client area\n")); // If the control is taller than the current view // scroll up, sacrificing the bottom of the control. pt.y += min(rcChild.top - rcParent.top,                   rcChild.bottom - rcParent.bottom +                    GetSystemMetrics(SM_CYHSCROLL) +                    GetSystemMetrics(SM_CXEDGE)); }

if (rcChild.left < rcParent.left) {                   TRACE(_T("control is left of the client area\n")); pt.x -= rcParent.left - rcChild.left + GetSystemMetrics(SM_CYEDGE); }               else if (rcChild.right > rcParent.right) {                   TRACE(_T("control is right of the client area\n")); // If the control is wider than the current view // scroll right, sacrificing the right edge of the control. pt.x += min(rcChild.left - rcParent.left,                   rcChild.right - rcParent.right +                    GetSystemMetrics(SM_CXVSCROLL) +                    GetSystemMetrics(SM_CYEDGE)); }               ScrollToPos(pt);  //Assume mapping mode is MM_TEXT.

}       }   //  Tab Key }  //  Key Down return CFormView::PreTranslateMessage(pMsg); }

<div class="status_section">

STATUS
Microsoft has confirmed that this is a bug in the Microsoft products that are listed at the beginning of this article.

<div class="moreinformation_section">

Steps to Reproduce Behavior
<ol> Create an MFC Windows CE application with the view class derived from CFormView.</li> Make the dialog resource long enough so it is bigger than the screen on the Windows CE device.</li> Put an Edit control on the far left and far right of the dialog resource.</li>  Add the following code to the PreTranslateMessage handler in the CFormView derived class.

BOOL CMyFormViewView::PreTranslateMessage(MSG* pMsg) {   if(pMsg->message == WM_KEYUP) {       if(pMsg->wParam == VK_TAB) {           CWnd* pWnd = GetFocus; CRect rcChild, rcParent, rcTemp; pWnd->GetWindowRect(&rcChild); GetWindowRect(&rcParent);

//Get document size. Assume mapping mode is MM_TEXT and ViewPort //and Window origins have not been set so we can also assume LP==DP. CSize size = GetTotalSize;

// Determine if scroll bars are visible // and subtract their size from the view. if (size.cx > rcParent.right - rcParent.left) rcParent.bottom -= GetSystemMetrics(SM_CYHSCROLL); if (size.cy > rcParent.bottom - rcParent.top) rcParent.right -= GetSystemMetrics(SM_CXVSCROLL);

// Succeed only if no part of the child appears in the parent. rcTemp.IntersectRect(rcChild, rcParent);

if (rcTemp != rcChild) {               POINT pt;

pt.x = GetDeviceScrollPosition.x;               pt.y = GetDeviceScrollPosition.y;

if (rcChild.top < rcParent.top) {                   TRACE(_T("control is above the client area\n")); pt.y -= rcParent.top - rcChild.top + GetSystemMetrics(SM_CXEDGE); }               else if (rcChild.bottom > rcParent.bottom) {                   TRACE(_T("control is below the client area\n")); // If the control is taller than the current view // scroll up, sacrificing the bottom of the control. pt.y += min(rcChild.top - rcParent.top,                   rcChild.bottom - rcParent.bottom +                    GetSystemMetrics(SM_CYHSCROLL) +                    GetSystemMetrics(SM_CXEDGE)); }

if (rcChild.left < rcParent.left) {                   TRACE(_T("control is left of the client area\n")); pt.x -= rcParent.left - rcChild.left + GetSystemMetrics(SM_CYEDGE); }               else if (rcChild.right > rcParent.right) {                   TRACE(_T("control is right of the client area\n")); // If the control is wider than the current view // scroll right, sacrificing the right edge of the control. pt.x += min(rcChild.left - rcParent.left,                   rcChild.right - rcParent.right +                    GetSystemMetrics(SM_CXVSCROLL) +                    GetSystemMetrics(SM_CYEDGE)); }               ScrollToPosition(pt);  //Assume mapping mode is MM_TEXT.

}       }   //  Tab Key }  //  Key Down return CFormView::PreTranslateMessage(pMsg); }                       </li> Build the application and run it on the device.</li> Set focus to the edit control on the left, and press Tab.</li> Notice that the horizontal scroll bar moves but the view does not.</li></ol>

Keywords: kbbug kbfix kbnoupdate KB219282

-

[mailto:TECHNET@MICROSOFT.COM Send feedback to Microsoft]

© Microsoft Corporation. All rights reserved.