trackBar控件如何使滑块的位置为鼠标点击的位置

参考了网友的代码,但是定位不够精准。想了一下原因,必然是控件宽度和track的长度不相同造成的,因此要将两边的宽度差减去才可以。经过测试,这个宽度差是12左右。代码如下:

 private void tkBarTimeElapse_MouseDown(object sender, MouseEventArgs e)
        {
    
    
            // for precision of the position of the track bar  
            int borderW = 12;// key point 
            float barLen = tkBarTimeElapse.Width - borderW;
            float curPos = e.X - tkBarTimeElapse.Location.X - borderW / 2;
            if (curPos > barLen) curPos = barLen;
            if (curPos <0) curPos = 0;
            tkBarTimeElapse.Value = (int)( curPos*      	Convert.ToDouble(tkBarTimeElapse.Maximum) / barLen);

            。。。。

        }

如果有可以测量这个宽度差的完美方法,请不吝赐教。
MaraSun 于 2020-10-29
北京海淀泛五地区

猜你喜欢

转载自blog.csdn.net/Uman/article/details/109355541
今日推荐