在其他过程里,如何调用form的paint事件?
在其他过程里,如何调用form的paint事件?
[解决办法]
你这个例子在那个帖子里我看过了,并且我也回复过,这个例子里,你把数据该成从串口读取而已,这根本就不能用,运行肯定出错,因为你的线程循环时,根本就不肯定串口有数据可读,当串口无数据时,必然导致错误。
正确的做法在线程中判断串口是否有数据到达,有数据后,读取所有数据放在数组中,另外写一个画图的程序,在程序中用串口收到的数据去画图。简单给你写几句吧:
Public Class Form1 Dim OLDY As Integer = 0 Dim OldX As Integer = 0 Dim G As Graphics Dim bmp As Bitmap Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load bmp = New Bitmap(picEMG.Width, picEMG.Height, Imaging.PixelFormat.Format32bppArgb) G = Graphics.FromImage(bmp) G.Clear(Color.White) picEMG.Image = bmp End Sub Private Sub ReadCommDate() '这个函数的启动方式,你依然可以用你的线程方式,当然还有更多方法 If SerialPort1.BytesToRead > 0 Then Dim B() As Byte Dim N As Integer N = SerialPort1.BytesToRead ReDim B(N - 1) SerialPort1.Read(B, 0, N) DrawImage(B) picEMG.Image = bmp End If End Sub Private Sub DrawImage(ByVal B() As Byte) For i As Integer = 0 To UBound(B) g.DrawLine(Pens.Blue, New Point(OldX, OLDY), New Point(OldX + 2, B(i))) OldX = OldX + 2 OLDY = B(i) If OldX >= picEMG.Width Then OldX = 0 g.Clear(Color.White) End If Next End SubEnd Class