To make things clear from the beginning, by nature this class is used in a very different way than the default message box. The default message box is just a rather simple function that doesn't remember any settings.
This class however can be created and it'll remember the settings you give it. This changes the usage of the message box quite a lot and if you're just looking for Unicode MsgBox function replacement, this does not do that in a syntax compatible way.
Now that this is clear, here are the list of features:
Uses the same message box MsgBox function displays, as Unicode version.
Change button captions!
Cancel, Try Again, Continue (according to Microsoft you should always use this instead of the deprecated Abort, Retry, Ignore)
Customize the icon shown next to the message box by providing a resource icon!
Customize the icon of the window (shown next to the caption).
Change language of the buttons without explicitly changing them individually (this pretty much requires you to enumerate the languages supported by the system, you can't display say Chinese or German buttons if those languages are not supported by the system - however, the feature is still there)
Set modality of the message box: modeless, application level modality, system wide modality or task level modality. Also see Foreground property.
AlwaysOnTop supported. This means you can make the message box appear above everything (but you shouldn't do this unless it is very important or user expects it).
Change text alignment (AlignRight and RightToLeft properties).
One file only! No additional modules etc.
The class does hooking to change the texts of the buttons. This means if you're an advanced coder you can use the hWnd to customize the window further, subclass it if you like and as a result control it much more.
Usage sample:
Code:
Option Explicit
Private Sub Form_Load()
Dim MessageBox As UniMsgBox
Set MessageBox = New UniMsgBox
With MessageBox
.ButtonYes = "Oh yeah!"
.ButtonNo = "NO! NEVER!"
If .Show( _
"Bill Gates is cool?", _
"Just a simple question", _
[Yes / No], _
[Default Button 2], _
[Icon Question] _
) = [Result Yes] Then
.Show "You're hired!", "Windows will not crash anymore", , , [Icon Critical]
Else
.Show "Dear user, the following blue screen is sponsored but not endorsed by Microsoft.", "Revenge!"
End If
End With
Set MessageBox = Nothing
Unload Me
End Sub
If you want a simple Unicode aware replacement of the MsgBox function, here is that too:
Code:
Option Explicit
Private Const MB_USERICON = &H80&
Private Type MsgBoxParams
cbSize As Long
hWndOwner As Long
hInstance As Long
lpszText As Long
lpszCaption As Long
dwStyle As Long
lpszIcon As Long
dwContextHelpId As Long
lpfnMsgBoxCallback As Long
dwLanguageId As Long
End Type
Private Declare Function MessageBoxIndirectW Lib "user32" (lpMsgBoxParams As MsgBoxParams) As Long
' note: I didn't bother to go ahead and start hacking with HelpFile and Context
Public Function MsgBox(ByVal Prompt As String, Optional ByVal Buttons As VbMsgBoxStyle = vbOKOnly, Optional ByVal Title As String, Optional ByVal ResourceIcon As String, Optional ByVal hWndOwner As Long) As VbMsgBoxResult
Dim udtMsgBox As MsgBoxParams
' if no owner is specified, try to use the active form
If hWndOwner = 0 Then If Not Screen.ActiveForm Is Nothing Then hWndOwner = Screen.ActiveForm.hWnd
With udtMsgBox
.cbSize = Len(udtMsgBox)
' important to set owner to get behavior similar to the native MsgBox
.hWndOwner = hWndOwner
.hInstance = App.hInstance
' set the message
.lpszText = StrPtr(Prompt)
' if no title is given, use the application title like the native MsgBox
If LenB(Title) = 0 Then Title = App.Title
.lpszCaption = StrPtr(Title)
' thought this would be a nice feature addition
If LenB(ResourceIcon) = 0& Then
.dwStyle = Buttons
Else
.dwStyle = (Buttons Or MB_USERICON) And Not (&H70&)
.lpszIcon = StrPtr(ResourceIcon)
End If
End With
' show the message box
MsgBox = MessageBoxIndirectW(udtMsgBox)
End Function
It is syntax compatible with MsgBox as long as you haven't used HelpFile and HelpContext. These two have been replaced with resource icon and with a possibility to set message box's owner.
Excelent UniMsgBox!
I use to change Msgbox Button Caption it is very small.
Can you send an sample-Attached Files how to load res file?...and how to use all other properties.
I tried with icon resource....but....something not work.
Thank you
Option Explicit
' Icon extraction for UniMsgBox sample
Private Declare Function DestroyIcon Lib "user32" (ByVal hIcon As Long) As Long
Private Declare Function ExtractIcon Lib "shell32.dll" Alias "ExtractIconA" (ByVal hInst As Long, ByVal lpszExeFileName As String, ByVal nIconIndex As Long) As Long
Private Sub Command1_Click()
Dim Test As New UniMsgBox, lngIcon As Long
Test.AlwaysOnTop = True
lngIcon = ExtractIcon(App.hInstance, "shell32.dll", 46)
Test.SetButtons , , , , "Restart &Later", , , , "Restart &Now"
If Test.Show("Updating your computer is almost complete. You must restart your computer for the updates to take effect." & _
vbNewLine & vbNewLine & "Do you want to restart your computer now?", "Automatic Updates", _
[Yes / No], [Default Button 1], [Icon None], lngIcon) = [Result Yes] Then
MsgBox "Oh behave!"
End If
If lngIcon Then DestroyIcon lngIcon
End Sub
Edit
Wrong icon, I guess. This sets the icon of the window, not the icon to be displayed alongside the message.
Ok! Thank you....But my question still remain!
Can you send me a sample HOW TO SET ICON DISPAYED ALONGSIDE THE MESSAGE?
Not icon of the windows is my problem, but message icon.
HOW CAN I SET THIS ICON?
If I want to use "Simple msgbox function" that can change the text of the button, for ex: OK = "Đồng ý".
Then how can I re-write the "simple msgbox function" above.