SendMessage only works once on same object
I have two apps, App1 and App2. App1 has the HWND of App2's Command1 button. In App1 I have this code:
App1 Code
Code:
Private Sub Command1_Click()
If Command1.Caption = "Start" Then
Command1.Caption = "Stop"
SetForegroundWindow App2Command1Hwnd
SendMessageByString App2Command1Hwnd, BM_CLICK, 0, 0
Else
Command2.Caption = "Start"
SetForegroundWindow App2Command1Hwnd
SendMessageByString App2Command1Hwnd, BM_CLICK, 0, 0
End If
End Sub
The first time I click on Command1 in App1 it sends the BM_CLICK to App2's Command1 and clicks it. Code under Start in App2 runs
The second time I click on Command1 in App1 nothing changes in App2's Command1 click event. Processing continues; does not stop. Actually the button is not fired at all because I have MsgBoxes telling me where control is. First time MsgBox prints "Inside If", second time no message is displayed where I thought it would have been "Inside Else".
App2's Form gets the Focus on both times I click on App1's Command1 button
App2 Code
Code:
Private Sub Command1_Click()
If Command1.Caption = "Start" Then
'
' ENTERS HERE
'
MsgBox "Inside If"
Command1.Caption = "Stop"
Else
'
' NEVER ENTERS HERE
'
MsgBox "Inside Else"
Command1.Caption = "Start"
End If
End Sub
Re: SendMessage only works once on same object
Hey JMS, I might be wrong on this, but does it require you to send a 'mouseup' to let free the Command1_Click event? Like when you use keybd_event API you need to send the button up or else it can cause problems.
Re: SendMessage only works once on same object
MAX, I thought that BM_CLICK was both mouse down and mouse up (hence Click).
I'll try adding a BM_MOUSEUP and see what happens. Thanks for the thought, however.
Re: SendMessage only works once on same object
Quote:
Originally Posted by
jmsrickland
MAX, I thought that BM_CLICK was both mouse down and mouse up (hence Click).
I'll try adding a BM_MOUSEUP and see what happens. Thanks for the thought, however.
BM_CLICK is the same as sending WM_LBUTTONDOWN and WM_LBUTTONUP, so no need to send more messages!
First thing to check is your API declare, SendMessageByString just sounds wrong, you should be using the normal Sendmessage API as it is in the VB6 API viewer, bm_click should be sent as a long in VB6 and not a string!
Remember when using sendmessage... if app2 goes into a busy loop after receiving a message from app1 then app1 will freeze until app2 finishes the work or calls DoEvents, also if app2 is in a busy loop it will not act upon messages, use timers and/or call doevents as needed, You may also want to look up and read about PostMessage and SendMessageTimeOut and see how they differ.
Code:
Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As Long, ByVal wMsg As Long, ByVal wParam As Long, lParam As Any) As Long
Re: SendMessage only works once on same object
Well I have tried SendMessage and PostMessage. It simply doesn't work. I decided to use two buttons, one to start and the other to stop. With two buttons it works correctly.