标题:API的问题
只看楼主
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
结帖率:100%
已结贴  问题点数:20 回复次数:15 
API的问题
程序代码:
form1中的代码:
Dim WindowHandle As Long

Private Sub Form_Click()
    SendMessage WindowHandle, WM_CLOSE, 0, 0
End Sub

Private Sub Form_Load()
    WindowHandle = FindWindow(vbNullString, "新建 文本文档 - 记事本")
    Debug.Print WindowHandle
    pWndProc = GetWindowLong(WindowHandle, GWL_WNDPROC)  '获取当前窗口的消息地址

    SetWindowLong WindowHandle, GWL_WNDPROC, AddressOf WindowProc    '把地址指向模块中的 WindowProc 回调函数

End Sub

Private Sub Form_Unload(Cancel As Integer)

    SetWindowLong WindowHandle, GWL_WNDPROC, pWndProc    '不在处理窗体默认的消息时应该返回给系统

End Sub


模块modulel中的代码:
Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) As Long
Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public 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 WM_CLOSE = &H10
Public Const GWL_WNDPROC = (-4)

Public pWndProc As Long '把它当然指针来使用
Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long


Public Function WindowProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

    

    Debug.Print Hex$(Msg)   '以十六进制的方式查看当前的消息情况
     If Msg = WM_CLOSE Then '处理 WM_CLOSE 窗口关闭消息



        WindowProc = 1

        Exit Function



    End If



    

    WindowProc = CallWindowProc(pWndProc, hwnd, Msg, wParam, lParam)    '把不需要处理的消息返回给系统(是把已处理的消息按 lpPrevWndFunc(原窗口消息地址) 返回给该地址)

End Function
  这个程序怎么拦截不了关闭消息,纠结中,麻烦大家帮忙解答。

[ 本帖最后由 nbaqqqq 于 2010-5-2 11:12 编辑 ]
搜索更多相关主题的帖子: API 
2010-05-01 20:54
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
得分:0 
额,没人回。
2010-05-01 21:54
xiaomarn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:348
专家分:2026
注 册:2009-3-18
得分:20 
form1:
Option Explicit


Dim pWndProc As Long

Private Sub Form_Click()
    If wndHandle <> 0 Then
        MsgBox "find it "
    End If
    SendMessage wndHandle, WM_COMMAND, WM_CLOSE, 0
End Sub

Private Sub Form_Load()
    wndHandle = FindWindow(vbNullString, "计算器")              '找的计算器测试的
    If wndHandle <> 0 Then
        MsgBox "find it "
    End If
    pWndProc = GetWindowLong(wndHandle, GWL_WNDPROC)
    SetWindowLong wndHandle, GWL_WNDPROC, AddressOf WndProc
End Sub

Private Sub Form_Unload(Cancel As Integer)
    SetWindowLong wndHandle, GWL_WNDPROC, pWndProc
End Sub
module1:
Option Explicit

Public Declare Function FindWindow Lib "user32" Alias "FindWindowA" (ByVal lpClassName As String, ByVal lpWindowName As String) As Long

Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long

Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long

Public Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long

Public 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 WM_CLOSE = &H10

Public Const GWL_WNDPROC = (-4)

Public wndHandle As Long

Public Const WM_RBUTTONDOWN = &H204
Public Const WM_COMMAND = &H111

Public Function WndProc(ByVal hWnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    If Msg = WM_COMMAND And wParam = WM_CLOSE Then         '拦截消息
        
    Else
        WndProc = CallWindowProc(wndHandle, hWnd, Msg, wParam, lParam)
    End If
End Function
错在ssendmessage 的参数上了。测试了一下好像达到楼主要求了。
2010-05-01 22:54
xiaomarn
Rank: 11Rank: 11Rank: 11Rank: 11
等 级:小飞侠
威 望:5
帖 子:348
专家分:2026
注 册:2009-3-18
得分:0 
晕,好像还是错了,期待高手解决,潜水学习去了。sorry!
2010-05-01 23:01
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
得分:0 
你程序对的,能拦截。就是那个SendMessage wndHandle, WM_COMMAND, WM_CLOSE, 0参数错了。
2010-05-02 10:34
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
得分:0 
额,怎么又拦截不了了。
2010-05-02 10:38
ytsm0526
Rank: 2
来 自:西安市
等 级:论坛游民
威 望:2
帖 子:203
专家分:60
注 册:2007-10-28
得分:0 
学习中,谢谢

作舍道边,三年不成。
多抽出一分钟时间学习,让你的生命更加精彩!,
2010-05-02 10:41
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
得分:0 
回复 4楼 xiaomarn
这位大哥,是你在帮我。。。
2010-05-02 11:06
nbaqqqq
Rank: 3Rank: 3
等 级:论坛游侠
帖 子:202
专家分:137
注 册:2009-11-6
得分:0 
难道是记事本或计算器获得焦点,消息才会传到回调函数中处理?高手快来帮帮忙啊。
2010-05-02 11:34
bczgvip
Rank: 14Rank: 14Rank: 14Rank: 14
等 级:贵宾
威 望:66
帖 子:1310
专家分:5312
注 册:2009-2-26
得分:0 
你要干什么?
2010-05-02 12:31



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-305079-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.963805 second(s), 7 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved