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

时间:2016-08-02 02:01:25

标签: excel vba excel-vba barcode

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

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

现在我遇到的问题是我无法判断条形码的宽度。

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

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

不幸的是,它使用常用字体的查找表。表12中没有code128.fft。

的条目

所以我有点被困在这里。如果我只是将我的图表调整为任何条形码的长度,那么我在条形码图像中会浪费很多空白空间。因为我在2" x4"上打印这些条形码。贴纸,你可以猜到空间是非常宝贵的。

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

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

谢谢!

2 个答案:

答案 0 :(得分:5)

对于此功能,您需要命名一个单元格BARCODE并设置它的字体代码128.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

答案 1 :(得分:1)

我不记得我从哪里得到了确定字体大小的原始代码。我将其修改为易于使用的功能,可用于自动调整文本框的大小以适应其内容。将下面的代码放到它自己的模块中,然后你可以 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