I make a program that get the console handle and then move it on the screen like animation, make it "fly" on the screen, move it automatically.
My program takes the resolution of the screen, takes the console handle, and then move it with the MoveWindow
function.
My problem is that the window doesn't move at all, and I don't get any errors.
My Code:
#include <iostream>
#include <Windows.h>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
POINT current_position;
while (true) {
int offset = rand() % 2;
int x_direction = rand() % 2 == 1 ? 1 : -1;
int y_direction = rand() % 2 == 1 ? 1 : -1;
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
HWND hwndConsole = GetWindow(GetConsoleWindow(), (UINT)¤t_position);
MoveWindow(hwndConsole, current_position.x + (offset * x_direction), current_position.y + (offset * y_direction), width, height, TRUE);
Sleep(10);
}
return 0;
}
EDIT:
With the help of the comments you wrote to me, I changed the code.
But now the size of the console is changing, and the console is not moving at all.
#include <iostream>
#include <Windows.h>
#include <ctime>
using namespace std;
int main()
{
srand(time(nullptr));
POINT current_position{};
while (true) {
HWND hwndConsole = GetConsoleWindow();
int offset = rand() % 2;
int x_direction = rand() % 2 == 1 ? 1 : -1;
int y_direction = rand() % 2 == 1 ? 1 : -1;
int width = GetSystemMetrics(SM_CXSCREEN);
int height = GetSystemMetrics(SM_CYSCREEN);
BOOL hwndMove = MoveWindow(hwndConsole, current_position.x + (offset * x_direction), current_position.y + (offset * y_direction), width, height, TRUE);
if (hwndMove == FALSE) {
cout << "Failed! & Error Code: " << GetLastError();
}
Sleep(10);
}
return 0;
}
Second Edit:
The width
and the height
are the size of the console, so I removed it, and changed the parameters to 100, 100.