Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Shaun E on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

customize text in a message box

Status
Not open for further replies.

lastout

Programmer
Apr 12, 2002
84
US
I know it's possible to center, bold, italicize, change the font or color of the text in my own custom message boxes (VBA). I read it somewhere a while ago but, now that I want to do it, I can't recall where. So many books, so many websites. Can anyone help me out with this? For instance, if I wanted to center the text "Please try again." MsgBox ????"Please try again."????

lastout (the game's not over till it's over)
 
I don't understand the question fully.

If you're using a VB MsgBox control then you can't easily centre text or control font or colour.

If you have written your own custom MsgBox Control, then it's yours! What properties and methods have you built in?
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
Thanks for replying. The message boxes I want to custom format text in are really simple such as:

MsgBox "Please try again.",,"Title not found."

In this case, Access automatically adds an OK button so I don't have to specify vbOKonly. And that's it. No special properties or anything. I just want it center and maybe change the color of the text to red or change the font to make it more eye-catching. I know it can be done pretty easily but I don't know the keywords or syntax.

Can you give me an example of how you would center the "Please try again" text in the msg above? I think I can probably catch on from there. lastout (the game's not over till it's over)
 
Although the subject has been covered in this forum, I can't locate the thread at present.

Try a search on this forum, or try:

and it's associated pages.
________________________________________________________________
If you want to get the best response to a question, please check out FAQ222-2244 first

'People who live in windowed environments shouldn't cast pointers.'
 
As johnwm suggests, centering text in VB6's MsgBox is not actually easy. It's nearly always easier to write your own MsgBox.

However, here's yet another modification of code I originally presented to change the text in a MsgBox's OK button. You should drop this into a module:
[tt]
Option Explicit
' Necessary constants for hooking
Private Const HCBT_ACTIVATE = 5
Public Const WH_CBT = 5

' Working variables that require global scope in hooking module
Private hHook As Long

' The API declarations we need
Public 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
Public Declare Function UnhookWindowsHookEx Lib "user32" (ByVal hHook As Long) As Long
Private Declare Function GetWindow Lib "user32" (ByVal hwnd As Long, ByVal wCmd As Long) As Long


Private Declare Function GetWindowLong Lib "user32" Alias "GetWindowLongA" (ByVal hwnd As Long, ByVal nIndex As Long) 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 Const GWL_STYLE = (-16)
Private Const ES_CENTER = &H1&

Private Declare Function GetClassName Lib "user32" Alias "GetClassNameA" (ByVal hwnd As Long, ByVal lpClassName As String, ByVal nMaxCount As Long) As Long

Private Const GW_CHILD = 5
Private Const GW_HWNDNEXT = 2

Private Declare Function SetWindowText Lib "user32" Alias "SetWindowTextA" (ByVal hwnd As Long, ByVal lpString As String) As Long

' Our wrapper for the normal MsgBox function
Public Function vbMsgBox(Prompt As String, Optional Buttons As VbMsgBoxStyle = vbOKOnly, Optional Title As String, Optional HelpFile As String, Optional Context As Long) As Long
hHook = SetWindowsHookEx(WH_CBT, AddressOf WinProc, App.hInstance, 0)
vbMsgBox = MsgBox(Prompt, Buttons, Title, HelpFile, Context)
End Function

Private Function WinProc(ByVal lMsg As Long, ByVal wParam As Long, ByVal lParam As Long) As Long
Dim hwndCaption As Long
Dim CurrentStyle As Long
Dim ClassName As String

If lMsg = HCBT_ACTIVATE Then
hwndCaption = GetWindow(wParam, GW_CHILD)
Do Until GetClass(hwndCaption) = "Static"
hwndCaption = GetWindow(hwndCaption, GW_HWNDNEXT)
Loop
CurrentStyle = GetWindowLong(hwndCaption, GWL_STYLE)
SetWindowLong hwndCaption, GWL_STYLE, CurrentStyle Or ES_CENTER
UnhookWindowsHookEx hHook
End If

WinProc = False
End Function


Private Function GetClass(hwnd As Long) As String
Dim strClassName As String
Dim nChars As Long

strClassName = Space(1024)
nChars = Len(strClassName)
GetClassName hwnd, strClassName, nChars
strClassName = Left(strClassName, InStr(strClassName, Chr(0)) - 1)
GetClass = strClassName
End Function
[/tt]
And here's an example of it in use. Add a command button to a form, and drop in the following code:
[tt]
Option Explicit

Private Sub Command1_Click()
'Display the message box
vbMsgBox "Please try again.", , "Title not found."
End Sub
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top