获取特定字体配置中文本的实际宽度?

Get actual width of text in a particular font configuration?

我正在打印条形码,作为过程的一部分,我有一个图表对象,上面有一个文本框。

我使用从这里获得的 clsBarcode class 在其上渲染条形码 Generating Code 128 Barcodes using Excel VBA

现在我遇到的问题是无法分辨条码的宽度。

我在该图表对象上生成条形码,然后将图表导出为 jpeg 文件。我一直在为图表对象使用固定大小,但现在我正在尝试打印不同大小的条形码,并且必须调整图表对象以匹配条形码大小,否则它会被剪裁。

我在这里找到了一个 strWidth 函数 http://www.ozgrid.com/forum/showthread.php?t=94339

不幸的是,它对常用字体使用查找 table。 table 中没有 code128.fft 的条目。

所以我有点被困在这里了。如果我只是将图表的大小调整为任何条形码的可能长尺寸,那么我的条形码图像中会浪费很多白色space。由于我将这些条形码打印在 2"x4" 个贴纸上,您可以猜到 space 非常珍贵。

我认为最好的方法是使用 code128 字符的值填充查找 table。条形码class表示字符32到126和200到211正在使用中。

如何计算出这些字符的 mafChrWid(i) 值?

谢谢!

对于此功能,您需要将单元格命名为 BARCODE 并设置其字体 code128.fft。

Function getBarCodeWidth(strBarcode As String) As Double

    With Range("BARCODE")
        .Formula = "=Code128_Str(" & strBarcode & ")"
        .Worksheet.Columns(.Column).AutoFit
        getBarCodeWidth = .Width
    End With

End Function

我不记得我从哪里得到确定字体大小的原始代码。我将其修改为一个易于使用的函数,可用于自动调整文本框的大小以适合其内容。将下面的代码放到它自己的模块中,然后您可以将 getLabelPixel(theControlYouWantToSizeToItsContents) 作为文本框宽度。

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 GetTextExtentPoint32 Lib "gdi32.dll" Alias "GetTextExtentPoint32A" (ByVal hdc As Long, ByVal lpsz As String, ByVal cbString As Long, lpSize As SIZE) 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 Const LOGPIXELSY As Long = 90

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

Private Type SIZE
    cx As Long
    cy As Long
End Type

Public Function getLabelPixel(textBox As Control) As Integer
    Dim font As New StdFont
    Dim sz As SIZE
    font.Name = textBox.FontName
    font.SIZE = textBox.FontSize
    font.Weight = textBox.FontWeight

    sz = GetLabelSize(textBox.Value, font)
    getLabelPixel = sz.cx * 15 + 50   'Multiply this by 15 to get size in twips and +50 to account for padding for access form. .cx is width for font height us .cy
End Function

Private Function GetLabelSize(text As String, font As StdFont) As SIZE
    Dim tempDC As Long
    Dim tempBMP As Long
    Dim f As Long
    Dim lf As LOGFONT
    Dim textSize As SIZE

    ' Create a device context and a bitmap that can be used to store a
    ' temporary font object
    tempDC = CreateDC("DISPLAY", vbNullString, vbNullString, ByVal 0)
    tempBMP = CreateCompatibleBitmap(tempDC, 1, 1)

    ' Assign the bitmap to the device context
    DeleteObject SelectObject(tempDC, tempBMP)

    ' Set up the LOGFONT structure and create the font
    lf.lfFaceName = font.Name & Chr$(0)
    lf.lfHeight = -MulDiv(font.SIZE, GetDeviceCaps(GetDC(0), 90), 72) 
    'LOGPIXELSY
    lf.lfItalic = font.Italic
    lf.lfStrikeOut = font.Strikethrough
    lf.lfUnderline = font.Underline
    lf.lfWeight = font.Weight
    'If font.Bold Then lf.lfWeight = 800 Else lf.lfWeight = 400
    f = CreateFontIndirect(lf)

    ' Assign the font to the device context
    DeleteObject SelectObject(tempDC, f)

    ' Measure the text, and return it into the textSize SIZE structure
    GetTextExtentPoint32 tempDC, text, Len(text), textSize

    ' Clean up (very important to avoid memory leaks!)
    DeleteObject f
    DeleteObject tempBMP
    DeleteDC tempDC
    ' Return the measurements

    GetLabelSize = textSize
End Function