GetKerningPairs (p/invoke)
Bonjour !
J'essaye actuellement d'utiliser les API Win32 avec VB.Net, et plus particulièrement la fonction "GetKerningPairs" qui permet de retrouver toutes les valeurs de kerning d'une police. Le problème est que j'ai toute les peines du monde à écrire ma fonction car je n'ai jamais trop compris comment travailler avec les IntPtr et autres.
J'ai beau chercher partout, impossible de trouver un exemple en VB, ni même en C#.
Voici où j'en suis...
J'ai donc commencé par déclarer les méthodes dont j'ai besoin "GetKerningPairs" et "SelectObject" comme ceci:
Code:
1 2 3 4 5 6 7 8
|
<DllImport("gdi32.dll")> _
Private Shared Function GetKerningPairs(ByVal hdc As IntPtr, ByVal nNumPairs As UInteger, ByRef kernPairs As IntPtr) As UInteger
End Function
<DllImport("gdi32.dll")> _
Private Shared Function SelectObject(ByVal hdc As IntPtr, ByVal hObject As IntPtr) As IntPtr
End Function |
Déjà là, je ne suis pas sûr de mon coup pour le 3ème paramètre de GetKerningPairs. Dois-je lui fournir un IntPtr ou directement un tableau de la structure "KerningPair" ... que voici:
Code:
1 2 3 4 5 6 7 8 9 10 11 12
| <StructLayout(LayoutKind.Sequential)> _
Public Structure KerningPair
Public LeftChar As Char
Public RightChar As Char
Public KerningValue As Integer
Public Sub New(ByVal chrLeft As Char, ByVal chrRight As Char, ByVal kerningVal As Integer)
LeftChar = chrLeft : RightChar = chrRight : KerningValue = kerningVal
End Sub
Public Overrides Function ToString() As String
Return String.Format("{{First={0}, Second={1}, Amount={2}}}", LeftChar, RightChar, KerningValue)
End Function
End Structure |
Et voici donc ma méthode qui pose problème. Je ne vois pas comment extraire des structures "KerningPair" du IntPtr une fois rempli. ET pour tout dire, je ne suis même pas sûr que le début soit bon.
Code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
|
Public Shared Function ExtractKerningPairs(ByVal font As Font) As List(Of KerningPair)
Dim g As Graphics
Dim hdc, hFont, old, kernPairs As IntPtr
Dim numPairs As UInteger
g = Graphics.FromHwnd(IntPtr.Zero)
hdc = g.GetHdc
hFont = font.ToHfont
old = SelectObject(hdc, hFont)
numPairs = GetKerningPairs(hdc, 0, Nothing)
kernPairs = Marshal.AllocHGlobal(CInt(numPairs))
GetKerningPairs(hdc, numPairs, kernPairs)
Dim res As New List(Of KerningPair)
'??????????????????
SelectObject(hdc, old)
Marshal.FreeHGlobal(kernPairs)
g.ReleaseHdc(hdc)
g.Dispose()
Return res
End Function |
Ca fait maintenant bien 3 jours que j'essaye de trouver une alternative, mais en vain. J'essaye aussi de comprendre de façon plus général comment utiliser du code non managé, mais c'est là que je me rends compte de la différence entre un vrai développeur et un bricolo sous .Net :roll:
Merci d'avance pour votre aide.