加载Label速度很慢。
Sub SetLabel()
Dim II As Integer
For II = 0 To 41
DateLabel(II) = New Label
Me.Controls.Add(DateLabel(II))
With DateLabel(II)
.TextAlign = ContentAlignment.MiddleCenter
.BackColor = Color.White
.AutoSize = False
.Size = New Size(29, 17)
.Top = 49 + (II \ 7) * 21
.Left = 5 + (II Mod 7) * 33
.Text = II + 1
.BringToFront()
End With
Next
End Sub
1G内存,Vs2005下,40个Label竟然需要1秒钟——因为要做成控件。
这种明显有刷新缓慢的现象,肯定是不成的。
我用——固定模式,事先放上去40个Label,加载同样的慢,应该不是代码的问题。
怎么解决呢?
VB下,我自己的控件,一次性上百个,都感觉不到丝毫延迟的。。。
.Net下,怎么解决类似的问题啊?
------解决方案--------------------这是因为容器控件不停重绘导致的性能低下
在 进入 For 前
Me.Controls.SuspendLayout();
然后再 For
循环完全 End 后
家
Me.Controls.ResumeLayout();
应该可以大大提高显示性能
写个耗用时间语句对比一下
------解决方案--------------------//用Label很浪费,参考直接绘制
Private Sub Form1_Paint(ByVal sender As System.Object, ByVal e As System.Windows.Forms.PaintEventArgs) Handles MyBase.Paint
' Create string to draw.
Dim drawString As String
Dim drawFont As Font = Font
Dim drawBrush As New SolidBrush(Color.Black)
Dim i As Integer
Dim drawRect As RectangleF
Dim drawFormat As New StringFormat
drawFormat.Alignment = StringAlignment.Center
drawFormat.LineAlignment = StringAlignment.Center
For i = 0 To 41
drawString = i + 1
drawRect = New RectangleF(5 + (i Mod 7) * 33, 49 + (i \ 7) * 21, 29.0F, 17.0F)
e.Graphics.FillRectangle(Brushes.White, 5 + (i Mod 7) * 33, 49 + (i \ 7) * 21, 29, 17)
e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat)
Next
End Sub