c++,代码,使游戏机程序内容适合屏幕(显示相同的尺寸),在所有不同的Windows计算机上,Visual Studio

发布于 2025-02-04 19:24:08 字数 1055 浏览 2 评论 0原文

我是Stackoverflow的新手,但是,我总是在这里找到好的技巧和窍门,我想我会加入。

我目前正在大学第二年,我决定开始从事我的第一个投资组合项目。我的项目将是基于文本的转弯游戏,目前在控制台上运行,目的是学习DX11并在此处导入并创建GUI。但是,我目前专注于首先将其作为控制台应用程序。

目前,我遇到了一个问题,使我的应用程序在字体大小上增加(动态上,以便在每个Windows计算机上显示相同),或者只是将输出规模提高以适应其内容以适合屏幕(在每个Windows计算机上)。我尝试了许多有关字体大小更改的方法,对我有用并在其他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);

虽然此方法确实在我的PC和所有其他计算机上都可以使用,但它的作用不如我能做会希望的。由于此代码只是将文本放大,因此在不同的监视器上可能太大或太小。而我正在寻找一种可以简单地将内容在我的控制台程序输出上符合屏幕上的方法,以便在每个计算机上显示相同的内容。

虽然我知道如何放大控制台上的字体(放大),但我正在寻找一种按照我想要的启动方式显示程序的方法。

我请问是否有人在此问题上有任何提示,请记住这不是作业作业或任何分级项目,这仅适用于我的投资组合项目,这已经在教我很多。

很高兴成为这个社区的成员,谢谢您!

I'm new to stackoverflow, however, I always find good tips and tricks here and I thought I would join.

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. 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). I have tried many methods concerning font-size change, the one that worked for me and worked on other PCs is the following:

#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.

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

梦言归人 2025-02-11 19:24:08

如果要将字体尺寸扩展到整个显示屏,则需要检索显示属性,因此您可以更改字体宽度和高度相对于显示宽度和高度。

由于您使用的是Windows API,因此我使用了Windows.h中包含的功能。您只能一次选择一个显示器。

您可以使用sdl_getCurrentDisPlayMode(DisplayIndex,displayMode)指定显示。

您可能需要#include&lt; cmath&gt;才能根据环境使用std :: rough()

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);

您可能想检查一下,如果您的活动显示器在游戏循环中发生了变化,并再次调整所有内容。因此,我建议将所有代码放置以检索宽度和高度,并在其自身功能中调整字体大小,并在您的活动显示器(主机所在的位置)更改时将其称为循环中。

请注意您可以使用 ctrl+鼠标车轮放大字体。

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. You can only choose one of your displays at a time tho.

You specify the display with SDL_GetCurrentDisplayMode(displayIndex, displayMode).

You might need to #include <cmath> to use std::round() depending on environment.

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.

DO NOTE that you can zoom in the font with CTRL+Mouse wheel.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文