-1

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.

I'd like to share the program with my friends. But I'm stumbling on a litlle problem.
As resolutions change, the buttons and textfields change position.

That's why I'd like to allow my friends to write the mouseposition is a XML-file, that I load in my program.

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;
        }
}

And this is my 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>

The Schema looks like this:

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="CursorPositions">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="Textbox-X" type="xs:unsignedShort" />
        <xs:element name="Textbox-Y" type="xs:unsignedShort" />
        <xs:element name="SliderBegin-X" type="xs:unsignedShort" />
        <xs:element name="SliderBegin-Y" type="xs:unsignedShort" />
        <xs:element name="SubmitButton-X" type="xs:unsignedShort" />
        <xs:element name="SubmitButton-Y" type="xs:unsignedShort" />
      </xs:sequence>
    </xs:complexType>
  </xs:element>
</xs:schema>

Sadly enough, on testing, this always returns {0,0}.

Does anyone know what's wrong?
Or maybe you have a solution?

PS: for those who want to know, moving the mouse works like this:

private void MoveMouse(int X, int Y) {
    Cursor.Position = new Point(X, Y);
    mouse_event(MOUSEEVENTF_LEFTDOWN, X, Y, 0, 0); // press left mouse button
    mouse_event(MOUSEEVENTF_LEFTUP, X, Y, 0, 0); // release left mouse button
}

You do need to include this part in the top of your code:

[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;
BlueCacti
  • 9,729
  • 3
  • 20
  • 24
  • 1
    What exactly are you trying to build? There should be some other "better" way of doing this. – Shoban Apr 03 '12 at 15:21
  • @Shoban I'm trying to make a little bot. A friend of mine did it with AutoIT, which works pretty fine. But I want to do it in C#, as a project for myself – BlueCacti Apr 03 '12 at 15:29
  • My opinion: This is not the best way of building a bot i.e making it move mouse cursor and click stuff. How about diff resolutions? Howabout restored window? Have you check all these? How about a unexpected alert/window by another applications? – Shoban Apr 03 '12 at 15:43
  • @Shoban I know it's definitly not perfect. It's more like a project for myself. I want to fix the resolutionproblem by letting the user customize the xml-file. I have indeed encountered problems with windows popping up, but that's the users problem. For the moment I just want to finish this project so I can move on, because I hate having to leave a project behind unfinished :s – BlueCacti Apr 03 '12 at 15:48
  • ok!User's problem is our problem :) – Shoban Apr 03 '12 at 15:56
  • @Shoban I know :p But I'll make it better before I'll publish it. But ATM I just want to get it working – BlueCacti Apr 03 '12 at 17:24

2 Answers2

0

I fixed the problem by using a textfile.
It's not the solution I wanted, but it works ...

private void Initialize() {
    if (CheckPositions()) {
        ReadPositions();
        MessageBox.Show("Loaded positions", "Go on!", MessageBoxButtons.OK, MessageBoxIcon.Information);
    } else
        MessageBox.Show("Something went wrong trying to read positions.txt\r\nDelete the file and try again.", "Woops", MessageBoxButtons.OK, MessageBoxIcon.Error);
}

private bool CheckPositions() {
    if (File.Exists(Application.StartupPath + @"\positions.txt") && File.ReadAllLines(Application.StartupPath + @"\positions.txt").Length == 6)
        return true;
    else
        if (EnterPositions())
            return CheckPositions();
        else
            return false;
}

private bool EnterPositions() {
    bool finished = false;

    MessageBox.Show("To make sure the mouse clicks on the right places when the program runs, you need to run through these 3 steps." +
    "\r\nGo to the site and click OK", "First steps", MessageBoxButtons.OK, MessageBoxIcon.Information);

    TextWriter tw = new StreamWriter(Application.StartupPath + @"\positions.txt",false);

    MessageBox.Show("Please place your mouse on the textfield" + "\r\n and press Enter", "Textbox");
    tw.WriteLine(string.Format("{0,-4:0000} # Textbox X", Cursor.Position.X));
    tw.WriteLine(string.Format("{0,-4:0000} # Textbox Y", Cursor.Position.Y));

    MessageBox.Show("Please place your mouse on the 0 (zero) position of the Slider" + "\r\n and press Enter", "Slider");
    tw.WriteLine(string.Format("{0,-4:0000} # Slider X", Cursor.Position.X));
    tw.WriteLine(string.Format("{0,-4:0000} # Slider Y", Cursor.Position.Y));

    MessageBox.Show("Please place your mouse on the submit button" + "\r\n and press Enter", "Submit");
    tw.WriteLine(string.Format("{0,-4:0000} # Submit X", Cursor.Position.X));
    tw.WriteLine(string.Format("{0,-4:0000} # Submit Y", Cursor.Position.Y));

    tw.Close();
    finished = true;

    return finished;
}

And to read the positions:

private void ReadPositions() {
    TextReader tr = new StreamReader(Application.StartupPath + @"\positions.txt");

    string line;
    while ((line = tr.ReadLine()) != null) {
        if(line.Contains("Textbox X"))
            textX = int.Parse(line.Substring(0,4));
        else if (line.Contains("Textbox Y"))
            textY = int.Parse(line.Substring(0, 4));
        else if (line.Contains("Slider X"))
            sliderX = int.Parse(line.Substring(0, 4));
        else if (line.Contains("Slider Y"))
            sliderY = int.Parse(line.Substring(0, 4));
        else if (line.Contains("Submit X"))
            submitX = int.Parse(line.Substring(0, 4));
        else if (line.Contains("Submit Y"))
            submitY = int.Parse(line.Substring(0, 4));
    }

    tr.Close();
}
BlueCacti
  • 9,729
  • 3
  • 20
  • 24
0

If you want to "Click" or send other UI events, you will probably want to use the SendInput() API:

http://msdn.microsoft.com/en-us/library/ms646310%28VS.85%29.aspx

Also see:

Moving mouse cursor programmatically

Community
  • 1
  • 1
Matthew Watson
  • 104,400
  • 10
  • 158
  • 276
  • 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 – BlueCacti Apr 03 '12 at 15:35
  • Moving the mouse seems to work now. Seems like the problem lays at loading the positions from the xml file – BlueCacti Apr 03 '12 at 19:11