想要写一个类似TrackBar的控件用于播放器控制

stealthbk 2023-11-14 14:56:34

自带的太丑,想要写一个类似TrackBar的控件用于播放器控制,类似这个的,求vb.net,代码及工程文件。

 

 

...全文
471 5 打赏 收藏 转发到动态 举报
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
123工艺品 2023-12-11
  • 打赏
  • 举报
回复

img

stealthbk 2023-11-15
  • 打赏
  • 举报
回复 1

修改了趋于完美,就是头尾的半圆解决不了,感谢新华大神!
Imports System.ComponentModel
Imports System.Drawing.Drawing2D
Imports System.Windows.Forms

Public Class PlayerTrackBar
Inherits Control

Private _minimum As Integer = 0
Private _maximum As Integer = 100
Private _value As Integer = 0
Private _trackheight As Integer = 5
Private _starcolor As Color = Color.FromArgb(0, 0, 255)
Private _endcolor As Color = Color.FromArgb(255, 0, 255)
Private _trackcolor As Color = Color.FromArgb(57, 71, 79)

Public Sub New()
    SetStyle(ControlStyles.OptimizedDoubleBuffer Or
             ControlStyles.UserPaint Or
             ControlStyles.AllPaintingInWmPaint, True)

    Me.BackColor = Color.Gray
End Sub

<Category("Appearance")>
Public Property Minimum As Integer
    Get
        Return _minimum
    End Get
    Set(value As Integer)
        If value < 0 Then
            Throw New ArgumentException("Minimum value cannot be negative.")
        End If
        _minimum = value
        Invalidate()
    End Set
End Property

<Category("Appearance")>
Public Property Maximum As Integer
    Get
        Return _maximum
    End Get
    Set(value As Integer)
        If value <= _minimum Then
            Throw New ArgumentException("Maximum value must be greater than Minimum value.")
        End If
        _maximum = value
        Invalidate()
    End Set
End Property
Public Property Starcolor As Color
    Get
        Return _starcolor
    End Get
    Set(value As Color)
        _starcolor = value
        Invalidate()
    End Set
End Property
Public Property Endcolor As Color
    Get
        Return _endcolor
    End Get
    Set(value As Color)
        _endcolor = value
        Invalidate()
    End Set
End Property
Public Property Trackcolor As Color
    Get
        Return _trackcolor
    End Get
    Set(value As Color)
        _trackcolor = value
        Invalidate()
    End Set
End Property

<Category("Appearance")>
Public Property Trackheight As Integer
    Get
        Return _trackheight
    End Get
    Set(value As Integer)
        _trackheight = value
        Invalidate()
    End Set
End Property
Public Property Value As Integer
    Get
        Return _value
    End Get
    Set(value As Integer)
        If value < _minimum Then
            _value = _minimum
        ElseIf value > _maximum Then
            _value = _maximum
        Else
            _value = value
        End If
        Invalidate()
        OnValueChanged(EventArgs.Empty)
    End Set
End Property

Public Event ValueChanged As EventHandler

Protected Overridable Sub OnValueChanged(e As EventArgs)
    RaiseEvent ValueChanged(Me, e)
End Sub

Protected Overrides Sub OnPaint(e As PaintEventArgs)
    MyBase.OnPaint(e)

    Dim trackRect As Rectangle = ClientRectangle
    trackRect.Inflate(-2, -2)

    Dim trackRectF As New Rectangle(trackRect.Left, trackRect.Top + _trackheight, trackRect.Width, _trackheight)
    Using brushff As New SolidBrush(_trackcolor)
        e.Graphics.FillRectangle(brushff, trackRectF)

    End Using

    Dim thumbWidth As Integer = CInt((Value - Minimum) / (Maximum - Minimum) * trackRect.Width)
    Dim thumbRect As New Rectangle(trackRect.Left, trackRect.Top + _trackheight, thumbWidth, _trackheight)
    Dim thumbPoint As New RectangleF(CInt((Value - Minimum) / (Maximum - Minimum) * trackRect.Width) - (_trackheight * 3 / 2), trackRect.Top, _trackheight * 3, _trackheight * 3)

    Using brushs As New LinearGradientBrush(New Point(0, 0), New Point(thumbRect.Width, thumbRect.Height), _starcolor, _endcolor)
        e.Graphics.FillRectangle(brushs, thumbRect)
    End Using
    Using brushf As New SolidBrush(_endcolor)
        e.Graphics.FillEllipse(brushf, thumbPoint)

    End Using
End Sub

Protected Overrides Sub OnMousemove(e As MouseEventArgs)
    MyBase.OnMouseDown(e)
    If e.Button = MouseButtons.Left Then
        Dim percentage As Single = CSng(e.X) / Width
        Value = CInt((Maximum - Minimum) * percentage + Minimum)
    End If
End Sub
Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
    MyBase.OnMouseDown(e)
    If e.Button = MouseButtons.Left Then
        Dim percentage As Single = CSng(e.X) / Width
        Value = CInt((Maximum - Minimum) * percentage + Minimum)
    End If
End Sub

End Class

stealthbk 2023-11-15
  • 打赏
  • 举报
回复

实际测试,无法拖动,还有那个小圆圆的手柄如何实现

新华 2023-11-14
  • 打赏
  • 举报
回复 1

在 VB.NET 中,你可以通过创建一个自定义控件类来实现这个目标。以下是一个简单的示例代码,演示如何创建一个自定义的播放器控制条。

Imports System.ComponentModel
Imports System.Windows.Forms

Public Class PlayerTrackBar
    Inherits Control

    Private _minimum As Integer = 0
    Private _maximum As Integer = 100
    Private _value As Integer = 0

    Public Sub New()
        SetStyle(ControlStyles.OptimizedDoubleBuffer Or
                 ControlStyles.UserPaint Or
                 ControlStyles.AllPaintingInWmPaint, True)

        Me.BackColor = Color.Gray
    End Sub

    <Category("Appearance")>
    Public Property Minimum As Integer
        Get
            Return _minimum
        End Get
        Set(value As Integer)
            If value < 0 Then
                Throw New ArgumentException("Minimum value cannot be negative.")
            End If
            _minimum = value
            Invalidate()
        End Set
    End Property

    <Category("Appearance")>
    Public Property Maximum As Integer
        Get
            Return _maximum
        End Get
        Set(value As Integer)
            If value <= _minimum Then
                Throw New ArgumentException("Maximum value must be greater than Minimum value.")
            End If
            _maximum = value
            Invalidate()
        End Set
    End Property

    <Category("Appearance")>
    Public Property Value As Integer
        Get
            Return _value
        End Get
        Set(value As Integer)
            If value < _minimum Then
                _value = _minimum
            ElseIf value > _maximum Then
                _value = _maximum
            Else
                _value = value
            End If
            Invalidate()
            OnValueChanged(EventArgs.Empty)
        End Set
    End Property

    Public Event ValueChanged As EventHandler

    Protected Overridable Sub OnValueChanged(e As EventArgs)
        RaiseEvent ValueChanged(Me, e)
    End Sub

    Protected Overrides Sub OnPaint(e As PaintEventArgs)
        MyBase.OnPaint(e)

        Dim trackRect As Rectangle = ClientRectangle
        trackRect.Inflate(-2, -2)

        Dim thumbWidth As Integer = CInt((Value - Minimum) / (Maximum - Minimum) * trackRect.Width)
        Dim thumbRect As New Rectangle(trackRect.Left, trackRect.Top, thumbWidth, trackRect.Height)

        Using brush As New SolidBrush(Color.DodgerBlue)
            e.Graphics.FillRectangle(brush, thumbRect)
        End Using
    End Sub

    Protected Overrides Sub OnMouseDown(e As MouseEventArgs)
        MyBase.OnMouseDown(e)
        If e.Button = MouseButtons.Left Then
            Dim percentage As Single = CSng(e.X) / Width
            Value = CInt((Maximum - Minimum) * percentage + Minimum)
        End If
    End Sub
End Class

这个自定义控件 PlayerTrackBar 继承自 Control,它具有类似于 TrackBar 的外观,你可以在你的 VB.NET 项目中使用它。

要在 VB.NET 项目中使用这个自定义控件,你可以按照以下步骤:

  1. 在 Visual Studio 中,打开或创建一个 VB.NET 项目。
  2. 将上述代码复制到一个新的类文件中,例如 PlayerTrackBar.vb。
  3. 在解决方案资源管理器中,右键单击项目并选择“添加” -> “现有项”。
  4. 选择刚刚添加的 PlayerTrackBar.vb 文件。

然后,你就可以在工具箱中看到 PlayerTrackBar 控件,将其拖放到窗体上,并使用它来控制播放器进度。在窗体中,你可以通过订阅 ValueChanged 事件来捕获值的变化。

请注意,这只是一个简单的示例,你可以根据实际需求进行进一步的定制和优化。

stealthbk 2023-11-15
  • 举报
回复 1
@新华 已经测过可行,就是外观太抽了。思路已经打开,感谢感谢!

16,554

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧