Search code examples
directx-9

How can I add my own font to DirectX?


I want to add new font and draw text on the screen.

But When I draw text by use this font, the text font is Arial.

I don't know the reason.

Here is my code.

Please see and help me.

HANDLE          hFind;
WIN32_FIND_DATA wfd;
WCHAR           szFontPath[MAX_PATH];
int             nNum;

swprintf(szFontPath, L"%s\\Fonts\\*.*", m_szAppPath);
hFind = FindFirstFile(szFontPath, &wfd);
if(hFind == INVALID_HANDLE_VALUE)
    return;
do 
{
    if(wfd.cFileName[0] == L'.')
        continue;
    swprintf(szFontPath, L"%s\\Fonts\\%s", m_szAppPath, wfd.cFileName);
    nNum = AddFontResource(szFontPath);
} 
while(FindNextFile(hFind, &wfd));
PostMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
FindClose(hFind);

---------------------- In other reference function--------------------------------

int                     nHeight;
LPDIRECT3DSURFACE9      pSurface;
HDC                     hDC;

m_pDevice = pDevice;
m_pDevice->GetBackBuffer(0, 0, D3DBACKBUFFER_TYPE_MONO, &pSurface);
pSurface->GetDC(&hDC);
nHeight = -MulDiv( dwSize, GetDeviceCaps(hDC, LOGPIXELSY), 72 );
pSurface->ReleaseDC(hDC);
m_hFont = CreateFont( nHeight, 0, 0, 0, bBold, bItalic, false, false, 
    HANGUL_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, ANTIALIASED_QUALITY,
    DEFAULT_PITCH/* | FF_DONTCARE*/, L"Helvetica-Condensed-Black-Se");

pSurface->Release();

Solution

  • i'm not sure you are allowed to specify a path for your font. you may need to "install" it by copying it to c:\windows\fonts (or the xp equivalent)