简体   繁体   English

C++,使控制台程序内容适合所有不同 windows 计算机上的屏幕(显示相同大小)的代码,Visual Studio

[英]C++, code to make console program contents fit to screen(display the same size) on all different windows computers, Visual Studio

I'm new to stackoverflow, however, I always find good tips and tricks here and I thought I would join.我是 stackoverflow 的新手,但是,我总能在这里找到好的提示和技巧,我想我会加入。

I'm currently in my second year of college and I decided to start working on my first portfolio project.我目前在大学二年级,我决定开始我的第一个投资组合项目。 My project is going to be a text-based turn-based RPG game that runs on the console for now, with the intention of learning DX11 and import it there and create a GUI.我的项目将是一个基于文本的回合制 RPG 游戏,目前在控制台上运行,目的是学习 DX11 并将其导入那里并创建一个 GUI。 However, I'm currently focused on creating it as a console application first.但是,我目前专注于首先将其创建为控制台应用程序。

Currently, I'm having an issue making my application increase in either font-size (dynamically so that it displays the same on every windows computer) or just simply have the output scale up for its contents to fit the screen (on every windows computer).目前,我遇到了一个问题,使我的应用程序增加字体大小(动态地以便它在每台 windows 计算机上显示相同的字体大小)或者只是简单地让 output 放大其内容以适合屏幕(在每台 windows 计算机上) ). I have tried many methods concerning font-size change, the one that worked for me and worked on other PCs is the following:我尝试了很多关于字体大小更改的方法,对我有用并在其他 PC 上工作的方法如下:

#include <windows.h>


CONSOLE_FONT_INFOEX cfi{};
    cfi.cbSize = sizeof(cfi);
    cfi.nFont = 0;
    cfi.dwFontSize.X = 0;                   // Width of each character in the font
    cfi.dwFontSize.Y = 38;                  // Height
    cfi.FontFamily = FF_DONTCARE;
    cfi.FontWeight = FW_NORMAL;
    std::wcscpy(cfi.FaceName, L"Consolas"); // Choose your font
    SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

While this method does work on my pc and all other computers, it doesn't do the job as well as I would have hoped.虽然此方法在我的电脑和所有其他计算机上都有效,但效果不如我希望的好。 Since this code simply just enlarges text, on different monitors it can either be too large or too small.由于此代码只是简单地放大文本,因此在不同的显示器上它可能太大或太小。 Whereas I am searching for a method that would simply fit the content on my console program output to screen, so that it displays the same on every computer.而我正在寻找一种方法,它可以简单地将我的控制台程序 output 上的内容适合屏幕,以便它在每台计算机上显示相同的内容。

While I know how to enlarge the font on the console (zoom in), I am looking for a method that displays the program the way I want it to on launch.虽然我知道如何在控制台上放大字体(放大),但我正在寻找一种在启动时以我希望的方式显示程序的方法。

I am kindly asking if anyone has any tips in this matter, keep in mind this isn't homework assignment, or any graded project, this is simply for my portfolio project which is already teaching me a lot.我想问问是否有人对此事有任何建议,请记住这不是家庭作业,也不是任何评分项目,这只是针对我的投资组合项目,它已经教会了我很多东西。

Glad to be a member of this community, thank you in advance!很高兴成为这个社区的一员,在此先感谢您!

If you want to scale your font size to your whole display, you would need to retrieve your display properties, so you can change the font width and height relative to the display width and height.如果要将字体大小缩放到整个显示,则需要检索显示属性,以便可以更改相对于显示宽度和高度的字体宽度和高度。

Since you are using the Windows API, I used functions included in Windows.h .由于您使用的是 Windows API,因此我使用了Windows.h中包含的函数。 You can only choose one of your displays at a time tho.您一次只能选择一个显示器。

You specify the display with SDL_GetCurrentDisplayMode(displayIndex, displayMode) .您可以使用SDL_GetCurrentDisplayMode(displayIndex, displayMode)指定显示。

You might need to #include <cmath> to use std::round() depending on environment.您可能需要#include <cmath>才能根据环境使用std::round()

int charactersPerRow = 142;
int charactersPerCol = 44;

HWND activeWindow = GetActiveWindow();
HMONITOR activeMonitor = MonitorFromWindow(activeWindow, MONITOR_DEFAULTTONEAREST);  // Get Monitor closest to console
MONITORINFO monitorInfo{};
monitorInfo.cbSize = sizeof(MONITORINFO);
GetMonitorInfo(activeMonitor, &monitorInfo);                        // Receive monitor info and store it in monitorInfo

int displayWidth = monitorInfo.rcMonitor.right - monitorInfo.rcMonitor.left;
int displayHeight = monitorInfo.rcMonitor.bottom - monitorInfo.rcMonitor.top;

CONSOLE_FONT_INFOEX cfi{};
cfi.cbSize = sizeof(cfi);
cfi.nFont = 0;
cfi.dwFontSize.X = static_cast<short>(std::round((float)displayWidth / charactersPerRow));        // Width of each character in the font rounded
cfi.dwFontSize.Y = static_cast<short>(std::round((float)displayHeight / charactersPerCol));       // Height rounded
cfi.FontFamily = FF_DONTCARE;
cfi.FontWeight = FW_NORMAL;
std::wcscpy(cfi.FaceName, L"Consolas");                             // Choose your font
SetCurrentConsoleFontEx(GetStdHandle(STD_OUTPUT_HANDLE), FALSE, &cfi);

You might wanna check, if your active monitor has changed in your game loop and resize everything again.您可能想检查一下,如果您的活动监视器在您的游戏循环中发生了变化,并再次调整所有内容的大小。 So i would recommend putting all the code for retrieving width and height and resizing the font in its own function and call it in your loop whenever your active monitor (where your console is located in) changes.所以我建议将所有用于检索宽度和高度并调整字体大小的代码放在它自己的 function 中,并在活动监视器(控制台所在的位置)发生变化时在循环中调用它。

DO NOTE that you can zoom in the font with CTRL+Mouse wheel .注意,您可以使用CTRL+Mouse wheel放大字体。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在 Visual Studio Code 中构建 C++ 程序 - Building a C++ Program in Visual Studio Code Visual Studio 2015(C ++)代码大小 - Visual Studio 2015 (C++) code size Visual Studio C++:在不同的可执行文件名下使用不同数量的 RAM 的相同程序 - Visual Studio C++ : same program using different amounts of RAM under different executable names Visual Studio 2013 C ++控制台程序中的输入结束 - End of Input in a Visual Studio 2013 C++ Console Program 使用不同版本的 Visual Studio 构建 C++ 代码会产生不同的 .exe 文件大小? - Building C++ code with different version of Visual Studio produces different file size of .exe? 为什么在Visual Studio中用c ++编写GUI应用程序的代码与控制台应用程序不同? - Why the code to write GUI application in c++ in Visual Studio is different than to console application? 为什么控制台C ++程序在Visual Studio中具有不同的起始代码? - Why do console C++ programs have a different starting code in Visual Studio? 使 Visual Studio 代码调试控制台 colors 匹配虚幻引擎 C++ 错误/警告语法 - Make Visual Studio Code Debug Console colors match Unreal Engine C++ Error/Warning Syntax c ++控制台屏幕大小 - c++ console screen size 如何使Visual Studio 2017 C ++项目在计算机之间更加便携? - How to make Visual Studio 2017 C++ project more portable between computers?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM