Link to home
Start Free TrialLog in
Avatar of slapak
slapak

asked on

Auto scroll in CFormView

I have some controls in CFormView, and when I tabbed to those which out of the visible area, I can't see the caret or the active control. How can I make auto scroll depending on the caret's position or the active control position. I would like a global method as posible i.e. avoiding treating each control.
ASKER CERTIFIED SOLUTION
Avatar of piano_boxer
piano_boxer

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of piano_boxer
piano_boxer

A little update to my prev answer:

As a formview is derived from CScrollView, a better way to scroll the view would be to call ScrollToPosition() and GetDeviceScrollPosition() to get the current scroll position instead of using SetScrollInfo() and ScrollWindowEx().
Avatar of slapak

ASKER

All right, but it works only if I don't have radio button or other CButton derivatives. What can I do in this case?
To add support for other that edit controls, you need to look for aditional notification values in the WM_COMMAND handler.
NOTE: You need to enable the notify window flag for every control for this to work!

Button:     BN_SETFOCUS
Edit:       EN_SETFOCUS
ListBox:    LBN_SETFOCUS
ComboBox:   CBN_SETFOCUS

The new common controls send WM_NOTIFY messages to its parent, so
we need to handle them a little different.

Insert the decleration of the handler in the views class header:

    void OnSetFocusNotify( NMHDR * pNotifyStruct, LRESULT * result );

Insert this line above END_MESSAGE_MAP for the views class:

    ON_NOTIFY_RANGE(NM_SETFOCUS, 0, 0xffff, OnSetFocusNotify)


Insert the handler code in the views cpp-file:

void CXxxxView::OnSetFocusNotify( NMHDR * pNotifyStruct, LRESULT * result )
{
    CWnd* pControl = CWnd::FromHandle(pNotifyStruct->hwndFrom);
    ASSERT_VALID(pControl);

    CRect rcControl;
    pControl->GetWindowRect(&rcControl);
    ScreenToClient(&rcControl);

    [ insert code to scroll here ]
}