Results 1 to 8 of 8

Thread: Centering text in a MsgBox

  1. #1

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    498

    Centering text in a MsgBox

    How can I center text in each line of a multi-line MsgBox. My MsgBox's will have either three or four lines.

  2. #2
    Member
    Join Date
    Jul 2019
    Location
    Ahmedabad
    Posts
    32

    Re: Centering text in a MsgBox


  3. #3

    Thread Starter
    Hyperactive Member
    Join Date
    Mar 2017
    Posts
    498

    Re: Centering text in a MsgBox

    I'm on XP so I don't think I have that Task Dialog

  4. #4
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,203

    Re: Centering text in a MsgBox

    Quote Originally Posted by Ordinary Guy View Post
    How can I center text in each line of a multi-line MsgBox. My MsgBox's will have either three or four lines.
    You don't!

    1) create your own Form "frmMessageBox"
    2) Use Task Dialog (as mentioned by Sherin)
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

  5. #5

  6. #6
    Fanatic Member
    Join Date
    Jan 2013
    Posts
    666

    Re: Centering text in a MsgBox

    Quote Originally Posted by Ordinary Guy View Post
    How can I center text in each line of a multi-line MsgBox. My MsgBox's will have either three or four lines.
    Question: I want to do something non-standard with MsgBox.

    Stock Answer: Write your own dialog that does what you want.

    Whilst you could spend [lots of] time making the standard dialog do what you want, it's probably going to be quicker and more maintainable, long term, for you to just write your own.

    I would suggest that is one case where DRY ("Don't wRite Your own") doesn't apply.

    Regards, Phill W.

  7. #7
    Hyperactive Member
    Join Date
    Aug 2017
    Posts
    348

    Re: Centering text in a MsgBox

    One way of centering the MsgBox text is by hooking the creation of the MsgBox and adding the SS_CENTER window style to the Static control that displays the text.


    Name:  Centered MsgBox Text.png
Views: 75
Size:  6.8 KB

    Code:
    Option Explicit 'In a Standard (.BAS) Module
    
    Private Const GWL_STYLE      As Long = (-16&)
    Private Const HCBT_CREATEWND As Long = 3
    Private Const SS_CENTER      As Long = &H1
    Private Const SS_ICON        As Long = &H3
    Private Const WH_CBT         As Long = 5
    
    Private Declare Function CallNextHookEx Lib "user32.dll" (ByVal hHook As Long, ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
    Private Declare Function GetClassNameW Lib "user32.dll" (ByVal hWnd As Long, ByVal lpClassName As Long, ByVal nMaxCount As Long) As Long
    Private Declare Function GetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long) As Long
    Private Declare Function SetWindowLongW Lib "user32.dll" (ByVal hWnd As Long, ByVal nIndex As Long, ByVal dwNewLong As Long) As Long
    Private Declare Function SetWindowsHookExW Lib "user32.dll" (ByVal idHook As Long, ByVal lpfn As Long, ByVal hMod As Long, ByVal dwThreadId As Long) As Long
    Private Declare Function SysReAllocStringLen Lib "oleaut32.dll" (ByVal pBSTR As Long, Optional ByVal pszStrPtr As Long, Optional ByVal Length As Long) As Long
    Private Declare Function UnhookWindowsHookEx Lib "user32.dll" (ByVal hHook As Long) As Long
    Private Declare Sub InitCommonControls Lib "comctl32.dll" ()
    
    Private m_hHook As Long
    
    Private Sub Main()
        InitCommonControls
        Randomize
        App.Title = "Centered MsgBox Text"
    
        Do:  m_hHook = SetWindowsHookExW(WH_CBT, AddressOf CBTProc, 0&, App.ThreadID)
        Loop While MsgBox(GetRandomSentences, GetRandomIcon Or vbRetryCancel) = vbRetry
    End Sub
    
    Private Function CBTProc(ByVal nCode As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
        If nCode = HCBT_CREATEWND Then
            If GetClassName(wParam) = "Static" Then
                CBTProc = GetWindowLongW(wParam, GWL_STYLE)
    
                If (CBTProc And SS_ICON) <> SS_ICON Then
                    SetWindowLongW wParam, GWL_STYLE, CBTProc Or SS_CENTER
                    m_hHook = UnhookWindowsHookEx(m_hHook): Debug.Assert m_hHook
                End If
            End If
        End If
    
        CBTProc = CallNextHookEx(0&, nCode, wParam, lParam)
    End Function
    
    Private Function GetClassName(ByVal hWnd As Long) As String
        Const MAX_CLASS_NAME = 256&
    
        SysReAllocStringLen VarPtr(GetClassName), , MAX_CLASS_NAME
        SysReAllocStringLen VarPtr(GetClassName), StrPtr(GetClassName), _
        GetClassNameW(hWnd, StrPtr(GetClassName), MAX_CLASS_NAME + 1&)
    End Function
    
    Private Function GetRandomIcon() As VbMsgBoxStyle
        GetRandomIcon = Choose(Int(5! * Rnd) + 1!, 0&, vbCritical, vbQuestion, vbExclamation, vbInformation)
    End Function
    
    Private Function GetRandomSentences() As String
        Const MIN_NUMBER_OF_LINES = 2!, MAX_NUMBER_OF_LINES = 10!
        Const MIN_CHARS_PER_LINE = 10!, MAX_CHARS_PER_LINE = 50!
        Const ALPHABET = "abcde fghij klmno pqrst uvwxy z "
        Dim Got1stLetter As Boolean, i As Long, j As Long, sChar As String, sLines() As String
    
        ReDim sLines(1& To Int((MAX_NUMBER_OF_LINES - MIN_NUMBER_OF_LINES + 1!) * Rnd + MIN_NUMBER_OF_LINES)) As String
    
        For i = 1& To UBound(sLines)
            sLines(i) = Space$(Int((MAX_CHARS_PER_LINE - MIN_CHARS_PER_LINE + 1!) * Rnd + MIN_CHARS_PER_LINE))
        Next
    
        For i = 1& To UBound(sLines)
            Got1stLetter = False
    
            For j = 1& To Len(sLines(i))
                Do: sChar = Mid$(ALPHABET, Int(Len(ALPHABET) * Rnd + 1!), 1&)
    
                    If AscW(sChar) <> 32 Then
                        Exit Do
                    ElseIf j > 1& Then
                        If j < Len(sLines(i)) Then
                            If AscW(Mid$(sLines(i), j - 1&)) <> 32 Then Exit Do
                        End If
                    End If                                                  'Do Loop will repeat until 1st & last char
                Loop                                                        'isn't a space and there are no double spaces
    
                If Not Got1stLetter Then
                    If AscW(sChar) <> 32 Then
                        sChar = UCase$(sChar)                               'Capitalize 1st letter
                        Got1stLetter = True
                    End If
                End If
    
                Mid$(sLines(i), j) = sChar
            Next
    
            Debug.Assert 65 <= AscW(sLines(i)) And AscW(sLines(i)) <= 90    'If tripped, 1st char isn't capitalized
            Debug.Assert InStr(sLines(i), "  ") = 0&                        'If tripped, sLines(i) contains double spaces
            Debug.Assert AscW(Right$(sLines(i), 1&)) <> 32                  'If tripped, last char is a space
    
            sLines(i) = sLines(i) & Mid$("!.?", Int(3! * Rnd + 1!), 1&)     'Append either an exclamation point, period or question mark
        Next
    
        GetRandomSentences = Join(sLines, vbNewLine)
    End Function

  8. #8
    PowerPoster Zvoni's Avatar
    Join Date
    Sep 2012
    Location
    To the moon and then left
    Posts
    2,203

    Re: Centering text in a MsgBox

    Nice Victor, but.....

    ....do i really have to say anything?....
    One System to rule them all, One IDE to find them,
    One Code to bring them all, and to the Framework bind them,
    in the Land of Redmond, where the Windows lie
    ---------------------------------------------------------------------------------
    People call me crazy because i'm jumping out of perfectly fine airplanes.
    ---------------------------------------------------------------------------------
    For health reasons i try to avoid reading unformatted Code

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width