To register for an Internet.com membership to receive newsletters and white papers, use the Register button ABOVE.
To participate in the message forums BELOW, click here
VBForums  

VB Wire News
MSDN Subscribers: Download the VS 2010 Release Candidate
MSDN Subscribers: Download the VS 2010 Release Candidate
Sell Your Code and Make Money?
Creating your own Tetris game using VB.NET
Article :: Improving Software Economics, Part 4 of 7: Top 10 Principles of Iterative Software Management



Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier

Reply Post New Thread
 
Thread Tools Search this Thread Display Modes
Old Sep 27th, 2008, 11:09 AM   #1
Merri
VB6, XHTML & CSS hobbyist
 
Merri's Avatar
 
Join Date: Oct 02
Location: Finland
Posts: 5,948
Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)
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( _
            "Do you... like... me? It is not that important but...", _
            "Just a simple question", _
            [Yes / No], _
            [Default Button 2], _
            [Icon Question] _
        ) = [Result Yes] Then
            .Show "I knew I could trust you! *KISS*", "Windows will not crash anymore", , , [Icon Critical]
        Else
            .Show "U ruined ma life i dont wants to ever see U again!111", "Ur windoze will be hxrd!"
        End If
    End With
    
    Set MessageBox = Nothing
    
    Unload Me
End Sub
Attached Files
File Type: zip UniMsgBox_2008-08-03.zip (10.2 KB, 396 views)
__________________
VB6 in occasional use. I'm all HTML, CSS & JavaScript these days.
« Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB »
« OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 »
Merri is offline   Reply With Quote
Old Sep 27th, 2008, 11:11 AM   #2
Merri
VB6, XHTML & CSS hobbyist
 
Merri's Avatar
 
Join Date: Oct 02
Location: Finland
Posts: 5,948
Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)
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.
__________________
VB6 in occasional use. I'm all HTML, CSS & JavaScript these days.
« Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB »
« OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 »

Last edited by Merri; Oct 12th, 2008 at 03:50 AM.
Merri is offline   Reply With Quote
Old Oct 11th, 2008, 09:30 PM   #3
tanjiunnyann
Junior Member
 
Join Date: Sep 08
Posts: 26
tanjiunnyann is an unknown quantity at this point (<10)
Re: VB6 UniMsgBox - Unicode message box class

Nice, I'm using second one, it really good and handy!!!
tanjiunnyann is offline   Reply With Quote
Old Nov 21st, 2008, 02:53 AM   #4
cliv
Junior Member
 
Join Date: Feb 06
Posts: 17
cliv is an unknown quantity at this point (<10)
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
cliv is offline   Reply With Quote
Old Nov 21st, 2008, 09:56 AM   #5
Merri
VB6, XHTML & CSS hobbyist
 
Merri's Avatar
 
Join Date: Oct 02
Location: Finland
Posts: 5,948
Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)
Re: VB6 UniMsgBox - Unicode message box class

A resource icon will only work with a compiled application.
__________________
VB6 in occasional use. I'm all HTML, CSS & JavaScript these days.
« Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB »
« OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 »
Merri is offline   Reply With Quote
Old Nov 22nd, 2008, 04:52 AM   #6
cliv
Junior Member
 
Join Date: Feb 06
Posts: 17
cliv is an unknown quantity at this point (<10)
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!
cliv is offline   Reply With Quote
Old Nov 22nd, 2008, 08:05 AM   #7
Merri
VB6, XHTML & CSS hobbyist
 
Merri's Avatar
 
Join Date: Oct 02
Location: Finland
Posts: 5,948
Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)Merri is a splendid one to behold (700+)
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.
__________________
VB6 in occasional use. I'm all HTML, CSS & JavaScript these days.
« Antec Sonata II: Core 2 Duo E7400, ASRock P45TS, Asus EN9600GT 512 MB, 4 GB, 1.25 TB »
« OS: Windows 7 | Laptop: Amilo Pi 2530-12P| Netbook: Asus EEE 901 »

Last edited by Merri; Nov 22nd, 2008 at 08:14 AM.
Merri is offline   Reply With Quote
Old Nov 24th, 2008, 01:10 PM   #8
cliv
Junior Member
 
Join Date: Feb 06
Posts: 17
cliv is an unknown quantity at this point (<10)
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?
cliv is offline   Reply With Quote
Old Jan 9th, 2009, 04:48 PM   #9
Jim Davis
Frenzied Member
 
Jim Davis's Avatar
 
Join Date: Mar 01
Location: Mars base one Username: Jim Davis Password: yCrm33
Posts: 1,284
Jim Davis has a spectacular aura about (125+)Jim Davis has a spectacular aura about (125+)
Re: VB6 UniMsgBox - Unicode message box class

Quote:
If you want a simple Unicode aware replacement of the MsgBox function, here is that too
Yay Excellent! Thanks a lot.
Jim Davis is offline   Reply With Quote
Reply

Go Back   VBForums > VBForums CodeBank > CodeBank - Visual Basic 6 and earlier


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -5. The time now is 01:12 PM.




To view more projects, click here

Acceptable Use Policy


The Network for Technology Professionals

Search:

About Internet.com

Legal Notices, Licensing, Permissions, Privacy Policy.
Advertise | Newsletters | E-mail Offers

Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.