I am using the AddFontResource function to locally set the font for the current login session.
private void installFont(string fontPath) { IntPtr HWND_BROADCAST = new IntPtr(0xFFFF); const int WM_FONTCHANGE = 0x1D; string fontLocation = Environment.ExpandEnvironmentVariables(fontPath); int result = AddFontResourceA(fontLocation);
The int result returned by the AddFontResource
function is 1, which according to the documentation is the number of successfully installed font windows.
If the function succeeds, the return value indicates the number of fonts added.
If the function does not work, the return value is zero. No extended error information is available.
Then I programmatically test the font using the following code.
private static bool IsFontInstalled(string fontName) { using (var testFont = new Font(fontName, 8)) { return fontName.Equals(testFont.Name, StringComparison.InvariantCultureIgnoreCase); } }
However, the isFontInstalled
function always returns false.
This function runs a simple test where it tries to create a font using the set font name. If the installation was successful, the new font will have the name of the font used, if it is not installed, the default will be a different font name of the system.
NOTE I acknowledge that my current font installation verification implementation may not work programmatically in all cases, feel free to offer excellent verification methods, I assume that part of the problem may be that my current implementation only works for checking fonts installed using the registry .
I use the same function to check if the font that I install through the registry is installed and it works as expected. Any insight on how to use the font that was apparently installed?
According to the docs:
This function sets the font for the current session only. When the system reboots, the font will not be present. To have the font installed even after restarting the system, the font must be specified in the registry.
In my understanding, the current session lasts until the user logs out, and this should include the test function of this program.