Rubrik: Oberfläche · MessageBox | VB-Versionen: VB5, VB6 | 07.01.03 |
Komfortable MsgBox mit frei editierbaren Buttons Ersatz für die Standard System-MsgBox mit der Möglichkeit, die Buttons beliebig zu beschriften. | ||
Autor: Dartrax | Bewertung: | Views: 59.934 |
ohne Homepage | System: Win9x, WinNT, Win2k, WinXP, Win7, Win8, Win10, Win11 | Beispielprojekt auf CD |
Dieser Tipp verwendet nicht die Standard-MsgBox-Anweisung von VB, sondern die alternative API-Variante. Die "neue" MessageBox wird hierbei durch Aufruf der "Ersatz-Funktion" CoolBox aufgerufen, wobei Titel, Text, Symbol und natürlich die Buttonbeschriftungen als Parameter übergeben werden. Weiterhin benötigt die CoolMsgBox keinen VB-Timer, sondern benutzt die API-Variante, so dass die MsgBox aus jeder Form heraus genutzt werden kann, ohne erst einen Timer auf die Form platzieren zu müssen.
Kopieren Sie nachfolgenden Code in ein MODUL.
Option Explicit ' Benötigte API's für die Timer-Steuerung Private Declare Function SetTimer Lib "user32" ( _ ByVal Hwnd As Long, _ ByVal nIDEvent As Long, _ ByVal uElapse As Long, _ ByVal lpTimer As Long) As Long Private Declare Function KillTimer Lib "user32" ( _ ByVal Hwnd As Long, _ ByVal nIDEvent As Long) As Long Private Const MY_NID = 88 Private Const MY_ELAPSE = 25 ' Wartezeit: 25 MSek. ' Benötigte API's für das Manipulieren der MsgBox Private Declare Function MessageBox Lib "user32" _ Alias "MessageBoxA" ( _ ByVal Hwnd As Long, _ ByVal lpText As String, _ ByVal lpCaption As String, _ ByVal wType As Long) As Long Private Declare Function GetActiveWindow Lib "user32" () As Long Private Declare Function SendDlgItemMessage Lib "USER32.DLL" _ Alias "SendDlgItemMessageA" ( _ ByVal hDlg As Long, _ ByVal nIDDlgItem As Long, _ ByVal wMsg As Long, _ ByVal wParam As Long, _ ByVal lParam As String) As Long ' Benötigte Konstanten Private Const WM_SETTEXT = &HC ' MsgBox-Bildsymbole Public Enum CoolBoxIcon Critical = 16 Question = 32 Exclamation = 48 Information = 64 End Enum ' Variablen zur Speicherung der gewünschten ' Buttonbeschriftung Private m_Caption1 As String Private m_Caption2 As String Private m_Caption3 As String ' WindowHandle Private m_hWnd As Long
' MsgBox anzeigen Public Function CoolBox(ByVal Hwnd As Long, _ ByVal Text As String, _ ByVal Title As String, _ ByVal Button1 As String, _ Optional ByVal Button2 As String, _ Optional ByVal Button3 As String, _ Optional ByVal Symbol As CoolBoxIcon) As Long Dim nResult As Long ' Fensterhandle m_hWnd = Hwnd ' Beschriftung der Buttons m_Caption1 = Button1 m_Caption2 = Button2 m_Caption3 = Button3 ' API-Timer starten nResult = SetTimer(m_hWnd, MY_NID, MY_ELAPSE, _ AddressOf Coolbox_TimerEvent) ' API Message-Box mit gewünschter Buttonalzahl aufrufen If Button2 = "" And Button3 = "" Then ' Ein Button nResult = MessageBox(m_hWnd, Text, Title, _ Symbol Or vbOKOnly) ElseIf Button2 <> "" And Button3 = "" Then ' Zwei Buttons nResult = MessageBox(m_hWnd, Text, Title, _ Symbol Or vbYesNo) Else ' Drei Buttons nResult = MessageBox(m_hWnd, Text, Title, _ Symbol Or vbAbortRetryIgnore) End If ' Antwort auswerten und Rückgabewert festlegen If nResult = 1 Or nResult = 3 Or nResult = 6 Then ' erster Button wurde gedrückt CoolBox = 1 ElseIf nResult = 4 Or nResult = 7 Then ' zweiter Button wurde gedrückt CoolBox = 2 Else ' dritter Button wurde gedrückt CoolBox = 3 End If End Function
' Timer-Event! Sub Coolbox_TimerEvent() Dim nWnd As Long ' API-Timer wieder deaktivieren KillTimer m_hWnd, MY_NID ' Fensterhandle der MsgBox nWnd = GetActiveWindow() ' Buttons neu beschriften If m_Caption2 = "" And m_Caption3 = "" Then ' nur ein Button SendDlgItemMessage nWnd, vbCancel, WM_SETTEXT, 0, m_Caption1 ElseIf m_Caption2 <> "" And m_Caption3 = "" Then ' Zwei Buttons SendDlgItemMessage nWnd, vbYes, WM_SETTEXT, 0, m_Caption1 SendDlgItemMessage nWnd, vbNo, WM_SETTEXT, 0, m_Caption2 Else ' Drei Buttons SendDlgItemMessage nWnd, vbAbort, WM_SETTEXT, 0, m_Caption1 SendDlgItemMessage nWnd, vbRetry, WM_SETTEXT, 0, m_Caption2 SendDlgItemMessage nWnd, vbIgnore, WM_SETTEXT, 0, m_Caption3 End If End Sub
Beschreibung der Parameter für den Aufruf von CoolBox:
hWnd: | Window-Handle der aufrufenden Form, z.B. Form1.hWnd |
Text: | Text, der in der MsgBox angezeigt werden soll. Für Zeilenumbruch bitte Chr$(13) verwenden. |
Title: | Text, der in der Titelzeile der MsgBox angezeigt werden soll |
Button1: | Beschriftung für den 1. Button |
Button2: | Optional. Beschriftung für den 2. Button. |
Button3: | Optional. Beschriftung für den 3. Button. |
Symbol: | Optional. Eine der Konstanten Critical, Quersion, Exclamation oder Information |
Der Rückgabewert der Funktion entspricht dem gewählten Button, also 1 bzw. 2 oder 3.
Beispiel für den Aufruf:
Dim nButton As Long ' MsgBox mit 3 Buttons nButton = CoolBox(Me.hWnd, _ "CoolMsgBox mit beliebiger Buttonbeschriftung", _ "CoolBox", _ "Supi!", "Prima!", "Exit!", _ CoolBoxIcon.Information) ' Auswertung des gewählten Buttons If nButton = 3 Then End End If