我正在学习如何绘制自定义控件并将一些代码放在一起,但在调整控件大小时出现"内存不足"异常。虽然每次抽奖后我都会处理掉我的画笔和铅笔,但我仍然得到这个例外。有人可以告诉我我做错了什么吗?我似乎想不通。
这是我的代码:
Imports System.Drawing.Drawing2D
Public Class Draw_Sub
Inherits Windows.Forms.Panel
Public Sub New()
Me.DoubleBuffered = True
Me.Width = 30
Me.Height = 500
End Sub
Private Sub Draw_Sub_Paint(sender As Object, e As PaintEventArgs) Handles Me.Paint
Cntrls_Draw(0, 0, Me.Width, Me.Height, 3, e)
Cntrls_Draw(0, 100, Me.Width, Me.Width, 3, e)
End Sub
Public Sub Cntrls_Draw(ByVal X_pos As Integer,
ByVal Y_pos As Integer,
ByVal Wdth As Integer,
ByVal Hght As Integer,
ByVal Bev_Hght As Integer,
ByVal e As PaintEventArgs)
Dim Width_bev = Bev_Hght * 2 / Wdth
Dim Width_Scale = 1 - Width_bev
Dim Height_bev = Bev_Hght * 2 / Hght
Dim Height_Scale = 1 - Height_bev
Dim Draw_pnts As Point() = {New Point(X_pos, Y_pos),
New Point(Wdth, Y_pos),
New Point(Wdth, Hght),
New Point(X_pos, Hght)}
Dim pthGrBrush As New PathGradientBrush(Draw_pnts)
Dim colors As Color() = {Color.FromArgb(50, 49, 65, 81),
Color.FromArgb(50, 0, 0, 0)}
Dim relativePositions As Single() = {0.0F, 1.0F}
pthGrBrush.FocusScales = New PointF(Width_Scale, Height_Scale)
Dim colorBlend As New ColorBlend()
colorBlend.Colors = colors
colorBlend.Positions = relativePositions
pthGrBrush.InterpolationColors = colorBlend
e.Graphics.FillRectangle(pthGrBrush, 0, 0, Wdth, Hght)
pthGrBrush.Dispose()
'AO line rectangle to give a sharper look at the bottom sides
Dim Rect As New Rectangle(Bev_Hght, Bev_Hght, Wdth - (Bev_Hght * 2), Hght - (Bev_Hght * 2))
Dim My_Pen As New Pen(Color.FromArgb(25, 0, 0, 0), 3)
e.Graphics.DrawRectangle(My_Pen, Rect)
My_Pen.Dispose()
End Sub
End Class
多亏了安德鲁·莫顿,似乎我的问题是双倍到单倍的转换。更改此件后:
Dim Width_bev = Bev_Hght * 2 / Wdth
Dim Width_Scale = 1 - Width_bev
Dim Height_bev = Bev_Hght * 2 / Hght
Dim Height_Scale = 1 - Height_bev
对此:
Dim Width_bev = CSng(Bev_Hght * 2 / Wdth)
Dim Width_Scale = 1 - Width_bev
Dim Height_bev = CSng(Bev_Hght * 2 / Hght)
Dim Height_Scale = 1 - Height_bev
似乎已经解决了这个问题。