Windows CE 5.0下.NET CF2.0 C#应用AddFontResourceEx调用无效排查
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 usescoredll.dllinstead of the desktop’sgdi32.dll, andAddFontResourceExhas 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_ENUMas 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 aWM_FONTCHANGEmessage. 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 theFontproperty using the font’s internal name (not necessarily the filename). For example, if your font file isCustomFont.ttfbut its internal name isCustom 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
EnumFontFamiliesExto verify it’s present in the system.Check for loading errors
Since you setSetLastError = truein the P/Invoke, callMarshal.GetLastWin32Error()right afterAddFontResourceExto 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

