为ACCESS添加多个Timer功能

 众所周知,ACCESS只有一个Timer事件,并不能处理多个触发事件,感觉十分不爽。
  现在我们可以借助API轻松实现多个定时器,而且调用也比较方便,下面是个简单的例子

'模块代码:

'===============================================================
'功能: 添加多个计时器
'用法: 设置计时器 SetTimer Me.hwnd, 1, 10000, AddressOf TimerProc1
' 关闭计时器 KillTimer Me.hwnd, 1
'作者: andymark
' QQ : 42503577 ewang11@163.com
'
'=================================================================


Declare Function SetTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long) As Long
'创建一个计时器
'参数: hwnd 窗口句柄
' nIDEvent 定时器ID,多个定时器时,可以通过该ID判断是哪个定时器
' uElapse 时间间隔,单位为毫秒
' lpTimerFunc 回调 函数

Declare Function KillTimer Lib "user32" (ByVal hwnd As Long, ByVal nIDEvent As Long) As Long
'关闭销毁计时器


'Timer回调涵数
Public Sub TimerProc1(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
MsgBox "测试第1个Timer事件"
End Sub
Public Sub TimerProc2(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
MsgBox "测试第2个Timer事件"
End Sub

Public Sub TimerProc3(ByVal hwnd As Long, ByVal nIDEvent As Long, ByVal uElapse As Long, ByVal lpTimerFunc As Long)
MsgBox "测试第3个Timer事件"
End Sub


' 窗体代码

Private Sub Form_Load()
'设置10秒间隔,调用回调涵数TimerProc1
SetTimer Me.hwnd, 1, 10000, AddressOf TimerProc1
'设置4秒间隔,调用回调涵数TimerProc2
SetTimer Me.hwnd, 2, 4000, AddressOf TimerProc2
'设置14秒间隔,调用回调涵数TimerProc3
SetTimer Me.hwnd, 3, 14000, AddressOf TimerProc3
End Sub

Private Sub Form_Unload(Cancel As Integer)
'关闭所有计时器
KillTimer Me.hwnd, 1
KillTimer Me.hwnd, 2
KillTimer Me.hwnd, 3
End Sub
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要实现在 Delphi 的 ListView 中显示 Access 数据并实现自动刷新的功能,你可以使用 TADOQuery 组件和 TTimer 组件。下面是一个示例代码: ```delphi procedure TForm1.FormCreate(Sender: TObject); begin // 设置定时器的间隔为1秒 Timer1.Interval := 1000; end; procedure TForm1.Timer1Timer(Sender: TObject); begin // 每次定时器触发时,刷新数据 LoadDataFromAccess; end; procedure TForm1.LoadDataFromAccess; var Connection: TADOConnection; Query: TADOQuery; ListItem: TListItem; begin // 创建 ADO 连接对象 Connection := TADOConnection.Create(Self); Connection.ConnectionString := 'Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\path\to\your\database.mdb'; // 创建 ADO 查询对象 Query := TADOQuery.Create(Self); Query.Connection := Connection; Query.SQL.Text := 'SELECT * FROM YourTable'; // 打开连接和查询 Connection.Open; Query.Open; // 清空 ListView 中的项 ListView1.Items.Clear; // 遍历查询结果,将数据添加到 ListView 中 while not Query.Eof do begin ListItem := ListView1.Items.Add; ListItem.Caption := Query.FieldByName('Column1').AsString; ListItem.SubItems.Add(Query.FieldByName('Column2').AsString); // 添加更多列的数据... Query.Next; end; // 关闭连接和查询 Query.Close; Connection.Close; // 释放对象 Query.Free; Connection.Free; end; ``` 在这个示例中,我们使用了一个 TTimer 组件来定期触发刷新数据的操作。在窗体的 OnCreate 事件中,设置了 Timer 的间隔为1秒。然后,在 Timer 的 OnTimer 事件中,调用了 LoadDataFromAccess 这个函数来刷新数据。这样,每隔1秒钟,ListView 中的数据就会自动刷新一次。 你可以根据你的实际需求,调整定时器的间隔和刷新数据的逻辑。同时,注意在释放对象时,按照顺序释放,避免出现内存泄漏。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值