C++ 如何在 Visual Studio 中设置输出控制台宽度
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21238806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to set output console width in Visual Studio
提问by Michael
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.
每当我从 Visual Studio 2013 构建和运行我的 C++ 代码时,控制台窗口宽度是不可调整的,因此,导致我的输出比我想要的更快地推送到下一行。
How can I get Visual Studio to make the console window width larger?
如何让 Visual Studio 使控制台窗口宽度更大?
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.
如果我需要在我的应用程序中插入代码来执行此操作,有没有一种方法可以进行编译时检查,以便在不在 Windows 上编译时删除代码?我试图使代码尽可能可移植。
回答by drescherjm
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:
我经常与从 Visual Studio 调试的不需要代码更改的控制台应用程序一起使用的一种解决方案是执行以下操作:
- Right Click on title bar of your running console application
- Select Properties
- Select Layout
- 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.
关闭对话框后,Windows 应根据您的 Windows 版本保存设置或提示您保存。我相信Windows 8或更高版本不会提示,而Windows 7或更低版本会提示。
回答by herohuyongtao
Use
Console::SetWindowSize()
method (under .NET framework).You can refer to herefor its documentation and code examples.
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 herefor more information.
使用
Console::SetWindowSize()
方法(.NET 框架下)。你可以参考这里的文档和代码示例。
或者你可以使用
MoveWindow()
方法(你也可以移动窗口):#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 // ... }
在这里查看更多信息。
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.
如果你真的想让你的代码尽可能便携,也许你应该通过运行cmd
提示来手动设置它。单击顶部的图标。选择defaults
。输入您想要的设置。
回答by AStopher
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:
如果我想将控制台窗口设置为距屏幕边缘 15 个像素,我会这样做:
Console.WindowWidth = Console.LargestWindowWidth - 15