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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
| Option Explicit
'---Cette partie ne peux pas être raccourcie---
' par contre elle peut être placée dans un module à part
'en changeant les Private par Public
Private Type LOGFONT
lfHeight As Long '
lfWidth As Long
lfEscapement As Long
lfOrientation As Long
lfWeight As Long '
lfItalic As Byte '
lfUnderline As Byte '
lfStrikeOut As Byte '
lfCharSet As Byte
lfOutPrecision As Byte
lfClipPrecision As Byte
lfQuality As Byte
lfPitchAndFamily As Byte
lfFaceName As String * 32 '
End Type
Public Type hv
X As Long
Y As Long
End Type
Private Declare Function CreateDC Lib "gdi32.dll" Alias "CreateDCA" (ByVal lpDriverName As String, ByVal lpDeviceName As String, ByVal lpOutput As String, lpInitData As Long) As Long
Private Declare Function CreateCompatibleBitmap Lib "gdi32.dll" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateFontIndirect Lib "gdi32.dll" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function SelectObject Lib "gdi32.dll" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32.dll" (ByVal hObject As Long) As Long
Private Declare Function MulDiv Lib "kernel32.dll" (ByVal nNumber As Long, ByVal nNumerator As Long, ByVal nDenominator As Long) As Long
Private Declare Function GetDC Lib "user32.dll" (ByVal hwnd As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32.dll" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function DeleteDC Lib "gdi32.dll" (ByVal hdc As Long) As Long
Private Declare Function GetTextExtentPoint32 Lib "gdi32" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, _
ByVal lpsz As String, ByVal cbString As Long, lpSize As hv) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32" (ByVal nIndex As Long) As Long
' code libre d'utilisation et/ou diffusion. Seule obligation : y ajouter la mention suivante :
' ******code provenant du forum VBFrance de Codes-Sources - Auteur : ucfoutu*************
'la fonction :
Private Function dimt(ch As String, pol_Name As String, pol_Size As Long, pol_Bold As Boolean, Optional pol_Italic As Byte, Optional pol_Strikethrough As Byte, Optional pol_Underline As Byte) As hv
Dim cdc As Long, ccb As Long, cfi As Long, lgf As LOGFONT, tch As hv
cdc = CreateDC("DISPLAY", "", "", ByVal 0)
ccb = CreateCompatibleBitmap(cdc, 1, 1)
DeleteObject SelectObject(cdc, ccb)
lgf.lfFaceName = pol_Name & Chr$(0): lgf.lfHeight = -MulDiv(pol_Size, GetDeviceCaps(GetDC(0), 90), 72)
lgf.lfItalic = pol_Italic: lgf.lfStrikeOut = pol_Strikethrough: lgf.lfUnderline = pol_Underline
lgf.lfWeight = 400
If pol_Bold = True Then lgf.lfWeight = lgf.lfWeight * 2
cfi = CreateFontIndirect(lgf)
DeleteObject SelectObject(cdc, cfi)
GetTextExtentPoint32 cdc, ch, Len(ch), tch
DeleteObject cfi: DeleteObject ccb: DeleteDC cdc
dimt = tch
End Function
'l'appel de la fonction :
Sub W_texte()
Dim retx As Long
With Range("B4")
retx = dimt(.Value, .Font.Name, .Font.Size, .Font.Bold, .Font.Italic).X
End With
MsgBox retx
End Sub |
Partager