Jump to content

Create a MsgBox with buttons disabled for specific amount of time


Go to solution Solved by argumentum,

Recommended Posts

Posted

Hi,

I would like to create an message box with disabled buttons for specified amount of time

after that period the buttons should be automatically enabled.

 

Example code (but not working as desired):

MsgBox(64, 'Sometitle', 'Please make sure to read this' & @CRLF & 'Are you sure want to continue ?')
Sleep(5000)

 

I'm wondering If such a thing could be achieved

Waiting for your thoughts, thank you!

  • Moderators
Posted

You could make customized MsgBox() funcs (I believe that's the route Melba took, haven't looked in years) like @argumentum suggested.

You could write an outside script and execute before the msgbox to disable the message box button (eg using run() to run script from memory or from an executable) before the msgbox (way more of a hassle me thinks).

You could set windows hooks, I did it here: 

But I didn't add a disable/timer option, but it's totally possible. (Warning, I haven't updated that thing in years but the concept is still there)

Honestly, with the way Melba keeps up with things, if he has that option, that's probably the way to go.

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted (edited)

@argumentum Nice, this UDF seems to be really great it's has more features and options more than the usual MsgBox

 

Now I'm trying to figure out how could I add some buttons into this example.

Here's the same example used in _ExtMsgBox:

#include "ExtMsgBox.au3"

; Set the message box right justification, not TOPMOST, disable closure, colours (orange text on green background) and change font
_ExtMsgBoxSet(1 + 2 + 64, 0)

$sMsg = "Please make sure to read this:" & @CRLF
$sMsg &= "Some important text is here" & @CRLF & @CRLF
$sMsg &= "Are you sure want to continue ?"

_ExtMsgBox(128, " ", "Test 6", $sMsg, 20, 100, 500)

; Reset to default
_ExtMsgBoxSet(Default)

After the message box timeout is finished the button should be enabled

only then the user can click on the button, If user didn't click on the button then the message box should be remained.

Edited by simplercoder000
Some typo fixes
Posted (edited)
  On 2/15/2024 at 1:13 AM, SmOke_N said:

You could write an outside script and execute before the msgbox to disable the message box button

Expand  
If StringInStr($CmdLineRaw, "/MyMessingAroundWithyMsgBox") Then Exit MyMessingAroundWithyMsgBox()
Func MyMessingAroundWithyMsgBox()
    AutoItWinSetTitle("MyMessingAroundWithyMsgBox")
    Local $sTitle = StringTrimRight(@ScriptName, 4), $sText = "Please make sure to", $sCtrl = "Button1"
    WinWait($sTitle, $sText)
    ControlDisable($sTitle, $sText, $sCtrl)
    For $n = 5 To 1 Step -1
        ControlSetText($sTitle, $sText, $sCtrl, $n)
        Sleep(999)
    Next
    ControlSetText($sTitle, $sText, $sCtrl, "&Ok")
    ControlEnable($sTitle, $sText, $sCtrl)
EndFunc

TellThemAndTellThemGood()
Func TellThemAndTellThemGood()
    ShellExecute(@AutoItExe, '"' & @ScriptFullPath & '" /MyMessingAroundWithyMsgBox')
    WinWait("MyMessingAroundWithyMsgBox")
    Local $sMsg = "Please make sure to read this:" & @CR
    $sMsg &= "Some important text is here" & @CR & @CR
    $sMsg &= "Are you sure want to continue ?"
    MsgBox(262144 + 64, StringTrimRight(@ScriptName, 4), $sMsg, 60)
EndFunc

This is what you would like to see @simplercoder000, right ?

Edited by argumentum
better code

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

  • Moderators
Posted

Hide it if you're having issues (ControlHide/ControlShow if you're using @argumentum's example)

Common sense plays a role in the basics of understanding AutoIt... If you're lacking in that, do us all a favor, and step away from the computer.

Posted (edited)
  On 2/15/2024 at 2:08 AM, simplercoder000 said:

But I think there's an glitch in the MessageBox, the timeout can be skipped by fast clicking on the button.

Expand  

No it can not while the button is disable. But it does take time until it get's disable, some, 300 or less mSec ?.

The notion is there. If anyone click that fast on it is because they are aware and tired of it.
What are you going to use this in that it has to be a MsgBox() ?. Couldn't you code your own GUI ?

Edit: I made changes to make it faster at disabling it.

Edited by argumentum

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

Posted

Another way :

#include <WinAPI.au3>

Global $bEnable, $hHook

Example()

Func Example()
  Local $hProc = DllCallbackRegister(CbtHookProc, "int", "int;int;int")
  $hHook = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProc), 0, _WinAPI_GetCurrentThreadId())
  $bEnable = False
  MsgBox(0, "test", "test",5)
  $bEnable = True
  Local $iRet = MsgBox(0, "test", "test")
  _WinAPI_UnhookWindowsHookEx($hHook)
  DllCallbackFree($hProc)
EndFunc   ;==>Example2

Func CbtHookProc($nCode, $wParam, $lParam)
  If $nCode = 5 Then ; 5=HCBT_ACTIVATE
    $hWnd = HWnd($wParam)
    If Not $bEnable Then ControlDisable($hWnd, "", "Button1")
  EndIf
  Return _WinAPI_CallNextHookEx($hHook, $nCode, $wParam, $lParam)
EndFunc   ;==>CbtHookProc

 

Posted
  On 2/15/2024 at 2:12 AM, SmOke_N said:

Hide it if you're having issues (ControlHide/ControlShow if you're using @argumentum's example)

Expand  

I tried this but it's not what I needed.

 

  On 2/15/2024 at 2:18 AM, argumentum said:

No it can not while the button is disable. But it does take time until it get's disable, some, 300 or less mSec ?.

Expand  

I don't want them to skip it, because this is gone be displayed every time they run the program.

  On 2/15/2024 at 2:18 AM, argumentum said:

What are you going to use this in that it has to be a MsgBox() ?. Couldn't you code your own GUI ?

Expand  

Here's an example on what I want to achieve:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

$MsgText = 'Continue using this software at your own risk !' & @CRLF
$MsgText &= 'By clicking on OK button you are agree in the software agreement !'

$returned = MsgBox(65, 'Info', $MsgText)
If $returned = $IDCANCEL Then
    Exit
Else
    #do nothing
EndIf


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 339, 131, 192, 124)
$Label1 = GUICtrlCreateLabel("Welcome to the empty frame software :)", 72, 32, 191, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

You may note that I have used two buttons here but that's not a problem I can be good with one button.

  • Solution
Posted (edited)

ok, thanks to @Nine, now you have a UDF file. And call it kind of like MsgBox()

Your script:

#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>
#include <MsgBox_Extn.au3> ; the magic ; https://www.autoitscript.com/forum/index.php?showtopic=211499&view=findpost&p=1530323


$MsgText = 'Continue using this software at your own risk !' & @CRLF
$MsgText &= 'By clicking on OK button you are agree in the software agreement !'


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 339, 131, -1, -1)
$Label1 = GUICtrlCreateLabel("Welcome to the empty frame software :)", 72, 32, 191, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


;~ $returned = MsgBox(BitOR($MB_TOPMOST, $MB_ICONINFORMATION, $MB_OKCANCEL), 'Info', $MsgText) ; This is replcaced by either

; set any of the 3 possible buttons, from left to right, with an alternate text.
;~ _MsgBox_Extn_SetButtonText(Default, "Run away") ; Each setting applies to a next call only.

; pass 1 value, as the delay countdown:
$returned = _MsgBox_Extn(5, BitOR($MB_TOPMOST, $MB_ICONINFORMATION, $MB_OKCANCEL), 'Info', $MsgText, 0, $Form1)

; or pass 3 comma delimited parameters been: "delay, X pos, Y pos"
;    also, the buttom number to delay, can be chosen by "/2" for the 2nd button for example. Default is Button1.
;~ $returned = _MsgBox_Extn("5/1, 20, 100", BitOR($MB_TOPMOST, $MB_ICONINFORMATION, $MB_OKCANCEL), 'Info', $MsgText, 10, $Form1)

; or pass 3 comma delimited parameters been: "0 delay, X pos, Y pos" to just move the MsgBox
;~ $returned = _MsgBox_Extn("0, 20, 100", BitOR($MB_TOPMOST, $MB_ICONINFORMATION, $MB_OKCANCEL), 'Info', $MsgText, 10, $Form1)

; or pass 5 comma delimited parameters been: "delay, X pos, Y pos, Width, Height" but is in my view, nonsense.
;~ $returned = _MsgBox_Extn("5/-2, 20, 100, 600, 300", BitOR($MB_TOPMOST, $MB_ICONINFORMATION, $MB_OKCANCEL), 'Info', $MsgText, 10, $Form1)

If $returned = $IDCANCEL Then
    GUIDelete()
    Exit 5 ; it exited with an errorlevel of, 5 in this case.
EndIf

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            GUIDelete()
            ExitLoop

    EndSwitch
WEnd


The UDF file:     MsgBox_Extn.au3

  Reveal hidden contents

image.png.b87ba02f4575bb190c2a635c7724a2d7.png

There you go.

Edit: Fixed "Run Once", never running again.
Edit2: Fixed button, not restoring the original text.
Edit3: Added X,Y  "WinMove()".
Edit4: Fixed when delay is zero because the user just wants to move the MsgBox().
Edit5: Changed the name to a more fitting one.
Edit6: Added to optionally choose the button to disable.
Edit7: Added to optionally change a default button text.
Edit8: Fixed stuff and added stuff. ( Example for some of new stuff provided )

  Reveal hidden contents

Edit9: Added _MsgBox_Extn_SetMessageText()
Edit10: Added "disable all buttons", if the 2nd number is a negative   (" 5 sec. / -1 ButtonNumber ", .
Edit11: Removed callback parameters in function. This is simpler.

Edited by argumentum
better code ?

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

Posted (edited)
#include <GUIConstantsEx.au3>
#include <StaticConstants.au3>
#include <WindowsConstants.au3>
#include <MsgBoxConstants.au3>

#include <MsgBox_Extn.au3> ; the magic

$MsgText = 'Continue using this software at your own risk !' & @CRLF
$MsgText &= 'By clicking on OK button you are agree in the software agreement !'


#Region ### START Koda GUI section ### Form=
$Form1 = GUICreate("Form1", 339, 131, -1, -1)
$Label1 = GUICtrlCreateLabel("Welcome to the empty frame software :)", 72, 32, 191, 17)
GUISetState(@SW_SHOW)
#EndRegion ### END Koda GUI section ###


;~ $returned = MsgBox(65, 'Info', $MsgText)
$returned = _MsgBox_Extn(5, 65, 'Info', $MsgText, 0, $Form1)
If $returned = $IDCANCEL Then
    GUIDelete()
    Exit
Else
    #do nothing
EndIf

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            Exit

    EndSwitch
WEnd

image.png.31b1f3190546df6ffefa58b4944917d4.png

I hope that's good enough :P

Edited by argumentum
renamed function

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

Posted (edited)

...what do people with insomnia do ?. Code 🤪
Added WinMove the MsgBox() to, anywhere. Center it on you GUI if the GUI is not centered ?

And renamed the UDF and function. It does more and quite likely, will have more use as a box mover, than the original timer delay aspect of it. 

Edited by argumentum
renamed everything

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

Posted (edited)
  On 2/15/2024 at 3:42 AM, SmOke_N said:

My goodness, ya'll are murderfying my script lol.

Expand  

..you know, going down the rabbits hole, at trying to add "SetDlgItemText" to rename the buttons, might as well just have added the timer thing to your script. 😅

Edit: ..the idea started after reading https://uxmovement.com/buttons/why-the-ok-button-is-no-longer-okay/ and I'm like, meh, I'll add that too. Wrong.

Edited by argumentum
more

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

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
×
×
  • Create New...