gpt4 book ai didi

C++ 使用 AddFontResource() 临时添加到系统字体表中以在控制台中使用的字体查找索引

转载 作者:行者123 更新时间:2023-11-30 02:56:17 24 4
gpt4 key购买 nike

我正在尝试临时安装一种字体以在 win32 控制台中使用

int AddFontResource(LPCTSTR lpszFilename);

BOOL WINAPI SetConsoleFont(HANDLE hOutput, DWORD fontIndex)

我从 this 得到这个函数网站。

虽然这两个函数似乎都可以正常工作,但我不知道如何找到添加的字体索引以与 SetConsoleFont 一起使用。

AddFontResource 不返回临时字体的索引值或键。

这是我的相关代码:

#include "Level.h"
#include "ConsoleFont.h" //acquired from above mentioned site

#include <Windows.h>

//-------------------------------------------------------------------------------

void init();
void cleanup();

int main()
{
FileManager *pFileManager = new FileManager(); //unrelated
Level *lvl1 = new Level("filename",pFileManager); //unrelated


///TEMPORARY PLANNING
// using add font resource. how can i get this fonts index value?
int err = AddFontResource(L"Files/gamefont.fnt");
if (err == 0)
{
MessageBox(NULL,L"loading font failed",L"Error",0);
}
else
{
wchar_t message[100];
swprintf_s(message,100,L"AddFontResource returned: %d",err);
MessageBox(NULL,LPTSTR(message),L"error",0);
}
SendMessage(HWND_BROADCAST, WM_FONTCHANGE,0,0);

//acquiring handle to current active screen buffer
HANDLE tempHandle = GetStdHandle(STD_OUTPUT_HANDLE);
if (tempHandle == INVALID_HANDLE_VALUE)
{
MessageBox(NULL,L"Failed to aquire Screen Buffer handle",L"Error",0);
}

//I dont know what to set this to. this is the crux of the problem.
DWORD fontIndex = 1;
if (FALSE == SetConsoleFont(tempHandle,fontIndex))
{
MessageBox(NULL,L"loading console font failed",L"Error",0);
}

//draws a house when in correct font
std::cout<<"!!!!!!!!#\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !\n"
<<"!!!!!!!!!\n"
<<"! !! !! !#\n"
<<"!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"<<std::endl;

///PLANNING OVERS


bool quit = false;
while(!quit)
{
//still to be implemented
}



err = RemoveFontResource(L"Files/gamefont.fnt");
if (err==0)
{
MessageBox(NULL,L"removing font failed",L"Error",0);
}
return 0;
}

我不知道如何找到我的新字体的索引值,或者即使使用我当前的方法可以做到这一点。

如果有人知道或有更好的方法请帮助我。任何帮助或提示表示赞赏。必须可以在 win32 控制台中使用自定义字体而无需修改注册表。我确定:)

最佳答案

不幸的是,您进入了 Win API 的黑暗世界。没有用于控制台字体表查找的文档(或者至少我永远找不到它)。您可以尝试“GetNumberOfConsoleFonts()”方法来查看返回的内容。我认为索引 10 处的字体是 Lucida Console。你将不得不玩一下。此外,这可能不适用于您拥有的操作系​​统版本。在 XP 上为我工作。无需尝试其他任何东西。老实说,它也从未在 XP 上完全运行。

对于注册表,

字体注册在这里:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

控制台注册表在这里:

HKEY_CURRENT_USER\Console

如果您最终修改注册表,所做的更改可能不会立即反射(reflect)出来。您需要重新启动控制台或发送特殊的 WM_* 消息(抱歉不记得名字了)。

如果您能找到解决方案/解决方法,那就太好了:)

关于C++ 使用 AddFontResource() 临时添加到系统字体表中以在控制台中使用的字体查找索引,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15733596/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com