vb通过怎样通过按键使程序暂停运行,再按一个按键重新开始运行?
一个vb程序有两个窗体form1、form2,并有一个时间控件timer、一个按钮控件command1。想通过按退出键使程序暂停运行,再按一次回车重新开始运行。但怎么没有...
一个vb程序有两个窗体form1、form2,并有一个时间控件timer、一个按钮控件command1。想通过按退出键使程序暂停运行,再按一次回车重新开始运行。但怎么没有效果?代码如下:
Dim tt As Long, xx As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Me.Hide
End Sub
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = False
tt = 2
End Sub
Private Sub Timer1_Timer()
If tt = 0 Then
Form2.Show
Form1.Hide
xx = xx + 1
ElseIf tt = 1 Then
Form1.Show
Form2.Hide
End If
tt = tt + 1
If tt >= 5 Then tt = 0
If xx >= 20 Then End
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Timer1.Interval = 0
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
Timer1.Interval = 300
End If
End Sub 展开
Dim tt As Long, xx As Integer
Private Sub Command1_Click()
Timer1.Enabled = True
Me.Hide
End Sub
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = False
tt = 2
End Sub
Private Sub Timer1_Timer()
If tt = 0 Then
Form2.Show
Form1.Hide
xx = xx + 1
ElseIf tt = 1 Then
Form1.Show
Form2.Hide
End If
tt = tt + 1
If tt >= 5 Then tt = 0
If xx >= 20 Then End
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Timer1.Interval = 0
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
Timer1.Interval = 300
End If
End Sub 展开
3个回答
展开全部
当窗体隐藏后是无法接收你按的键的(只有拥有焦点的窗体才能接收,否则的话你在这个窗体打的字却出现在另一个软件的窗体中,这还了得!)。这种时候应该用API函数设置系统全局热键才行(也就是说给Windows系统注册一个热键,告诉它按这个键就怎么怎么样),这个对于你来说可能就复杂点了。
更多追问追答
追问
愿闻其详。请给个例子代码
追答
以下是一个简单的例子,按Ctrl+F7显示窗体,按Ctrl+F8隐藏窗体。需要用到一个Timer控件
Private Declare Function GetAsyncKeyState Lib "user32" (ByVal vKey As Long) As Integer
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = True
End Sub
Private Sub Timer1_Timer()
If GetAsyncKeyState(vbKeyF7) 0 And GetAsyncKeyState(vbKeyControl) 0 Then
Me.Show
ElseIf GetAsyncKeyState(vbKeyF8) 0 And GetAsyncKeyState(vbKeyControl) 0 Then
Me.Hide
End If
End Sub
注意哦,尽量不要用ESC键、回车键这些常用的单键作为热键哦,因为这个热键是在任何时候(包括你在玩游戏、打字等等)都起作用的,所以最好用其他软件不会用到的组合键,以免造成冲突。
展开全部
form1的代码
-------------------------------------------------------
Private Sub Command1_Click()
Timer1.Enabled = True
Me.Hide
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
Form_KeyPress KeyAscii
End Sub
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = False
tt = 2
End Sub
Private Sub Timer1_Timer()
If tt = 0 Then
Form2.Show
Form1.Hide
xx = xx + 1
ElseIf tt = 1 Then
Form1.Show
Form2.Hide
End If
tt = tt + 1
If tt >= 5 Then tt = 0
If xx >= 20 Then End
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
End If
End Sub
------------------------
form2的代码
---------------------------------------
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Form1.Timer1.Interval = 0
Form1.Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Form1.Timer1.Enabled = True
Form1.Timer1.Interval = 300
End If
End Sub
-------------------------------------------------------
Private Sub Command1_Click()
Timer1.Enabled = True
Me.Hide
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
Form_KeyPress KeyAscii
End Sub
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = False
tt = 2
End Sub
Private Sub Timer1_Timer()
If tt = 0 Then
Form2.Show
Form1.Hide
xx = xx + 1
ElseIf tt = 1 Then
Form1.Show
Form2.Hide
End If
tt = tt + 1
If tt >= 5 Then tt = 0
If xx >= 20 Then End
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
End If
End Sub
------------------------
form2的代码
---------------------------------------
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Form1.Timer1.Interval = 0
Form1.Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Form1.Timer1.Enabled = True
Form1.Timer1.Interval = 300
End If
End Sub
追问
可用,想改进下:怎样让按空格键停止,再按空格键开始呢?另外,如果两个窗体都是一直隐藏的状态,怎么办?
追答
还真不容易
form1
---------------
Dim tt As Long, xx As Integer, dj As Boolean
Private Sub Command1_Click()
If dj = False Then
Timer1.Enabled = True
Me.Hide
dj = False
End If
End Sub
Private Sub Command1_KeyPress(KeyAscii As Integer)
Form_KeyPress KeyAscii
dj = True
End Sub
Private Sub Form_Load()
Timer1.Interval = 300
Timer1.Enabled = False
tt = 2
End Sub
Private Sub Timer1_Timer()
If tt = 0 Then
Form2.Show
Form1.Hide
xx = xx + 1
ElseIf tt = 1 Then
Form1.Show
Form2.Hide
End If
tt = tt + 1
If tt >= 5 Then tt = 0
If xx >= 20 Then End
End Sub
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then '按esc键
Timer1.Enabled = Not Timer1.Enabled
End If
End Sub
--------
form2
--------
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 32 Then '按esc键
Form1.Timer1.Enabled = Not Form1.Timer1.Enabled
End If
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
Private Sub Form_KeyPress(KeyAscii As Integer)
If KeyAscii = 27 Then '按esc键
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
End If
End Sub
If KeyAscii = 27 Then '按esc键
Timer1.Enabled = False
ElseIf KeyAscii = 13 Then '按回车
Timer1.Enabled = True
End If
End Sub
追问
测试你的代码加上不管用啊。
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询