虽然昨天的程序是实现了功能,利用了textbox可以设置好延时时间。但是发现用vb自带的sleep,会完全占用资源,sleep时就不能再对界面进行操作了,即使按下关闭也不行。
在网上查询了好些资料,发现可以使用doevents来解决这个问题。
注:sleep的时候,cpu会被完全占用。而doevents是释放cpu占有权的事件,这样正好可以解决问题。
在网上找了一个合适的函数可以使用,代码如下:
Protected Sub WaitingSeconds(ByVal seconds As Integer)
Dim tmpNow As Date = Now
While Now.Subtract(tmpNow).Seconds < seconds
Windows.Forms.Application.DoEvents()
End While
End Sub
此时,昨天sleep导致的问题就此解决。还是来再发一遍新的程序代码吧:(注:界面不变)
Public Class Form1
Public Declare Sub keybd_event Lib "user32" (ByVal bVk As Byte, ByVal Scan As Byte, ByVal dwFlags As Long, ByVal dwExtraInfo As Long)
Declare Sub Sleep Lib "kernel32" (ByVal dwMilliseconds As Long)
Const KEYEVENTF_KEYUP = &H2
Const VK_TAB = 9
Const VK_A = 65
Const VK_B = 66
Const VK_C = 67
Const VK_D = 68
Const VK_E = 69
Const VK_F = 70
Const VK_G = 71
Const VK_H = 72
Const VK_I = 73
Const VK_J = 74
Const VK_K = 75
Const VK_L = 76
Const VK_M = 77
Const VK_N = 78
Const VK_O = 79
Const VK_P = 80
Const VK_Q = 81
Const VK_R = 82
Const VK_S = 83
Const VK_T = 84
Const VK_U = 85
Const VK_V = 86
Const VK_W = 87
Const VK_X = 88
Const VK_Y = 89
Const VK_Z = 90
Const VK_0 = 96
Const VK_1 = 97
Const VK_2 = 98
Const VK_3 = 99
Const VK_4 = 100
Const VK_5 = 101
Const VK_6 = 102
Const VK_7 = 103
Const VK_8 = 104
Const VK_9 = 105
Const VK_WINDOW = 91
Dim delay001 As Single
Dim delay002 As Single
Dim delay003 As Single
Dim delay004 As Single
Dim delay005 As Single
Dim looptime As Single
Dim i As Long
Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged
delay001 = CInt(TextBox1.Text.Trim)
End Sub
Private Sub TextBox2_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox2.TextChanged
delay002 = CInt(TextBox2.Text.Trim)
End Sub
Private Sub TextBox3_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox3.TextChanged
delay003 = CInt(TextBox3.Text.Trim)
End Sub
Private Sub TextBox4_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox4.TextChanged
delay004 = CInt(TextBox4.Text.Trim)
End Sub
Private Sub TextBox5_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox5.TextChanged
delay005 = CInt(TextBox5.Text.Trim)
End Sub
Private Sub TextBox6_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox6.TextChanged
looptime = CInt(TextBox6.Text.Trim)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
For i As Int16 = 1 To looptime Step 1
Call Sleep(2000)
Call keybd_event(VK_TAB, 0, 0, 0) '按下TAB键
Call Sleep(100) '延时
Call keybd_event(VK_TAB, 0, KEYEVENTF_KEYUP, 0) '释放TAB键
Call WaitingSeconds(delay001) '延时
Call keybd_event(VK_7, 0, 0, 0) '按下7键
Call Sleep(100) '延时
Call keybd_event(VK_7, 0, KEYEVENTF_KEYUP, 0) '释放7键
Call WaitingSeconds(delay002) '延时
Call keybd_event(VK_7, 0, 0, 0) '按下7键
Call Sleep(100) '延时
Call keybd_event(VK_7, 0, KEYEVENTF_KEYUP, 0) '释放7键
Call WaitingSeconds1(delay003) '延时
Call keybd_event(VK_1, 0, 0, 0) '按下1键
Call Sleep(100) '延时
Call keybd_event(VK_1, 0, KEYEVENTF_KEYUP, 0) '释放1键
Call WaitingSeconds(delay004) '延时
Call keybd_event(VK_1, 0, 0, 0) '按下1键
Call Sleep(100) '延时
Call keybd_event(VK_1, 0, KEYEVENTF_KEYUP, 0) '释放1键
Call WaitingSeconds(delay005) '延时
Next
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
End
End Sub
Protected Sub WaitingSeconds(ByVal seconds As Integer)
Dim tmpNow As Date = Now
While Now.Subtract(tmpNow).Seconds < seconds
Windows.Forms.Application.DoEvents()
End While
End Sub
End Class
至此,我们已经初战告捷。