像 VLC 一样的音量滑块

2024-01-05

I am searching for Volume Slider that looks and behave just like VLC's slider.
enter image description here

我发现以下关于如何设置滑块样式的帖子:音量滑块自定义控件 https://stackoverflow.com/questions/8547248/volume-slider-customcontrol
但我也想要同样的行为......

行为之间有什么区别: 当您单击滑块 [在 WPF 处] 并将鼠标移动到滑块区域上(同时按住鼠标按钮)时,它也应该将滑块移动到滑块上鼠标的位置。

我找不到如何做到这一点..也许我应该使用与 Slider 不同的东西?

谢谢您的帮助!


滑块上有一个属性称为是否启用移动到点 http://msdn.microsoft.com/en-us/library/system.windows.controls.slider.ismovetopointenabled.aspx它将滑块设置为正确的值,但仅当您单击时它不会在拖动时更新。

要在拖动时更新,您必须在移动鼠标时自己更新值,该方法跟踪.来自点的值 http://msdn.microsoft.com/en-us/library/system.windows.controls.primitives.track.valuefrompoint.aspx为您提供正确的值,轨道是滑块模板的一部分。

Example

public class DraggableSlider : Slider
{
    public DraggableSlider()
    {
        this.IsMoveToPointEnabled = true;
    }

    private Track track;
    public override void OnApplyTemplate()
    {
        base.OnApplyTemplate();
        track = Template.FindName("PART_Track", this) as Track;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);
        if(e.LeftButton == MouseButtonState.Pressed && track != null)
        {
            Value = track.ValueFromPoint(e.GetPosition(track));
        }
    }


    protected override void OnPreviewMouseDown(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseDown(e);
        ((UIElement)e.OriginalSource).CaptureMouse();
    }

    protected override void OnPreviewMouseUp(MouseButtonEventArgs e)
    {
        base.OnPreviewMouseUp(e);
        ((UIElement)e.OriginalSource).ReleaseMouseCapture();
    }
}

OnPreviewMouseUp/Down 会覆盖捕获鼠标,我尝试了 VLC,但它不会捕获鼠标,因此您可以根据需要删除它们。即使鼠标离开控件,捕获鼠标也可以更改值,类似于滚动条的工作方式。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

像 VLC 一样的音量滑块 的相关文章

随机推荐