像 VLC 一样的音量滑块 [英] Volume Slider like VLC

查看:18
本文介绍了像 VLC 一样的音量滑块的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻找外观和行为与 VLC 的滑块相似的音量滑块.

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

我发现以下关于如何设置滑块样式的帖子:Volume Slider CustomControl
但我也想要同样的行为......

I found the following post about how to style the slider: Volume Slider CustomControl
but I also want the same behavior...

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

What is the difference between the behaviors: When you click on the slider [at the WPF] and move the mouse on the slider area (while mouse button still being hold) it should move the slider also to the position of the mouse on the slider.

我找不到怎么做.也许我应该使用与 Slider 不同的东西?

I couldn't find how to do it.. maybe I should use something different than Slider?

感谢您的帮助!

推荐答案

滑块上有一个名为 IsMoveToPointEnabled 将滑块设置为正确的值,但仅当您单击它时,它不会在您拖动时更新.

There is a property on the slider called IsMoveToPointEnabled that sets the slider to the correct value but only when you click it doesn't update as you drag.

要在拖动时更新,您必须在鼠标移动时自己更新值,方法 Track.ValueFromPoint 为您提供正确的值,轨道是滑块模板的一部分.

To update as you drag you have to update the value yourself when the mouse is moved, the method Track.ValueFromPoint gives you the correct value, the track is part of the sliders template.

例子

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,但它不会捕获鼠标,因此您可以根据需要删除它们.即使鼠标离开控件(类似于滚动条的工作方式),捕获鼠标也可以更改值.

The OnPreviewMouseUp/Down overrides capture the mouse, I tried VLC and that doesn't capture the mouse so you could remove them if you wanted. Capturing the mouse allows the value to change even if the mouse leaves the control similar to how scrollbars work.

这篇关于像 VLC 一样的音量滑块的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆