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

C++ Finding Index for Font temporarily added to System Font Table with AddFontResource() to use in Console

本文关键字:字体 索引 查找 控制台 C++ AddFontResource 添加 系统 使用      更新时间:2023-10-16

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

int AddFontResource(LPCTSTR lpszFilename);

BOOL WINAPI SetConsoleFont(HANDLE hOutput, DWORD fontIndex)

我从这个网站上获得了这个功能。

尽管这两个函数似乎都很好用,但我不知道如何找到添加的字体索引来与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_MACHINESOFTWAREMicrosoftWindows NTCurrentVersionFonts

控制台注册表在这里:

HKEY_CURRENT_USERConsole

如果您最终修改了注册表,这些更改可能不会立即反映出来。您需要重新启动控制台或发送一条特殊的WM_*消息(对不起,不记得名称(。

如果你能找到一个解决方案/解决方法,那就太好了:(

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

这是错误的AddFontResource返回加载的字体数,所以ELSE中的代码没有意义。