[API]
RECT
CreateCompatibleDC
DeleteDC
SelectObject
AdjustWindowRect

[INCLUDE]
DrawText
SendMessage

[CODE]
Public Sub AutoSizeXBox_Resize(ByVal hwnd As Long, ByVal Caption As String, ByVal xMaxWidth As Long, ByVal xHeight As Long)
  Dim xType1 As RECT
  Dim xType2 As RECT
  Dim hFont As Long
  Dim hFontOld As Long
  Dim hdc As Long
  Dim xLong1 As Long
  Dim xSp As Long
  Const XBoxSp As Long = 4&
  
  xSp = XBoxSp + xHeight
  
  hdc = CreateCompatibleDC(0&)
  If Not hdc = 0& Then
    hFont = SendMessage(hwnd, &H31&, 0&, 0&)
    If Not hFont = 0& Then Call SelectObject(hdc, hFont)
    xType1.Right = &H3FFF&
    xType1.Bottom = &H3FFF&
    If Not DrawText(hdc, Caption, xType1, &H420&) = 0& Then
      xType2 = xType1
      If Not AdjustWindowRect(xType2, GetWindowLong(hwnd, -16&), 0&) = 0& Then
        xLong1 = xType1.Right + xSp - xType1.Left
        If xLong1 > xMaxWidth Then 
          xLong1 = xMaxWidth
          #If AutoSizeXBox_CanEllipsis And SetWindowText And Ellipsis_TextEnd Then
            Caption = Ellipsis_TextEnd(hdc, Caption, xMaxWidth - xSp + xType1.Right - (xType2.Right - xType2.Left))
            #End If
          EndIf
        Call SetWindowPos(hwnd, 0&, 0&, 0&, xLong1, xHeight, &H216&)
        #If AutoSizeXBox_CanEllipsis And SetWindowText And Ellipsis_TextEnd Then
          Call SetWindowText(hwnd, Caption)
          #End If
        End If
      End If
    If Not hFontOld = 0& Then Call SelectObject(hdc, hFontOld)
    Call DeleteDC(hdc)
    End If
  End Sub