panel颜色渐变
各位大虾好!
如何用panel做一个颜色渐变,就像给被子里倒水一样,谢谢!~
[解决办法]
LinearGradientBrush 填充
[解决办法]
''' <summary>
''' 将指定Panel填充渐变
''' </summary>
''' <param name="Pn">目标Panel控件</param>
''' <param name="StartColor">顶部起始色</param>
''' <param name="EndColor">底部结束色</param>
''' <param name="BorderColor">边框颜色</param>
''' <remarks></remarks>
Private Sub FillPanelWithColor(Pn As Panel, StartColor As Color, EndColor As Color, BorderColor As Color)
Try
If Pn.BackgroundImage IsNot Nothing Then Pn.BackgroundImage.Dispose()
Pn.BackgroundImage = New Bitmap(Pn.Width, Pn.Height)
Using G As Graphics = Graphics.FromImage(Pn.BackgroundImage)
Using Lg As New Drawing2D.LinearGradientBrush(New Point(0, 0), New Point(0, Pn.Height), StartColor, EndColor)
G.FillRectangle(Lg, New Rectangle(0, 0, Pn.Width, Pn.Height))
G.DrawRectangle(New Pen(BorderColor), New Rectangle(0, 0, Pn.Width - 1, Pn.Height - 1))
End Using
End Using
Catch
End Try
End Sub
’-----用法-----
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
FillPanelWithColor(Panel1, Color.LightBlue, Color.Blue, Color.Black)
End Sub