Home » Forms » How to center caption on Form Title Bar
How to center caption on Form Title Bar
Posted on January 5, 2009
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 | 'Forms - How to center caption on Form Title Bar Option Explicit Public Sub CenterC(frm As Form) Dim SpcF As Integer 'How many spaces can fit Dim clen As Integer 'caption length Dim oldc As String 'oldcaption Dim i As Integer oldc = frm.Caption Do While Left(oldc, 1) = Space(1) DoEvents oldc = Right(oldc, Len(oldc) - 1) Loop Do While Right(oldc, 1) = Space(1) DoEvents oldc = Left(oldc, Len(oldc) - 1) Loop clen = Len(oldc) If InStr(oldc, "!") <> 0 Then If InStr(oldc, " ") <> 0 Then clen = clen * 1.5 Else clen = clen * 1.4 End If Else If InStr(oldc, " ") <> 0 Then clen = clen * 1.4 Else clen = clen * 1.3 End If End If 'see how many characters can fit SpcF = frm.Width / 61.2244 'how many space cam fit in the caption SpcF = SpcF - clen If SpcF > 1 Then DoEvents 'speed up the program frm.Caption = Space(Int(SpcF / 2)) + oldc Else 'if the form is too small for spaces frm.Caption = oldc End If End Sub 'Add the following code in your form Dim oldsize As Long Private Sub Form_Resize() If Me.Width = oldsize Then Exit Sub Else CenterC Me oldsize = Me.Width End If End Sub Private Sub Form_Load() CenterC Me oldsize = Me.Width End Sub |
Enjoy this article?
Filed under: Forms
Leave a comment