Home > front end >  Why is RemoveFontResourceW not working to remove a font in Python?
Why is RemoveFontResourceW not working to remove a font in Python?

Time:03-20

I added a font resource file to my Tkinter GUI app using

gdi32 = ctypes.WinDLL('gdi32')
gdi32.AddFontResourceW(font_path)

It works great, however, I can't seem to remove the font from the user's system.

When the user exits the program, I want to remove the font resource using

gdi32.RemoveFontResourceW(font_path)

however, it does not seem to work at all. What am I missing here?

For reference, here is the Microsoft documentation that explains the usage of the add/remove font functions

CodePudding user response:

Per the RemoveFontResourceW() documentation:

If there are outstanding references to a font, the associated resource remains loaded until no device context is using it. Furthermore, if the font is listed in the font registry (HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts) and is installed to any location other than the %windir%\fonts\ folder, it may be loaded into other active sessions (including session 0).

So, make sure there really are no more references to the font before you try to remove it.

Also, make sure you are broadcasting WM_FONTCHANGE, like the documentation says:

We recommend that if an app adds or removes fonts from the system font table that it notify other windows of the change by sending a WM_FONTCHANGE message to all top-level windows in the system. The app sends this message by calling the SendMessage function with the hwnd parameter set to HWND_BROADCAST.

  • Related