0

I created a windows form and added two buttons and a browser in the form. I used a program to get the mouse coordinates and click a login button. I would like to implement this without moving the cursor. I am not looking to find the login button ID or use HTML request/response.

Any idea if it is possible to click non html button in a browser without moving the cursor.

This code below works but only for other controls within the form but not for the browser that is part of the form

using System;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;

namespace simulateclicktest
{
    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 = new Point(157
                ,455);
            // 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("clicked");
        }

    }
}
user3726459
  • 172
  • 1
  • 14

1 Answers1

1

Try find button ID and use this code:

private void button1_Click(object sender, EventArgs e)
{
    webBrowser1.Document.GetElementById("button_ID").InvokeMember("click");
}

If you have webBrowser control on the form and know ID of button, DllImport is no needed.

c4pricorn
  • 3,471
  • 1
  • 11
  • 12
  • as I said, I strictly want to click the button with a mouse without moving the cursor. – user3726459 Nov 11 '15 at 22:14
  • Right. This code simulate button click in webbrowser. You can use in any function (not only button click event). – c4pricorn Nov 11 '15 at 22:17
  • I already know how to do that, it is not exactly what I am looking for. I am looking for code to send mouse click messages to the application. I got my code to work. SendMessage(hWnd, 0x0201, 0, myparam); instead of SendMessage(handle, BM_CLICK, IntPtr.Zero, IntPtr.Zero); the login button is not HTML – user3726459 Nov 11 '15 at 22:39
  • @user3726459 You should work on your question and make some edits, then: you say in the question that you want to "click html button in a browser". – 31eee384 Nov 11 '15 at 22:45