在场景中输出横向或纵向压缩的中文字符

该博客展示了一段参考外文代码编写的VBA代码。代码中定义了常量和结构体,声明了多个函数,实现了将OLE StdFont转换为LOGFONT结构,进而创建字体句柄并进行文本打印的功能。还通过按钮点击事件调用函数,设置不同字体样式打印文本。

    今天参考一个外文代码写的:

(作者:Steve McMahon   steve@vbaccelerator.com,

网址:  http://www.shitalshah.com/vbxlr/tips/vba0035.htm )

 

Private Const LF_FACESIZE = 32
Private Const FW_NORMAL = 400
Private Const FW_BOLD = 700
Private Const FF_DONTCARE = 0
Private Const DEFAULT_QUALITY = 0
Private Const DEFAULT_PITCH = 0
Private Const DEFAULT_CHARSET = 1
Private Const DT_CALCRECT = &H400
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(LF_FACESIZE) As Byte
End Type
Private Type RECT
   Left As Long
   Top As Long
   Right As Long
   Bottom As Long
End Type

 

Private Declare Function CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function DrawText Lib "user32" Alias "DrawTextA" (ByVal hdc As Long, ByVal lpStr As String, ByVal nCount As Long, lpRect As RECT, ByVal wFormat As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function OffsetRect Lib "user32" (lpRect As RECT, ByVal x As Long, ByVal y As Long) As Long

 


Private Sub printtext(ByVal hdc As Long, ByVal mystr As String, myfont As StdFont, Optional ByVal fontwidth As Integer = 30, Optional ByVal fontheight As Integer = 15, Optional ByVal fontbold As Boolean = False, Optional ByVal fontitlaic As Boolean = False, Optional ByVal fontunderline As Boolean = False, Optional ByVal fontStrikethrough As Boolean = False)

 

Dim tLF As LOGFONT
Dim hFnt As Long
Dim hFntOld As Long
Dim tR As RECT
Dim sFont As String
Dim iChar As Integer
Dim temp() As Byte

 

   ' Convert an OLE StdFont to a LOGFONT structure:
   With tLF
     sFont = myfont.Name
     temp = StrConv(sFont, vbFromUnicode)
     For iChar = 1 To Len(sFont)
       .lfFaceName(iChar - 1) = temp(iChar - 1)
     Next iChar
     ' Based on the Win32SDK documentation:
        .lfItalic = myfont.Italic
      lfWeight = IIf(myfont.Bold, FW_BOLD, FW_NORMAL)
      .lfWidth = fontwidth
     .lfHeight = fontheight
     .lfUnderline = fontunderline
     .lfStrikeOut = fontStrikethrough
     .lfCharSet = myfont.Charset
   End With

 


 
   hFnt = CreateFontIndirect(tLF)  ' Convert the LOGFONT into a font handle

 

   ' Test the font out:
   hFntOld = SelectObject(hdc, hFnt)
   DrawText hdc, mystr, -1, tR, DT_CALCRECT
   OffsetRect tR, 32, 32
   DrawText hdc, mystr, -1, tR, 0&
   SelectObject hdc, hFntOld

 

   '  remember to delete the font when finished
  
   DeleteObject hFnt

 

End Sub

 

Private Sub Command1_Click()
Me.Cls
Dim myfont As New StdFont
myfont.Name = "arial"
printtext Me.hdc, "扁扁的几个字", myfont, 50, 20

 

End Sub

 

Private Sub Command2_Click()
Dim myfont As New StdFont
myfont.Name = "arial"
printtext Me.hdc, "修长的几个字", myfont, 10, 200, True, True, False, False
End Sub

 

 

 

输出:



 

<think>嗯,用户需要寻找适用于QR码的窄长字体,特别是纵向狭窄且横向较短的字体。首先,我需要理解QR码的应用场景。QR码通常需要高密度编码,所以字体不能占用太多空间,同时要保证可读性,尤其是在小尺寸打印时。 用户提到的“纵向狭窄且横向较短”可能意味着字体在垂直方向紧凑,水平方向字符宽度较窄,这样可以在有限的空间内显示更多信息。比如,标签小票上的QR码旁边可能需要这样的字体来显示产品信息URL。 接下来,我需要考虑哪些字体符合这些特征。常见的等宽字体如Courier New可能不够紧凑,可能要找专门设计的窄体字体。比如OCR-B虽然用于光学识别,但可能不适合狭窄空间。可能需要推荐更紧凑的无衬线字体,比如ImpactArial Narrow,这些字体设计上比较紧凑,适合节省空间。 另外,用户可能还需要字体支持高可读性,特别是在小字号时。比如DIN Condensed和Roboto Condensed是专门设计的紧缩字体,适合这种情况。特别是DIN Condensed,常用于需要高空间利用率的场合,如交通标志工业标签。 还要考虑免费和商用授权的问题。比如Roboto Condensed是Apache许可证,可以免费使用。而有些字体可能需要购买许可,比如Helvetica Condensed,但用户可能更倾向于免费选项。 用户可能没有明确说明是否需要支持特殊字符,但通常QR码旁的文字可能包含字母、数字和常见符号,所以字体需要覆盖这些字符集。此外,字体是否易于嵌入到生成QR码的工具软件中也很重要,比如使用CSS的font-family属性编程库中的字体支持。 最后,可能需要提醒用户在实际使用前测试字体的可读性,尤其是在不同打印尺寸和分辨率下,确保文字清晰可辨,不影响QR码的扫描。</think>### 适用于QR码的窄长字体推荐 QR码通常需要配合紧凑的字体以节省空间并保持可读性,尤其是在狭窄的纵向布局中。以下是符合需求的字体推荐及选择依据: #### 1. **DIN Condensed** - **特点**:专为高密度排版设计,纵向高度紧凑,横向字符宽度窄,适合小尺寸打印[^1]。 - **应用场景**:工业标签、二维码旁的产品编码。 - **授权**:部分版本免费商用(如DIN 2014 Condensed)。 #### 2. **Roboto Condensed** - **特点**:Google设计的无衬线字体,横向压缩明显,支持多种语言字符,适合数字和字母的密集排列。 - **开源**:Apache License 2.0,可免费商用[^2]。 #### 3. **Arial Narrow** - **特点**:Windows系统内置字体,横向宽度比标准Arial减少约20%,兼容性高。 - **局限性**:需确保使用环境已安装该字体。 #### 4. **OCR-B (压缩版本)** - **特点**:专为机器识别优化,部分改造版本(如OCR-B Narrow)保留高可读性同时缩小横向间距。 - **适用性**:适合需要兼顾人工阅读和扫描的场景。 #### 5. **Impact** - **特点**:粗体窄设计,纵向占用空间小,适合标题类短文本。 - **注意**:过度压缩可能影响小字号清晰度。 --- ### 选择建议 1. **测试可读性**:在小字号(如8pt以下)打印样本,确保字符(如`1`、`l`、`I`)无混淆。 2. **编码兼容性**:确认字体支持所需字符集(如特殊符号非拉丁字母)。 3. **工具适配**:在生成QR码的工具中(如Python的`qrcode`库),通过CSS配置指定字体: ```python # 示例:使用Roboto Condensed生成带文本的QR码(需环境支持) import qrcode img = qrcode.make("DATA") img.save("qrcode.png") ``` ```css /* 在HTML/CSS中嵌入字体 */ .qr-text { font-family: 'Roboto Condensed', sans-serif; font-size: 6px; } ``` ---
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值