Performing a mouse click without moving the cursor

I couldn't find any solution other than moving the cursor to the class Cursor

by clicking mouse_event

and then moving the cursor to its previous position. I am playing with the function right now SendInput

, but I have no chance of a good solution. Any advice?

+2


source to share


2 answers


You should use the Win32 API. Use pInvoked SendMessage from user32.dll

pInvoked function

Then read about mouse events: Mouse input to msdn



And then read about: System Events and Mouse Mess .......

There is also a lot of information: Information

+6


source


Here's an example following Hooch's approach.

I have created a form that contains 2 buttons. When you click on the first button, the position of the second button is resolved (screen coordinates). Then the handle for that button is removed. Finally, the SendMessage (...) (PInvoke) function is used to send a click event without moving the mouse.



public partial class Form1 : Form
{
    [DllImport("user32.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, 
        IntPtr wParam, IntPtr lParam);

    [DllImport("user32.dll", EntryPoint = "WindowFromPoint", 
        CharSet = CharSet.Auto, ExactSpelling = true)]
    public static extern IntPtr WindowFromPoint(Point point);

    private const int BM_CLICK = 0x00F5;

    public Form1()
    {
        InitializeComponent();
    }

    private void button1_Click(object sender, EventArgs e)
    {
        // Specify the point you want to click
        var screenPoint = this.PointToScreen(new Point(button2.Left, 
            button2.Top));
        // Get a handle
        var handle = WindowFromPoint(screenPoint);
        // Send the click message
        if (handle != IntPtr.Zero)
        {
            SendMessage( handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero);
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        MessageBox.Show("Hi", "There");
    }
}

      

+3


source







All Articles