Results 1 to 5 of 5

Thread: SendMessage only works once on same object

  1. #1

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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
    Last edited by jmsrickland; Jun 10th, 2013 at 04:06 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  2. #2
    PowerPoster
    Join Date
    Aug 2011
    Location
    B.C., Canada
    Posts
    2,887

    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.

  3. #3

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.
    Last edited by jmsrickland; Jun 10th, 2013 at 07:46 PM.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

  4. #4
    VB For Fun Edgemeal's Avatar
    Join Date
    Sep 2006
    Location
    WindowFromPoint
    Posts
    4,255

    Re: SendMessage only works once on same object

    Quote Originally Posted by jmsrickland View Post
    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

  5. #5

    Thread Starter
    PowerPoster
    Join Date
    Jan 2008
    Posts
    11,074

    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.


    Anything I post is an example only and is not intended to be the only solution, the total solution nor the final solution to your request nor do I claim that it is. If you find it useful then it is entirely up to you to make whatever changes necessary you feel are adequate for your purposes.

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