double spin(int);
void RadiusChange();
bool Control = false;
int radius;;
int APIENTRY WinMain(HINSTANCE hInstance,HINSTANCE hPrevInstance,LPSTR lpCmdLine,int showcmd)
{
char Buff[1];
ifstream f;
f.open("Settings.txt");
f.getline(Buff,3);
f.close();
radius = atoi(Buff);
Sleep(2000);
POINT pt;
GetCursorPos(&pt);
int degrees = NULL;
while (1)
{
Sleep(20);
int x = int(pt.x + radius*cos(spin(degrees)));
int y = int(pt.y + radius*sin(spin(degrees)));
SetCursorPos(x, y);
degrees = (degrees + 1) % 360;
RadiusChange();
}
double spin(int degrees)
{
constdouble pi = 3.1415926;
return degrees * pi / 180.0 + 0.5;
}
void RadiusChange()
{
if(Control == false)
{
radius--;
if(radius < 2)
Control = true;
}
else
{
radius++;
if(radius > 255)
Control = false;
}
}
It's an endless loop that moves the mouse in a circular pattern...problem is since its an endless loop i can't put this code with a GUI without getting it stuck...it won't process incoming messages or anything...i have to use TaskMgr to shut it down...any idea how to solve it?
In order to have a GUI, you need to have a messge pump. A message pump is a loop where you process Windows notifications and messages. If I remember correctly, you use GetMessage() to retrieve a message from the queue, then use TranslateMessage() to translate any accelerators, and then use use DispatchMessage() to have the message sent to the Window Procedure of the corresponding window.
Now, note that GetMessage() is a blocking call. This means that it will block execution of the thread if there are no incoming messages or notifications. So, what you need is:
A. Change the loop and use PeekMessage(), which is non-blocking. If there are no messages, then you take the opportunity to programatically move the mouse. If there are messages, then you translate and dispatch them.
B. Make your application spawn a worker thread that moves the mouse in the circular fashion.