How to set output console width in Visual Studio

∥☆過路亽.° 提交于 2020-05-11 04:12:43

问题


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.


回答1:


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.




回答2:


  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.




回答3:


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


来源:https://stackoverflow.com/questions/21238806/how-to-set-output-console-width-in-visual-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!