为什么 RemoveFontResourceW 无法删除 Python 中的字体?

Why is RemoveFontResourceW not working to remove a font in Python?

我使用

向我的 Tkinter GUI 应用程序添加了一个字体资源文件
gdi32 = ctypes.WinDLL('gdi32')
gdi32.AddFontResourceW(font_path)

效果很好,但是,我似乎无法从用户系统中删除该字体。

当用户退出程序时,我想删除使用

的字体资源
gdi32.RemoveFontResourceW(font_path)

然而,它似乎根本不起作用。 我在这里错过了什么?

作为参考,这里是 Microsoft documentation,它解释了 add/remove 字体函数的用法

根据 RemoveFontResourceW() 文档:

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).

因此,在尝试删除字体之前,请确保确实没有更多引用该字体。

此外,请确保您正在广播 WM_FONTCHANGE,如文档所述:

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.