I have problems with sending a Click to an application with the user32.dll. the button does not be clicked, but in spy++ the message do appear. i'm using win7 x64
The code is written in c#:
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr FindWindow(string strClassName, string strWindowName);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
public static extern IntPtr FindWindowEx(IntPtr parentHandle, IntPtr childAfter, string lpClassName, string lpWindowTitle);
[DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
private static extern IntPtr SendMessage(IntPtr hwnd, uint Msg, IntPtr wParam, IntPtr lParam);
[DllImport("user32.dll")]
static extern IntPtr GetWindow(IntPtr hWnd, uint wCmd);
//
// Finds a window in the whole tree of childs for a parent window.
//
static IntPtr FindWindowRecursive(IntPtr hParent, string szClass, string szCaption)
{
IntPtr hResult = FindWindowEx(hParent, IntPtr.Zero, szClass, szCaption);
if (hResult != IntPtr.Zero)
return hResult; // found it
// enumerate all childs and if found one that has childs go in
IntPtr hChild = FindWindowEx(hParent, IntPtr.Zero, null, null); // first child
if (hChild != IntPtr.Zero)
{
// let's enumerate
do
{
hResult = FindWindowRecursive(hChild, szClass, szCaption);
if (hResult != IntPtr.Zero)
return hResult; // found it
} while ((hChild = GetWindow(hChild, GW_HWNDNEXT)) != IntPtr.Zero);
}
return IntPtr.Zero; // no childs, so no window was found
}
static void Main(string[] args)
{
IntPtr win = FindWindow("Omnipage17_MainWnd_Class", "Unbenanntes OmniPage-Dokument 1 - OmniPage");
SetForegroundWindow(win);
ShowWindowAsync(win, SW_RESTORE);
IntPtr ButtonHandle = FindWindowRecursive(win, "BarButton", "c");
SetActiveWindow(win);
//sEND Lbuttondown
IntPtr ptr = SendMessage(ButtonHandle, 0x0201, new IntPtr(0x0001), MakeLParam(81,28));
//Thread.Sleep(10);
//Mousemove
ptr = SendMessage(ButtonHandle, 0x0200, new IntPtr(0x0001), MakeLParam(86,24));
//lbuttonup
ptr = SendMessage(ButtonHandle, 0x0202, new IntPtr(0x0001), MakeLParam(81, 28));
//SendMessage(ButtonHandle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
}
Here are the messages of spy++ of that button:
If i send the the messages i get following: i don't if that is that problem but the lbuttondown, buttoup appears 2 times ( S + R ) and if i'm clicking it manuelly it gets 1 message (P) also tried to do it with WM_CLICK but then i have the same problem with it
Edit: Using now PostMessage so spy++ shows the same messages as i click it manually, but still the button seems not to be clicked
With this library I have the same problem.
code:
SetForegroundWindow(win);
Rectangle re;
GetWindowRect(ButtonHandle, out re);
Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);
Message are sent, but button isn't be clicked
Edit:
thanks for that link (http://www.hanselman.com/blog/IntroducingLync2010SuperSimpleAutoAnswerVideoKioskWithFullScreen.aspx), but also with this library i have the same problem :/
code:
SetForegroundWindow(win);
Rectangle re;
GetWindowRect(ButtonHandle, out re);
Cursor.Position = new Point((re.X + re.Width)/2, (re.Y + re.Height)/2);
WindowsInput.InputSimulator.SimulateKeyDown(WindowsInput.VirtualKeyCode.LBUTTON);
WindowsInput.InputSimulator.SimulateKeyUp(WindowsInput.VirtualKeyCode.LBUTTON);
Message are sent, but button isn't be clicked
Edit2:
Answer of user was deleted, because i posted my comment as an answer:
This is not an answer, this belongs in your question. It doesn't match your code, clearly you are still posting BM_CLICK. Which is wrong, it should be sent and you should either send BM_CLICK or post the mouse messages. And you are looking at the wrong window, it is button's parent that gets the BN_CLICK notification and acts on it. Having the processs' keyboard state wrong would be a typical failure mode. – Hans Passant 18 hours ago
Regarding to that, why should it be the parents windows? bcs in spy++ (screenshot below i serached for that button (Class: BarButton) and the handle i get from user32.dll is also the same as that one in spy++