16

Whenever I build and run my C++ code from Visual Studio 2013, the console window width is un-adjustable and because of this, causes my output to be pushed onto the next line sooner than I'd like.

How can I get Visual Studio to make the console window width larger?

If I need to insert code in my application to do this, is there a way I can put a compile-time check so that it removes the code when not compiling on Windows? I'm trying to make the code as portable as possible.

2
  • 2
    Can't you right click on the title bar of the window and use the edit menu to set the size? It should keep that even after a rebuild of your application.
    – drescherjm
    Commented Jan 20, 2014 at 16:14
  • 1
    Ah. That worked! For portability reasons, I think this is the best solution. Thanks! :)
    – Michael
    Commented Jan 20, 2014 at 17:14

3 Answers 3

25

One solution that I use frequently with console applications I debug from Visual Studio that does not require code changes is to do the following:

  1. Right Click on title bar of your running console application
  2. Select Properties
  3. Select Layout
  4. Then set the window size.

After you close the dialog box, Windows should save the settings or prompt you to save depending on your version of Windows. I believe Windows 8 or newer does not prompt, while Windows 7 or lower prompts.

14
  1. Use Console::SetWindowSize() method (under .NET framework).

    You can refer to here for its documentation and code examples.

  2. Or you can use MoveWindow() method (you can also move the window):

    #include <windows.h>
    using namespace std;
    int main (void)
    {
        HWND console = GetConsoleWindow();
        RECT r;
        GetWindowRect(console, &r); //stores the console's current dimensions
    
        MoveWindow(console, r.left, r.top, 800, 100, TRUE); // 800 width, 100 height
    
        // ...
    }
    

    Check out here for more information.


If you really want to make your code as portable as possible, maybe you should manually set it by running a cmd prompt. Click on the icon at the top. Select defaults. Enter the settings you want.

4
  • What #include do I need for that to work? And how can I made it NOT get compiled in when building the code on say GCC? (like a via a macro or something?)
    – Michael
    Commented Jan 20, 2014 at 16:32
  • @Michael Or simply, you can follow drescherjm's suggestion to change it under the edit menu. Commented Jan 20, 2014 at 16:37
  • @Michael See updated. You just need to #include <windows.h> to make it work for the second method. Commented Jan 20, 2014 at 16:41
  • ah you're right. I think for portability reasons I should do what @drescherjm suggested and have it set via the console properties. Thanks! :)
    – Michael
    Commented Jan 20, 2014 at 17:14
-1

You can simply use this:

Console.WindowWidth = Console.LargestWindowWidth - [insert number of pixels from the end of the screen]
Console.WindowHeight = Console.LargestWindowHeight - [insert number of pixels from the end of the screen]

If I wanted to set the console window to be 15 pixels from the edge of the screen, I would do this:

Console.WindowWidth = Console.LargestWindowWidth - 15
3
  • 2
    As a note, LargestWindowHeight and LargestWindowWidth are actually numbers of rows and columns the console window can support, not pixel counts. So subtracting 15 actually subtracts 15 columns of space, not 15 pixels. Just sharing for anyone stumbling across this in the future.
    – Tyler Lee
    Commented Jan 11, 2016 at 16:25
  • This answer applies only to C++CLI and .NET applications.
    – Jim Fell
    Commented Apr 1, 2020 at 20:21
  • @JimFell I've never used C++ CLI and this worked perfectly fine for me. Perhaps the spec has been updated since I posted this answer to remove the functionality?
    – AStopher
    Commented Apr 2, 2020 at 7:57

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.