Help explain what this does please

Please help me explain why this does what it does. Note I am not using this for anything bad. Learning purposes only. My goal is to find anything I can c++ related and break it down piece by piece to expand my knowledge. Thanks!

-Irhcsa

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#include <Windows.h>

using namespace std;

int main()
{
	FreeConsole(); // Is the main purpose of this to just make the cmd prompt disapear on start?

	SwapMouseButton(true); // I understand this. Pretty basic.

	int i;  // I am pretty familar with integers.
	int X, Y; // So at this point we have 3 integers, i, X, and Y right?
	for (i = 1; i < 1000; i++) // I understand that i++ just adds +1 each time. But what tells it to add the +1 and when?
	{
		X = rand() % 1035; // Here is where I'm completely lost. What does any of this mean?
		Y = rand() % 769;
		SetCursorPos(X, Y);
		Sleep(10); // Why does this go for 10 seconds? I thought 10 seconds was Sleep(10000).
	}

	SwapMouseButton(false); // Fixes your mouse. Pretty basic.

}
FreeConsole: http://msdn.microsoft.com/en-us/library/windows/desktop/ms683150%28v=vs.85%29.aspx

SwapMouseButton: http://msdn.microsoft.com/en-us/library/windows/desktop/ms646264%28v=vs.85%29.aspx

SetCursorPos: http://msdn.microsoft.com/en-us/library/windows/desktop/ms648394%28v=vs.85%29.aspx

As for the rest...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <Windows.h>

using namespace std;

int main()
{
    FreeConsole();  // This tells Windows this process is no longer using the console.  This
                    //  is a rather lame way to remove the console.  A better way
                    //  would be to simply not create a console program to begin with.

    SwapMouseButton(true); // You are correct, this swap left and right mouse buttons

    int i;
    int X, Y; // you are correct, 3 integers, named i, X, Y
    
    // This is a for loop.  A for loop consists of 4 parts:  initialization, condition, iteration, and body.
    //  the initialization is "i = 1".  This is done at the start.  This just assigns 1 to our i variable.
    //  The condition is "i < 1000".  This is checked after initialization, and after each time the loop
    //     runs.  If the condition is false, the loop exits.  If the condition is true, the loop keeps
    //     looping.
    //  The iteration is "i++".  This happens at the end of each loop.  So this will increment i
    //  The body is everything inside the {braces}
    //
    // Effectively, i is just a counter which keeps track of how many times we loop.  The loop is set
    //  to run exactly 999 times (each time, i will contain a different value).
    for (i = 1; i < 1000; i++)
    {
        // rand() produces a random number
        //  % gives you the remainder after a division.  IE:  7 % 3 == 1 because 7/3 has a remainder of 1
        //
        // A property of a remainder is that it will be >= 0 and < divisor.
        // Therefore, when used with rand(), it effectively gives you a random number between [0,X)
        X = rand() % 1035; // <- so this will make X be a random number between [0,1035)
        Y = rand() % 769;  // <- and Y is a random number betwee [0,769)
        SetCursorPos(X, Y);  // <- so we're basically moving the cursor to a random position on the screen
        Sleep(10); // This does not sleep for 10 seconds, it sleeps for 10 milliseconds
                   // however, since the loop is run 999 times, the entire program will run for about 10 seconds
                   //  but every 10ms, the cursor will move to another random place on the screen
    }

    SwapMouseButton(false);

}

Last edited on
Thanks Disch. You really broke that down and made it easy to understand.
One more thing Disch, you said it is lame to use FreeConsole();

How would I create a non console program?

All I've ever done is use it through a console.
It depends on the build tools you're using.

Most IDEs, when you create a new project, give you the option to create a console program or a Windows program.

Windows programs have a different entry point:

1
2
3
4
5
6
// instead of int main(), you'd have this:

int WINAPI WinMain(HINSTANCE inst, HINSTANCE prev, LPSTR cmd, int show)
{
  // no console here
}
If it doesn't have the window's option there might be a GUI option or flag to enable.
Topic archived. No new replies allowed.