Jump to content

MsgBox with altered buttons captions


Recommended Posts

Hi everybody :)
I needed to display a couple of MsgBox with altered buttons captions.
Here is the way I just scripted it, using Timers (thanks to @KaFu for the idea, a few months ago)

#include <MsgBoxConstants.au3>
#include <WinAPISysWin.au3>

Opt("MustDeclareVars", 1) ;0=no, 1=require pre-declaration

Global $g_aCaption, $g_sTitle ; 2 global variables for _TimerProc()

 Dim $g_aCaption[3] = ["Hello", "How", "Are You ?"] ; 3 buttons (max) in MsgBox (not counting the 4th special button $MB_HELP)
 $g_sTitle = "This is MsgBox #1"
Local $iChoice1 = _MsgBox(BitOr($MB_YESNOCANCEL, $MB_DEFBUTTON3, $MB_TOPMOST, $MB_ICONQUESTION), $g_sTitle, _
    "1st line of text" & @crlf & "2nd line of text")
; ConsoleWrite("A: " & $iChoice1 & @crlf)

Dim $g_aCaption[2] = ["Copy text", "Ok"] ; 2 buttons
$g_sTitle = "This is MsgBox #2"
Local $iChoice2 = _MsgBox(BitOr($MB_OKCANCEL, $MB_DEFBUTTON2, $MB_TOPMOST), $g_sTitle, _
    "I need these 2 buttons captions in another script)") ; think of $MB_YESNO too (2 buttons without Esc allowed)
; ConsoleWrite("B: " & $iChoice2 & @crlf)

Dim $g_aCaption[1] = ["Hello !"] ; 1 button (not really useful but well...)
$g_sTitle = "This is MsgBox #3"
Local $iChoice3 = _MsgBox(BitOr($MB_OK, $MB_TOPMOST), $g_sTitle, _
    "Revolution 9, such a strange song", 5) ; timaout 5s (returns -1 if timed out)
; ConsoleWrite("C: " & $iChoice3 & @crlf)

;===============================================
Func _MsgBox($iFlag, $g_sTitle, ByRef $sText, $iTimeOut = 0, $hWnd = 0)

    Local $hTimerProc = DllCallbackRegister('_TimerProc', 'none', 'hwnd;uint;uint_ptr;dword')
    Local $iTimerID = _WinAPI_SetTimer(0, 0, 10, DllCallbackGetPtr($hTimerProc))

    Local $iChoice = MsgBox($iFlag, $g_sTitle, $sText, $iTimeOut, $hWnd)

    _WinAPI_KillTimer(0, $iTimerID)
    DllCallbackFree($hTimerProc)
    Return $iChoice
EndFunc   ;==>_MsgBox

;===============================================
Func _TimerProc($hWnd, $iMsg, $iTimerID, $iTime)

    If WinActive($g_sTitle) Then
        _WinAPI_KillTimer(0, $iTimerID)
        If Ubound($g_aCaption) > 3 Then ReDim $g_aCaption[3]
        For $i = 0 To Ubound($g_aCaption) - 1
            ControlSetText($g_sTitle, "", "Button" & ($i + 1), $g_aCaption[$i])
        Next
    EndIf
EndFunc   ;==>_TimerProc

MsgBoxwithalteredbuttoncaptions(2).png.65cf99cf1cc11931bad23b5e6769dd26.png

If you guys got comments or improvements on this, please share your thoughts here, thanks !

Edited by pixelsearch
minor improvement in pic with 2 buttons
Link to comment
Share on other sites

On 8/1/2024 at 7:29 PM, pixelsearch said:

I needed to display a couple of MsgBox with altered buttons captions.

I made a UDF some time ago for that.

 

2 hours ago, lIlIIlIllIIIIlI said:

ultimately that typical code execution is paused when the prompt for user input is displayed?

Yes, is a MsgBox. You now have the ability to change the button text. That's all.
For anything else, build a GUI and code at will.

Follow the link to my code contribution ( and other things too ).
FAQ - Please Read Before Posting.
autoit_scripter_blue_userbar.png

Link to comment
Share on other sites

Posted (edited)

@funkey always great to hear from you.
Thanks for sharing your alternate way to do it.

@lIlIIlIllIIIIlI  Yes, MsgBox is a blocking function with predefined captions & return values. That's why we can't get the handle of its window when displayed, in a simple way by using WinGetHandle ( "title" [, "text"] )

So if we want to use it with altered captions (instead of creating our own dialogue GUI) then we have to find ways to do it. Gladly with the help of these great people on the Forum, they show us it's possible.

@argumentum thanks for the link :thumbsup:

Edited by pixelsearch
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...