0

I work on my own WinAPI project and use ANSI version of API language C/C++ but I have faced with the issue when I running the program on computer with non-Russian version windows. I see unreadable symbols instead of russian letters.

I try to use method AddFontResourceEx and on Russian version Windows it works correctly but when I run on non-Russian version Windows I have got an error "Font 1 Error" and I see unreadable symbols. How can I solve this issue?

Font "MY_ARIAL.TTF" is in the folder with exe-file

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static LOGFONT lf1; 

    switch (uMsg)
    {
    case WM_CREATE:

         if(AddFontResourceEx("MY_ARIAL.TTF", FR_PRIVATE, NULL)!=0)
         {
             SendMessage(HWND_BROADCAST, WM_FONTCHANGE,0,0);
             lstrcpy((LPSTR)&lf1.lfFaceName, "My_Arial");           
         }
         else       
             MessageBox(hWnd,"Font 1 Error","error",MB_OK);  
    }
}
Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770
  • 3
    *and use ANSI version* - There's your problem. Always use the wide versions of winapi functions unless you plan on supporting a version of Windows that's almost two decades old, in which case you should use `TCHAR`s. – chris Jul 23 '14 at 19:05

1 Answers1

0

Try this instead:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static LOGFONT lf1 = {0}; 

    switch (uMsg)
    {
    case WM_CREATE:

         if (AddFontResourceEx(TEXT("MY_ARIAL.TTF"), FR_PRIVATE, NULL))
         {
             SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
             lstrcpy(lf1.lfFaceName, TEXT("My_Arial"));
         }
         else       
             MessageBox(hWnd, TEXT("Font 1 Error"), TEXT("error"), MB_OK);  
    }
}

Or:

LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
    static LOGFONTW lf1 = {0}; 

    switch (uMsg)
    {
    case WM_CREATE:

         if (AddFontResourceExW(L"MY_ARIAL.TTF", FR_PRIVATE, NULL))
         {
             SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
             lstrcpyW(lf1.lfFaceName, L"My_Arial");
         }
         else       
             MessageBoxW(hWnd, L"Font 1 Error", L"error", MB_OK);  
    }
}

Either way, keep in mind that you are specifying a relative path to AddFontResourceEx(), so it is subject to whatever path GetCurrentDirectory() returns, which MAY NOT be what you are expecting! NEVER use a relative path, ALWAYS use an absolute path instead. You can retrieve your app's folder using GetModuleFileName() and then remove the filename portion (everything after the last '\' character). Then you can append "MY_ARIAL.TTF" to that and pass the whole thing to AddFontResourceEx(). Look at PathRemoveFileSpec() and PathCombine() for that.

Remy Lebeau
  • 555,201
  • 31
  • 458
  • 770