Results 1 to 12 of 12

Thread: VB6 UniMsgBox - Unicode message box class

  1. #1

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    VB6 UniMsgBox - Unicode message box class

    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
    Attached Files Attached Files
    Last edited by Merri; Aug 1st, 2010 at 06:00 PM.

  2. #2

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: VB6 UniMsgBox - Unicode message box class

    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.
    Last edited by Merri; Oct 12th, 2008 at 02:50 AM.

  3. #3
    Junior Member
    Join Date
    Sep 2008
    Posts
    26

    Re: VB6 UniMsgBox - Unicode message box class

    Nice, I'm using second one, it really good and handy!!!

  4. #4
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: VB6 UniMsgBox - Unicode message box class

    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

  5. #5

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: VB6 UniMsgBox - Unicode message box class

    A resource icon will only work with a compiled application.

  6. #6
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: VB6 UniMsgBox - Unicode message box class

    Thank you for quick answer. I never use resource file until now.
    Can you send me a sample how to display an icon from resource?

    Than you!

  7. #7

    Thread Starter
    VB6, XHTML & CSS hobbyist Merri's Avatar
    Join Date
    Oct 2002
    Location
    Finland
    Posts
    6,654

    Re: VB6 UniMsgBox - Unicode message box class

    I have this piece of code at http://kontu.selfip.info/vb6/projects/Unicode – extracted from the main project.

    Code:
    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.
    Last edited by Merri; Nov 22nd, 2008 at 08:14 AM.

  8. #8
    Lively Member
    Join Date
    Feb 2006
    Posts
    96

    Re: VB6 UniMsgBox - Unicode message box class

    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?

  9. #9
    Frenzied Member Jim Davis's Avatar
    Join Date
    Mar 2001
    Location
    Mars base one Username: Jim Davis Password: yCrm33
    Posts
    1,284

    Re: VB6 UniMsgBox - Unicode message box class

    If you want a simple Unicode aware replacement of the MsgBox function, here is that too
    Yay Excellent! Thanks a lot.

  10. #10
    Lively Member
    Join Date
    Feb 2009
    Posts
    72

    Re: VB6 UniMsgBox - Unicode message box class

    Hi Merri,

    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.

    Tks,

    LVD

  11. #11
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: VB6 UniMsgBox - Unicode message box class

    Hi Merri,
    How to change the button font and font size
    Tks!

  12. #12
    New Member
    Join Date
    Jan 2012
    Posts
    2

    Re: VB6 UniMsgBox - Unicode message box class

    Hi Merri,
    How to change the button font and font size
    Tks!

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