I need to simulate a mouse click, but I need it to simulate a click that will effect the open window (In this case the browser). Is there an easy way to do this or is it not possible? Any help is appreciated.

Recommended Answers

All 2 Replies

Do you just need the browser window to "take focus" or does something else need to happen.
From what kind of app are you starting (another browser, console app, win forms, etc.)?

You could use the SendInputs() function in the WinAPI. This function does not target particular windows however. First thing you'll need is structure definitions:

struct INPUT
    {
        public uint type;
        public MOUSEINPUT mi;
    }

    struct MOUSEINPUT
    {
        public int dx;
        public int dy;
        public uint mouseData;
        public uint dwFlags;
        public uint time;
        public IntPtr dwExtraInfo;
    }

In whatever class you'd be using it, declare the external function:

[DllImport("user32.dll")]
private static extern uint SendInput(uint nInputs, INPUT[] pInputs, int cbSize);

I did a quick test to move the cursor relative to its current position:

SendInput(0, null, Marshal.SizeOf(typeof(INPUT)));

            INPUT input = new INPUT();
            input.type = 0x0000;

            MOUSEINPUT mouseInput = new MOUSEINPUT();
            mouseInput.dx = 300;
            mouseInput.dy = 150;
            mouseInput.mouseData = 0;
            mouseInput.dwFlags = 0x0001;
            mouseInput.time = 0;
            mouseInput.dwExtraInfo = IntPtr.Zero;

            input.mi = mouseInput;

            INPUT[] inputArray = new INPUT[] { input };

            SendInput((uint)inputArray.Length, inputArray, Marshal.SizeOf(typeof(INPUT)));

Take a look at these references to modify it:
SendInput() Function
INPUT Structure
MOUSEINPUT Structure

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.