0

I'm trying to develop a Smart Device program for Windows CE 5.0 device in my car with Visual Stdio 2008 pro and c# with .NET 2.0. I want to add a font using AddFontResourceEx, but I can't get it to work. Here is the code:

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }
    private const uint FR_PRIVATE=0x10;
    [DllImport("GDI32.dll",EntryPoint="AddFontResourceEx")]
    static extern int AddFontResourceEx(string lpszFilename, uint fl, IntPtr pdv);

    private void button1_Click(object sender, EventArgs e)
    {
        AddFontResourceEx(".\\ELEPHNT.TTF", FR_PRIVATE, IntPtr.Zero);
       label1.ForeColor = Color.FromArgb(155, 25, 34);
       label1.Font = new Font("ELEPHNT.TTF", 18, FontStyle.Regular);
       label1.Text = "Hello world!";
    }
}

It builds succesfully and I can run the program, but the font won't change. I added the font file to the same directory where the program is. Could you please tell me what is wrong?

Michi
  • 23
  • 5
  • You can't use a C++ declaration inside C# code, you need to use pinvoke and compatible data types: – Valter Minute Feb 18 '18 at 10:29
  • 1. had to P/Invoke coredll.dll instead of gdi32.dll 2. the address to the font file should be @\\MMC_STORAGE\\KORN.TTF 3. the font in my device is a bitmap font and not a vector font 4. the entrypoint should be AddFontResourceW 5. Font label should be called by its fontname and not by the name of the fontfile 6. The other statements with AddFontResource should not have Ex at the end 7. The emulator should be soft reset after hanging and not just EXIT'ed by the closing cross of the frame. – Michi Jan 08 '19 at 12:21

1 Answers1

0

You can't use a C++ declaration inside C# code, you need to use pinvoke and compatible data types: http://www.pinvoke.net/search.aspx?search=addfontresource&namespace=[All] In Windows CE there is no relative path, so you'll have to use the full path of your font file, starting from .

Valter Minute
  • 2,177
  • 1
  • 11
  • 13
  • I tried to add my comment and the changed code here, but reached the max number of characters. Therefore i changed my original question above. Could you please take a look at it? I know I made a mess of it, but that was the only way I could make it work. – Michi Feb 26 '18 at 00:56
  • you wrote: "... have to use the full path of your font file, starting from ." You didnt finish this sentence. Could you please let me know what you wanted to mention there? – Michi Mar 11 '18 at 23:16
  • it was starting from root "\" sorry for the issue – Valter Minute Mar 15 '18 at 16:20
  • I finally figured it out: 1. had to use coredll.dll instead of gdi32.dll 2. the address to the font file should be @\\MMC_STORAGE\\ELEPHNT.FNT 3. the font in my device is a bitmap font and not a vector font 4. the entrypoint should be AddFontResourceW 5. Font label should be called by its fontname and not by the name of the fontfile 6. The other statements with AddFontResource should not have Ex at the end Further more I found that when using an emulator, it should be soft reset'ed after hanging and not just EXIT'ed by the closing cross of the frame. Thanks Valter for all your support – Michi Jan 08 '19 at 12:32