How to change the UserForm Caption Font

erik.van.geit

MrExcel MVP
Joined
Feb 1, 2003
Messages
17,832
Hi, guys and galls,

Please do the following.
Create a Userform
Change the FONT property: name or size or both

What do you expect? I thought that the caption font and size would change. But nothing happens. :confused:

Perhaps I am missing the obvious... What's the FONT property made for then?

If someone can help, that would be nice.

kind regards,
Erik

PS: some code I played with
Code:
Private Sub UserForm_Click()

Me.Font.Size = 10
Me.Font.Name = "Trebuchet MS"
MsgBox Me.Font.Name & vbNewLine & Me.Font.Size
 
Me.Font.Size = 40
Me.Font.Name = "Arial"
MsgBox Me.Font.Name & vbNewLine & Me.Font.Size

End Sub
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Hi Erik,

The userform's font is used to determine the default font properties of any controls you add onto the userform. (See 'Font Object' topic in the VBA helpfiles).

Colin
 
Upvote 0
Hi Erik,

In my opinion you can use a little technique with macro. First make UserForm with No Title Bar then make your own Title Bar with Label control.
 
Upvote 0
Colin,
Thank you for your answer. Indeed, that's it: I've never noticed that, even after creating hundreds of forms. Each day we learn something new.

Sahak,
thanks for your answer. I know that code, but that's quite a lot for such a simple project I'm working at, I would use other alternatives.

So it would seem that we can not change the UserForm Caption Font using "simple techniques" ? Strange that this option is not available?

best regards,
Erik
 
Upvote 0
Hi Erik,

No it can't be changed with native Excel functionality - it's not an Excel setting - but perhaps an API expert would find a way - maybe there's a solution out there to be found using Google? On your PC you can change it by going into the control panel and playing with the Display Properties.

Colin
 
Upvote 0
Thanks, Colin,

API solutions were already in my library. I googled a lot before asking the question and didn't find anything "simple" + useful.
One of my basic rules is that the users settings may not be changed: I don't ever change the Excel menu functionalities directly, certainly not the windows settings.

Good that you confirmed that
it's not an Excel setting
Back to the drawing board :-)

Thank you for your contribution.
Erik
 
Upvote 0
Hi all,

Old thread but found this challenging as nowhere on the internet could I find a solution apart from the little hack of removing the whole title bar of the userform and faking one with a label.

As a result of some stubborn trying and some nasty application crashing , I seem to have achieved a rather nice looking/functionning userform with a formatted title bar and a formatted caption text.

here is a WORKBOOK DEMO.

Here is the main code in a standard module :

Code:
Option Explicit
 
Private Type POINTAPI
    x As Long
    y As Long
End Type
 
Private Type RECT
    Left As Long
    Top As Long
    Right As Long
    Bottom As Long
End Type
 
Private Type LOGBRUSH
    lbStyle As Long
    lbColor As Long
    lbHatch As Long
End Type
 
Private Type PAINTSTRUCT
    hdc As Long
    fErase As Long
    rcPaint As RECT
    fRestore As Long
    fIncUpdate As Long
    rgbReserved(32) As Byte
End Type
 
 
Private Type LOGFONT
    lfHeight As Long
    lfWidth As Long
    lfEscapement As Long
    lfOrientation As Long
    lfWeight As Long
    lfItalic As String * 1
    lfUnderline As String * 1
    lfStrikeOut As String * 1
    lfCharSet As String * 1
    lfOutPrecision As String * 1
    lfClipPrecision As String * 1
    lfQuality As String * 1
    lfPitchAndFamily As String * 1
    lfFaceName As String * 32
End Type
 
Private Declare Function CreateFontIndirect Lib "gdi32" _
Alias "CreateFontIndirectA" _
(lpLogFont As LOGFONT) As Long
 
Private Declare Function GetWindowDC Lib "user32" _
 (ByVal hwnd As Long) As Long
 
Private Declare Function SendMessage Lib "user32.dll" _
Alias "SendMessageA" _
(ByVal hwnd As Long, _
ByVal wMsg As Long, _
ByVal wParam As Long, _
ByRef lParam As Any) As Long
 
Private Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, _
ByVal hdc As Long) As Long
 
Private Declare Function TextOut Lib "gdi32" _
Alias "TextOutA" _
(ByVal hdc As Long, _
ByVal x As Long, _
ByVal y As Long, _
ByVal lpString As String, _
ByVal nCount As Long) As Long
 
Private Declare Function SetBkMode Lib "gdi32" _
(ByVal hdc As Long, _
ByVal nBkMode As Long) As Long
 
Private Declare Function SelectObject Lib "gdi32" _
(ByVal hdc As Long, _
ByVal hObject As Long) As Long
 
Private Declare Function SetWindowsHookEx Lib _
"user32" Alias "SetWindowsHookExA" _
(ByVal idHook As Long, _
ByVal lpfn As Long, _
ByVal hmod As Long, _
ByVal dwThreadId As Long) As Long
 
Private Declare Function UnhookWindowsHookEx Lib "user32" _
(ByVal hHook As Long) As Long
 
Private Declare Function CallNextHookEx Lib "user32" _
(ByVal hHook As Long, _
ByVal ncode As Long, _
ByVal wParam As Long, _
lParam As Any) As Long
 
Private Declare Function GetCurrentThreadId Lib "kernel32" _
() As Long
 
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 Declare Function GetClassName Lib "user32" _
Alias "GetClassNameA" _
(ByVal hwnd As Long, _
ByVal lpClassName As String, _
ByVal nMaxCount As Long) As Long
 
Private Declare Function DeleteObject Lib "gdi32" _
(ByVal hObject As Long) As Long
 
Private Declare Function SetTextColor Lib "gdi32" _
(ByVal hdc As Long, _
ByVal crColor As Long) As Long
 
Private Declare Function CreateBrushIndirect Lib "gdi32" _
(lpLogBrush As LOGBRUSH) As Long
 
Private Declare Function FillRect Lib "user32.dll" _
(ByVal hdc As Long, _
ByRef lpRect As RECT, _
ByVal hBrush As Long) As Long
 
Private Declare Function SetRect Lib "user32" _
(lpRect As RECT, _
ByVal X1 As Long, _
ByVal Y1 As Long, _
ByVal X2 As Long, _
ByVal Y2 As Long) As Long
 
Private Declare Function GetWindowRect Lib "user32.dll" _
(ByVal hwnd As Long, _
ByRef lpRect As RECT) As Long
 
Private Declare Function BeginPaint Lib "user32.dll" _
(ByVal hwnd As Long, _
ByRef lpPaint As PAINTSTRUCT) As Long
 
Private Declare Function EndPaint Lib "user32.dll" _
(ByVal hwnd As Long, _
ByRef lpPaint As PAINTSTRUCT) As Long
 
Private Declare Function DeleteDC Lib "gdi32" _
(ByVal hdc As Long) As Long
 
Private Declare Function RedrawWindow Lib "user32" _
(ByVal hwnd As Long, _
lprcUpdate As Any, _
ByVal hrgnUpdate As Long, _
ByVal fuRedraw As Long) As Long
 
Private Declare Function InvalidateRect Lib "user32.dll" _
(ByVal hwnd As Long, _
ByVal lpRect As Long, _
ByVal bErase As Long) As Long
 
Private Declare Function SetCursorPos Lib "user32.dll" _
(ByVal x As Long, _
ByVal y As Long) As Long
 
Private Declare Function GetCursorPos Lib "user32.dll" _
(ByRef lpPoint As POINTAPI) As Long
 
Private Declare Function GetSystemMetrics Lib "user32" _
(ByVal nIndex As Long) As Long

Private Const WH_CBT As Long = 5
Private Const HCBT_ACTIVATE As Long = 5
Private Const GWL_WNDPROC As Long = -4
 
Private Const WM_ACTIVATE As Long = &H6
Private Const WM_PAINT As Long = &HF&
Private Const WM_SHOWWINDOW As Long = &H18
Private Const WM_EXITSIZEMOVE As Long = &H232
Private Const WM_DESTROY As Long = &H2
 
Private Const RDW_INTERNALPAINT As Long = &H2
 
Private Const SM_CXSCREEN As Long = 0
Private Const SM_CYSCREEN As Long = 1
Private Const SM_CYCAPTION As Long = 4
 
Private tPoint As POINTAPI
Private tRect As RECT
Private lPrevWnd As Long
Private lhHook As Long
Private bHookEnabled As Boolean
Private oForm As Object
Private sCaptionText As String
Private lCaptionColour As Long
Private lFontSize As Long
Private lFontColour As Long
Private bBold As Boolean

Sub HookUserForm _
 (ByVal Form, ByVal CaptionColour, _
 ByVal FontColour, ByVal FontSize, ByVal Bold)
 
    'install a cbt hook to monitor for
    'the activation of a window.
    If Not bHookEnabled Then
        'store parms in mod level vars.
        Set oForm = Form
        sCaptionText = Form.Caption
        lCaptionColour = CaptionColour
        lFontColour = FontColour
        lFontSize = FontSize
        bBold = Bold
        lhHook = SetWindowsHookEx _
        (WH_CBT, AddressOf HookProc, 0, GetCurrentThreadId)
        bHookEnabled = True
        'show userform.
        Form.Show
    Else
        MsgBox "The hook is already set.", vbInformation
    End If
 
End Sub
 
Private Sub TerminateHook()
 
   'important to unhook when done!
    UnhookWindowsHookEx lhHook
    bHookEnabled = False
 
End Sub
 
Private Function HookProc _
(ByVal idHook As Long, ByVal wParam As Long, _
ByVal lParam As Long) As Long
 
    Dim sBuffer As String
    Dim lRetVal As Long
    
    'check if a window has been activated.
    If idHook = HCBT_ACTIVATE Then
    
    'if so,get it's class name.
    sBuffer = Space(256)
    lRetVal = GetClassName(wParam, sBuffer, 256)
    
    'check if it is an xl userform window
    'that is being activated.
    
    If Left(sBuffer, lRetVal) = "ThunderDFrame" Or _
    Left(sBuffer, lRetVal) = "ThunderXFrame" Then
    
    'if so,subclass it .
    lPrevWnd = SetWindowLong _
    (wParam, GWL_WNDPROC, AddressOf CallBackProc)
    
    'done. so remove CBT hook.
    Call TerminateHook
    End If
    
    End If
    
    'Call next hook.
    HookProc = CallNextHookEx _
    (lhHook, idHook, ByVal wParam, ByVal lParam)
 
End Function
 
Private Function CallBackProc _
(ByVal hwnd As Long, ByVal Msg As Long, _
ByVal wParam As Long, ByVal lParam As Long) As Long
 
    Dim lDc As Long
    
    On Error Resume Next
    'get current userform position.
    GetWindowRect hwnd, tRect
    Select Case Msg
        Case WM_PAINT, WM_ACTIVATE
            If Msg = WM_ACTIVATE Then
                Call MoveCursor
            End If
            lDc = GetWindowDC(hwnd)
            Call DrawTitleBar(hwnd, lCaptionColour)
            SetBkMode lDc, 1
            SetTextColor lDc, lFontColour
            CreateFont lDc, Bold:=bBold
            TextOut lDc, 6, GetSystemMetrics(SM_CYCAPTION) / 3, _
            sCaptionText, Len(sCaptionText)
            ReleaseDC hwnd, lDc
            InvalidateRect hwnd, 0, 0
        Case WM_EXITSIZEMOVE, WM_SHOWWINDOW
            If Msg = WM_SHOWWINDOW Then
                SetCursorPos tPoint.x, tPoint.y
            End If
            SendMessage hwnd, WM_PAINT, 0, 0
            Call MoveCursor
        Case WM_DESTROY
            SetWindowLong hwnd, GWL_WNDPROC, lPrevWnd
    End Select
    
    'process other msgs.
    CallBackProc = CallWindowProc _
    (lPrevWnd, hwnd, Msg, wParam, ByVal lParam)
 
End Function
 
Private Sub CreateFont(DC As Long, Optional Bold As Boolean)
 
    Dim uFont As LOGFONT
    Dim lNewFont As Long
    
    With uFont
        .lfFaceName = "Tahoma" & Chr$(0)
        .lfHeight = lFontSize
        .lfWidth = 8
        .lfWeight = IIf(Bold, 900, 100)
    End With
    
    lNewFont = CreateFontIndirect(uFont)
    DeleteObject (SelectObject(DC, lNewFont))
 
End Sub
 
Private Sub DrawTitleBar _
(lhwnd As Long, Color)
 
    Dim tPS As PAINTSTRUCT
    Dim tLB As LOGBRUSH
    Dim tR As RECT
    Dim lDc As Long
    Dim l As Long
    Dim hBrush As Long
 
    BeginPaint lhwnd, tPS
    lDc = GetWindowDC(lhwnd)
    tLB.lbColor = Color
    'Create a new brush
    hBrush = CreateBrushIndirect(tLB)
    
    With oForm
        SetRect tR, 0, 0, GetSystemMetrics _
        (SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN)
    End With
    'Fill the form with our brush
'    FillRect lDc, tR, hBrush
      FillRect lDc, tR, hBrush
    
    Call DeleteObject(hBrush)
    RedrawWindow lhwnd, ByVal 0&, ByVal 0&, RDW_INTERNALPAINT
    DeleteDC lDc
    Call EndPaint(lhwnd, tPS)
 
End Sub

Private Sub DrawCloseButton()
 
    DoEvents
    SetCursorPos tPoint.x, tPoint.y
    
End Sub
 
Private Sub MoveCursor()
 
    GetCursorPos tPoint
    SetCursorPos tRect.Right - 15, tRect.Top + 15
    Application.OnTime Now + TimeSerial(0, 0, 0.1), _
    "DrawCloseButton"
 
End Sub
 
'=========Wrapper function===============.
Sub ShowFormatedUserForm( _
 _
    ByVal Form As Object, _
    ByVal CaptionColor As Long, _
    Optional ByVal FontColour As Long = vbWhite, _
    Optional ByVal FontSize As Long = 12, _
    Optional ByVal Bold As Boolean = False)
        Call HookUserForm _
 _
            (ByVal Form, _
            ByVal CaptionColor, _
            ByVal FontColour, _
            ByVal FontSize, _
            ByVal Bold)
End Sub

And below is an example of how to format a userform via the above wrapper function :

Code:
Sub Test()
 
Call ShowFormatedUserForm( _
 _
    Form:=UserForm1, _
    CaptionColor:=vbMagenta, _
    FontColour:=vbYellow, _
    FontSize:=14, _
    Bold:=True)

End Sub

Tested on Win XP sp2 excel 2003 only.

Regards.
 
Upvote 0
Jafar, THAT! looks amazing. I am not even going to start trying to pretend I'm actually understanding what you are doing and I haven't tested it yet - but from looking at the ShowFormatedUserForm sub I would say one property is still missing - Font type :)
 
Upvote 0
Jafar, THAT! looks amazing. I am not even going to start trying to pretend I'm actually understanding what you are doing and I haven't tested it yet - but from looking at the ShowFormatedUserForm sub I would say one property is still missing - Font type :)

Yes I shouldn't have omitted the Font type . I guess one could easily and conviniently store the Font names in an UDT at the top of the Main code module and define the Font type parameter as the UDT type so when passing the font type argument in the wrapper Sub, the VBA Intellsense displays all the font names for easy editing.

Regards.
 
Upvote 0
I guess one could easily and conviniently store the Font names in an UDT at the top of the Main code module and define the Font type parameter as the UDT type

And? Will you? - I certainly haven't got a clue on how to go about that (and only the foggiest of what you are talking about) ... :-D
 
Upvote 0

Forum statistics

Threads
1,223,230
Messages
6,170,883
Members
452,364
Latest member
springate

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top