VB6用API截取窗口消息来判断窗体是否失去焦点-会的是神!
例子代码量很少,就是运行时窗体一最小化后VB就毫无提示立刻崩溃。。。‘模块内容OptionExplicitDeclareFunctionSetWindowLongLib"...
例子代码量很少,就是运行时窗体一最小化后VB就毫无提示立刻崩溃。。。
‘模块内容
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_ACTIVATE = &H6
Public Const WA_ACTIVE = 1
Public Const WA_CLICKACTIVE = 2
Public Const WA_INACTIVE = 0
Public preWndProc As Long
Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fActive As Integer
If Msg = WM_ACTIVATE Then
'取得wParam的LowWord
fActive = CInt(wParam And &HFFFF)
If fActive = WA_INACTIVE Then
Debug.Print "InActive "
Else
Debug.Print "Active"
End If
End If
'将之送往原来的Window Procedure
WndProc = CallWindowProc(preWndProc, hwnd, Msg, wParam, lParam)
End Function
’form1内容
Option Explicit
Private Sub Form_Load()
preWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'记录原本的Window Procedure的位址preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'设定Combo1的window Procedure到wndproc
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
'取消Message的截取,而使之又只送往原来的Window Procedure
SetWindowLong Me.hwnd, GWL_WNDPROC, preWndProc
End Sub 展开
‘模块内容
Option Explicit
Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Public Const GWL_WNDPROC = (-4)
Public Const WM_ACTIVATE = &H6
Public Const WA_ACTIVE = 1
Public Const WA_CLICKACTIVE = 2
Public Const WA_INACTIVE = 0
Public preWndProc As Long
Public Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
Dim fActive As Integer
If Msg = WM_ACTIVATE Then
'取得wParam的LowWord
fActive = CInt(wParam And &HFFFF)
If fActive = WA_INACTIVE Then
Debug.Print "InActive "
Else
Debug.Print "Active"
End If
End If
'将之送往原来的Window Procedure
WndProc = CallWindowProc(preWndProc, hwnd, Msg, wParam, lParam)
End Function
’form1内容
Option Explicit
Private Sub Form_Load()
preWndProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'记录原本的Window Procedure的位址preWinProc = GetWindowLong(Me.hwnd, GWL_WNDPROC)
'设定Combo1的window Procedure到wndproc
SetWindowLong Me.hwnd, GWL_WNDPROC, AddressOf WndProc
End Sub
Private Sub Form_Unload(Cancel As Integer)
'取消Message的截取,而使之又只送往原来的Window Procedure
SetWindowLong Me.hwnd, GWL_WNDPROC, preWndProc
End Sub 展开
展开全部
有2种可能性
第一种:
子类化过程中(WndProc())内容做修改后IDE下运行本身就会崩溃。只有重新打开IDE运行才不会崩溃。
第二种:
你的代码中出现了错误,暂停以后子类化跳转出错了于是崩溃了。
请试试修改为:
Dim fActive As Long
If Msg = WM_ACTIVATE Then
'取得wParam的LowWord
fActive = wParam And &HFFFF
If fActive = WA_INACTIVE Then
Debug.Print "InActive "
Else
Debug.Print "Active"
End If
End If
保存然后重新打开这个项目运行,看看是否有错。
另外 子类化崩溃很正常。因为一旦有错直接crash.
第一种:
子类化过程中(WndProc())内容做修改后IDE下运行本身就会崩溃。只有重新打开IDE运行才不会崩溃。
第二种:
你的代码中出现了错误,暂停以后子类化跳转出错了于是崩溃了。
请试试修改为:
Dim fActive As Long
If Msg = WM_ACTIVATE Then
'取得wParam的LowWord
fActive = wParam And &HFFFF
If fActive = WA_INACTIVE Then
Debug.Print "InActive "
Else
Debug.Print "Active"
End If
End If
保存然后重新打开这个项目运行,看看是否有错。
另外 子类化崩溃很正常。因为一旦有错直接crash.
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询