locked
How to perform a click in another application - MAKELPARAM alternative in .NET ? RRS feed

  • Question

  • 'Situation:

    'From Application A I would like to execute code that performs a click on a given coordinate in Application B; for instance activate a button in Application B.

     

    'Possible solution:

    'The function SendMessage() is a possible solution by - among other parameters - passing the Application B handle, but the x- & y-coordinates I'm not able to put in

     

    'Problem:

    'I know that the MAKELPARAM() macro exist for C++ programmers but it is not available in .NET.

     

    'MAKELPARAM "prepares" so to speak the x- and y-coordinates so they can function as one parameter in the SendMessage function.

     

    'I'm not sure what happens behind the scene when MAKELPARAM takes action, but I would like to convert the x- & y-coordinates to the right format like it does it.

    'Any ideas of how to do this ? 

     

    'Comments:

    'Using other parameters and thus performing other tasks the messages go through and perform so the problem should be the wrong format of the coordinates.

     

    Imports System.Runtime.InteropServices

     

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

     

    Public Const WM_LBUTTONDOWN As Integer = &H201

    Public Const WM_LBUTTONUP As Integer = &H202

     

    Dim lParam As Long = '??? <== this variable will be the holder of the x & y coordinates

     

    SendMessage(aWindowHandle, WM_LBUTTONDOWN, 1, lParam)

     

    'Answers are more than welcome in C# too :-)

     

     

    Saturday, October 27, 2007 2:13 AM

Answers

All replies

  • Hi, Bahia2000
    Public Function MAKELPARAM(wLow As Long, wHigh As Long) As Long

     MAKELPARAM = LoWord(wLow) Or (&H10000 * LoWord(wHigh))

    End Function


    Sunday, October 28, 2007 2:56 AM
  • Hi Kornev

     

    Thanks for your reply.

     

    I added the function MAKELPARAM you gave me and a LoWord one (see below), since it is used by the MAKELPARAM function. I passed the x- & y-coordinates, but no button down event fires. Any ideas why ?:

     

    '*****************************************************

     

    Imports System.Runtime.InteropServices

     

    Private Declare Function SendMessage Lib "user32" Alias "SendMessageA" (ByVal hwnd As IntPtr, ByVal wMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long

     

    Public Const WM_LBUTTONDOWN As Integer = &H201

    Public Const WM_LBUTTONUP As Integer = &H202

     

    Dim x As Long = 200 '<= x-coordinate

    Dim y As Long = 200 '<= y-coordinate

     

     

    SendMessage(aWindowHandle, WM_LBUTTONDOWN, 1, MAKELPARAM(x,y))

     

     

    Public Function MAKELPARAM(ByVal wLow As Long, ByVal wHigh As Long) As Long

            MAKELPARAM = LoWord(wLow) Or (&H10000 * LoWord(wHigh))

    End Function

     

    Public Function LoWord(ByVal lDWord As Long) As Long

            If lDWord And &H8000& Then

                LoWord = lDWord Or &HFFFF0000

            Else

                LoWord = lDWord And &HFFFF&

            End If

    End Function

     

    '*****************************************************

    Sunday, October 28, 2007 1:58 PM
  •  

    An information to the previous post.

     

    The receiving Application B's (another application) form's mouse down event fires, when the code is executed, but the desired buttons mouse down event does not fire even though the coordinates are correct.

     

    I tried to send the SendMessage to the sender application (Application A = same application), which also has a button, and the same happend. The form's mouse down event fires, but the desired button does not.

     

    Any ideas why the form event fires and not the button event, when the coordinates point to the button ?.

    Sunday, October 28, 2007 5:51 PM
  • Hi Bahia,

    Please check out the following references which already implemented what you want to achieve:

    Interoperating with Windows Media Player using P/Invoke and C#, An article demonstrating the usage of Platform Invoke Services in Visual C# to interoperate with Windows Media Player.

    Using P/Invoke to Call Unmanaged APIs from Your Managed Classes - These sample applications show how to mute sounds, change Windows resolution, and display balloon tips from your managed code. (33 printed pages)

    Hope this helps!

    Thanks!

    Monday, October 29, 2007 10:24 AM
  • Hi Feng Chen

     

    Thank you for your answer. I will be away all week, but can't wait to come back and have a look at your links since I have spend so many hours in vain trying to find a solution :-)

     

    Thank you again for your tip and have a good week to come :-)

    Tuesday, October 30, 2007 12:53 AM