Solved

Auto scroll in CFormView

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

Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Process filename extension 3 186
format the code in java 6 95
Unix Command -- Challenging  question 7 91
Making an alias 7 95
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

809 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