You need to enable JavaScript to run this app. Windows CE 5.0下.NET CF2.0 C#应用AddFontResourceEx调用无效排查
优惠活动
大模型
产品
解决方案
定价
更多

Windows CE 5.0下.NET CF2.0 C#应用AddFontResourceEx调用无效排查

Troubleshooting AddFontResourceEx Not Working on Windows CE 5.0 with .NET CF 2.0

Hey there, I’ve wrestled with font loading headaches on Windows CE devices before, so let’s break down why your custom font isn’t showing up. Here are the key checks to run through:

  • Verify your P/Invoke declaration is CE-compatible
    Windows CE uses coredll.dll instead of the desktop’s gdi32.dll, and AddFontResourceEx has specific flag requirements for private fonts. Make your declaration looks like this:

    [DllImport("coredll.dll", SetLastError = true)]
    private static extern int AddFontResourceEx(string lpszFilename, uint fl, IntPtr pdv);
    
    // Critical flags for private, app-only font loading on CE
    private const uint FR_PRIVATE = 0x10;
    private const uint FR_NOT_ENUM = 0x20;
    

    When calling it, use FR_PRIVATE | FR_NOT_ENUM as the flag combination—this ensures the font loads only for your app and doesn’t pollute the system font list.

  • Use an absolute path for the font file
    Windows CE doesn’t guarantee your app’s directory is the current working directory. Always build the full path using your assembly’s location to avoid path issues:

    string fontPath = Path.Combine(
        Path.GetDirectoryName(Assembly.GetExecutingAssembly().GetName().CodeBase),
        "yourfont.ttf"
    );
    

    Double-check that the font file is actually deployed to this directory on the device—sometimes Visual Studio doesn’t copy non-project files automatically. Add it to your project and set "Copy to Output Directory" to "Copy always" to fix this.

  • Broadcast a font change message after loading
    Even if the font loads successfully, the system won’t update its font cache until you send a WM_FONTCHANGE message. Add this P/Invoke and call it right after loading the font:

    [DllImport("coredll.dll")]
    private static extern IntPtr SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);
    
    private const uint WM_FONTCHANGE = 0x001D;
    private const int HWND_BROADCAST = 0xffff;
    
    // Run this immediately after AddFontResourceEx:
    SendMessage((IntPtr)HWND_BROADCAST, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
    
  • Confirm you’re setting the Label’s font correctly
    Loading the font doesn’t automatically apply it to your controls. You need to explicitly set the Font property using the font’s internal name (not necessarily the filename). For example, if your font file is CustomFont.ttf but its internal name is Custom Sans Serif, use:

    label1.Font = new Font("Custom Sans Serif", 10, FontStyle.Regular);
    

    To find the internal name, use a tool like FontForge, or enumerate loaded fonts with EnumFontFamiliesEx to verify it’s present in the system.

  • Check for loading errors
    Since you set SetLastError = true in the P/Invoke, call Marshal.GetLastWin32Error() right after AddFontResourceEx to get a Windows error code. A return value of 0 means no fonts were loaded—this could point to a corrupted font file or invalid path.

  • Follow up on Valter Minute’s hints
    If Valter already pointed you toward P/Invoke or path issues, double down on those first. For example, if he mentioned flag values, ensure you’re using CE-specific flags instead of desktop ones.

内容的提问来源于stack exchange,提问作者Michi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 04:17:58