Buggy code:
tvItem . mask = OS . TVIF_STATE ; OS . SendMessage ( hwnd , TVM_GETITEM , 0 , tvItem ) ; state = tvItem . state ; } else { state = OS . SendMessage ( hwnd , TVM_GETITEMSTATE , handle , TVIS_EXPANDED ) ; } if ( ( ( state & ( OS . TVIS_EXPANDED ) ) != 0 ) == expanded ) return ; RECT oldRect = null ; RECT [ ] rects = null ; SCROLLINFO oldInfo = null ; int count = 0 ; int hBottomItem = 0 ; boolean redraw = false ; boolean noScroll = true ; int hTopItem = OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_FIRSTVISIBLE , 0 ) ; if ( noScroll && ( hTopItem != 0 ) ) { oldInfo = new SCROLLINFO ( ) ; oldInfo . cbSize = SCROLLINFO . sizeof ; oldInfo . fMask = OS . SIF_ALL ; if ( ! ( OS . GetScrollInfo ( hwnd , SB_HORZ , oldInfo ) ) ) { oldInfo = null ; } if ( ( ( parent . drawCount ) == 0 ) && ( OS . IsWindowVisible ( hwnd ) ) ) { boolean noAnimate = true ; count = OS . SendMessage ( hwnd , TVM_GETVISIBLECOUNT , 0 , 0 ) ; rects = new RECT [ count + 1 ] ; int hItem = hTopItem ; int index = 0 ; while ( ( ( hItem != 0 ) && ( noAnimate || ( hItem != ( handle ) ) ) ) && ( index < count ) ) { RECT rect = new RECT ( ) ; if ( OS . TreeView_GetItemRect ( hwnd , hItem , rect , true ) ) { rects [ ( index ++ ) ] = rect ; } hItem = OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_NEXTVISIBLE , hItem ) ; } if ( noAnimate || ( hItem != ( handle ) ) ) { redraw = true ; count = index ; hBottomItem = hItem ; oldRect = new RECT ( ) ; OS . GetClientRect ( hwnd , oldRect ) ; int topHandle = parent . topHandle ( ) ; OS . UpdateWindow ( topHandle ) ; OS . DefWindowProc ( topHandle , WM_SETREDRAW , 0 , 0 ) ; if ( hwnd != topHandle ) { OS . UpdateWindow ( hwnd ) ; OS . DefWindowProc ( hwnd , WM_SETREDRAW , 0 , 0 ) ; } } } } int hOldItem = OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_CARET , 0 ) ; parent . ignoreExpand = true ; OS . SendMessage ( hwnd , TVM_EXPAND , ( expanded ? OS . TVE_EXPAND : OS . TVE_COLLAPSE ) , handle ) ; parent . ignoreExpand = false ; if ( noScroll && ( hTopItem != 0 ) ) { boolean collapsed = false ; if ( ! expanded ) { RECT rect = new RECT ( ) ; while ( ( hTopItem != 0 ) && ( ! ( OS . TreeView_GetItemRect ( hwnd , hTopItem , rect , false ) ) ) ) { hTopItem = OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_PARENT , hTopItem ) ; collapsed = true ; } } boolean scrolled = true ; if ( hTopItem != 0 ) { OS . SendMessage ( hwnd , TVM_SELECTITEM , TVGN_FIRSTVISIBLE , hTopItem ) ; scrolled = hTopItem != ( OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_FIRSTVISIBLE , 0 ) ) ; } if ( ( ( ! collapsed ) && ( ! scrolled ) ) && ( oldInfo != null ) ) { SCROLLINFO newInfo = new SCROLLINFO ( ) ; newInfo . cbSize = SCROLLINFO . sizeof ; newInfo . fMask = OS . SIF_ALL ; if ( OS . GetScrollInfo ( hwnd , SB_HORZ , newInfo ) ) { if ( ( oldInfo . nPos ) != ( newInfo . nPos ) ) { <START_BUG> int lParam = ( OS . SB_THUMBPOSITION ) | ( ( ( oldInfo . nPos ) << 16 ) & - 65536 ) ; <END_BUG> OS . SendMessage ( hwnd , WM_HSCROLL , lParam , 0 ) ; } } } if ( redraw ) { boolean fixScroll = false ; if ( ( ! collapsed ) && ( ! scrolled ) ) { RECT newRect = new RECT ( ) ; OS . GetClientRect ( hwnd , newRect ) ; if ( OS . EqualRect ( oldRect , newRect ) ) { int hItem = hTopItem ; int index = 0 ; while ( ( hItem != 0 ) && ( index < count ) ) { RECT rect = new RECT ( ) ; if ( OS . TreeView_GetItemRect ( hwnd , hItem , rect , true ) ) { if ( ! ( OS . EqualRect ( rect , rects [ index ] ) ) ) { break ; } } hItem = OS . SendMessage ( hwnd , TVM_GETNEXTITEM , TVGN_NEXTVISIBLE , hItem ) ; index ++ ; } fixScroll = ( index == count ) && ( hItem == hBottomItem ) ; } } int topHandle = parent . topHandle ( ) ; OS . DefWindowProc ( topHandle , WM_SETREDRAW , 1 , 0 ) ; if ( hwnd != topHandle ) { OS . DefWindowProc ( hwnd , WM_SETREDRAW , 1 , 0 ) ; } if ( fixScroll ) { parent . updateScrollBar ( ) ; SCROLLINFO info = new SCROLLINFO ( ) ; info . cbSize = SCROLLINFO . sizeof ; info . fMask = OS . SIF_ALL ; if ( OS . GetScrollInfo ( hwnd , SB_VERT , info ) ) { OS . SetScrollInfo ( hwnd , SB_VERT , info , true ) ; } if ( ( handle ) == hBottomItem ) { RECT rect = new RECT ( ) ;

Fix:
int lParam = OS . MAKELPARAM ( SB_THUMBPOSITION , oldInfo . nPos ) ;