Moving (centering) the console window

Hi everyone. So I've been working on a small personal project, and I'm currently trying to have the console window open up where I want (in this case, centered on the user's screen). I've looked online for answers on how to this and I've tried a few things but to no success.

The thing everything I found seemed to be different from one another, so I'm kind of confused now. If I get it correctly, I must start by getting the "handle" and also finding the SystemMetrics, but I get lost on how to actually use the corresponding functions. Could anyone point me in the right direction please ?

This is what I have so far, but it possibly is all wrong:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
/*******************************************- POSITIONNING THE CONSOLE WINDOW -****************************************************/

//Finding the user's screen resolution
int Width = GetSystemMetrics(SM_CXSCREEN);
int Height = GetSystemMetrics(SM_CYSCREEN);

//Assigning variables for MoveWindows parameters
int NewWidth = (Width - 80) / 2;	//--- Used as a parameter to center the console window horizontally (MoveWindows int x)
int NewHeight = (Height - 80) / 2;	//--- Used as a parameter to center the console window vertically (MoveWindows int y)
int WindowWidth = 80;	//--- Used as a parameter to specify the width of the console window (MoveWindows int nWidth)
int WindowHeight = 25;	//--- Used as a parameter to specify the height of the console window (MoveWindows int nHeight)

HWND hWnd = GetConsoleWindow();
MoveWindow(hWnd, 1200, NewHeight, WindowWidth, WindowHeight, TRUE);
Last edited on
Have you tried that code? What happens? Are there errors? What are they? Have you tried looking up the functions that error online to see if you are calling them properly?
For some unknown reason it ran fine the first time, but did not actually change the position of the console window.

Now I get errors:

error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
error C2365: 'MoveWindow' : redefinition; previous definition was 'function
see declaration of 'MoveWindow'
error C2078: too many initializers

These are all for line 14. It refers me to this in WinUser,h:
1
2
3
4
5
6
7
8
9
10
WINUSERAPI
BOOL
WINAPI
MoveWindow(
    _In_ HWND hWnd,
    _In_ int X,
    _In_ int Y,
    _In_ int nWidth,
    _In_ int nHeight,
    _In_ BOOL bRepaint);


I'm fairly new to using different functions. Am I not doing it the right way ? Should I type it exactly as shown above ?

Do you get that error with exactly the code you have posted above?
Pretty much yes. The only other tiny of code that comes after is the main function which is currently empty. I put a temporarily _getch() just so it doesn't close automatically. No errors related to that part whatsoever.
I just figured it out. I had to declare the MoveWindow function first, then call it in my main function using my own parameters. Here's the code:

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
/***********************- POSITIONNING THE CONSOLE WINDOW -*******************************/

//Finding the user's screen resolution
int Width = GetSystemMetrics(SM_CXSCREEN);
int Height = GetSystemMetrics(SM_CYSCREEN);

//Assigning variables for MoveWindows parameters
int WindowWidth = 680;		//--- Used as a parameter to specify the width of the console window (MoveWindows int nWidth)
int WindowHeight = 350;		//--- Used as a parameter to specify the height of the console window (MoveWindows int nHeight)
int NewWidth = (Width - WindowWidth) / 2;		//--- Used as a parameter to center the console window horizontally (MoveWindows int x)
int NewHeight = ((Height - WindowHeight) / 2) - 150;		//--- Used as a parameter to center the console window vertically (MoveWindows int y)

//Getting the console window handle
HWND hWnd = GetConsoleWindow();

//Declaring the function
BOOL WINAPI MoveWindow(_In_ HWND hWnd, _In_ int NewWidth, _In_ int NewHeight, _In_ int WindowWidth, _In_ int WindowHeight, _In_ BOOL bRepaint);



/***************************- MAIN FUNCTION -***************************************/
int main(void)
{
	//Using function to center the console window
	MoveWindow(hWnd, NewWidth, NewHeight, WindowWidth, WindowHeight, TRUE);


	_getch();

	return 0;
}


EDIT: I added - 150 on line 11 because I determined I liked the window to be a little higher than centered vertically speaking.
Last edited on
Hi, it's me again. Today I was messing around with the previously shown code that basically lets me choose the size and the positioning of the window when running the program.

I tried making it bigger, so I changed the following lines:
int WindowWidth = 680; int Windowheight = 350;
(Lines 8 and 9 in the original code)

To this:
int WindowWidth = 800; int WindowHeight = 500;

What happened kind of surprised me. The width of the console window didn't change (it remained at 680, I tested it by outputting a long string), but the height didn't change to 500. I am now wondering: Is there size limitations ? Is it because of the functions I am using ? If so, is there a way to get around it or other functions to use to achieve my goal ?

As I said earlier, I haven't changed my code at all except for those two lines. I am not getting any error messages and the program compiles just fine. Any help would be greatly appreciated. Thank you !
Last edited on
Sorry I'm late to post here, but here's my answer:

Don't do that.


You are wasting your time. See, the console window doesn't belong to your program. It belongs to the user -- the human sitting in front of the PC. He wants his console window to appear where he wants it. That's why the console window gives him options to set the geometry for every console window that appears.


If your program requires the use of a text window that has specific size and position requirements, consider using a window with a windows Edit control or a Rich Edit control, and manipulating that.


To answer your other questions -- yes, there are limitations, but they are constrained by the text buffer, which you must modify to allow for bigger windows.


Sorry this isn't the kind of answer you are looking for, but it is the correct way to handle the UI.
I understand what you mean, thank you for the reply. In this particular case I'm basically just making a tiny program that will output sentences and maybe a drawing made of ASCII characters. The program is destined to one person only, and is basically just a joke.

Are you saying that whatever changes I make to the console window will not apply when that person runs it on their computer ? If that's not the case, do you still consider it bad practice, even if the program is meant to be opened by only one friend and has no particular use other than having a quick laugh ?

Topic archived. No new replies allowed.