locked
TrackBar Snapping RRS feed

  • Question

  • Hi

    Env: Win10, VC++, MFC

    I create a TrackBar (CSliderCtrl) and want snap it to each 100 step. but the result is not work and the slider is stuck or move smoothly. In the MSDN wrote the TRBN_THUMBPOSCHANGING notification result is ignored but i think is is important to snap!

    my code is:

    void CZoomCtrl::OnSliderChanging(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	NMTRBTHUMBPOSCHANGING* pNMTR = (NMTRBTHUMBPOSCHANGING*)pNMHDR;
    	*pResult = 1;
    
    	int nPos = pNMTR->dwPos;
    	
    	if ((nPos % 100) == 0)
    	{		
    		nPos = (int)(nPos / 100) * 100;
    		m_wndZoomSlider.SetPos(nPos);
    		*pResult = 0;
    	}
    	
    }

    Thanks

    Saturday, July 9, 2016 6:01 PM

Answers

  • I solved the problem. The correct code is below.

    void CZoomCtrl::OnSliderChanging(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	NMTRBTHUMBPOSCHANGING* pNMTR = (NMTRBTHUMBPOSCHANGING*)pNMHDR;
    
    	if (pNMTR->nReason == TB_ENDTRACK)
    		return;
    		
    	int nPos = pNMTR->dwPos;	
    	if ((nPos % 100) < 50) // do not use equal (==) here ;-)
    	{		
    		nPos = nPos / 100 * 100;
    		m_wndZoomSlider.SetPos(nPos);
    	}
    
    	*pResult = 1;
    }
    

    • Marked as answer by drjackool Sunday, July 10, 2016 7:50 AM
    Sunday, July 10, 2016 7:50 AM