VB 判断窗体失去焦点
注意问题.本窗体整个窗体都被PIC控件覆盖,LostFocus不能用请各位大侠帮忙,API最好不用Tim循环的...
注意问题. 本窗体整个窗体都被 PIC控件 覆盖, LostFocus不能用请各位大侠帮忙,API 最好不用Tim循环的
展开
3个回答
2013-09-04
展开全部
思路,用windows API函数 GetForegroundWindow 得到前台窗口的句柄,然后跟窗口的句柄比较... 如果相同就是得到焦点,否则失去焦点,例子中我用的timer检测,,,,,,代码如下 Option ExplicitPrivate Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Form_Load()
Timer1.Interval = 10
End SubPrivate Sub Timer1_Timer()
Dim thwnd As Long
thwnd = GetForegroundWindow
If thwnd <> Me.hWnd Then
Me.Caption = "失去焦点"
Else
Me.Caption = "得到焦点"
End If
End Sub
Private Sub Form_Load()
Timer1.Interval = 10
End SubPrivate Sub Timer1_Timer()
Dim thwnd As Long
thwnd = GetForegroundWindow
If thwnd <> Me.hWnd Then
Me.Caption = "失去焦点"
Else
Me.Caption = "得到焦点"
End If
End Sub
2013-09-04
展开全部
'第一种:这下只能在多窗口程序中有效:Private Sub Form_Deactivate()
MsgBox "失去焦点了"
End Sub '第二种:要用API:'添加一个定时器,interval设为1000Private Declare Function GetActiveWindow Lib "user32" () As Long
Dim H As Long
Private Sub Timer1_Timer()
H = GetActiveWindow
If H <> Me.hWnd Then
MsgBox "失去焦点"
End If
End Sub
MsgBox "失去焦点了"
End Sub '第二种:要用API:'添加一个定时器,interval设为1000Private Declare Function GetActiveWindow Lib "user32" () As Long
Dim H As Long
Private Sub Timer1_Timer()
H = GetActiveWindow
If H <> Me.hWnd Then
MsgBox "失去焦点"
End If
End Sub
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
2013-09-04
展开全部
简单点的方法是:
在窗体上放一个Timer,将Interval设为100。然后输入以下代码:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
If GetForegroundWindow() <> hWnd Then StartSecurity
End Sub
在窗体上放一个Timer,将Interval设为100。然后输入以下代码:
Private Declare Function GetForegroundWindow Lib "user32" () As Long
Private Sub Timer1_Timer()
If GetForegroundWindow() <> hWnd Then StartSecurity
End Sub
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询