Registering font at runtime under Windows XP

  • Thread starter Thread starter Sylvain Audet
  • Start date Start date
Hi Sulvain,

First, the font must be installed in the fonts folder for this to work
since that is where the InstalledFontCollection is derived. If the font is
not moved to the Fonts folder to install it, the InstalledFontCollection
will not list it.

So the first question is a known problem. In order to workaround it, we
need to use PInvoke to call EnumFontFamiliesEx to query installed fonts.

For more information on it, please refer to:
http://groups.google.com/groups?hl=en&lr=&ie=UTF-8&oe=UTF-8&threadm=nggF4aSf
BHA.1272%40cpmsftngxa09&rnum=1&prev=/groups%3Fhl%3Den%26lr%3D%26ie%3DUTF-8%2
6oe%3DUTF-8%26q%3DGDI%2BInstalledFontCollection

For the second refresh folder question, based on my test, we may need to
modify the
registry key as below so that when we restarts the windows form application
will be able to use the font.

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

We may need to add the font information into the registry manually.

Here the code to modify the registry.(Just for test, you may need to change
according to what font you want to add)
Private Sub Button4_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button4.Click
Dim fonts As RegistryKey = _
Registry.LocalMachine.OpenSubKey("SOFTWARE\Microsoft\Windows
NT\CurrentVersion\Fonts", True)
fonts.SetValue("3 of 9 Barcode (TrueType)", "3OF9.TTF")
Dim str As String = fonts.GetValue("3 of 9 Barcode (TrueType)",
"3OF9.TTF")
MsgBox(str)
End Sub

In this way, when we install fonts and then add the value into the
SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts registry key. After
restart the windows application we will be able to use the new added font,
we do not need to refresh the C:\windows\Fonts folder.


Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Sulvain,

For the font of CrystalReportViewer, my test is to set the font property of
the CrystalReportViewer control.

My code is shown as below.
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles Button3.Click
Me.CrystalReportViewer1.Font = New Font("3 of 9 Barcode", 12)
MsgBox(Me.CrystalReportViewer1.Font.Name)
End Sub

Is this what do you mean by the font in the CrystalReportViewer?
If I have any misunderstanding, please post here.

Best regards,

Peter Huang
Microsoft Online Partner Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Hi Peter,
Your solution is not working. Although I add the registry key, the font
doesn't get listed as an installed font using the font enumeration code in
the google message link you provided.

I also tried to explicitly set my ReportViewer's font property to
"Barcode 3 of 9" after installing it and since the font is not recognized to
be installed, the Font.Name property remains "Microsoft Sans Serif".

I will send you my litte vb project with this new update for your
convenience, in case you find something I may be doing wrong.

Thanks,
 
I must make a correction here, after adding the registry key, the font IS
available, "without" refreshing the \Windows\Fonts folder, once I restart my
application. - This fixes one problem!

The remaining problem here is that I still need to restart my application to
be able to use the font as soon as it get installed.

Thanks,
 
Hi Sylvain,

Unfortunately, as Peter discussed, GDIPlus caches the installed fonts upon
startup. Any fonts added after the application starts will not be
recognized as a valid font by System.Drawing or GDIPlus.

The only workaround is to interoperate with GDI, and *remain in GDI for all
usages of the font*. In other words, going to GDI to get an HFONT, then
trying to create a Font object with that HFONT (with Font::FromHFont) will
not work since GDIPlus still does not recognize the font. GDIPlus will
never recognize the font unless you restart the application. The only
workaround is to use GDI to create an HFONT for the new font, then use GDI
to output text using the new font, then delete the HFONT upon completion
(all in GDI).

If you attempt to access a font (or an HFONT for a font) that was not in
existence upon application startup, then you get an error message saying
that this is not a valid TrueType font. This error message is somewhat
misleading in this case. A clearer message would be something like "This
font is not recognized as a valid font by GDIPlus".

I'm sorry I don't have better news, but hopefully this will give you some
direction.

Regards,
David Wooden
Microsoft Developer Support


Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Well. Thanks David and Peter, and all others microsoft folks who may have
worked on this for all your efforts.

I have a last question though. Do you guys believe that once my font
gets installed, if I load up my CrystalViewer form into a new appDomain, it
could behave like if I would be starting up a new application?

Thanks,
 
if I load up my CrystalViewer form into a new appDomain, it
That's a great idea, but unfortunately it won't fix the problem because a
new AppDomain is not a new process. AppDomains in the same process share
the same instance of GDIPlus.

Regards,
David Wooden
Microsoft Developer Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Are new threads started within an application sharing the same instance of
GDIPlus also?
 
Are new threads started within an application sharing
Yes.

Regards,
David Wooden
Microsoft Developer Support

Get Secure! - www.microsoft.com/security
This posting is provided "AS IS" with no warranties, and confers no rights.
 
Well! I gues that is it for the thread! Thanks to all, once again, for your
help.

Regards,
 
Back
Top