Solved

Auto scroll in CFormView

Posted on 1998-07-01
4
556 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

821 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