CodeItBetter Programming Another VB Programming Blog

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

Related posts:

  1. How to scroll the form caption
  2. How to Center the form relative to Screen
  3. How to Center the form relative to MDI form
  4. How to center the form on the screen
  5. How to Align the Form to the Left/Center/Right
  6. How to move a form without a title bar
  7. How to Center align a Control on form
  8. How to Move a Form without using its Title Bar
  9. How to Hide/Show the Title bar at Run-Time
  10. How to Make A Gradient Title Bar

Filed under: Forms Leave a comment
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.