Hey,guys
I want to print a word in the center of a CMD's window

If I can get the size of it,I could calculate the position where I put the word.

Anybody who can help me?

Much Thanks

Dani AI

Generated

Short answer: for centering text in a Windows console, measure the window in character cells and position the cursor there. That is simpler and more reliable for console output than getting pixel dimensions. As noted, GetConsoleWindow + GetWindowRect returns pixels (useful for GUI-style placement), but the usual console APIs return character coordinates directly — use GetConsoleScreenBufferInfo and SetConsoleCursorPosition. ’s point about target macros applies only if calling newer window-only functions; the screen-buffer approach below works across normal console targets.

#include <windows.h>
#include <iostream>
#include <string>

int main() {
    HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
    CONSOLE_SCREEN_BUFFER_INFO csbi;
    if (!GetConsoleScreenBufferInfo(hOut, &csbi)) return 1;

    int cols = csbi.srWindow.Right - csbi.srWindow.Left + 1;
    int rows = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
    std::string word = "Centered";
    int x = csbi.srWindow.Left + (cols - (int)word.length()) / 2;
    int y = csbi.srWindow.Top + rows / 2;

    COORD coord; coord.X = (SHORT)x; coord.Y = (SHORT)y;
    SetConsoleCursorPosition(hOut, coord);
    std::cout << word << std::flush;
    return 0;
}

Notes and troubleshooting:

  • srWindow is in character coordinates; add srWindow.Left/Top because the buffer origin may not be zero. SetConsoleCursorPosition expects buffer coordinates.
  • GetConsoleScreenBufferInfo fails if output is redirected to a file or pipe; check return codes and handle errors.
  • For Unicode or wide/double-width glyphs use wide APIs and measure display widths (strlen != display columns for multibyte characters).
  • If truly needing pixel centering (rare for console text), follow ’s pixel approach and map font cell size (GetCurrentConsoleFontEx/GetConsoleFontSize) to pixels — more complex but possible.

Per : posting the exact code/compile errors helps diagnose issues; the snippet above is a minimal, working example to test.

Recommended Answers

All 7 Replies

Assuming this is a console program on MS-Windows operating system.
1. call GetComnsoleWindow() to get the HWND of the window

2. call GetWindowRect() to get the RECT structure that contains the window's size.

Assuming this is a console program on MS-Windows operating system.
1. call GetComnsoleWindow() to get the HWND of the window

2. call GetWindowRect() to get the RECT structure that contains the window's size.

Hey,thank you.
But I am not very familiar with Windows API

Could you show me how to implement it ?
Perhaps I need detailed code

Well, its only the two functions I posted. I gave you the links, just read them to see how they are called. Yes I could easily write the code for you, but you will learn a lot more if you read and study those links yourself. Just include windows.h header file.

commented: Yes! +17

Well, its only the two functions I posted. I gave you the links, just read them to see how they are called. Yes I could easily write the code for you, but you will learn a lot more if you read and study those links yourself. Just include windows.h header file.

Well,thanks for your help.
I did have read the MSDN.
But as I said, I know nothing about API.

I tried to define a variable hWnd whose type is HWND

Then I make hWnd equal to the return of GetConsoleWindow()
However , it fails.
I wonder why?

Because, obviously, you did it wrong. Don't you think that showing us what you tried to do would give us some helpful information? This is not the Psychic Programmer's Forum.

This isn't rocket science either. As Walt said you need to post what you did -- the complete program.

>>However , it fails.
What fails? Compiler gives you errors? Or GetConsoleWindow() fails to return a valid HWND pointer?

Also tell us the compiler you are using.

Note that you have to #define _WIN32_WINNT 0x0500 before
including windows.h in order to be able to use GetConsoleWindow.
It says so in the remarks section of the relevant link posted above.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.