+ -
当前位置:首页 → 问答吧 → VB 中给对象计时的问题

VB 中给对象计时的问题

时间:2011-10-11

来源:互联网

我想给一个对象计时如红绿灯 我想计算红灯亮的时间 看它亮灯是否超时··· 一般我们都会有timer控件来做这件事。

但是 我想计时的对象 有几十个···怎么办呢??那么多TIMER控件会严重影响到程序效率吧?

有什么有其他的办法 解决类似的问题呢?

作者: mumumeteor   发布时间: 2011-10-11

如果时间间隔相同,可以采用多个变量,在同一计时事件中对它加1,要启用某个事件时,对相应的变量置0,然后在计时事件过程中对它加1后判断是否到达某个阈值而进行某种操作.

作者: qianjin036a   发布时间: 2011-10-12

一个定时器就可以了,比如你的灯是个控件数组,那么在Time事件里循环控件参数就可以了。
如:
VB code

Private Sub Form_Load()
    Dim i       As Long
    Dim x       As Long
    Dim y       As Long
    Dim wCount  As Long
    Dim dwWidth As Long
    
    '========================================
    ' 注意 LED.Index 不能为空,将其设置为 0
    '========================================
    dwWidth = (15 * 12)
    wCount = (Me.Width - 15 * 6) / dwWidth
    
    Randomize
    LED(0).Tag = Int((60 * Rnd) + 0)
    If Int((2 * Rnd) + 0) Then
        LED(0).BackColor = &HFF&
    Else
        LED(0).BackColor = &HFF00&
    End If
    LED(0).Move 0, 0, dwWidth, dwWidth
    ' 动态加载99个名为LED的 PictureBox 控件
    For i = 1 To 99
        x = x + 1
        If x >= wCount Then
            x = 0
            y = y + 1
        End If
        ' 加载新的控件数组元素
        Load LED(i)
        ' 像控件数组元素的 Tag 属性随机分配一个 0-60 之间的整数
        LED(i).Tag = Int((60 * Rnd) + 0)
        
        ' 随机设置控件的背景色为红色或绿色
        If Int((2 * Rnd) + 0) Then
            LED(i).BackColor = &HFF&
        Else
            LED(i).BackColor = &HFF00&
        End If
        LED(i).Move x * dwWidth, y * dwWidth, dwWidth, dwWidth
        LED(i).Visible = True
    Next i
    
    Timer1.Interval = 100
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Dim i       As Long
    Randomize
    For i = 0 To 99
        ' 判断每个控件当前色存活的时间是否已要完结
        If Int(LED(i).Tag) <= 1 Then
            ' 判断原来如果是红色就变绿色,是绿色就变红色
            If LED(i).BackColor = &HFF& Then
                LED(i).BackColor = &HFF00&
            Else
                LED(i).BackColor = &HFF&
            End If
            ' 重新随机给颜色设置一个存活时间
            LED(i).Tag = Int((60 * Rnd) + 0)
        Else
            LED(i).Tag = Int(LED(i).Tag) - 1
        End If
    Next i
End Sub

作者: SupermanKing   发布时间: 2011-10-12

这样改一下效果更加好:
1、新建一个标准 EXE 工程
2、在窗体中放置一个 PictureBox 控件
3、设置 PictureBox 控件的名称为 LED,同时设置 Index 属性为 0
4、进入代码编辑,粘贴以下代码
VB code

Private Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Type RECT
        Left As Long
        Top As Long
        Right As Long
        Bottom As Long
End Type
Dim LEDCount    As Long

Private Sub Form_Load()
    Dim i           As Long
    Dim x           As Long
    Dim y           As Long
    Dim wCount      As Long
    Dim hCount      As Long
    Dim dwWidth     As Long
    Dim wRect       As RECT
    
    '========================================
    ' 注意 LED.Index 不能为空,将其设置为 0
    '========================================
    dwWidth = (15 * 12)
    
    GetClientRect Me.hwnd, wRect
    wCount = (wRect.Right * 15) / dwWidth
    hCount = (wRect.Bottom * 15) / dwWidth
    LEDCount = wCount * hCount - 1
    
    Randomize
    LED(0).Tag = Int((60 * Rnd) + 0)
    If Int((2 * Rnd) + 0) Then
        LED(0).BackColor = &HFF&
    Else
        LED(0).BackColor = &HFF00&
    End If
    LED(0).Move 0, 0, dwWidth, dwWidth
    ' 动态加载99个名为LED的 PictureBox 控件
    For i = 1 To LEDCount
        x = x + 1
        If x >= wCount Then
            x = 0
            y = y + 1
        End If
        ' 加载新的控件数组元素
        Load LED(i)
        ' 像控件数组元素的 Tag 属性随机分配一个 0-60 之间的整数
        LED(i).Tag = Int((60 * Rnd) + 0)
        
        ' 随机设置控件的背景色为红色或绿色
        If Int((2 * Rnd) + 0) Then
            LED(i).BackColor = &HFF&
        Else
            LED(i).BackColor = &HFF00&
        End If
        LED(i).Move x * dwWidth, y * dwWidth, dwWidth, dwWidth
        LED(i).Visible = True
    Next i
    
    Timer1.Interval = 1
    Timer1.Enabled = True
End Sub

Private Sub Timer1_Timer()
    Dim i       As Long
    Randomize
    For i = 0 To LEDCount
        ' 判断每个控件当前色存活的时间是否已要完结
        If Int(LED(i).Tag) <= 1 Then
            ' 判断原来如果是红色就变绿色,是绿色就变红色
            If LED(i).BackColor = &HFF& Then
                LED(i).BackColor = &HFF00&
            Else
                LED(i).BackColor = &HFF&
            End If
            ' 重新随机给颜色设置一个存活时间
            LED(i).Tag = Int((60 * Rnd) + 0)
        Else
            LED(i).Tag = Int(LED(i).Tag) - 1
        End If
    Next i
End Sub

作者: SupermanKing   发布时间: 2011-10-12

热门下载

更多