C# 如何在.net中获取字体紧排对
我正在尝试使用此p/Invoke调用获取字体紧排对:C# 如何在.net中获取字体紧排对,c#,vb.net,fonts,gdi+,kerning,C#,Vb.net,Fonts,Gdi+,Kerning,我正在尝试使用此p/Invoke调用获取字体紧排对: Imports System.Runtime.InteropServices Public Class Kerning Structure KERNINGPAIR Public wFirst As UInt16 Public wSecond As UInt16 Public iKernelAmount As UInt32 End Structure <DllImport("gdi32.dll")> _
Imports System.Runtime.InteropServices
Public Class Kerning
Structure KERNINGPAIR
Public wFirst As UInt16
Public wSecond As UInt16
Public iKernelAmount As UInt32
End Structure
<DllImport("gdi32.dll")> _
Private Shared Function GetKerningPairs(hdc As IntPtr,
nNumPairs As UInteger, <Out> lpkrnpair As KERNINGPAIR()) As UInteger
End Function
Sub ExaminePairs()
Dim f As Font
For Each myFontFamily In System.Drawing.FontFamily.Families
f = New Font(myFontFamily, 25)
Dim pairs As UInteger = 0
Dim pairsArray() As KERNINGPAIR
ReDim pairsArray(pairs)
Dim a = GetKerningPairs(f.ToHfont(), pairs, Nothing)
If a <> 0 Then
MsgBox("Found!")
End If
f.Dispose()
Next
End Sub
End Class
导入System.Runtime.InteropServices
公共类紧排
结构核对
公共wFirst As UInt16
公共WSSecond As UInt16
公共iKernelAmount As UInt32
端部结构
_
专用共享函数GetKerningPairs(hdc作为IntPtr,
nNumPairs作为UInteger,lpkrnpair作为KERNINGPAIR())作为UInteger
端函数
副主考人()
调暗f作为字体
对于System.Drawing.FontFamily.Families中的每个myFontFamily
f=新字体(myFontFamily,25)
按UInteger=0调整对的尺寸
Dim Pairsaray()作为紧排对
ReDim成对排列(成对)
Dim a=GetKerningPairs(f.ToHfont(),pairs,Nothing)
如果是0,那么
MsgBox(“找到!”)
如果结束
f、 处置
下一个
端接头
末级
每当找到具有已定义字距对的字体时(根据以下说明),TesteParis函数应显示一个消息框:
但它似乎总是返回0
我需要找到一种方法来获取给定字体的所有紧排对(有多少,然后是它们的结构)
有人知道怎么做吗?接受的答案显示了如何从VB.NET调用GetKerningPairs
。下面是为适应您的需要而修改的代码:
导入系统图形
导入System.Runtime.InteropServices
公共类紧排
结构核对
公共wFirst为短
公营部门
公共iKernelAmount为整数
端部结构
公共共享函数GetKerningPairs(ByVal hdc作为IntPtr,ByVal nPairs作为Integer,ByVal pairs()作为KERNINGPAIR)作为整数
端函数
私有共享函数SelectObject(ByVal hdc作为IntPtr,ByVal hObject作为IntPtr)作为IntPtr
端函数
公共共享函数GetKerningPairs(ByVal字体作为字体)作为IList(KerningPairs的)
Dim pairs()作为紧排对
使用g作为Graphics=Graphics.FromHwnd(IntPtr.Zero)
g、 PageUnit=GraphicsUnit.Pixel
尺寸hdc为IntPtr=g.GetHdc
Dim hFont As IntPtr=font.ToHfont
变暗旧为IntPtr=SelectObject(hdc、hFont)
尝试
Dim numPairs As Integer=GetKerningPairs(hdc,0,Nothing)
如果numPairs>0,则
pairs=新的内核对(numPairs-1){}
numPairs=GetKerningPairs(hdc、numPairs、pairs)
返回对
其他的
一无所获
如果结束
最后
old=SelectObject(hdc,old)'替换dc中选择的任何对象
结束尝试
终端使用
端函数
副主考人()
对于FontFamily中的每个myFontFamily。Families
尝试
使用f=新字体(myFontFamily,25)
Dim pairs=GetKerningPairs(f)
如果配对不是空的话
Debug.Print(“#Pairs:{0}”,Pairs.Count)
其他的
Debug.Print(“未找到对”)
如果结束
终端使用
特例
Print({1}的错误:{0}),例如Message,myfontflasmily.Name)
结束尝试
下一个
端接头
末级
GetKerningPairs
的第一个参数可能是一个HDC
,但您正在传递一个HFONT
。