Jump to content
IAMK

Customised InputBox?

Recommended Posts

IAMK

Hello,

 

I know you can set flags, e.g. 1, 2, 3 to change the buttons in a MsgBox from "OK" + "Cancel" to "Yes" + "No" + "Cancel", etc...

However...

1- Is it possible to manually say what the buttons say? E.g. "Pass" + "Fail"?

2- Is it possible to do that with an InputBox rather than a MsgBox?

3- What in your opinion would be better design for the user? A ) Pressing "Fail" on a MsgBox creates a "InputBox" with only a textbox and "Submit" button? B ) Only being able to press "Fail" on an InputBox if the textbox is not empty?

 

Thank you in advance.

Edited by IAMK

Share this post


Link to post
Share on other sites
benners

1. ChrisL did something similar with a message box here

2. You maybe able to modify this to work with an input or easy option would be to create your own GUI

3. Depends on the context. What leads to the msgbox prompt.?,  what are pass and fail supposed to do?

Share this post


Link to post
Share on other sites
IAMK

@benners I have tried using ChrisL's ControlSetText on both MsgBox and InputBox, but I must not understand it properly.
E.g. ControlSetText("AutoIT_TestChecker", "", "Button2", "FAIL")

My understanding is that ControlSetText overrides the text in the buttons (in this case the button called "Button2") with the text "FAIL" in windows with the title "AutoIT_TestChecker". I have tried this both before opening the InputBox and while it is open.

 

As for 3 ) , a tester will read what the InputBox/ToolTip says, then press "Pass" or "Fail" accordingly, typing a comment if they press "Fail".

Share this post


Link to post
Share on other sites
benners

You only need to use the function _MsgBoxChangeButtons() to change the button text.

#include <MsgBoxConstants.au3>

$i_Ret = _MsgBoxChangeButtons(BitOR($MB_ICONQUESTION, $MB_OKCANCEL), 'AutoIT_TestChecker', 'some text here', 'Pass', 'Fail')

; do something based on the return ($i_Ret)
If $i_Ret = 1 Then
    MsgBox(0, 'Reply', 'You pressed pass')
Elseif $i_Ret = 2 then
    MsgBox(0, 'Reply', 'You pressed fail')
EndIf

Func _MsgBoxChangeButtons($iFlag, $sTitle, $sText, $sButton1, $sButton2 = '', $sButton3 = '', $iMBTimeOut = 0, $xMBpos = "", $yMBpos = "")
    If Not IsNumber($iFlag) Then
        SetError(1)
        Return -1
    ElseIf Not IsNumber($iMBTimeOut) Then
        SetError(2)
        Return -1
    ElseIf $xMBpos <> "" And IsNumber($xMBpos) = 0 Then
        SetError(3)
        Return -1
    ElseIf $yMBpos <> "" And IsNumber($yMBpos) = 0 Then
        SetError(4)
        Return -1
    EndIf

    Local $MBFile = FileOpen(@TempDir & '\MiscMMB.txt', 2)
    Local $MBLine0 = ''
    Local $MBLine1 = '#NoTrayIcon'
    Local $MBLine2 = 'Opt("WinWaitDelay", 0)'
    Local $MBLine3 = 'WinWait("' & $sTitle & '")'
    Local $MBLine4 = 'ControlSetText("' & $sTitle & '", "", "Button1", "' & $sButton1 & '")'
    Local $MBLine5 = 'ControlSetText("' & $sTitle & '", "", "Button2", "' & $sButton2 & '")'
    Local $MBLine6 = 'ControlSetText("' & $sTitle & '", "", "Button3", "' & $sButton3 & '")'
    Local $MBline7 = 'WinMove("' & $sTitle & '", ""' & ', ' & $xMBpos & ', ' & $yMBpos & ')'
    Local $MBline8 = '$pos = WingetPos("' & $sTitle & '", "")'
    Local $MBline9 = 'WinMove("' & $sTitle & '", ""' & ', ' & $xMBpos & ',$pos[1])'
    Local $MBline10 = 'WinMove("' & $sTitle & '", ""' & ', $pos[0], ' & $yMBpos & ')'

    If $sButton2 = '' Then
        FileWrite(@TempDir & '\MiscMMB.txt', $MBLine0 & @CRLF & $MBLine1 & @CRLF & $MBLine2 & @CRLF & $MBLine3 & @CRLF & $MBLine4)
    ElseIf $sButton2 <> '' And $sButton3 = '' Then
        FileWrite(@TempDir & '\MiscMMB.txt', $MBLine0 & @CRLF & $MBLine1 & @CRLF & $MBLine2 & _
                @CRLF & $MBLine3 & @CRLF & $MBLine4 & @CRLF & $MBLine5)
    ElseIf $sButton2 <> '' And $sButton3 <> '' Then
        FileWrite(@TempDir & '\MiscMMB.txt', $MBLine0 & @CRLF & $MBLine1 & @CRLF & $MBLine2 & @CRLF & _
                $MBLine3 & @CRLF & $MBLine4 & @CRLF & $MBLine5 & @CRLF & $MBLine6)
    EndIf
    If $xMBpos <> "" And $yMBpos <> "" Then
        FileWriteLine(@TempDir & '\MiscMMB.txt', @CRLF & $MBline7)
    ElseIf $xMBpos <> "" And $yMBpos = "" Then
        FileWriteLine(@TempDir & '\MiscMMB.txt', @CRLF & $MBline8)
        FileWriteLine(@TempDir & '\MiscMMB.txt', @CRLF & $MBline9)
    ElseIf $xMBpos = "" And $yMBpos <> "" Then
        FileWriteLine(@TempDir & '\MiscMMB.txt', @CRLF & $MBline8)
        FileWriteLine(@TempDir & '\MiscMMB.txt', @CRLF & $MBline10)
    EndIf


    $MBPID1 = Run(@AutoItExe & ' /AutoIt3ExecuteScript ' & EnvGet('TEMP') & '\MiscMMB.txt')
    $MBBox = MsgBox($iFlag, $sTitle, $sText, $iMBTimeOut)
    FileClose($MBFile)

    Do
        FileDelete(@TempDir & '\MiscMMB.txt')
    Until Not FileExists(@TempDir & '\MiscMMB.txt')

    Return $MBBox
EndFunc   ;==>_MsgBoxChangeButtons

It would be better to create your own GUI with the button  and input box. you'd have more control

 

Share this post


Link to post
Share on other sites
IAMK

@benners Oh, I was trying to strip it down as much as possible.

I guess I'll just include that function so as to keep my code clean.

Thank you for your help.

Share this post


Link to post
Share on other sites
IAMK

@benners It works just fine for normal applications made.

However, trying to improve on it, is there a way to set the pos at the start rather than to move the window to the pos? Seeing the MsgBox flash is not a nice thing, especially since the default location is on screen 1, and I don't want anything being seen on screen 1 during testing (I just set the pos to -y (top screen)).

Also, I can't seem to find any AutoIT call/variable to set for width/height like InputBox does.

It seems I need to resort to my own GUI...

Share this post


Link to post
Share on other sites
benners

As for as I know there isn't a method to move the message box before showing it.

3 hours ago, IAMK said:

It seems I need to resort to my own GUI...

It would be the best option in my opinion

Share this post


Link to post
Share on other sites
funkey

Try this:

 

#include <WinAPI.au3>
#include <Constants.au3>

Global $MsgBoxX = @DesktopWidth + 20    ;Adjust position here
Global $MsgBoxY = 20                    ;and here

Local $hProcMsgBox = DllCallbackRegister("CbtHookProcMsgBox", "int", "int;int;int")
Local $TIDMsgBox = _WinAPI_GetCurrentThreadId()
Local $hHookMsgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProcMsgBox), 0, $TIDMsgBox)



Local $iRet = MsgBox(34, "Select example", "Please select the skin you want to try")



_WinAPI_UnhookWindowsHookEx($hHookMsgBox)
DllCallbackFree($hProcMsgBox)



#region Just for fun!!
;##########################################################
Func CbtHookProcMsgBox($nCode, $wParam, $lParam)
    Local $RET = 0
    If $nCode < 0 Then
        $RET = _WinAPI_CallNextHookEx($hHookMsgBox, $nCode, $wParam, $lParam)
        Return $RET
    EndIf
    Switch $nCode
        Case 5 ;5=HCBT_ACTIVATE
            _WinAPI_SetDlgItemText($wParam, 3, "1")
            _WinAPI_SetDlgItemText($wParam, 4, "2")
            _WinAPI_SetDlgItemText($wParam, 5, "3")
            _WinAPI_SetWindowPos($wParam, $HWND_TOP, $MsgBoxX, $MsgBoxY, _WinAPI_GetWindowWidth($wParam), _WinAPI_GetWindowHeight($wParam), $SWP_NOZORDER)
    EndSwitch
    Return $RET
EndFunc   ;==>CbtHookProcMsgBox

Func _WinAPI_SetDlgItemText($hDlg, $nIDDlgItem, $lpString)
    Local $aRet = DllCall('user32.dll', "int", "SetDlgItemText", _
            "hwnd", $hDlg, _
            "int", $nIDDlgItem, _
            "str", $lpString)
    Return $aRet[0]
EndFunc   ;==>_WinAPI_SetDlgItemText

;##########################################################
#endregion Just for fun!!

 


Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
IAMK

@funkey That's nice, but it works 90% of the time. There were still 2 times it flickered.

Share this post


Link to post
Share on other sites
funkey

So this is the last chance ;)

 

#include <WinAPI.au3>
#include <Constants.au3>

Global Const $tagCBT_CREATEWND = "ptr lpcs;HWND tagCBT_CREATEWND"
Global Const $tagCREATESTRUCT = "ptr lpCreateParams;handle hInstance;HWND hMenu;HWND hwndParent;int cy;int cx;int y;int x;LONG style;ptr lpszName;ptr lpszClass;DWORD dwExStyle"

Local $hProcMsgBox = DllCallbackRegister("CbtHookProcMsgBox", "int", "int;int;int")
Local $TIDMsgBox = _WinAPI_GetCurrentThreadId()
Local $hHookMsgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($hProcMsgBox), 0, $TIDMsgBox)



#region Your code starts here

Global $MsgBoxX = @DesktopWidth + 20 ;Adjust position here
Global $MsgBoxY = 20 ;and here

Global $BtnDesc1 = "1", $BtnDesc2 = "2", $BtnDesc3 = "3"
Global $iRet = MsgBox(34, "Select example", "Please select the skin you want to try")

Global $BtnDesc1 = "4", $BtnDesc2 = "5", $BtnDesc3 = "6"
$iRet = MsgBox(34, "Select example 2", "Please select the skin you want to try")

#endregion Your code ends here



_WinAPI_UnhookWindowsHookEx($hHookMsgBox)
DllCallbackFree($hProcMsgBox)

#region Just for fun!!
;##########################################################
Func CbtHookProcMsgBox($nCode, $wParam, $lParam)
    Local $tcw, $tcs

    If $nCode < 0 Then
        Return _WinAPI_CallNextHookEx($hHookMsgBox, $nCode, $wParam, $lParam)
    EndIf

    Switch $nCode
        Case 3 ;5=HCBT_CREATEWND
            If _WinAPI_GetClassName(HWnd($wParam)) = "#32770" Then
                $tcw = DllStructCreate($tagCBT_CREATEWND, $lParam)
                $tcs = DllStructCreate($tagCREATESTRUCT, DllStructGetData($tcw, "lpcs"))

                DllStructSetData($tcs, "x", $MsgBoxX)
                DllStructSetData($tcs, "y", $MsgBoxY)
            EndIf
        Case 5 ;5=HCBT_ACTIVATE
            _WinAPI_SetDlgItemText($wParam, 3, $BtnDesc1)
            _WinAPI_SetDlgItemText($wParam, 4, $BtnDesc2)
            _WinAPI_SetDlgItemText($wParam, 5, $BtnDesc3)
    EndSwitch
    Return _WinAPI_CallNextHookEx($hHookMsgBox, $nCode, $wParam, $lParam)
EndFunc   ;==>CbtHookProcMsgBox

Func _WinAPI_SetDlgItemText($hDlg, $nIDDlgItem, $lpString)
    Local $aRet = DllCall('user32.dll', "int", "SetDlgItemText", _
            "hwnd", $hDlg, _
            "int", $nIDDlgItem, _
            "str", $lpString)
    Return $aRet[0]
EndFunc   ;==>_WinAPI_SetDlgItemText

;##########################################################
#endregion Just for fun!!

 

Edited by funkey

Programming today is a race between software engineers striving to
build bigger and better idiot-proof programs, and the Universe
trying to produce bigger and better idiots.
So far, the Universe is winning.

Share this post


Link to post
Share on other sites
benched42

I have used Melba23's excellent Extended Message Box UDF. There is a LOT you can do with this function.

Extended Message Box UDF


Who lied and told you life would EVER be fair?

Share this post


Link to post
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

  • Similar Content

    • dmkirkland
      By dmkirkland
      I'm wondering if, once again, I might be able to get some assistance from those of you who are much more knowledgeable than I. I have written a function that uses an InputBox and asks for the name of the adapter the user wants to manipulate. The script is designed to assign the name of the adapter to the entire program (a global variable) for use in other functions throughout its use. It can also be called again to change the adapter or, if it mistakenly didn't get set, set it. The issue I'm having is that I can't figure out a way to keep the function from "clearing" or "deleting" the variable if the cancel button is pressed. The idea is that if the user assigns the variable at the start of the program running (the function is called before anything else happens in the script) then, mistakenly/accidentally, runs the function again and, at the InputBox, the user clicks "Cancel" it will leave the variable alone. Currently, I've been able to (after much trial, tribulation, and troubleshooting) get it actually cancel the operation when "Cancel" is pressed or assign the variable as needed/preferred when the information is entered and "OK" is pressed. However, if "Cancel" is pressed (again, after the variable has already been assigned), it completely clears the variable's assignment (as evidenced by calling the variable in another function. Below is the code for the function. Please let me know if more functions are needed for diagnosis.
      Func selectAdapter()
          Global $adapterName
          $adapterName = InputBox("Select Adapter", "Please input the adapter name" & @CRLF & @CRLF & "or leave it at the default:", "Ethernet")
          If @error = 1 Then
              Return
          EndIf
      EndFunc
    • funkey
      By funkey
      Hello,
      it's quite often, that someone asks how to change the texts of the MsgBox buttons or the InputBox buttons or how to change the position of ta MsgBox. Since years I use CBT hooks for that, but now I made a small UDF out of it for the ease of use. Of course you can build your own GUI or use already existing UDFs to do the same, but I like this way and you can hack (hook) the inbuild InputBox.
       
      HookDlgBox.au3
      #include-once #include <WinAPI.au3> Global Const $tagCBT_CREATEWND = "ptr lpcs;HWND tagCBT_CREATEWND" Global Const $tagCREATESTRUCT = "ptr lpCreateParams;handle hInstance;HWND hMenu;HWND hwndParent;int cy;int cx;int y;int x;LONG style;ptr lpszName;ptr lpszClass;DWORD dwExStyle" Global $g__hProcDlgBox = DllCallbackRegister("__DlgBox_CbtHookProc", "int", "int;int;int") Global $g__TIdDlgBox = _WinAPI_GetCurrentThreadId() Global $g__hHookDlgBox = _WinAPI_SetWindowsHookEx($WH_CBT, DllCallbackGetPtr($g__hProcDlgBox), 0, $g__TIdDlgBox) Global Const $g__MaxDlgBtns = 5 ; maximum of 5 buttons to rename text Global Const $g__MaxDlgItemId = 11 ; maximun ID of buttons to search is 11 as this is the maximun used in Messagebox Global $g__DlgBoxPosX, $g__DlgBoxPosY, $g__DlgBoxWidth, $g__DlgBoxHeight Global $g__aDlgBoxBtnText[$g__MaxDlgBtns] Global $g__DlgBtnCount = 0 _DlgBox_SetDefaults() OnAutoItExitRegister("__DlgBox_UnregisterHook") Func _DlgBox_SetButtonNames($TxtBtn1 = Default, $TxtBtn2 = Default, $TxtBtn3 = Default, $TxtBtn4 = Default, $TxtBtn5 = Default) $g__aDlgBoxBtnText[0] = $TxtBtn1 $g__aDlgBoxBtnText[1] = $TxtBtn2 $g__aDlgBoxBtnText[2] = $TxtBtn3 $g__aDlgBoxBtnText[3] = $TxtBtn4 $g__aDlgBoxBtnText[4] = $TxtBtn5 $g__DlgBtnCount = @NumParams EndFunc ;==>_DlgBox_SetButtonNames Func _DlgBox_SetPosition($x = Default, $y = Default) ;only for MsgBox, not working and not needed for InputBox $g__DlgBoxPosX = $x $g__DlgBoxPosY = $y EndFunc ;==>_DlgBox_SetPosition Func _DlgBox_SetSize($w = Default, $h = Default) $g__DlgBoxWidth = $w $g__DlgBoxHeight = $h EndFunc ;==>_DlgBox_SetSize Func _DlgBox_SetDefaults() $g__DlgBoxPosX = Default $g__DlgBoxPosY = Default $g__DlgBoxWidth = Default $g__DlgBoxHeight = Default For $i = 0 To UBound($g__aDlgBoxBtnText) - 1 $g__aDlgBoxBtnText[$i] = Default Next EndFunc ;==>_DlgBox_SetDefaults Func __DlgBox_CbtHookProc($nCode, $wParam, $lParam) Local $tcw, $tcs Local $iSearch = 0 Local $ahBtn[$g__DlgBtnCount] If $nCode < 0 Then Return _WinAPI_CallNextHookEx($g__hHookDlgBox, $nCode, $wParam, $lParam) EndIf Switch $nCode Case 3 ;5=HCBT_CREATEWND If _WinAPI_GetClassName(HWnd($wParam)) = "#32770" Then ;Dialoig window class $tcw = DllStructCreate($tagCBT_CREATEWND, $lParam) $tcs = DllStructCreate($tagCREATESTRUCT, DllStructGetData($tcw, "lpcs")) If $g__DlgBoxPosX <> Default Then DllStructSetData($tcs, "x", $g__DlgBoxPosX) If $g__DlgBoxPosY <> Default Then DllStructSetData($tcs, "y", $g__DlgBoxPosY) If $g__DlgBoxWidth <> Default Then DllStructSetData($tcs, "cx", $g__DlgBoxWidth) If $g__DlgBoxHeight <> Default Then DllStructSetData($tcs, "cy", $g__DlgBoxHeight) EndIf Case 5 ;5=HCBT_ACTIVATE For $i = 1 To $g__MaxDlgItemId If IsHWnd(_WinAPI_GetDlgItem($wParam, $i)) Then If $g__aDlgBoxBtnText[$iSearch] <> Default Then _WinAPI_SetDlgItemText($wParam, $i, $g__aDlgBoxBtnText[$iSearch]) $iSearch += 1 If $iSearch >= UBound($ahBtn) Then ExitLoop EndIf Next EndSwitch Return _WinAPI_CallNextHookEx($g__hHookDlgBox, $nCode, $wParam, $lParam) EndFunc ;==>__DlgBox_CbtHookProc Func __DlgBox_UnregisterHook() _WinAPI_UnhookWindowsHookEx($g__hHookDlgBox) DllCallbackFree($g__hProcDlgBox) EndFunc ;==>__DlgBox_UnregisterHook Func _WinAPI_SetDlgItemText($hDlg, $nIDDlgItem, $lpString) Local $aRet = DllCall('user32.dll', "int", "SetDlgItemText", _ "hwnd", $hDlg, _ "int", $nIDDlgItem, _ "str", $lpString) Return $aRet[0] EndFunc ;==>_WinAPI_SetDlgItemText  
      Simple example to see how to use it
      #include "HookDlgBox.au3" _DlgBox_SetButtonNames("1", "two", "3") MsgBox(4, "Test 1", "Custom button texts") _DlgBox_SetPosition(20, 20) MsgBox(66, "Test 2", "Custom position and button texts") _DlgBox_SetButtonNames("Submit", "Don't submit", "Don't know") InputBox("Test 3", "Where were you born?", "Planet Earth") _DlgBox_SetSize(800, 800) InputBox("Test 4", "Where were you born?", "Planet Earth") _DlgBox_SetSize(Default, 800) MsgBox(66, "Test 5", "Strange but working") _DlgBox_SetButtonNames(Default, "Wait", "What?") _DlgBox_SetSize(Default, Default) _DlgBox_SetPosition(500, 500) MsgBox(66, "Test 6", "So far so good!") _DlgBox_SetDefaults() MsgBox(6, "Test 7", "Default position and button texts") Hope you like it.
       
      Best regards
      funkey
      HookDlgBox Example.au3
      HookDlgBox.au3
    • StudioMaker
      By StudioMaker
      Hey anyone knows how i can have a inputbox that is connected to an HotKeySet? Like if u put in like "a" in the inputbox it sets the hotkey to "a"?
       
    • akira2891
      By akira2891
      Hi,
      I was wondering is this all inputs can be done in 1 box with 3 fields and not to use another gui.
      And how can i validate data for example if i don't enter username he prompt me a message box all time until field is empty.
      I try like this but it give me a msgbox and continue on another field.
      $username = InputBox("Add new user", "Username", "", "", 200, 130, Default, Default, 0) if $username <> " " then msgbox(0, "", "Please enter username") endif $email = InputBox("Add new user", "Email", "", "", 200, 130, Default, Default, 0) $password = InputBox("Add new user", "Password", "", "*", 200, 130, Default, Default, 0)  
    • kneze
      By kneze
      Hi all,
      i would like to enter a vlaue to Inputbox 1 an press button1. Script should search value in a text file and put value after "; " to Inputbox 2.
      #cs ---------------------------------------------------------------------------- AutoIt Version: 3.3.14.2 Author: myName Script Function: Template AutoIt script. #ce ---------------------------------------------------------------------------- ; Script Start - Add your code below here #include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <file.au3> $Form1 = GUICreate("Form1", 257, 119, 192, 124) $Input1 = GUICtrlCreateInput("Input1", 15, 35, 121, 21) $Input2 = GUICtrlCreateInput("Input2", 15, 60, 121, 21) $Button1 = GUICtrlCreateButton("Button1", 145, 35, 75, 25) GUISetState(@SW_SHOW) $iLines = _FileCountLines(@ScriptDir & "\data.txt") ;MsgBox(0, "Char read:", $ilines) While 1 $nMsg = GUIGetMsg() Switch $nMsg Case $GUI_EVENT_CLOSE Exit Case $Button1 EndSwitch WEnd can anyone help?
      data.txt
×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.