简体   繁体   English

AddFontFile XP C#

[英]AddFontFile XP C#

I have a project where I need to load a Postscript font from disk. 我有一个项目,需要从磁盘加载Postscript字体。 I found I could use "AddFontFile". 我发现我可以使用“ AddFontFile”。 Doing some research I see that I have to pipe the two fonts http://msdn.microsoft.com/en-us/library/system.drawing.text.privatefontcollection.addfontfile.aspx together so I tried: 做一些研究,我发现我必须将两个字体http://msdn.microsoft.com/en-us/library/system.drawing.text.privatefontcollection.addfontfile.aspx一起传送,所以我尝试了:

fontCollection = new PrivateFontCollection();
fontCollection.AddFontFile(@"C:\Temp\Font\myfont.PFM|C:\Temp\Font\myfont.PFB");

I'm getting aa error "Illegal characters in path". 我收到一个错误“路径中的非法字符”。 I'm not sure if I'm piping the two fonts correctly. 我不确定我是否正确地传递了两种字体。

Any help would be great, I should mention we are still on XP not sure if that makes a differnts or not. 任何帮助都将是很大的,我应该提到我们仍然在XP上不确定是否会有所作为。

Mike 麦克风

You cannot have the pipe | 您不能使用管道| character in your filename. 文件名中的字符。 PrivateFontCollection.AddFontFile requires a valid filepath. PrivateFontCollection.AddFontFile需要有效的文件路径。 Hence your "Illegal characters in path" exception. 因此,您的“路径中的非法字符”异常。 The input at MSDN is A String that contains the file name of the font to add. MSDN上的输入是A String that contains the file name of the font to add. Try passing a single file at a time - I don't know about this piping idea.. 尝试一次传递一个文件-我不知道这种管道设计方法。

As for your Postscript desire, the Remarks section states that OpenTypes have limited support. 关于您的Postscript需求,“备注”部分指出OpenTypes支持有限。

After some searching I got it to work and I'd like to share on how I solved the this: AddFontFile was the wrong API i Needed to use AddFontResource instead 经过一番搜索后,我开始使用它了,我想分享一下我如何解决这个问题:AddFontFile是错误的API,我需要改用AddFontResource

            String fontPath = @"C:\Temp\Font2\GXSTRA03.PFM|C:\Temp\Font2\GXSTRA03.PFB";
            int result = AddFontResource(fontPath);
            long msg = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);

to remove the font resource 删除字体资源

RemoveFontResource(@"C:\Temp\Font2\GXSTRA03.PFM|C:\Temp\Font2\GXSTRA03.PFB");
long msg = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);

If anyone is new on making a external WinApi call here is the import and import DLL code that I used 如果有人在进行外部WinApi调用方面是新手,那么这里是我使用的import和import DLL代码

using System.Runtime.InteropServices;

private static uint WM_FONTCHANGE = 0x1D;

        Import("gdi32.dll")]
        static extern int AddFontResource(string lpFilename);

        [DllImport("gdi32.dll")]
        static extern bool RemoveFontResource(string lpFileName);

        [DllImport("user32.dll", CharSet = CharSet.Auto)]
        private static extern int SendMessage(IntPtr hWnd, uint Msg, IntPtr wParam, IntPtr lParam);

This fonts now shows in word and notepad ect.. 现在,该字体以单词和记事本等显示。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM