0

I am currently using the MoveWindow() function in the header Windows.h, and using this function, I can move and resize the output console window any way I want.

MoveWindow(GetConsoleWindow(), x, y, width, height, TRUE);
// x and y is the position of the topleft corner of window

However, I cannot figure out how to center the screen without hard-coding the position of the window. Is there a way to set the position of the window to change depending on the width and height that I set? Thanks!

P.S. I am pretty new to C++

4
  • Calculate it. You will have functions to get the screen size and your console window size. It all involves some addition and subtractions. Commented Jul 15, 2018 at 3:37
  • @jeffrycopps I tried to calculate and came up with the equation: x = (1280 - width) / 2 and y = (1080 - height) / 2 (my screen resolution is 1280x1080). It worked for y but it did not work for x. Commented Jul 15, 2018 at 4:59
  • What did it do for x? And you should use functions to get the width and height programmatically. A lot more people have 1920x1080 than 1280x1080. Commented Jul 16, 2018 at 1:42
  • I checked my monitor and I am sure that it is 1280x1080. For x, the console window was too far to the left to be centered. Commented Jul 16, 2018 at 4:58

1 Answer 1

4

Get the WindowRect ( not to confuse with the ClientWindow)of your screen and find middle position, but ClientRect will remain unchanged since we are not resizing.Try this snippet:

Edited: For proper centering and to allow user to specify the position

void MoveWindow(int posx, int posy) 
{
    RECT rectClient, rectWindow;
    HWND hWnd = GetConsoleWindow();
    GetClientRect(hWnd, &rectClient);
    GetWindowRect(hWnd, &rectWindow);
    MoveWindow(hWnd, posx, posy, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, TRUE);
}

void MoveCenter()
{
    RECT rectClient, rectWindow;
    HWND hWnd = GetConsoleWindow();
    GetClientRect(hWnd, &rectClient);
    GetWindowRect(hWnd, &rectWindow);
    int posx, posy;
    posx = GetSystemMetrics(SM_CXSCREEN) / 2 - (rectWindow.right - rectWindow.left) / 2,
    posy = GetSystemMetrics(SM_CYSCREEN) / 2 - (rectWindow.bottom - rectWindow.top) / 2,

    MoveWindow(hWnd, posx, posy, rectClient.right - rectClient.left, rectClient.bottom - rectClient.top, TRUE);
}

int main(int argc, char *argv[])
{
    MoveWindow(10, 10);
    return 0;
}
Sign up to request clarification or add additional context in comments.

3 Comments

I tried your code, but it doesn't quite center the window. The window is positioned too high and to the right in my screen. I also want the window to center itself based on the width and height I enter in.
Can you explain what the code above does? What does the MoveCenter() code really do? To me, it seems like it is just allowing me to set the position of the window, which can already be done with the MoveWindow() function. I think I am missing something here. Sorry for being so nooby at this. I am pretty new to the language :)
the code works but you need to say that the windows.h header is necessary, In addition, the names of the methods must start with lowercase letters to work correctly, otherwise the program has a conflict over the names.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.