Results 1 to 6 of 6

Thread: A SendMessage - question

  1. #1

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Exclamation A SendMessage - question

    Hey dudes,

    To send e message from one prog to another, I use this code:
    Code:
    myHwnd = Findwindow (...)
    nCode = "CreateLog"
    SendMessage(myhWnd, WM_KEYDOWN, nCode, 1)
    Butn, now I'm sending this to the WM_KEYDOWN event, how can I send it to a selfmade sub or function, something like "GetMessages" ??

    Thanks
    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  2. #2
    Fanatic Member crispin's Avatar
    Join Date
    Aug 2000
    Location
    2 clicks west of a Quirkafleeg...Cornwall, England
    Posts
    754
    you would need to SubClass the receiving app and trap the Message....
    Crispin
    VB6 ENT SP5
    VB.NET
    W2K ADV SVR SP3
    WWW.BLOCKSOFT.CO.UK

    [Microsoft Basic: 1976-2001, RIP]

  3. #3

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278
    That sounds good but how to?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  4. #4
    Megatron
    Guest
    I don't quite understand what you mean.. You said:
    how can I send it to a selfmade sub or function,
    .
    You cannot send a message to a function/sub, rather you send it to a window. Perhaps I'm reading it wrong? Please elabourate.

  5. #5

    Thread Starter
    Hyperactive Member WP's Avatar
    Join Date
    Aug 2000
    Location
    Belgium
    Posts
    278

    Question

    Prog1:
    Code:
    Sub
      Call SendMessage (myHwnd, WM_KEYDOWN, &H999, 1)
    End Sub
    Prog2:
    Code:
    Sub Form_KeyDown (KeyCode As Long, x As x)
      MsgBox KeyCode
    End Sub
    Now the message has been received in the Form_KeyDown procedure. But I want to receive it in another sub (like "GetMessages").

    You get it?

    WP

    Visual Basic 6.0 EE SP5 / .Net
    Windows XP

  6. #6
    Fanatic Member Kaverin's Avatar
    Join Date
    Oct 2000
    Posts
    930
    You could subclass your form and then send it a custom message, and then in your message handler procedure, check for that custom message. You would make your custom message value with WM_USER + someval.
    VB Code:
    1. 'in a module
    2. Option Explicit
    3.  
    4. Public Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    5. Public Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    6. Public Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" (ByVal lpPrevWndFunc As Long, ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    7.  
    8. Public Const GWL_WNDPROC As Long = -4
    9. Public Const WM_USER As Long = &H400
    10. Public Const WM_KEYDOWN As Long = &H100 'the specific message you wanted
    11. Public Const CUSTOM As Long = WM_USER + 1 'this is just a sample of a custom message
    12.  
    13. Public lngOldProc As Long 'holds the address of the old window procedure
    14.  
    15. Public Sub Hook(ByVal hWnd As Long)
    16.    'save the old procedure address
    17.    lngOldProc = GetWindowLong(hWnd, GWL_WNDPROC)
    18.    'set the new procedure address
    19.    'CustomProc [b]must[/b] be located in a standard module, not an object module
    20.    SetWindowLong hWnd, GWL_WNDPROC, AddressOf CustomProc
    21. End Sub
    22.  
    23. Public Sub UnHook(ByVal hWnd As Long)
    24.    'restore the old procedure
    25.    'this [b]must[/b] be done before the program terminates, or you'll crash VB
    26.    SetWindowLong hWnd, GWL_WNDPROC, lngOldProc
    27. End Sub
    28.  
    29. Public Function CustomProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    30.    'this is your custom message handler procedure
    31.    Select Case wMsg
    32.       Case WM_KEYDOWN
    33.          'got a keydown message, wParam is the virtual key
    34.          'you could have an "Exit Function" here if you didn't want to allow the system to act on this message (the same goes for all cases)
    35.       Case CUSTOM
    36.          'got the custom message (if it was ever sent)
    37.       Case Else
    38.          'not required, but if you got here, the procedure can't handle the message, so let the system do it
    39.          'you may also want to allow the system to process them all anyway, and you just want to see when certain messages are used
    40.          'in that case, stick the CallWindowProc call outside of the select case
    41.          CustomProc = CallWindowProc(lngOldProc, hWnd, wMsg, wParam, lParam)
    42.    End Select
    43. End Function
    44.  
    45. 'now with all of that out the way, something like this would be in your form
    46. '[b] do not press the IDE stop button.  use the X of the form to close[/b]
    47. Private Sub Form_Load()
    48.    Hook Me.hWnd 'begin the subclassing
    49. End Sub
    50.  
    51. Private Sub Form_Unload(Cancel As Integer)
    52.    UnHook Me.hWnd 'end the subclassing
    53. End Sub
    With that in effect, CustomProc will receive any messages that the form window gets. So if your other program uses something like this:
    VB Code:
    1. SendMessage hWnd, CUSTOM, 0, 0
    that CustomProc procedure will catch it, and you can do something then if you want.
    I'm baaaack...
    VB5 Professional Edition, VC++ 6
    Using a 1 gHz Thunderbird, 256 mb RAM, 40 gb HD system with Win98se

    I feel special because I finally figured out how to loop midis: Post link
    I'm a fanatic too

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width