Skip to main content
 首页 » 编程设计

c#之如何在 .NET 中使用不在 GDI+ 中的 Windows GDI 方法

2024年02月13日10zhoujg

我想在 .NET 应用程序中调用在 GDI+ 中找不到的 GDI 方法。

特别是this method它获取指定字体的字距调整对。我想在目前不受支持的 HTML5 Canvas 上实现字距调整(字母间距),我认为最好的方法是在我的服务器上提取字距调整对并将字距调整表返回给客户端。

如何使用 .NET 中的此类 Windows 库函数?

请您参考如下方法:

您可以在 Windows API 中找到此方法以调用此方法:

  • 1 : 将 System.Runtime.InteropServices 命名空间添加到您的项目
  • 2 : 将API 类添加到您的项目中并让它使用GetKerningPairs 方法 请记住,此函数使用一个名为 KERNINGPAIR 的结构,我们需要确保它已在我们的类中定义,否则我们将出现编译错误!

    class API 
    { 
        [DllImport("gdi32.dll")] 
        static extern uint GetKerningPairs(IntPtr hdc, uint nNumPairs, 
        [Out] KERNINGPAIR[] lpkrnpair); 
     
        [StructLayout(LayoutKind.Sequential)] 
        struct KERNINGPAIR 
        { 
            public ushort wFirst; // might be better off defined as char 
            public ushort wSecond; // might be better off defined as char 
            public int iKernAmount; 
     
            public KERNINGPAIR(ushort wFirst, ushort wSecond, int iKernAmount) 
            { 
                this.wFirst = wFirst; 
                this.wSecond = wSecond; 
                this.iKernAmount = iKernAmount; 
            } 
     
            public override string ToString() 
            { 
                return (String.Format("{{First={0}, Second={1}, Amount={2}}}", wFirst, wSecond, iKernAmount)); 
            } 
        } 
    } 
    

现在你可以通过API类调用这个方法了