How to move the mouse

Posted by GroundZero on Stack Overflow See other posts from Stack Overflow or by GroundZero
Published on 2012-04-03T15:16:36Z Indexed on 2012/04/03 17:29 UTC
Read the original article Hit count: 204

I'm making a little bot in C#.
At the moment it works pretty well, it can load text from a file and type it for you.

But for now, I need to manualy click the textfield to put the focus on it, remaximize my form and then click the Type-button. After the typing, I need to manualy slide the scorebar and press submit.

I'd like to know how I can move my mouse with C# and if possible, if possible I'd like to load the mouse positions from a xml-file.

I need to move to the textfield, click in it to focus on it, start the type script, move to the slider, hold the mouse down on it while dragging, releasing it on the correct position & clicking on the submitbutton

This is what I have for now:

To load in the variables, I'm using this script:

private void Initialize() {
    XmlTextReader reader = new XmlTextReader(Application.StartupPath + @"..\..\..\CursorPositions.xml");

    while (reader.Read()) {
        switch (reader.NodeType) {
            case XmlNodeType.Element: // The node is an element.
                element = reader.Value;
                break;
            case XmlNodeType.Text: //Display the text in each element.
                switch (element) {
                    case "Textbox-X":
                        textX = int.Parse(reader.Value);
                        break;
                    case "Textbox-Y":
                        textY = int.Parse(reader.Value);
                        break;
                    case "SliderBegin-X":
                        sliderX = int.Parse(reader.Value);
                        break;
                    case "SliderBegin-Y":
                        sliderY = int.Parse(reader.Value);
                        break;
                    case "SubmitButton-X":
                        submitX = int.Parse(reader.Value);
                        break;
                    case "SubmitButton-Y":
                        submitY = int.Parse(reader.Value);
                        break;
                }
                break;
        }
}

This is the xml-file:

<?xml version="1.0" encoding="utf-8" ?>
<CursorPositions>
  <Textbox-X>430</Textbox-X>
  <Textbox-Y>270</Textbox-Y>

  <SliderBegin-X>430</SliderBegin-X>
  <SliderBegin-Y>470</SliderBegin-Y>

  <SubmitButton-X>860</SubmitButton-X>
  <SubmitButton-Y>365</SubmitButton-Y>
</CursorPositions>

To move the mouse I'm using this piece of code:

public partial class FrmMain : Form {

[System.Runtime.InteropServices.DllImport("user32.dll")]
public static extern void mouse_event(int dwFlags, int dx, int dy, int cButtons, int dwExtraInfo);

public const int MOUSEEVENTF_LEFTDOWN = 0x0002;
public const int MOUSEEVENTF_LEFTUP = 0x0004;
public const int MOUSEEVENTF_RIGHTDOWN = 0x0008;
public const int MOUSEEVENTF_RIGHTUP = 0x0010;

...

    private void btnStart_Click(object sender, EventArgs e) { // start button (de)activates loop
            if (!running) {
                    btnStart.Text = "Stop";
                btnStart.Cursor = Cursors.No;
            running = true;
        } else {
            btnStart.Text = "Start";
            btnStart.Cursor = Cursors.AppStarting;
            running = false;
        }
        while (running) {
            // move to textbox & type
            Cursor.Position = new Point(textX, textY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, textX, textY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, textX, textY, 0, 0);
            Type();

            // wait 90 seconds till slider available
            Thread.Sleep(90 * 1000);

            // move to slider & slide according to score
            Cursor.Position = new Point(sliderX, sliderY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, sliderX, sliderY, 0, 0);
            Cursor.Position = new Point(sliderX + 345 / 10 * score, sliderY);
            mouse_event(MOUSEEVENTF_LEFTUP, sliderX + 345 / 10 * score, sliderY, 0, 0);
            // submit
            Cursor.Position = new Point(submitX, submitY);
            mouse_event(MOUSEEVENTF_LEFTDOWN, submitX, submitY, 0, 0);
            mouse_event(MOUSEEVENTF_LEFTUP, submitX, submitY, 0, 0);

            // wait 10 sec to be sure it's submitted
            Thread.Sleep(10 * 1000);

            // refresh page
            SendKeys.SendWait("{F5}");

            // get new text
            NewText();

            // wait 10 sec to refresh and load song
            Thread.Sleep(10 * 1000);
        }
    }
}

PS: I get the coordinates via my form. I've got 2 labels that show my X & Y coordinates. To capture them outside the form, I press and hold my Left Mouse Button and 'drag' it outside the form to the correct place. This way I get the coordinates of my mouse outside the form

© Stack Overflow or respective owner

Related posts about visual-studio-2010

Related posts about mouse