进度条颜色vb.net

进度条颜色vb.net,vb.net,colors,progress-bar,Vb.net,Colors,Progress Bar,我正在尝试在vb.net中更改进度条的前颜色,有一种方法可以通过禁用XP视觉样式来实现,但我不想这样做,因为这样做会使应用程序看起来不干净,所以我一直在尝试找到一种方法,使其在不禁用该选项的情况下工作,我找到了一种方法,但它似乎只起作用当颜色设置为绿色时工作,如果设置为红色或黄色,则进度条没有颜色。这是我发现的不起作用的代码: Public Class Form1 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (

我正在尝试在vb.net中更改进度条的前颜色,有一种方法可以通过禁用XP视觉样式来实现,但我不想这样做,因为这样做会使应用程序看起来不干净,所以我一直在尝试找到一种方法,使其在不禁用该选项的情况下工作,我找到了一种方法,但它似乎只起作用当颜色设置为绿色时工作,如果设置为红色或黄色,则进度条没有颜色。这是我发现的不起作用的代码:

Public Class Form1

 Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Integer,
          ByVal wMsg As Integer, ByVal wParam As Integer, 
          ByVal lParam As Integer) As Integer

    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
       SendMessage(ProgressBar1.Handle, 1040, 2, 0)
       ProgressBar1.Increment(10)
       End Sub
  End Class

这似乎根本不起作用,所以我想知道是否有其他方法可以在不禁用XP视觉样式的情况下实现这一点

您可以通过继承
ProgressBar
类来创建自己的自定义进度条:

Imports System.Drawing.Drawing2D
Namespace WindowsFormsApplication1
    Class ProgressBarColor
        Inherits System.Windows.Forms.ProgressBar
        Private _Color As Color
        Private brush As New System.Drawing.Drawing2D.LinearGradientBrush(New Rectangle(1, 1, 1, 1), Color.FloralWhite, Color.Firebrick, LinearGradientMode.Vertical)
        Private m_rec As Rectangle
        Private draw As Boolean

        Private indraw As Boolean = True
        Public Sub New()
            MyBase.New()
            Me.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, True)
            _Color = Color.Red
            m_rec = New Rectangle(1, 1, 1, 1)
            draw = False
        End Sub

        <Category("Appearance"), Description("The Color of the progress bar")> _
        <Browsable(True)> _
        Public Property Color() As Color
            Get
                Return _Color
            End Get
            Set
                _Color = value
            End Set
        End Property


        <Category("Behavior"), Description("Whether or not the progress bar should draw itself when the value hasn't changed")> _
        <Browsable(True)> _
        Public Property DrawEveryFrame() As Boolean
            Get
                Return draw
            End Get
            Set
                draw = value
            End Set
        End Property

        Public Property Rec() As Rectangle
            Get
                Return m_rec
            End Get
            Set
                If value.Height = 0 Then
                    value.Height = 1
                End If
                If value.Width = 0 Then
                    value.Width = 1
                End If
                m_rec = value
                brush = New LinearGradientBrush(value, _Color, Color.FromArgb(_Color.R / 2, _Color.G / 2, _Color.B / 2), LinearGradientMode.Vertical)
            End Set
        End Property

        Protected Overrides Sub OnPaintBackground(pevent As PaintEventArgs)

        End Sub

        Protected Overrides Sub OnPaint(e As System.Windows.Forms.PaintEventArgs)
            Dim width As Integer = 0
            If Value <> 0 Then
                width = (e.ClipRectangle.Width * Value \ Maximum) - 4
            End If
            If (m_rec.X <> e.ClipRectangle.X OrElse m_rec.Y <> e.ClipRectangle.Y OrElse m_rec.Width <> width OrElse m_rec.Height <> e.ClipRectangle.Height - 4) OrElse draw OrElse indraw Then
                indraw = False
                Rec = New Rectangle(e.ClipRectangle.X, e.ClipRectangle.Y, Me.Size.Width, Me.Size.Height)
                If m_rec.Height = 0 Then
                    m_rec.Height = 1
                End If
                If ProgressBarRenderer.IsSupported Then
                    ProgressBarRenderer.DrawHorizontalBar(e.Graphics, e.ClipRectangle)
                End If
                m_rec.Width = CInt(m_rec.Width * (CDbl(Value) / Maximum)) - 4
                m_rec.Height -= 4
                If m_rec.Width = 0 Then
                    m_rec.Width = 1
                End If
                e.Graphics.FillRectangle(brush, 2, 2, m_rec.Width, m_rec.Height)
                MyBase.OnPaint(e)
            End If
        End Sub

    End Class
End Namespace
导入System.Drawing.Drawing2D
命名空间Windows窗体应用程序1
类颜色
继承System.Windows.Forms.ProgressBar
专用颜色作为颜色
作为新系统的专用笔刷。Drawing.Drawing2D.LinearGradientBrush(新矩形(1,1,1,1),Color.FloralWhite,Color.Firebrick,LinearGradientMode.Vertical)
私有m_rec为矩形
私有绘制为布尔值
私有indraw为布尔值=真
公共分新()
MyBase.New()
Me.SetStyle(System.Windows.Forms.ControlStyles.UserPaint,True)
_颜色=颜色。红色
m_rec=新矩形(1,1,1,1)
抽签=假
端接头
_
_
公共属性Color()作为颜色
得到
返回颜色
结束
设置
_颜色=值
端集
端属性
_
_
公共属性DraweryFrame()为布尔值
得到
回采
结束
设置
绘制=值
端集
端属性
公共属性Rec()为矩形
得到
返回m_rec
结束
设置
如果value.Height=0,则
值。高度=1
如果结束
如果value.Width=0,则
值。宽度=1
如果结束
m_rec=值
笔刷=新的LinearGradientBrush(值,_Color,Color.FromArgb(_Color.R/2,_Color.G/2,_Color.B/2),LinearGradientMode.Vertical)
端集
端属性
PaintBackground上的受保护覆盖子项(作为PaintEventArgs的pevent)
端接头
受保护的覆盖子OnPaint(如System.Windows.Forms.PaintEventArgs)
尺寸宽度为整数=0
如果值为0,则
宽度=(e.ClipRectangle.width*值\最大值)-4
如果结束
如果(m_rec.X e.ClipRectangle.X OrElse m_rec.Y e.ClipRectangle.Y OrElse m_rec.Width OrElse m_rec.Height e.ClipRectangle.Height-4)OrElse绘制OrElse indraw,则
indraw=False
Rec=新矩形(e.ClipRectangle.X,e.ClipRectangle.Y,Me.Size.Width,Me.Size.Height)
如果m_rec.Height=0,则
m_rec.Height=1
如果结束
如果支持ProgressDerrer.issupport,则
ProgressDerrer.DrawHorizontalBar(如图形、剪贴画)
如果结束
m_rec.Width=CInt(m_rec.Width*(CDbl(值)/最大值))-4
m_记录高度-=4
如果m_rec.Width=0,则
m_rec.Width=1
如果结束
e、 图形.填充矩形(画笔,2,2,m_rec.宽度,m_rec.高度)
MyBase.OnPaint(e)
如果结束
端接头
末级
结束命名空间

这将在工具箱中创建一个自定义控件,您应该能够在设计时添加该控件。这只是为了让你开始。希望这会有所帮助。(注意,如果栏在
DraweryFrame
设置为false时闪烁,您可能需要将
LinearGradientBrush
更改为标准的
System.Drawing.Brush
;或者从计时器或mousemove事件处理程序调用
ProgressBarColor1.Update()

查看此处:(它是C语言,但可以轻松转换为VB.NET)。这也不行@StyxxyMaybe可以帮忙吗?那么我应该把它添加到我的form1中还是单独的类中@DavidsBroads将其添加到您的表单1中,但作为一个单独的类,它将在工具箱中提供。@用户2387537,如果这回答了您的问题,请随意标记它。我发现,只要将样式设置为
选取框
,即可停止闪烁