I've got this code:
[DllImport("user32.dll")]
static extern void mouse_event(uint dwFlags, uint dx, uint dy, uint dwData, UIntPtr dwExtraInfo);
[Flags]
public enum MouseEventFlags
{
Move = 0x0001,
LeftDown = 0x0002,
LeftUp = 0x0004,
RightDown = 0x0008,
RightUp = 0x0010,
MiddleDown = 0x0020,
MiddleUp = 0x0040,
Absolute = 0x8000
}
public void SimMouseEvent(MouseEventFlags e, int x, int y)
{
mouse_event((uint)e, (uint)x, (uint)y, 0, UIntPtr.Zero);
}
public void SimLeftClick(int x, int y)
{
SimMouseEvent(MouseEventFlags.LeftUp | MouseEventFlags.RightUp, x, y);
}
My form looks like this:
When you click "Button" it runs this:
private void button3_Click(object sender, RoutedEventArgs e)
{
SimLeftClick(50, 50);
}
And on my Window I also have this:
private void Window_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
{
MessageBox.Show("click");
}
When I click the window it says "click" as expected, but when I click "Button" it doesn't seem to do anything.
Are the coordinates absolute, or relative? What about with multiple monitors? Do they only work on the focused application?
I would expect that 50,50 to either hit my window somewhere and trip the "click" handler or click on some random window because it missed my app completely and focus that instead... why isn't anything happening?