Click here to Skip to main content
15,886,518 members
Please Sign up or sign in to vote.
4.00/5 (1 vote)
See more:
The code I'm using :
C#
void function()
{
  Process []x=Process.GetProcessesByName(@"applicationName");
  const uint WM_LBUTTONDOWN = 0x0201;
  const uint WM_LBUTTONUP = 0x0202;
  int dword = MakeDword(17, 230);

  SendMessage(x[0].MainWindowHandle, WM_LBUTTONDOWN,      (IntPtr)MouseButtons.Left, (IntPtr)dword);

 SendMessage(x[0].MainWindowHandle, WM_LBUTTONUP,   (IntPtr)MouseButtons.Left, (IntPtr)dword);
}
public static int MakeDword(short pValueLow, short pValueHigh)
{
    if (pValueHigh == 0)
    {
        return (int)pValueLow;
    }
    int lTemp = sizeof( short);
    lTemp = pValueHigh << ((4) - (lTemp + 1));
    return (int)(lTemp | pValueLow);
}


applicationName.exe Should show having clicked at point 17,230 or whatever coordinate. But it doesn't seem this is happening.

Please tell me some solution, and what is wrong with this code.
Posted
Updated 23-Feb-11 7:53am
v2
Comments
TweakBird 23-Feb-11 13:54pm    
Please use <pre> tag for code blocks.

1 solution

You need the HWnd of the window that's going to receive the message, and use the PostMessage Windows API method (see pinvoke.net for details and prototypes). You should also only have to send the WM_LBUTTONUP to get the desired result. Next, you're going to have to google WM_LBUTTONUP to see what parameters to set in the PostMessage method call.

Here's the stuff you need for PostMessage:

using System.Runtime.InteropServices;

C#
//-------------------------------------------------------------------------
[return: MarshalAs(UnmanagedType.Bool)]
[DllImport("user32.dll", SetLastError=true, CharSet=CharSet.Auto)]
static extern bool PostMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
 
Share this answer
 
v2
Comments
Sergey Alexandrovich Kryukov 23-Feb-11 14:29pm    
Correct, my 5.
--SA
fjdiewornncalwe 23-Feb-11 18:18pm    
OP Comment moved from answer:
@John, thank you; the mistake I was making was including '(IntPtr)MouseButtons.Left' as wParam where there should be 0 as wParam.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900