Solved

Auto scroll in CFormView

Posted on 1998-07-01
4
546 Views
Last Modified: 2013-11-19
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.
0
Comment
Question by:slapak
  • 3
4 Comments
 
LVL 4

Accepted Solution

by:
piano_boxer earned 100 total points
ID: 1318609
Add this WM_COMMAND hander to your CFormView class. It will up and down it the control receiving focus is not visible.

NOTE: and the variable m_nScrollPos to the class header and initialize it to zero in the constructor;

BOOL CXxxxView::OnCommand(WPARAM wParam, LPARAM lParam)
    {
        if( lParam && HIWORD(wParam)==EN_SETFOCUS )
        {
            MessageBeep(-1);

            CWnd* pControl = CWnd::FromHandle((HWND)lParam);
            ASSERT_VALID(pControl);

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

            int nScroll = 0;
            if(rcControl.top < 0)
                nScroll = -rcControl.top;
            else
            {
                CRect rc;
                GetClientRect(&rc);
                if(rcControl.bottom > rc.bottom)
                    nScroll = rc.bottom-rcControl.bottom;
            }

            if(nScroll)
            {
                m_nScrollPos = m_nScrollPos-nScroll;
                SCROLLINFO si;
                si.cbSize = sizeof(si);
                si.fMask = SIF_POS;
                si.nPos = m_nScrollPos;
                SetScrollInfo(SB_VERT, &si);
                ScrollWindowEx(0, nScroll, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN|SW_INVALIDATE|SW_ERASE);
            }
        }
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1318610
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().
0
 

Author Comment

by:slapak
ID: 1318611
All right, but it works only if I don't have radio button or other CButton derivatives. What can I do in this case?
0
 
LVL 4

Expert Comment

by:piano_boxer
ID: 1318612
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 ]
}
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Show shut-down message as Windows 8 shuts down. 9 85
bigDiff challenge 17 84
has77  challenge 9 73
Problem to event 3 78
In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now