Option Explicit
Private Const LF_FACESIZE = 32
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(1 To LF_FACESIZE) As Byte
End Type
Private Type NONCLIENTMETRICS
cbSize As Long
iBorderWidth As Long
iScrollWidth As Long
iScrollHeight As Long
iCaptionWidth As Long
iCaptionHeight As Long
lfCaptionFont As LOGFONT
iSMCaptionWidth As Long
iSMCaptionHeight As Long
lfSMCaptionFont As LOGFONT
iMenuWidth As Long
iMenuHeight As Long
lfMenuFont As LOGFONT
lfStatusFont As LOGFONT
lfMessageFont As LOGFONT
End Type
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
' SubClassing
Private Declare Function SetWindowLong Lib "user32" Alias "SetWindowLongA" ( _
ByVal hwnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
Private Declare Function CallWindowProc Lib "user32" Alias "CallWindowProcA" ( _
ByVal lpPrevWndFunc As Long, ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
Private Const GWL_WNDPROC = (-4)
Private Const WM_NCACTIVATE = &H86
Private Const WM_NCPAINT = &H85
Private lPrevProc As Long
' Positioning & Drawing
Private Declare Function SystemParametersInfo Lib "user32" Alias "SystemParametersInfoA" ( _
ByVal uAction As Long, ByVal uParam As Long, ByRef lpvParam As Any, ByVal fuWinIni As Long) As Long
Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal nIndex As Long) As Long
Private Declare Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function SetTextColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function GetSysColor Lib "user32" (ByVal nIndex As Long) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetWindowDC Lib "user32" (ByVal hwnd 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 CreateFontIndirect Lib "gdi32" Alias "CreateFontIndirectA" (lpLogFont As LOGFONT) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Const SPI_GETNONCLIENTMETRICS = 41
Private Const LOGPIXELSY = 90
Private Const COLOR_ACTIVECAPTION = 2
Private Const COLOR_CAPTIONTEXT = 9
Public sExtraText As String
Private oForm As Form
Private ncm As NONCLIENTMETRICS
Private Function WndProc(ByVal hwnd As Long, ByVal Msg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim wDC As Long, r As RECT, lFont As Long, lOldFont As Long
Dim lWidth As Long, sngCharWidth As Single, sngTextWidth As Single, lNum As Long
WndProc = CallWindowProc(lPrevProc, hwnd, Msg, wParam, lParam)
Select Case Msg
Case WM_NCPAINT, WM_NCACTIVATE
' calculate the area we have to play with (15 is just padding)
lWidth = (oForm.Width \ Screen.TwipsPerPixelX) - 16 - ncm.iCaptionWidth * 3 - ncm.iBorderWidth * 2 - 15
sngCharWidth = oForm.TextWidth(" ") / Screen.TwipsPerPixelX
sngTextWidth = oForm.TextWidth(oForm.Caption & sExtraText) / Screen.TwipsPerPixelX
If lWidth - sngTextWidth > 0 Then
wDC = GetWindowDC(hwnd)
' this should make the back transparent (but doesn't)
SetBkColor wDC, 1& 'GetSysColor(COLOR_ACTIVECAPTION)
SetTextColor wDC, GetSysColor(COLOR_CAPTIONTEXT)
lFont = CreateFontIndirect(ncm.lfCaptionFont)
lOldFont = SelectObject(wDC, lFont)
With r
.Top = ncm.iBorderWidth * 6
.Left = lWidth + 16 - (oForm.TextWidth(sExtraText) / Screen.TwipsPerPixelX)
.Bottom = oForm.TextHeight(sExtraText) ' / Screen.TwipsPerPixelY
.Right = oForm.TextWidth(sExtraText) ' / Screen.TwipsPerPixelX
End With
DrawText wDC, sExtraText, Len(sExtraText), r, 0&
' clear up
SelectObject wDC, lOldFont
DeleteObject lFont
ReleaseDC hwnd, wDC
End If
End Select
End Function
Public Sub AddExtraTextSubClass(ByRef oFrm As Form)
Dim sFont As String, lPos As Long
Set oForm = oFrm
lPrevProc = SetWindowLong(oForm.hwnd, GWL_WNDPROC, AddressOf WndProc)
ncm.cbSize = Len(ncm)
SystemParametersInfo SPI_GETNONCLIENTMETRICS, ncm.cbSize, ncm, 0&
sFont = StrConv(ncm.lfCaptionFont.lfFaceName, vbUnicode)
lPos = InStr(sFont, vbNullChar)
If lPos Then sFont = Left$(sFont, lPos - 1)
oFrm.FontName = sFont
oFrm.FontSize = (-ncm.lfCaptionFont.lfHeight * 72) / GetDeviceCaps(oForm.hdc, LOGPIXELSY)
End Sub
Public Sub UnSubClass(ByRef oFrm As Form)
SetWindowLong oFrm.hwnd, GWL_WNDPROC, lPrevProc
Set oForm = Nothing
End Sub