16,554
社区成员
发帖
与我相关
我的任务
分享
自带的太丑,想要写一个类似TrackBar的控件用于播放器控制,类似这个的,求vb.net,代码及工程文件。
修改了趋于完美,就是头尾的半圆解决不了,感谢新华大神!
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
实际测试,无法拖动,还有那个小圆圆的手柄如何实现
在 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 项目中使用这个自定义控件,你可以按照以下步骤:
然后,你就可以在工具箱中看到 PlayerTrackBar 控件,将其拖放到窗体上,并使用它来控制播放器进度。在窗体中,你可以通过订阅 ValueChanged 事件来捕获值的变化。
请注意,这只是一个简单的示例,你可以根据实际需求进行进一步的定制和优化。