Soyez le premier à donner votre avis sur cette source.
Snippet vu 7 347 fois - Téléchargée 21 fois
Option Explicit 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************* Public Const vbahimetric As Double = (1 / 1000) / 2.54 * 1440 Public Const vbamillimeters As Double = (1 / 10) / 2.54 * 1440 Public Const vbacentimeters As Double = 1 / 2.54 * 1440 Public Const vbapoints As Integer = 20 Public Const vbapixels As Integer = 999 ' n'importe quoi sauf les autres valeurs Public Const vbainches As Integer = 1440 Public Const vbatwips As Integer = 1 Public Const vbaCharacters As Integer = 120 Public vba_font As New StdFont Public Type ecran Height As Integer Width As Integer End Type Function vba_ScaleX(nombre_unites As Double, de_echelle As Double, vers_echelle As Double) As Double vba_ScaleX = asy("X", nombre_unites, de_echelle, vers_echelle) End Function Function vba_ScaleY(nombre_unites As Double, de_echelle As Double, vers_echelle As Double) As Double vba_ScaleY = asy("Y", nombre_unites, de_echelle, vers_echelle) End Function Function vba_TwipsperPixelX() As Integer vba_TwipsperPixelX = tpp("X") End Function Function vba_TwipsperPixelY() As Integer vba_TwipsperPixelY = tpp("Y") End Function Function vba_Screen() As ecran vba_Screen.Height = GetSystemMetrics(1) vba_Screen.Width = GetSystemMetrics(0) End Function Public Function vba_TextWidth(texte As String, la_font As StdFont) As Single vba_TextWidth = dimt(texte, la_font).X End Function Public Function vba_TextHeight(texte As String, la_font As StdFont) As Single vba_TextHeight = dimt(texte, la_font).Y End Function '========+++++++++==============+++++++++++============++++++++================== Private Function asy(s As String, Q As Double, U0 As Double, U1 As Double) As Double Dim vbapix As Single, k As Integer, k1 As Integer vbapix = tpp(s) k = IIf(s = "X", 1, 2) asy = IIf(U0 = vbapixels, Q * vbapix, Q * U0) If U0 = vbaCharacters Then asy = asy * k asy = IIf(U1 = vbapixels, asy / vbapix, asy / U1) If U1 = vbaCharacters Then asy = asy / k End Function Private Function dimt(ch As String, ByVal pol As StdFont) 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 Private Function tpp(s As String) As Single Dim axe As Long, gdc As Long axe = IIf(s = "X", 88, 90) gdc = GetDC(0): tpp = vbainches / GetDeviceCaps(gdc, axe) ReleaseDC 0, gdc End Function
10 avril 2012 à 11:32
Pour ne rien te cacher : c'est le seul intérêt réel que j'y trouve également, car pour le reste ... J'estime que Excel, dont la vocation est celle d'un tableur, n'a pas à "faire mumuse" avec d'autres "choses".
Mais pendant qu'on y était et puisque certains paraissaient être intéressés (discussion récente), voilà qui est maintenant fait.
10 avril 2012 à 10:36
Perso, vba_TextWidth me sera d'une grande utilité.
Cool
Vous n'êtes pas encore membre ?
inscrivez-vous, c'est gratuit et ça prend moins d'une minute !
Les membres obtiennent plus de réponses que les utilisateurs anonymes.
Le fait d'être membre vous permet d'avoir un suivi détaillé de vos demandes et codes sources.
Le fait d'être membre vous permet d'avoir des options supplémentaires.