|
-
Aug 3rd, 2001, 06:28 AM
#1
Thread Starter
Hyperactive Member
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
-
Aug 3rd, 2001, 06:31 AM
#2
Fanatic Member
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]
-
Aug 3rd, 2001, 07:19 AM
#3
Thread Starter
Hyperactive Member
That sounds good but how to?
WP
-
Aug 4th, 2001, 07:53 AM
#4
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.
-
Aug 4th, 2001, 10:32 AM
#5
Thread Starter
Hyperactive Member
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
-
Aug 4th, 2001, 04:35 PM
#6
Fanatic Member
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:
'in a module
Option Explicit
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 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
Public Const GWL_WNDPROC As Long = -4
Public Const WM_USER As Long = &H400
Public Const WM_KEYDOWN As Long = &H100 'the specific message you wanted
Public Const CUSTOM As Long = WM_USER + 1 'this is just a sample of a custom message
Public lngOldProc As Long 'holds the address of the old window procedure
Public Sub Hook(ByVal hWnd As Long)
'save the old procedure address
lngOldProc = GetWindowLong(hWnd, GWL_WNDPROC)
'set the new procedure address
'CustomProc [b]must[/b] be located in a standard module, not an object module
SetWindowLong hWnd, GWL_WNDPROC, AddressOf CustomProc
End Sub
Public Sub UnHook(ByVal hWnd As Long)
'restore the old procedure
'this [b]must[/b] be done before the program terminates, or you'll crash VB
SetWindowLong hWnd, GWL_WNDPROC, lngOldProc
End Sub
Public Function CustomProc(ByVal hWnd As Long, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
'this is your custom message handler procedure
Select Case wMsg
Case WM_KEYDOWN
'got a keydown message, wParam is the virtual key
'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)
Case CUSTOM
'got the custom message (if it was ever sent)
Case Else
'not required, but if you got here, the procedure can't handle the message, so let the system do it
'you may also want to allow the system to process them all anyway, and you just want to see when certain messages are used
'in that case, stick the CallWindowProc call outside of the select case
CustomProc = CallWindowProc(lngOldProc, hWnd, wMsg, wParam, lParam)
End Select
End Function
'now with all of that out the way, something like this would be in your form
'[b] do not press the IDE stop button. use the X of the form to close[/b]
Private Sub Form_Load()
Hook Me.hWnd 'begin the subclassing
End Sub
Private Sub Form_Unload(Cancel As Integer)
UnHook Me.hWnd 'end the subclassing
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:
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|