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

时间:2013-03-31 19:30:19

标签: c++ winapi console-application custom-font

我正在尝试使用

暂时安装要在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控制台中使用自定义字体,而无需摆弄注册表。我很确定:)

2 个答案:

答案 0 :(得分:2)

不幸的是,你进入了Win API的黑暗世界。对于控制台字体表查找,没有文档(或者至少我找不到它)。您可以尝试使用方法“GetNumberOfConsoleFonts()”来查看返回的内容。我认为索引10的字体是Lucida Console。你必须要玩一点。此外,这可能不适用于您拥有的操作系​​统版本。在XP上为我工作。从来没有尝试过其他任何事情。老实说,从来没有完全使用XP。

对于注册表,

字体注册表位于:

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

控制台注册表位于:

HKEY_CURRENT_USER\Console

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

如果你能找到解决方案/解决方法,那将会很棒。)

答案 1 :(得分:1)

   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中的代码没有意义。