1

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)&current_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.

nakE
  • 362
  • 1
  • 13
  • 1
    Does this answer your question? [Can't center my console window by using the following code](https://stackoverflow.com/questions/42905649/cant-center-my-console-window-by-using-the-following-code) – Dialecticus Dec 10 '19 at 19:21
  • @Dialecticus I think no, the user wanted to center his console, to move it to a different position. In my question I want to make it move like animated , to make it move automatically. – nakE Dec 10 '19 at 19:26
  • 1
    What is the return value from `MoveWindow`? If it's an error, then what's the return value from `GetLastError`? – Alejandro Dec 10 '19 at 19:31
  • Error checking is not optional for winapi functions. That GetWindow() call makes no sense, delete it to get ahead. – Hans Passant Dec 10 '19 at 19:33
  • @Alejandro Hey, Error Code is: 1400 , Invalid Window Handle. – nakE Dec 10 '19 at 19:33
  • @nakE Then that's your problem. The `HWND` you're passing to the function is wrong. Check out how you're getting it. – Alejandro Dec 10 '19 at 19:37
  • https://learn.microsoft.com/en-us/windows/console/getconsolewindow versus https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getwindow and https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-movewindow – Richard Chambers Dec 10 '19 at 19:38
  • @HansPassant I only used it to solve the problem that I get from my current_position, `uninitialized local variable 'current_position' used` I don't know how can I solve it. I can't associate it a value. – nakE Dec 10 '19 at 19:41
  • @HansPassant Ok, I solve it by adding {} to my `current_position`, But the size of the console is increasing, and the console is not moving. – nakE Dec 10 '19 at 19:43
  • Sure, the MoveWindow call now tries to make it as large as the screen. And you never change the current_position so it stays (0, 0). Take a break to think over what you want to achieve, this isn't it. – Hans Passant Dec 10 '19 at 20:32

1 Answers1

1

Here's a really simplified version of what you're trying to do that slides the window down and to the right. I took out the randomness because it was difficult to tell intent from the example. I've also added a limitation to how long the window moves before program termination.

Use this as a starting point for what you want to accomplish.

#include <Windows.h>

int main()
{
    HWND hwndConsole = GetConsoleWindow();

    for (int i = 0; i < 200; i++)
    {
        RECT position;
        GetWindowRect(hwndConsole, &position);
        MoveWindow(hwndConsole, position.left + 3, position.top + 3, 300, 300, TRUE);
        Sleep(10);
    }

    return 0;
}