.net winapi postmessage
I know its popular question. anyway. i want to click button on form of other proccess, i cant get handle of button via spy++, so i will simulate mouse click. what i get:
this is global coordinates of point, i want click:
doubleclickcoords = (MSLLHOOKSTRUCT)Marshal.PtrToStructure(lParam, typeof(MSLLHOOKSTRUCT));
after that i get coordinates of form, let it be "mspaint", and find local coordinates of point, and via postmessage trying to send "click", but nothing happens. what am i missing??:
public static void SendDoubleMouseClick()
{
IntPt开发者_JS百科r iHandle = GetWnd("mspaint");
Rectangle myRect = new Rectangle();
RECT rct;
if (!GetWindowRect(iHandle, out rct))
{
MessageBox.Show("ERROR");
}
else
{
myRect.X = rct.Left;
myRect.Y = rct.Top;
myRect.Width = rct.Right - rct.Left;
myRect.Height = rct.Bottom - rct.Top;
}
PostMessage(iHandle, 0x0201, 0, new IntPtr((doubleclickcoords.pt.y - myRect.Y) * 0x10000 + (doubleclickcoords.pt.x - myRect.X)));
PostMessage(iHandle, 0x0202, 0, new IntPtr((doubleclickcoords.pt.y - myRect.Y) * 0x10000 + (doubleclickcoords.pt.x - myRect.X)));
}
EDIT: language is c#, winapi via p/invoke
If you want to automate another application, I suggest you take a look to the Automation API wich is meant for this specific purpose, instead of trying to hack Windows with post message. Most of the time, PostMessage through different processes won't work for security reasons. This is more an more true with Windows Vista, 7 and the UAC (User Accounts Settings).
This is a good article here http://msdn.microsoft.com/en-us/magazine/cc163465.aspx.
If it's just for a small utility and not part of a bigger application, you could consider using AutoIt - a scripting language specifically designed to control other processes. it is also able to generate stand-alone executables.
精彩评论