如何使用单击事件更新进度条 [英] How to update the a progress bar with a click event

查看:119
本文介绍了如何使用单击事件更新进度条的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我需要编写一个自定义进度条,用于跟踪正在重播的文件的进度。



我希望能够点击进度条容器中的任何位置(而不是绿色的条形本身),并抓住click事件。我打算这样做以在赛道上前后搜索。



由于WPF的进度条上没有内置点击事件,我使用以下工作周围:



Hi,

I need to write a customised progress bar that I am using to track progress on a file that is replaying.

I want to be able to click any position in the progress bar container (not the bar itself that is green), and grab the click event. I plan to do this to seek back and forward through the track.

As there is no built in click event on the progressbar for WPF, I used the following to work around:

private double SetProgressBarValue(double MousePosition)
 {
    if (replayer != null)
        replayer.JumpToFrame((int)PBar.Value);
    PBar.Value = PBar.Minimum;
    double ratio = MousePosition / PBar.ActualWidth;
    double ProgressBarValue = ratio * PBar.Maximum;

    return ProgressBarValue;
 }

private void PBar_MouseDown(object sender, MouseButtonEventArgs e)
 {
     double MousePosition = e.GetPosition(PBar).X;
     PBar.Value = SetProgressBarValue(MousePosition);
 }


 private void PBar_MouseMove(object sender, MouseEventArgs e)
 {
     if (e.LeftButton == MouseButtonState.Pressed)
     {
         double MousePosition = e.GetPosition(PBar).X;
         PBar.Value = SetProgressBarValue(MousePosition);

     }
 }





因此,在鼠标按下时,我允许更改进度。但是,该事件仅在单击绿色条时触发。不是进度条控件中没有进度的部分(用于向前跟踪我的重放文件)。



有人能告诉我一个方法吗?



提前致谢!



So on mouse down I allow progress to be changed. HOWEVER, the event only fires when clicking the green bar. Not the section of the progress bar control that has no progress held within (for forward tracking my replay file).

Can someone show me a way around this?

Thanks in advance!

推荐答案

谢谢谢谢,



由于点击事件仍然被进度条控件的不完整部分遮挡,我无法实现您的建议。 这是一个例子



这是我的代码实施:





Hi Sergey,

I have not been able to implement what you've advised due to the click event still being obscured by the incomplete portion of the progress bar control. Here is an example

Here is the code I have implemented:


public class UpdatableProgressBar : System.Windows.Controls.ProgressBar
    {
        public delegate void ValueChange(double oldValue, double newValue);
 
        public event ValueChange ValueChanged;
 
        public UpdatableProgressBar():base()
        {
            this.MouseDown += this_MouseDown;
            this.MouseEnter += this_MouseEnter;
            this.MouseMove += this_MouseMove;
            this.MouseLeave += this_MouseLeave;          
        }
 
        private void this_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Cursor = System.Windows.Input.Cursors.Hand;
        }
 
        private void this_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
        {
            this.Cursor = System.Windows.Input.Cursors.Arrow;
        }
 
        private double SetProgressBarValue(double MousePosition)
        {
            this.Value = this.Minimum;
            double ratio = MousePosition / this.ActualWidth;
            double ProgressBarValue = ratio * this.Maximum;
 
            if (ValueChanged != null)
                ValueChanged(this.Value, ProgressBarValue);
 
            return ProgressBarValue;
        }
 
        private void this_MouseDown(object sender, MouseButtonEventArgs e)
        {
            double MousePosition = e.GetPosition(this).X;
            this.Value = SetProgressBarValue(MousePosition);
        }
 
        private void this_MouseMove(object sender, MouseEventArgs e)
        {
            if (e.LeftButton == MouseButtonState.Pressed)
            {
                double MousePosition = e.GetPosition(this).X;
                this.Value = SetProgressBarValue(MousePosition);
 
            }
        }
    }





你知道为什么它的一部分是被遮盖了?我误解了你的建议吗?



非常感谢,

Rob



Do you know why part of it is being obscured? Have I misunderstood what you've advised?

Many thanks,
Rob


First of所有,如果你没有在 System.Windows.Controls.ProgressBar 类中没有点击事件,这是通过点击进度条的容器来制作这种不自然和丑陋的解决方法并不是一个好借口。难怪,这很难顺利完成。



相反,用可用鼠标事件组合点击事件,例如 MouseUp MouseDown 。请参阅:

http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar_events%28v=vs.110%29.aspx [ ^ ]。



对于某些背景:



毕竟,为什么 ProgressBar的作者将提供点击事件,如果其功能不需要点击?请记住单击(在其他控件中,例如 Button )是逻辑事件,这并不意味着物理鼠标事件,可以用键盘完成。与此相反,鼠标和键盘事件是继承自 System.Windows.UIElement 的原始事件:

http://msdn.microsoft.com/en-us/library/system.windows。 uielement(v = vs.110).aspx [ ^ ]。



一旦你需要更多抽象(逻辑上更高级别)的事件,你介绍点击等等。



-SA
First of all, if you hassle is not having a Click event in the class System.Windows.Controls.ProgressBar, this is not a good excuse for crafting such an unnatural and ugly workaround as working with the click of a progress bar's container. No wonder, it's hard to do smoothly.

Instead, "compose" the click event out of available mouse events, such as MouseUp and MouseDown. Please see:
http://msdn.microsoft.com/en-us/library/system.windows.controls.progressbar_events%28v=vs.110%29.aspx[^].

For some background:

After all, why would the author of the ProgressBar would provide Click event, if the click is not needed for its functionality? Remember that Click (in other controls, such as Button) is the "logical" event which does not imply physical mouse event, could be done with the keyboard. In contrast to that, mouse and keyboard events are "raw" events inherited from System.Windows.UIElement:
http://msdn.microsoft.com/en-us/library/system.windows.uielement(v=vs.110).aspx[^].

As soon as you need more abstract (logically higher-level) events, you introduce Click and whatever else.

—SA


这篇关于如何使用单击事件更新进度条的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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