windows 如何在 VB.NET 中的用户计算机上安装字体,以便 Word 等应用程序可以使用它?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/4725950/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 16:00:45  来源:igfitidea点击:

How do I install a font on a user's machine in VB.NET so applications like Word can use it?

.netwindowsvb.netfonts

提问by Tom

Need to install a font on all user's machines that will use the application I am writing which creates custom documents in Word. It's a barcode font.

需要在将使用我正在编写的应用程序的所有用户计算机上安装字体,该应用程序在 Word 中创建自定义文档。这是一种条形码字体。

I've successfully added the font to my solution and set its build action to Embedded Resource and have also successfully written code to check if the font is already installed on the user's system.

我已成功将字体添加到我的解决方案并将其构建操作设置为嵌入式资源,并且还成功编写了代码来检查字体是否已安装在用户的系统上。

Now I just need to figure out how to extract the font from my solution and install it onto the user's machine as if they installed the font themselves for use in Office applications, etc.

现在我只需要弄清楚如何从我的解决方案中提取字体并将其安装到用户的机器上,就好像他们自己安装了字体以用于 Office 应用程序等。

Most of the examples I've found out there are for using the font within the VB.NET application instead of outside it and the one's I have found which seem to fit my purpose aren't working for me.

我发现的大多数示例都是用于在 VB.NET 应用程序内而不是在其外部使用字体,而我发现的那些似乎符合我的目的的示例对我不起作用。

采纳答案by Hans Olsson

Copy the font into the Windows font folder and then you need to get the font added to the registry. I've not tried this myself, but I think it's possible to do this by opening the font using the ShellExecuteAapi in a similar way to as seen here.

将字体复制到 Windows 字体文件夹中,然后您需要将字体添加到注册表中。我自己没有尝试过这个,但我认为可以通过使用ShellExecuteAapi 以类似于这里看到的方式打开字体来做到这一点

Here's a vbscript way of doing it that might be useful as a starting point since you might be able to use similar syntax and functions in VB.Net: Hey, Scripting Guy! How Can I Install Fonts Using a Script?

这是一种执行此操作的 vbscript 方法,作为起点可能很有用,因为您可能能够在 VB.Net 中使用类似的语法和函数:嘿,脚本专家!如何使用脚本安装字体?

回答by Cody Gray

First, you need to copy the font to the Windows\Fonts directory (you'll want to make sure to use the Environment.GetFolderPathmethod provided by the .NET Framework instead of hard-coding the typical path to the Windows directory, just in case something is different in one of your users' environments).

首先,您需要将字体复制到 Windows\Fonts 目录(您需要确保使用Environment.GetFolderPath.NET Framework 提供的方法,而不是对 Windows 目录的典型路径进行硬编码,以防万一在您的用户环境之一中不同)。

Then, you need to call the AddFontResourcefunctionto add the font to the system font table. Since AddFontResourceis provided by the Windows API, you'll need to P/Invoke to call it from VB.NET code. The declaration looks something like this (the lpszFilenameparameter is the path to the font file that you want to add):

然后,您需要调用该AddFontResource函数将字体添加到系统字体表中。由于AddFontResource由 Windows API 提供,您需要 P/Invoke 从 VB.NET 代码调用它。声明看起来像这样(lpszFilename参数是要添加的字体文​​件的路径):

<DllImport("gdi32.dll"), CharSet := CharSet.Auto> _
Public Shared Function AddFontResource(ByVal lpszFilename As String) As Integer

Finally, if Word (or whatever application you intend to use the font in) is runningat the time you call the AddFontResourcefunction from your code, you need to inform it that the available fonts have changed. You do this by sending a WM_FONTCHANGEmessage to all top-level windows using the SendMessagefunction and setting the hWndparameter toHWND_BROADCAST. Again, you'll need to P/Invoke; the declarations look like this:

最后,如果 Word(或您打算在其中使用字体的任何应用程序)在您从代码中调用该函数时正在运行AddFontResource,您需要通知它可用的字体已更改。为此,您可以WM_FONTCHANGE使用该SendMessage函数向所有顶级窗口发送消息并将hWnd参数设置为HWND_BROADCAST。同样,您需要 P/Invoke;声明如下所示:

Public Const HWND_BROADCAST As Integer = &HFFFF
Public Const WM_FONTCHANGE As Integer = &H1D

<DllImport("user32.dll"), CharSet := CharSet.Auto> _
Public Shared Function SendMessage(ByVal hWnd As IntPtr, ByVal wMsg As Integer, ByVal wParam As IntPtr, ByVal lParam As IntPtr) As Integer

And you would call it to send the broadcast message like this:

你会调用它来发送这样的广播消息:

SendMessage(New IntPtr(HWND_BROADCAST), WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero)

NOTE:The above steps only install the font for the current Windows session. If you need the font to be available on subsequent restarts, you need to add it to the registry. The key to modify is this one:

注意:上述步骤仅安装当前 Windows 会话的字体。如果您需要该字体在后续重新启动时可用,则需要将其添加到注册表中。修改的关键是这个:

HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Fonts

回答by Fermin

First you copy the font to the windows font folder then call AddFontResourceusing p/invoke.

首先将字体复制到 windows 字体文件夹,然后使用 p/invoke调用AddFontResource

Here is an example, it's in C# but you should be able to work it out:

这是一个例子,它是在 C# 中,但你应该能够解决它:

UPDATE

更新

New URL

新网址

http://brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-C.aspx

http://brutaldev.com/post/2009/03/26/Installing-and-removing-fonts-using-C.aspx

回答by Jared Knipp

I used the installer project to install the fonts I needed with my application and followed this guide

我使用安装程序项目来安装我的应用程序所需的字体并遵循本指南