Centering the text in a message box

  • Thread starter Thread starter Rob_T
  • Start date Start date
R

Rob_T

Does anyone know if it's possible to center align the text in a standar
VB message box? Currently it's all aligned to the right and I can't se
any way of changing it.

It's not an important thing, I just think it would make the messag
look neater :rolleyes:

Any help appreciated.

Ro
 
Hi Rob_T,
Private Declare Function GetCurrentThreadId& Lib "kernel32" ()
Private Declare Function GetClassName& Lib "user32" Alias _
"GetClassNameA" (ByVal hwnd&, ByVal lpClassName$, ByVal nMaxCount&)
Private Declare Function UnhookWindowsHookEx& Lib "user32" (ByVal hHook&)
Private Declare Function SetWindowsHookEx& Lib "user32" Alias
"SetWindowsHookExA" _
(ByVal idHook&, ByVal lpfn&, ByVal hmod&, ByVal dwThreadId&)
Private Declare Function SetWindowLong& Lib "user32" Alias "SetWindowLongA"
_
(ByVal hwnd&, ByVal nIndex&, ByVal dwNewLong&)
Private Declare Function IsWindow& Lib "user32" (ByVal hwnd&)
Private Declare Function GetWindowText& Lib "user32" Alias "GetWindowTextA"
_
(ByVal hwnd&, ByVal lpString$, ByVal cch&)
Private Type CWPSTRUCT
lParam As Long
wParam As Long
Message As Long
hwnd As Long
End Type
Private Const WM_CREATE& = &H1
Private Const GWL_STYLE& = (-16) ' GetWindowLong
Private Const SS_LEFT& = &H0& ' Static control left alignment
Private Const SS_CENTER& = &H1& ' Static control center
alignment
Private Const SS_RIGHT& = &H2& ' Static control right alignment
Private Const WH_CALLWNDPROC& = 4 ' SetWindowsHookEx (Hook type)
Private msgHook&

Sub SpecialMsgBox()
msgHook = SetWindowsHookEx(WH_CALLWNDPROC, AddressOf HookMsg, 0,
GetCurrentThreadId)
MsgBox "For your information," & vbLf _
& "the text of this message box" & vbLf & "is centered !", 64, "User info"
End Sub

Private Function HookMsg&(ByVal nCode&, ByVal wParam&, msgStruct As
CWPSTRUCT)
If msgStruct.Message <> WM_CREATE Then Exit Function
Dim hwnd&: Static z&
hwnd = msgStruct.hwnd
Select Case GetWindowClass(hwnd)
Case "#32770": z = z + 1 '// Windows-Dialogs Class
Case "static" '// Windows-Labels Class
If HasText(hwnd) Then SetWindowLong hwnd, GWL_STYLE, SS_CENTER
z = z + 1
Case "button": z = z + 1 '// Windows-Buttons Class
End Select
If z = 4 Then Call UnhookWindowsHookEx(msgHook): z = 0
End Function

Private Function GetWindowClass$(ByVal hwnd&)
If IsWindow(hwnd) Then
Dim Buffer$, i%
Buffer = String(255, 0)
GetClassName hwnd, Buffer, 256
i = InStr(Buffer, Chr(0))
Buffer = IIf(i, Left(Buffer, i - 1), Buffer)
GetWindowClass = LCase(Buffer)
End If
End Function

Private Function HasText&(ByVal hwnd&)
Dim Buffer As String
Buffer = String(100, Chr$(0))
GetWindowText hwnd, Buffer, 100
HasText = Len(Left$(Buffer, InStr(Buffer, Chr$(0)) - 1))
End Function

Regards,
MP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top