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)
{
#if defined(_WIN32_WCE)
ASSERT(m_nMapMode == MM_TEXT);
#else
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);
}
#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);
}