Ross Borchers
Ross Borchers

Reputation: 145

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

I am trying to temporarily install a font to use in the win32 console with

int AddFontResource(LPCTSTR lpszFilename);

and

BOOL WINAPI SetConsoleFont(HANDLE hOutput, DWORD fontIndex)

I got hold of this function from this site.

Although both functions seem to work fine I have no idea how to find the added font index to use with SetConsoleFont.

AddFontResource returns no index value or key to the temporary font.

Here is my relevant code:

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

I don't know how to go about finding my new font's index value or even if this is possible using my current method.

If someone knows or has a better method please help me out. any help or hints are appreciated. It must possible to use a custom font in the win32 Console without fiddling with the registry. I'm sure of it :)

Upvotes: 2

Views: 2017

Answers (2)

Giobunny
Giobunny

Reputation: 96

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

this is wrong AddFontResource returns the number of fonts loaded, so the code in the ELSE doesn't make sense.

Upvotes: 1

Vaibhav Desai
Vaibhav Desai

Reputation: 2728

Unfortunately you entered the dark world on Win APIs. There is no documentation (or atleast I could never find it) for a console font table lookup. You can try the method "GetNumberOfConsoleFonts()" to see what is returned. I think the font at index 10 is Lucida Console. You'll have to play around a little. Also, this may not work for the OS version you have. Worked for me on XP. Never had to try on anything else. And honestly, never got it fully working on XP too.

For the registry,

Fonts registries are here:

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

Console registries are here:

HKEY_CURRENT_USER\Console

If you end up modifying the registry, the changes may not be reflected immediately. You need to either restart the console or send a special WM_* message (sorry don't remember the name).

Will be great if you can find a solution/workaround :)

Upvotes: 5

Related Questions