Buggy code:
TVITEM tvItem = new TVITEM ( ) ; tvItem . hItem = lpht . hItem ; tvItem . mask = ( ( OS . TVIF_HANDLE ) | ( OS . TVIF_PARAM ) ) | ( OS . TVIF_STATE ) ; tvItem . stateMask = OS . TVIS_STATEIMAGEMASK ; OS . SendMessage ( handle , TVM_GETITEM , 0 , tvItem ) ; int state = ( tvItem . state ) > > 12 ; if ( ( state & 1 ) != 0 ) { state ++ ; } else { -- state ; } tvItem . state = state << 12 ; OS . SendMessage ( handle , TVM_SETITEM , 0 , tvItem ) ; if ( ! ( OS . IsWinCE ) ) { int id = tvItem . hItem ; if ( ( OS . COMCTL32_MAJOR ) >= 6 ) { id = OS . SendMessage ( handle , TVM_MAPHTREEITEMTOACCID , tvItem . hItem , 0 ) ; } OS . NotifyWinEvent ( EVENT_OBJECT_FOCUS , handle , OBJID_CLIENT , ( ( int ) ( id ) ) ) ; } Event event = new Event ( ) ; event . item = _getItem ( tvItem . hItem , ( ( int ) ( tvItem . lParam ) ) ) ; event . detail = SWT . CHECK ; postEvent ( Selection , event ) ; return LRESULT . ZERO ; } } boolean selected = false ; boolean fakeSelection = false ; if ( ( lpht . hItem ) != 0 ) { if ( ( ( style ) & ( SWT . FULL_SELECTION ) ) != 0 ) { int bits = OS . GetWindowLong ( handle , GWL_STYLE ) ; if ( ( bits & ( OS . TVS_FULLROWSELECT ) ) == 0 ) fakeSelection = true ; } else { if ( hooks ( MeasureItem ) ) { selected = hitTestSelection ( lpht . hItem , lpht . x , lpht . y ) ; if ( selected ) { if ( ( ( lpht . flags ) & ( OS . TVHT_ONITEM ) ) == 0 ) fakeSelection = true ; } } } } if ( ( ! selected ) && ( ( ( style ) & ( SWT . FULL_SELECTION ) ) == 0 ) ) { if ( ( ( lpht . flags ) & ( OS . TVHT_ONITEM ) ) == 0 ) { Display display = this . display ; display . captureChanged = false ; if ( ! ( sendMouseEvent ( MouseDown , 1 , handle , WM_LBUTTONDOWN , wParam , lParam ) ) ) { if ( ( ! ( display . captureChanged ) ) && ( ! ( isDisposed ( ) ) ) ) { if ( ( OS . GetCapture ( ) ) != ( handle ) ) OS . SetCapture ( handle ) ; } return LRESULT . ZERO ; } int code = callWindowProc ( handle , WM_LBUTTONDOWN , wParam , lParam ) ; if ( ( ! ( display . captureChanged ) ) && ( ! ( isDisposed ( ) ) ) ) { if ( ( OS . GetCapture ( ) ) != ( handle ) ) OS . SetCapture ( handle ) ; } return new LRESULT ( code ) ; } } TVITEM tvItem = new TVITEM ( ) ; tvItem . mask = ( OS . TVIF_HANDLE ) | ( OS . TVIF_STATE ) ; tvItem . stateMask = OS . TVIS_SELECTED ; boolean hittestSelected = false ; if ( ( ( style ) & ( SWT . MULTI ) ) != 0 ) { tvItem . hItem = lpht . hItem ; OS . SendMessage ( handle , TVM_GETITEM , 0 , tvItem ) ; <START_BUG> hittestSelected = selected || ( ( ( tvItem . state ) & ( OS . TVIS_SELECTED ) ) != 0 ) ; <END_BUG> } int hOldItem = OS . SendMessage ( handle , TVM_GETNEXTITEM , TVGN_CARET , 0 ) ; if ( ( ( style ) & ( SWT . MULTI ) ) != 0 ) { tvItem . hItem = hOldItem ; OS . SendMessage ( handle , TVM_GETITEM , 0 , tvItem ) ; if ( hittestSelected || ( ( wParam & ( OS . MK_CONTROL ) ) != 0 ) ) { int uiState = ( ( int ) ( OS . SendMessage ( handle , WM_QUERYUISTATE , 0 , 0 ) ) ) ; if ( ( uiState & ( OS . UISF_HIDEFOCUS ) ) != 0 ) { OS . SendMessage ( handle , WM_CHANGEUISTATE , UIS_INITIALIZE , 0 ) ; } OS . UpdateWindow ( handle ) ; OS . DefWindowProc ( handle , WM_SETREDRAW , 0 , 0 ) ; } else { deselectAll ( ) ; } } Display display = this . display ; display . captureChanged = false ; if ( ! ( sendMouseEvent ( MouseDown , 1 , handle , WM_LBUTTONDOWN , wParam , lParam ) ) ) { if ( ( ! ( display . captureChanged ) ) && ( ! ( isDisposed ( ) ) ) ) { if ( ( OS . GetCapture ( ) ) != ( handle ) ) OS . SetCapture ( handle ) ; } return LRESULT . ZERO ; } hSelect = lpht . hItem ; dragStarted = gestureCompleted = false ; ignoreDeselect = ignoreSelect = true ; int code = callWindowProc ( handle , WM_LBUTTONDOWN , wParam , lParam ) ; int hNewItem = OS . SendMessage ( handle , TVM_GETNEXTITEM , TVGN_CARET , 0 ) ; if ( fakeSelection ) { if ( ( hOldItem == 0 ) || ( ( hNewItem == hOldItem ) && ( ( lpht . hItem ) != hOldItem ) ) ) { OS . SendMessage ( handle , TVM_SELECTITEM , TVGN_CARET , lpht . hItem ) ; hNewItem = OS . SendMessage ( handle , TVM_GETNEXTITEM , TVGN_CARET , 0 ) ; }

Fix:
hittestSelected = ( ( tvItem . state ) & ( OS . TVIS_SELECTED ) ) != 0 ;