我使用的几种延迟方法!窗口不假死!!!
我本人一般比较喜欢收藏一下本人的模块或者自己弄出来的整合在一个模块中,并使用Call的方法调用,所以在主窗体代码中就几句!建议:如果就几十行代码就不要在放在模块中今天分享我的延迟代码,因为有时候要用到延时,但窗口就会假死,下面分享几种方法!
在这里献丑了,新人一个···首次分享
===================================华丽的分割线=========================================================
一、循环计算延迟
===================================华丽的分割线=========================================================
'模块代码:
Option Explicit
Public Declare Function GetTickCount Lib "kernel32" () As Long'获取windows启动以来经历的时间
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)'延时API函数
Dim time1 As Long
Dim time2 As Long
Public Sub Delay(ByVal Milliseconds As Long)'单位是毫秒
time1 = GetTickCount
Do
DoEvents'让系统处理其它事件
time2 = GetTickCount
If Milliseconds - (time2 - time1) >= 2 Then
Sleep 1'减少CPU
End If
Loop Until time2 - time1 >= Milliseconds
End Sub
-----------------------------------华丽的分割线---------------------------------------------------------
'主窗体代码:
Option Explicit
Private Sub Command1_Click()
call delay (2000)
msgbox("消息!")
End Sub
===================================华丽的分割线=========================================================
二、多线程延时
===================================华丽的分割线=========================================================
'模块代码:
Option Explicit
Public Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)'延时API函数
Public Declare Function CreateThread Lib "kernel32" (nuil, ByVal dwStackSize As Long, ByVal lpStartAddress As Long, lpParameter As Any, ByVal dwCreationFlags As Long, lpThreadID As Long) As Long
'好像是结束线程
Public Declare Function TerminateThread Lib "kernel32" (ByVal hThread As Long, ByVal dwExitCode As Long) As Long
'好像是创建线程
Public id As Long
Public Function aa()'这里放入要调用的代码
Sleep 2000 '这里是延迟时间
Form1.Caption = "123"
End Function
Public Sub 创建线程()
id = CreateThread(0, 0, AddressOf aa, 0, 0, id)
End Sub
Public Sub 卸载线程()
TerminateThread id, 0
End Sub
-----------------------------------华丽的分割线---------------------------------------------------------
'窗口代码:
Private Sub Command1_Click()
Call 创建线程
End Sub
Private Sub Form_Unload(Cancel As Integer)
Call 卸载线程'一定要在结束程序时使用
End Sub
===================================华丽的分割线=========================================================
使用第二种方法在编译时候一定要设置为P-代码
另外还有第三种多进程(不是多线程!)的方法,但因为有点麻烦!想自动的话自己去百度ActiveX EXE 多线程,这里就不说了!
下次有空我会发全局热键、模拟键盘、模拟坐标、窗口句柄有关的代码!
写的好累给的安慰啊!!!
===================================华丽的分割线=========================================================