• VB实现长标题文本压缩


    在这里插入图片描述
    Option Explicit
    Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
    End Type
    '将文本描绘到指定的矩形中
    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 Const DT_SINGLELINE = &H20
    Private Const DT_LEFT = &H0
    Private Const DT_VCENTER = &H4
    Private Const DT_VERTICAL = (DT_SINGLELINE Or DT_LEFT Or DT_VCENTER) '垂直居中左对齐
    Dim TxtRect As RECT

    '返回文本函数(文本,矩形,显示位置格式)
    Private Function DrawTextEx(ByVal hDC As Long, ByVal Text As String, BoxRect As RECT, ByVal DisFormat As Long) As Long
    Dim TpyRect As RECT, RectWidth As Long, TetxWidth As Long, TetxLen As Long
    TpyRect = BoxRect
    RectWidth = BoxRect.Right - BoxRect.Left
    If TextWidth(Text) > RectWidth Then
    TetxLen = Len(Text)
    Do While TetxLen > 0
    TetxWidth = TextWidth(Left ( T e x t , T e t x L e n ) ) I f T e t x W i d t h < = R e c t W i d t h − T e x t W i d t h ( " . . . " ) T h e n E x i t D o T e t x L e n = T e t x L e n − 1 L o o p ′ 显示删减后的前段文本 T p y R e c t . L e f t = B o x R e c t . L e f t T p y R e c t . R i g h t = B o x R e c t . L e f t + T e t x W i d t h D r a w T e x t E x = D r a w T e x t ( h D C , L e f t (Text, TetxLen)) If TetxWidth <= RectWidth - TextWidth("...") Then Exit Do TetxLen = TetxLen - 1 Loop '显示删减后的前段文本 TpyRect.Left = BoxRect.Left TpyRect.Right = BoxRect.Left + TetxWidth DrawTextEx = DrawText(hDC, Left (Text,TetxLen))IfTetxWidth<=RectWidthTextWidth("...")ThenExitDoTetxLen=TetxLen1Loop显示删减后的前段文本TpyRect.Left=BoxRect.LeftTpyRect.Right=BoxRect.Left+TetxWidthDrawTextEx=DrawText(hDC,Left(Text, TetxLen), -1, TpyRect, DisFormat)
    '在删减文本后添加"…"
    TpyRect.Left = BoxRect.Left + TetxWidth
    TpyRect.Right = BoxRect.Left + RectWidth
    DrawTextEx = DrawText(hDC, “…”, -1, TpyRect, DisFormat)
    Else
    '当文本宽度<矩形时显示完整文本
    DrawTextEx = DrawText(hDC, Text, -1, TpyRect, DisFormat)
    End If
    End Function

    Private Sub Form_Load()
    Me.AutoRedraw = True: Me.ScaleMode = vbPixels
    End Sub

    Private Sub Form_Resize()
    TxtRect.Left = Me.ScaleLeft
    TxtRect.Top = Me.ScaleTop
    TxtRect.Right = Me.ScaleWidth
    TxtRect.Bottom = Me.ScaleHeight
    Me.Cls
    Call DrawTextEx(Me.hDC, “这里就是要在窗口或其他控件对象上显示的文本”, TxtRect, DT_VERTICAL)
    End Sub

  • 相关阅读:
    Windows本地搭建rtmp推流服务
    Oculus经验记录
    PCL 点云添加标签属性并将带标签的点云保存成文件
    Spring之概念和工作流程
    SpringBoot的Profile配置
    什么是Scrum?Scrum的核心要点和精髓
    安卓的ATV系统
    kotlin语法快速入门-接口与接口实现(8)
    【kali-密码攻击】(5.1.2)密码在线破解:Medusa
    LeetCode每日一题(2438. Range Product Queries of Powers)
  • 原文地址:https://blog.csdn.net/ty5858/article/details/126377071