Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set output console width in Visual Studio

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.

like image 342
Michael Avatar asked Jan 20 '14 16:01

Michael


People also ask

How do I change the width in Visual Studio?

Select the form, then find the Properties pane in Visual Studio. Scroll down to size and expand it. You can set the Width and Height manually.

How do I change the width of a console in C#?

SetWindowSize() Method in C# Console. SetWindowSize(Int32, Int32) Method is used to change the height and width of the console window to the specified values. Syntax: public static void SetWindowSize (int width, int height); Parameters: width: The width of the console window measured in columns.

How do I redirect console output to a file in Visual Studio?

You can use > to redirect the output of the command run by Visual Studio. Add it to the command arguments by selecting your project in the solution explorer and clicking PROJECT->Properties->Configuration Properties->Debugging. Then enter > output. txt into the Command Arguments.


2 Answers

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.

like image 106
drescherjm Avatar answered Sep 22 '22 15:09

drescherjm


  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.

like image 38
herohuyongtao Avatar answered Sep 18 '22 15:09

herohuyongtao