创建新的Font对象后的AddFontResourceW

时间:2019-02-26 15:14:16

标签: c# .net fonts pinvoke

在创建字体对象后,添加自定义字体时出现了一些问题。 这是一些演示代码:

[DllImport("user32.dll")]
private static extern int SendMessage(int hWnd, uint wMsg, int wParam, int lParam);

[DllImport("gdi32.dll", EntryPoint = "AddFontResourceW", SetLastError = true, CharSet = CharSet.Unicode)]
private static extern int AddFontResource([In][MarshalAs(UnmanagedType.LPWStr)]

public void Demo()
{
var font = new Font("Roboto", 8); //Create roboto font. Not installed yet, then use Times New Roman. Ok!
var result = AddFontResource(@"C:\Roboto.ttf"); //Install roboto font until system reboots (Sample filename). result == 1. Ok!
var test = SendMessage(0xffff, 0x001D, 0, 0);
font = new Font("Roboto", 8); //Font should be roboto, but still Times New Roman. 
}

随后的演示作品:

public void Demo()
{
var result = AddFontResource(@"C:\Roboto.ttf"); //Install roboto font until system reboots (Sample filename). result == 1. Ok!
var test = SendMessage(0xffff, 0x001D, 0, 0);
var font = new Font("Roboto", 8); //Font is roboto. OK!
}

有人可以告诉我,如果以前没有创建字体对象,为什么AddFontResourceW只起作用?或者我如何才能使第一个示例正常运行?

0 个答案:

没有答案