views:

45

answers:

2

Hi, I am working on dialog based MFC application in WinCE. I created few controls in a dialog and scrolled down. When i scroll up again, the controls in the first screen got disappeared.

Controls getting created in OnInitDialog() like below at coordinates (50,10)

test->Create(_T("Title"), WS_CHILD|WS_VISIBLE, CRect(50,10,200,40), this, ID_TITLE);

Scroll handling i am doing in OnVScroll() like below

 switch(nSBCode)
 {
  case SB_LINEDOWN:
  {
   if(nPos < max)
   {
    ScrollWindowEx(0, SCROLLDOWN_LINE_STEPSIZE, CRect(0,0, rect.right - 25, rect.bottom), NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE); 
    pScrollBar->SetScrollPos(nPos - SCROLLDOWN_LINE_STEPSIZE); //nPos+10
   }
   break;
  }
  case SB_LINEUP:
  {
   if(nPos > min)
   {
    ScrollWindowEx(0, SCROLLUP_LINE_STEPSIZE, CRect(0,0, rect.right - 25, rect.bottom), NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE); 
    pScrollBar->SetScrollPos(nPos - SCROLLUP_LINE_STEPSIZE); //Ex: nPos-10
   }
   break;
  }

 default:
  printf("Notimplemented");
  break;
 }

I am handling Scroll down and scroll up. While scrolling down, all the controls in Dialog are shown. But while scrolling up, the controls at the top got disappeared.

What's going on ? Should i implement OnPaint() method for drawing the controls each time i scroll ? If so, how ?

+1  A: 

I have the following code that works fine. I hope it will help you.

LRESULT CMyWindow::OnVScroll( UINT code, UINT position )
{
    SCROLLINFO info = { sizeof( SCROLLINFO ), SIF_ALL };
    GetScrollInfo( m_wnd, SB_VERT, &info );
    int previous_pos = info.nPos;
    switch( code )
    {
    case SB_TOP:
        info.nPos = 0;
        break;
    case SB_BOTTOM:
        info.nPos = info.nMax - info.nPage;
        break;
    case SB_LINEDOWN:
        info.nPos = min( info.nPos + 1, info.nMax - (int)info.nPage );
        break;
    case SB_LINEUP:
        info.nPos = max( info.nPos - 1, info.nMin );
        break;
    case SB_PAGEDOWN:
        info.nPos = min( info.nPos + (int)info.nPage, info.nMax - (int)info.nPage );
        break;
    case SB_PAGEUP:
        info.nPos = max( info.nPos - (int)info.nPage, info.nMin );
        break;
    case SB_THUMBTRACK:
    case SB_THUMBPOSITION:
        info.nPos = position;
        break;
    }
    int offset = previous_pos - info.nPos;
    if( offset != 0 )
    {
        ScrollWindowEx( m_wnd, 0, offset, NULL, NULL, NULL, NULL, SW_SCROLLCHILDREN | SW_INVALIDATE | SW_ERASE );
        SetScrollPos( m_wnd, SB_VERT, info.nPos, FALSE );
    }
    return 0L;
}
Tassos
@Tassos: Thank you so much. +1 and acceptance of answer.
bjskishore123
@bjskishore123 I'm glad it helped :)
Tassos
+1  A: 

From the ScrollWindowEx docs:

dy
Specifies the amount, in device units, of vertical scrolling. This parameter must be a negative value to scroll up.

I bolded the relevant phrase.

Hans Passant
@Hans: Yes, i am using a negative value only for scroll down. Sorry i didn't show the macro values in code.
bjskishore123