void TrackBar::OnMouseMove( int mX, int mY, bool mouseState ) { if(m_is_readonly || !canChange()) return; const Rect& rect = GetRect(); if(mouseState) { if(m_is_vertical) { int y = clip(rect.h - (mY), m_slider_radius/2, rect.h - m_slider_radius/2); if(y != m_slider_pix) { m_slider_pix = y; onChange(); } } else { int x = clip(mX, m_slider_radius/2, rect.w-m_slider_radius/2); if(x != m_slider_pix) { m_slider_pix = x; onChange(); } } } else { Rect sr = getSliderRect(); int x = mX - (sr.x+sr.w/2); int y = mY - (sr.y+sr.h/2); if(x*x + y*y < m_slider_radius*m_slider_radius) { m_on_it = true; return; } m_on_it = false; } }
// TODO mouse tracking -- snap back when too far away? // Note how this never overpaints, compared with code in magazine static void onPaint( HWND hwnd ) { const int DFCS_HOT = 4096; const RECT rcSlider = getSliderRect( hwnd ); const RECT rcClient = getClientRect( hwnd ); RECT rcLeft = rcClient; if ( isVertical( hwnd ) ) { rcLeft.bottom = rcSlider.top; } else { rcLeft.right = rcSlider.left; } RECT rcRight = rcClient; if ( isVertical( hwnd ) ) { rcRight.top = rcSlider.bottom; } else { rcRight.left = rcSlider.right; } const int leftColor = getHTCode( hwnd ) == htLeft ? COLOR_BTNSHADOW : getBkColor( hwnd ); const int rightColor = getHTCode( hwnd ) == htRight ? COLOR_BTNSHADOW : getBkColor( hwnd ); PAINTSTRUCT ps = { 0 }; BeginPaint( hwnd, &ps ); fillRect( ps.hdc, hwnd, &rcLeft , leftColor ); fillRect( ps.hdc, hwnd, &rcSlider, getSliderColor( hwnd ) ); fillRect( ps.hdc, hwnd, &rcRight , rightColor ); if ( isVertical( hwnd ) ) { const int width = rcClient.right - rcClient.left; rcLeft .top = rcLeft .bottom - width; rcRight.bottom = rcRight.top + width; } else { const int height = rcClient.bottom - rcClient.top; rcLeft .left = rcLeft .right - height; rcRight.right = rcRight.left + height; } const UINT extraLeft = getHTCode( hwnd ) == htLeftSlider ? DFCS_HOT : 0; const UINT extraRight = getHTCode( hwnd ) == htRightSlider ? DFCS_HOT : 0; DrawFrameControl( ps.hdc, &rcLeft , DFC_SCROLL, extraLeft | (isVertical( hwnd ) ? DFCS_SCROLLDOWN : DFCS_SCROLLRIGHT) ); DrawFrameControl( ps.hdc, &rcRight, DFC_SCROLL, extraRight | (isVertical( hwnd ) ? DFCS_SCROLLUP : DFCS_SCROLLLEFT ) ); EndPaint( hwnd, &ps ); }
static int hitTest( HWND hwnd, int x ) { const RECT rc = getSliderRect( hwnd ); const int left = isVertical( hwnd ) ? rc.top : rc.left ; const int right = isVertical( hwnd ) ? rc.bottom : rc.right; if ( x < left - getHandle( hwnd ) ) { return htLeft; } if ( x <= left ) { return htLeftSlider; } if ( x < right ) { return htSlider; } if ( x <= right + getHandle( hwnd ) ) { return htRightSlider; } return htRight; }