如何使这更好,更快? [英] How to make this better and faster?
问题描述
我在VB.NET 2003中编写了以下Win应用程序。该类只是
图片框,它们在实例化并添加到表单后以随机顺序运行。当我创建15个或更多我的
类的实例时,整个程序运行缓慢,我必须关闭
程序。我试图为每个类创建一个新线程,但是抛出
异常,因为分隔的线程不能从
子元素添加到表单中
我的代码就是这个(请告诉我是否有更好的方法)
coplile:
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/ t:winexe / main:form1
''--------------------------- --------代码启动
Imports System.Math
导入Microsoft.VisualBasic
进口系统。绘图
进口system.windows.forms
公共类Form1
继承System.Windows.Forms.Form
#Region" Windows窗体设计器生成的代码
Public Sub New()
MyBase.New()
' 'Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化
< br $>
结束子
''表格覆盖处理以清理组件列表。
受保护的重载覆盖子处理(ByVal处理为布尔)
如果处置那么
如果不是(组件什么都没有)那么
components.Dispose()
结束如果
结束如果
MyBase.Dispose(处置)
结束子
'' Windows窗体设计器要求
私有组件As System.ComponentModel.IContainer
''注意:Windows窗体设计器需要以下过程
''可以使用Windows窗体设计器修改它。
''不要莫使用代码编辑器来区分它。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize =新系统.Drawing.Size(384,266)
Me.Name =" Form1"
Me.Text =" Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal发送者为对象,ByVal e As
System.EventArgs)处理MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec作为Rec = New Rec(Me.CreateGraphics,Me)
End Sub
结束班级
'' - -------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X,Y As Integer
Dim frm As Form
WithEvents TIMER作为新计时器
Dim bmp As Bitmap
Dim PB作为PictureBox
Public Sub New(ByVal g As Graphics,ByVal form as Form)
X = Rnd() * 100
Y = Rnd()* 200
Gr = g
Me.frm =表格
ini ()
justAddX = True
justAddY = True
End Sub
''我们可以使用此处的图形对象在表单上绘制。但由于
GDI +
''缺乏理想的性能,我们使用图片框并将它们添加到
''空格形式而不是使用绘图对象:
Sub ini()
TIMER.Interval = Rnd()* 10 + 1
TIMER.Enabled =真实
PB =新PictureBox
使用PB
.BackColor = Color.FromArgb(Rnd()* 150,Rnd()* 200 ,Rnd()*
230)
.Left = X
.Top = Y
.Height = 6
。宽度= 6
结束
frm.Controls.Add(PB)
End Sub
''使用这两个布尔值来知道何时添加或替换数字
''此类对象的移动。
Dim justAddX,justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object,ByVal e As
System.EventArgs)处理TIMER.Tick
MoveMe()
结束子
''CuurentNum X,Y是添加到t的数字当速度击中墙壁时,它的速度为X,Y
''会产生一种变化:
Dim CurrentNumX As Integer = 1 >
Dim CurrentNumY As Integer = 1
Sub MoveMe()
如果X> (frm.Width - PB.Width) - 10然后
justAddX = False
CurrentNumX = Rnd()* 50
结束如果
如果Y> (frm.Height - PB.Height) - 30然后
justAddY = False
CurrentNumY = Rnd()* 40
结束如果
如果X< 0然后
justAddX = True
CurrentNumX = Rnd()* 80
结束如果
如果Y < 0然后
justAddY = True
CurrentNumY = Rnd()* 30
结束如果
如果justAddX那么
X + = CurrentNumX
否则
X - = CurrentNumX
结束如果
如果只是添加那么
Y + = CurrentNumY
否则
Y - = CurrentNumY
结束如果
PB.Left = X
PB.Top = Y
End Sub
结束班
''-------------------------------- -----------------------结束
代码------------
您可能想要检查API例程BitBlt。它从图形对象复制矩形
形状。它非常非常快。您可以使用Paint事件直接在表单上绘制
图像。
" ham-z"写道:
我在VB.NET 2003中编写了以下Win应用程序。该类只是简单的图片框,它们在实例化并添加到表单后以随机顺序运行。当我创建15个或更多我的
类实例时,整个程序运行缓慢,我必须关闭
程序。我试图为每个类创建一个新线程,但是抛出了一个异常,因为一个单独的线程不能被添加到
子类的表单中。
我的代码就是这个(请告诉我是否有更好的方法)
coplile:
vbc [filename]
/ r:systemystem,system .windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/ t:winexe / main:form1
''--------- --------------------------开始代码
Imports System.Math
导入Microsoft.VisualBasic
进口System.drawing
导入system.windows.forms
公共类Form1
继承System.Windows.Forms.Form
#Region " Windows窗体设计器生成的代码
Public Sub New()
MyBase.New()
''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化
End Sub
''表格覆盖处理清理组件列表。
受保护的重载覆盖子处理(ByVal处理为布尔值)
如果处理则
如果不是(组件是什么)那么
components.Dispose()
结束如果
MyBase.Dispose(处理)
结束子
''Windows窗体设计器要求
私有组件作为系统.ComponentModel.IContainer
''注意:Windows窗体设计器需要以下步骤
''它可以使用Windows窗体设计进行修改呃。
''不要使用代码编辑器修改它。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)> Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384,266)
Me.Name =" Form1"
Me.Text =Form1
End Sub
#End Region
私有子Form1_Click(ByVal发件人作为对象,ByVal e As
System.EventArgs)处理MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec作为Rec = New Rec(Me.CreateGraphics,Me) )
End Sub
结束课
''--------------------------- ------------------------ Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X,Y As Integer
Dim frm As Form
WithEvents TIMER作为新计时器
Dim bmp As Bitmap
Dim PB作为PictureBox
Public Sub New(ByVal g As Graphics,ByVal form as Form)
X = Rnd()* 100
Y = Rnd()* 200
Gr = g
Me.frm =表格
ini()
justAddX = True
justAddY = True
End Sub
' '我们可以在这里使用图形对象在表单上绘图。但由于GDI +
''缺乏理想的性能,我们使用图片框并将它们添加到
''空格形式而不是使用绘图对象:
Sub ini()
TIMER.Interval = Rnd()* 10 + 1
TIMER.Enabled = True
PB =新PictureBox
用PB
.BackColor = Color .FromArgb(Rnd()* 150,Rnd()* 200,Rnd()*
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
结束
frm.Controls.Add(PB)
End Sub
''使用这两个布尔值来知道何时添加或者替换数字
''这个类对象的移动。
Dim justAddX,justAddY As Boolean
私有子TIMER_Tick(ByVal发送者作为对象,ByVal e As
System.EventArgs)处理TIMER.Tick
MoveMe()
End Sub
''CuurentNum X,Y是加速到X的速度的数字,当
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
如果X> (frm.Width - PB.Width) - 10然后
justAddX = False
CurrentNumX = Rnd()* 50
结束如果
如果Y> (frm.Height - PB.Height) - 30然后
justAddY = False
CurrentNumY = Rnd()* 40
结束如果
如果X< 0然后
justAddX = True
CurrentNumX = Rnd()* 80
结束如果
如果Y< 0然后
justAddY = True
CurrentNumY = Rnd()* 30
结束如果
如果justAddX那么
X + = CurrentNumX
另外
X - = CurrentNumX
结束如果
如果justAddY则
Y + = CurrentNumY
否则
Y - = CurrentNumY
结束如果
PB.Left = X
PB.Top = Y
End Sub
结束课
'' -------------------------------------------------- -----代码结束------------
Ham,
不错的交叉发布,但是在我看来这个
问题的最正确的新闻组不在其中。
microsoft.public.dotnet.framework.drawing
我看到了你的问题,不过我想是因为最多的事实是
图形你可以放置它最好的新闻组。
顺便说一句,没有问题。
Cor
" ham- ž" < hamz-3e @ / ** / yahoo.com>。我在VB.NET 2003中编写了以下Win应用程序。该类只是简单的图片框,它们在实例化并添加到表单后以随机顺序运行。当我创建15个或更多我的
类实例时,整个程序运行缓慢,我必须关闭
程序。我试图为每个类创建一个新线程,但是
会抛出异常,因为分离的线程不能添加到
子类的表单中。 />
我的代码是这样的(请告诉我是否有更好的方法)
coplile:
vbc [filename]
/ r:system .dll,system.windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/ t:winexe / main:form1
''----- ------------------------------开始代码
进口System.Math
进口Microsoft.VisualBasic
导入System.drawing
导入system.windows.forms
公共类Form1
继承System.Windows.Forms.Form
#Region" Windows窗体设计器生成的代码
Public Sub New()
MyBase.New()
''Windows窗体设计器需要此调用。
InitializeComponent()
''在InitializeComponent()调用后添加任何初始化
End Sub
''表格覆盖处理清理组件列表。
受保护的重载覆盖子处理(ByVal处理为布尔值)
如果处理则
如果不是(组件是什么)那么
components.Dispose()
结束如果
MyBase.Dispose(处理)
结束子
''Windows窗体设计器要求
私有组件作为系统.ComponentModel.IContainer
''注意:Windows窗体设计器需要以下步骤
''可以使用Windows窗体设计器修改它。
''请勿使用代码编辑器对其进行修改。
< System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5,13)> Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384,266)
Me.Name =" Form1"
Me.Text =Form1
End Sub
#End Region
私有子Form1_Click(ByVal发件人作为对象,ByVal e As
System.EventArgs)处理MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec作为Rec = New Rec(Me.CreateGraphics,Me) )
End Sub
结束课
''--------------------------- ------------------------ Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X,Y As Integer
Dim frm As Form
WithEvents TIMER作为新计时器
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics,ByVal form as Form)
X = Rnd()* 100
Y = Rnd()* 200
Gr = g
Me.frm =表格
ini()
justAddX = True
justAddY = True
结束子
''我们可以使用这里的图形对象在表单上绘制。但由于GDI +
''缺乏理想的性能,我们使用图片框并将它们添加到
''空格形式而不是使用绘图对象:
Sub ini()
TIMER.Interval = Rnd()* 10 + 1
TIMER.Enabled = True
PB =新PictureBox
用PB
.BackColor = Color .FromArgb(Rnd()* 150,Rnd()* 200,Rnd()*
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
结束
frm.Controls.Add(PB)
End Sub
''使用这两个布尔值来知道何时添加或者替换数字
''这个类对象的移动。
Dim justAddX,justAddY As Boolean
私有子TIMER_Tick(ByVal发送者作为对象,ByVal e As
System.EventArgs)处理TIMER.Tick
MoveMe()
End Sub
''CuurentNum X,Y是添加的数字当速度达到墙壁时,速度为X,Y
''会产生一种变化:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
如果X> (frm.Width - PB.Width) - 10然后
justAddX = False
CurrentNumX = Rnd()* 50
结束如果
如果Y> (frm.Height - PB.Height) - 30然后
justAddY = False
CurrentNumY = Rnd()* 40
结束如果
如果X< 0然后
justAddX = True
CurrentNumX = Rnd()* 80
结束如果
如果Y< 0然后
justAddY = True
CurrentNumY = Rnd()* 30
结束如果
如果justAddX那么
X + = CurrentNumX
另外
X - = CurrentNumX
结束如果
如果justAddY则
Y + = CurrentNumY
否则
Y - = CurrentNumY
结束如果
PB.Left = X
PB.Top = Y
End Sub
结束课
'' -------------------------------------------------- -----代码结束------------
" Dennis" ; <德**** @ discussions.microsoft.com> schrieb:您可能想要检查API例程BitBlt。它从图形对象复制矩形
形状。它非常非常快。您可以使用Paint事件直接在表单上绘制图像。
我怀疑这会比''图形快得多.DrawImage [Unscaled]''。
-
MS Herfried K. Wagner
MVP< URL:http: //dotnet.mvps.org/>
VB< URL:http://dotnet.mvps.org/dotnet/faqs/>
I have written the following Win app in VB.NET 2003 . The class is simply
picture boxes that behave in a random order after they have been
instantiated and added to a form. When I create 15 or more instances of my
class, the whole program runs slowly in a way that I have to close the
program. I have tried to create a new thread for each class, but that throws
an exception , because a separated thread can''t be added to a form from a
child class.
My code is this ( please tell me if there are better ways for doing it)
coplile with :
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/t:winexe /main:form1
''----------------------------------- Start of CODE
Imports System.Math
imports Microsoft.VisualBasic
imports System.drawing
imports system.windows.forms
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
''This call is required by the Windows Form Designer.
InitializeComponent()
''Add any initialization after the InitializeComponent() call
End Sub
''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec As Rec = New Rec(Me.CreateGraphics, Me)
End Sub
End Class
''--------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X, Y As Integer
Dim frm As Form
WithEvents TIMER As New Timer
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics, ByVal form As Form)
X = Rnd() * 100
Y = Rnd() * 200
Gr = g
Me.frm = form
ini()
justAddX = True
justAddY = True
End Sub
'' We could use the graphics object here to draw on the form. But since
GDI+
'' lacks the ideal performance, we use picture boxes and add them to the
'' spacified form instead of using the drawing object:
Sub ini()
TIMER.Interval = Rnd() * 10 + 1
TIMER.Enabled = True
PB = New PictureBox
With PB
.BackColor = Color.FromArgb(Rnd() * 150, Rnd() * 200, Rnd() *
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
End With
frm.Controls.Add(PB)
End Sub
'' Use these two boolean to know when to add or substarct numbers
'' the movement of this class object.
Dim justAddX, justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TIMER.Tick
MoveMe()
End Sub
'' CuurentNum X , Y are the numbers added to the speed of X ,Y
'' which creates a kind of change when the Rec hits the walls:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
If X > (frm.Width - PB.Width) - 10 Then
justAddX = False
CurrentNumX = Rnd() * 50
End If
If Y > (frm.Height - PB.Height) - 30 Then
justAddY = False
CurrentNumY = Rnd() * 40
End If
If X < 0 Then
justAddX = True
CurrentNumX = Rnd() * 80
End If
If Y < 0 Then
justAddY = True
CurrentNumY = Rnd() * 30
End If
If justAddX Then
X += CurrentNumX
Else
X -= CurrentNumX
End If
If justAddY Then
Y += CurrentNumY
Else
Y -= CurrentNumY
End If
PB.Left = X
PB.Top = Y
End Sub
End Class
''------------------------------------------------------- End of
CODE------------
You might want to check into the API routine BitBlt. It copies rectangle
shapes from and to graphics objects. It''s very, very fast. You could draw
your images directly on the form using the Paint event.
"ham-z" wrote:
I have written the following Win app in VB.NET 2003 . The class is simply
picture boxes that behave in a random order after they have been
instantiated and added to a form. When I create 15 or more instances of my
class, the whole program runs slowly in a way that I have to close the
program. I have tried to create a new thread for each class, but that throws
an exception , because a separated thread can''t be added to a form from a
child class.
My code is this ( please tell me if there are better ways for doing it)
coplile with :
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/t:winexe /main:form1
''----------------------------------- Start of CODE
Imports System.Math
imports Microsoft.VisualBasic
imports System.drawing
imports system.windows.forms
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
''This call is required by the Windows Form Designer.
InitializeComponent()
''Add any initialization after the InitializeComponent() call
End Sub
''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec As Rec = New Rec(Me.CreateGraphics, Me)
End Sub
End Class
''--------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X, Y As Integer
Dim frm As Form
WithEvents TIMER As New Timer
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics, ByVal form As Form)
X = Rnd() * 100
Y = Rnd() * 200
Gr = g
Me.frm = form
ini()
justAddX = True
justAddY = True
End Sub
'' We could use the graphics object here to draw on the form. But since
GDI+
'' lacks the ideal performance, we use picture boxes and add them to the
'' spacified form instead of using the drawing object:
Sub ini()
TIMER.Interval = Rnd() * 10 + 1
TIMER.Enabled = True
PB = New PictureBox
With PB
.BackColor = Color.FromArgb(Rnd() * 150, Rnd() * 200, Rnd() *
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
End With
frm.Controls.Add(PB)
End Sub
'' Use these two boolean to know when to add or substarct numbers
'' the movement of this class object.
Dim justAddX, justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TIMER.Tick
MoveMe()
End Sub
'' CuurentNum X , Y are the numbers added to the speed of X ,Y
'' which creates a kind of change when the Rec hits the walls:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
If X > (frm.Width - PB.Width) - 10 Then
justAddX = False
CurrentNumX = Rnd() * 50
End If
If Y > (frm.Height - PB.Height) - 30 Then
justAddY = False
CurrentNumY = Rnd() * 40
End If
If X < 0 Then
justAddX = True
CurrentNumX = Rnd() * 80
End If
If Y < 0 Then
justAddY = True
CurrentNumY = Rnd() * 30
End If
If justAddX Then
X += CurrentNumX
Else
X -= CurrentNumX
End If
If justAddY Then
Y += CurrentNumY
Else
Y -= CurrentNumY
End If
PB.Left = X
PB.Top = Y
End Sub
End Class
''------------------------------------------------------- End of
CODE------------
Ham,
Nice crossposting, however the in my opinion most right newsgroup for this
question is not in it.
microsoft.public.dotnet.framework.drawing
I saw your problem, however I think because the fact that the most is
graphics you can place it the best in that newsgroup.
No problem at all that crossposting by the way.
Cor
"ham-z" <hamz-3e@/**/yahoo.com>.I have written the following Win app in VB.NET 2003 . The class is simply
picture boxes that behave in a random order after they have been
instantiated and added to a form. When I create 15 or more instances of my
class, the whole program runs slowly in a way that I have to close the
program. I have tried to create a new thread for each class, but that
throws
an exception , because a separated thread can''t be added to a form from a
child class.
My code is this ( please tell me if there are better ways for doing it)
coplile with :
vbc [filename]
/r:system.dll,system.windows.forms.dll,system.drawi ng.dll,microsoft.visualbasic.dll
/t:winexe /main:form1
''----------------------------------- Start of CODE
Imports System.Math
imports Microsoft.VisualBasic
imports System.drawing
imports system.windows.forms
Public Class Form1
Inherits System.Windows.Forms.Form
#Region " Windows Form Designer generated code "
Public Sub New()
MyBase.New()
''This call is required by the Windows Form Designer.
InitializeComponent()
''Add any initialization after the InitializeComponent() call
End Sub
''Form overrides dispose to clean up the component list.
Protected Overloads Overrides Sub Dispose(ByVal disposing As Boolean)
If disposing Then
If Not (components Is Nothing) Then
components.Dispose()
End If
End If
MyBase.Dispose(disposing)
End Sub
''Required by the Windows Form Designer
Private components As System.ComponentModel.IContainer
''NOTE: The following procedure is required by the Windows Form Designer
''It can be modified using the Windows Form Designer.
''Do not modify it using the code editor.
<System.Diagnostics.DebuggerStepThrough()> Private Sub
InitializeComponent()
''
''Form1
''
Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
Me.BackColor = System.Drawing.Color.White
Me.ClientSize = New System.Drawing.Size(384, 266)
Me.Name = "Form1"
Me.Text = "Form1"
End Sub
#End Region
Private Sub Form1_Click(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Click
CreateOBJ()
End Sub
Sub CreateOBJ()
Dim myRec As Rec = New Rec(Me.CreateGraphics, Me)
End Sub
End Class
''--------------------------------------------------- Class Rec
Public Class Rec
Dim Gr As Graphics
Dim X, Y As Integer
Dim frm As Form
WithEvents TIMER As New Timer
Dim bmp As Bitmap
Dim PB As PictureBox
Public Sub New(ByVal g As Graphics, ByVal form As Form)
X = Rnd() * 100
Y = Rnd() * 200
Gr = g
Me.frm = form
ini()
justAddX = True
justAddY = True
End Sub
'' We could use the graphics object here to draw on the form. But since
GDI+
'' lacks the ideal performance, we use picture boxes and add them to the
'' spacified form instead of using the drawing object:
Sub ini()
TIMER.Interval = Rnd() * 10 + 1
TIMER.Enabled = True
PB = New PictureBox
With PB
.BackColor = Color.FromArgb(Rnd() * 150, Rnd() * 200, Rnd() *
230)
.Left = X
.Top = Y
.Height = 6
.Width = 6
End With
frm.Controls.Add(PB)
End Sub
'' Use these two boolean to know when to add or substarct numbers
'' the movement of this class object.
Dim justAddX, justAddY As Boolean
Private Sub TIMER_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles TIMER.Tick
MoveMe()
End Sub
'' CuurentNum X , Y are the numbers added to the speed of X ,Y
'' which creates a kind of change when the Rec hits the walls:
Dim CurrentNumX As Integer = 1
Dim CurrentNumY As Integer = 1
Sub MoveMe()
If X > (frm.Width - PB.Width) - 10 Then
justAddX = False
CurrentNumX = Rnd() * 50
End If
If Y > (frm.Height - PB.Height) - 30 Then
justAddY = False
CurrentNumY = Rnd() * 40
End If
If X < 0 Then
justAddX = True
CurrentNumX = Rnd() * 80
End If
If Y < 0 Then
justAddY = True
CurrentNumY = Rnd() * 30
End If
If justAddX Then
X += CurrentNumX
Else
X -= CurrentNumX
End If
If justAddY Then
Y += CurrentNumY
Else
Y -= CurrentNumY
End If
PB.Left = X
PB.Top = Y
End Sub
End Class
''------------------------------------------------------- End of
CODE------------
"Dennis" <De****@discussions.microsoft.com> schrieb:You might want to check into the API routine BitBlt. It copies rectangle
shapes from and to graphics objects. It''s very, very fast. You could
draw
your images directly on the form using the Paint event.
I doubt that this will be much faster than ''Graphics.DrawImage[Unscaled]''.
--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
这篇关于如何使这更好,更快?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!