Jump to content

InputBox Change Icon Possible


Recommended Posts

Anyone know if it's possible to change the icon displayed in the top left corner when prompted when using InputBox?

I have searched help and this forum and cannot seem to find an answer.

Any help is appreciated.

The answer is the same as with the many requests for a customized MsgBox(), create your own with GuiCreate(), GuiCtrlCreateInput(), etc.

It's not that hard. Give it a shot and if you get stuck post your code for more help.

:)

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

  • 6 years later...

This topic is old, sorry to resuscitate him, but maybe the code below can help coders in future. Thanks to JScript by _StringSize() function.

#include-once
#include <GUIConstants.au3>
#include <ButtonConstants.au3>
#include <EditConstants.au3>
#include <WindowsConstants.au3>

Example()

Func Example()
    Local $text = "Alternative InputBox with automatic width/height adjustment and customizable icon line 1" & @CRLF & @CRLF & _
                "Alternative InputBox with automatic width/height adjustment and customizable icon line 2" & @CRLF & @CRLF & _
                "Alternative InputBox with automatic width/height adjustment and customizable icon line 3"

    Local $icon = @SystemDir & "\Notepad.exe"

    _InputBox("Customizable _InputBox", $text, "", "", -1, -1, Default, Default, 0, $icon)
    _InputBox("Customizable _InputBox", $text, "", "", 190, -1, Default, Default, 0, $icon)
    _InputBox("Customizable _InputBox", $text, "", "", 300, 500, Default, Default, 0, $icon)
    InputBox("Original InputBox", $text, "", "", 300, 500, Default, Default, 0, $icon)
EndFunc

; #ALTERNATIVE INPUTBOX#  ==========================================================================
; Name...........: _InputBox
; Description ...: Returns the string that was entered by user.
; Syntax.........: Same as original InputBox.
; Parameters ....: Same as original InputBox, except $ParentWindowHandle_Or_GuiIcon that can be a
;                  handle of a parent window (like original function) or a icon path.
;                  If $ParentWindowHandle_Or_GuiIcon is the handle of a parent window, a different
;                  icon still can be set in $GuiIcon (aditional and optional parameter, icon path).
; Author ........: shuaua
; ==================================================================================================
Func _InputBox($title, $text, $input = "", $pwchar = "", $width = -1, $height = -1, $xpos = Default, $ypos = Default, $timeout = 0, $ParentWindowHandle_Or_GuiIcon = "", $GuiIcon = "")
    ;; define font
    Local $InputBoxFontSize = 9
    Local $InputBoxFontWeight = 400
    Local $InputBoxFontName = "MS Sans Serif"
    ;; init
    Local $optbak = Opt("GUIOnEventMode")
    Opt("GUIOnEventMode", 0)
    Local $labelSize = _StringSize($text,$InputBoxFontSize,$InputBoxFontWeight,$InputBoxFontName)   ; sizes of label
    ;; default $width and $xpos
    If $width == Default Or $width <= 0 Then $width = $labelSize[0]+16          ; default width of GUI. 16 = 8+8 border distance
    If $width < 190 Then $width = 190                                           ; minimum GUI width
    If $width > 500 Then $width = 500                                           ; maximum GUI width
    If $xpos == Default Or $xpos <= 0 Then $xpos = @DesktopWidth/2-$width/2     ; horizontal coordinate
    ;; calculate line breaks
    Local $numberOfLines = StringSplit($text, @CR)                              ; number of lines in label text
    local $heightPerLine = $labelSize[1]/$numberOfLines[0]                      ; heigth of each line
    local $linesToAdd = 0                                                       ; number of aditional lines in case of line breaks
    Local $lineBreak
    Local $lineWidth
    For $i = 1 To $numberOfLines[0]
        $lineWidth = _StringSize($numberOfLines[$i],$InputBoxFontSize,$InputBoxFontWeight,$InputBoxFontName)    ; test if each line is bigger then label width
        $lineBreak = $lineWidth[0]/($width-16)                                                                  ; number of line breaks in each line of label
        If $lineBreak-0.0001 >= 1 Then
            $linesToAdd = $linesToAdd + Int($lineBreak)             ; cumulative
        EndIf
    Next
    ;; label parameters
    Local $labelWidth = $width-16                                   ; label width is the GUI width less 8+8 border distance
    Local $labelHeight = $labelSize[1]+$linesToAdd*$heightPerLine   ; fix the height according line breaks
    Local $labelXPos = 8
    Local $labelYPos = 8
    ;; input parameters
    local $editWidth = $labelWidth
    Local $editHeight = 21
    Local $editXPos = 8
    Local $editYPos = $labelHeight+6                                ; default vertical distance between controls is 6
    ;; OK button parameters
    Local $okWidth = 75
    Local $okHeight = 25
    Local $okXPos = $width/4-$okWidth/2                             ; ok button centered in 1/4 of GUI width
    Local $okYPos = $editYPos+$editHeight+6
    ;; Cancel button parameters
    Local $cancelWidth = 75
    Local $cancelHeight = 25
    Local $cancelXpos = 3/4*$width-$cancelWidth/2                   ; cancel button centered in 3/4 of GUI width
    Local $cancelYpos = $okYPos
    ;; default $height and $ypos
    If $height == Default Or $height <= 0 Then $height = $cancelYpos+$cancelHeight+$labelYPos                   ; default height of GUI. The sum of all heights and spacements
    If $height < $cancelYpos+$cancelHeight+$labelYPos Then $height = $cancelYpos+$cancelHeight+$labelYPos       ; minimum GUI heigth. The sum of all heights and spacements
    If $height > 3/4*@DesktopHeight Then $height = 3/4*@DesktopHeight                                           ; maximum GUI heigth
    If $ypos == Default Or $ypos <= 0 Then $ypos = @DesktopHeight/2-$height/2                                   ; vertical coordinate
    ;; fix buttons vertical positions after know GUI height
    $okYPos = $height-$labelYPos-$okHeight
    $cancelYpos = $okYPos
    $editYPos = $okYPos-$labelYPos-$editHeight
    ; parent window
    Local $PARENT_EXISTS = ($ParentWindowHandle_Or_GuiIcon <> "" And WinExists($ParentWindowHandle_Or_GuiIcon))
    Local $GUI_Opt_WS_POPUP = 0
    If $PARENT_EXISTS Then $GUI_Opt_WS_POPUP = $WS_POPUP
    ;; password opt
    Local $Input_Opt_BitOr = $GUI_SS_DEFAULT_INPUT
    If $pwchar Then $Input_Opt_BitOr = BitOR($GUI_SS_DEFAULT_INPUT,$ES_PASSWORD)
    ;; InputBox GUI
    Local $GUIInputBox = GUICreate($title, $width, $height, $xpos, $ypos, $GUI_SS_DEFAULT_GUI-$WS_MINIMIZEBOX+$GUI_Opt_WS_POPUP, 0, $ParentWindowHandle_Or_GuiIcon)
    GUISetFont($InputBoxFontSize, $InputBoxFontWeight, 0, $InputBoxFontName)
    Local $labelInput = GUICtrlCreateLabel($text, $labelXPos, $labelYPos, $labelWidth, $labelHeight)
    Local $editInput = GUICtrlCreateInput($input, $editXPos, $editYPos, $editWidth, $editHeight, $Input_Opt_BitOr)
    Local $ButtonOk = GUICtrlCreateButton("&OK", $okXPos, $okYPos, $okWidth, $okHeight, $BS_DEFPUSHBUTTON)
    Local $ButtonCancel = GUICtrlCreateButton("&Cancelar", $cancelXpos, $cancelYpos, $cancelWidth, $cancelHeight)
    If $ParentWindowHandle_Or_GuiIcon <> "" And Not StringIsXDigit(StringTrimLeft($ParentWindowHandle_Or_GuiIcon, 2)) Then GUISetIcon($ParentWindowHandle_Or_GuiIcon)
    If $GuiIcon Then GUISetIcon($GuiIcon)
    GUISetState()
    ;
    Local $error
    Local $timer = TimerInit()
    ;
    If $PARENT_EXISTS Then WinSetState($ParentWindowHandle_Or_GuiIcon, "", @SW_DISABLE)
    While 1
        Switch GUIGetMsg()
            Case $GUI_EVENT_CLOSE
                $input = ""
                $error = 1
                ExitLoop
            Case $ButtonCancel
                $input = ""
                $error = 1
                ExitLoop
            Case $ButtonOk
                $input = GUICtrlRead($editInput)
                $error = 0
                ExitLoop
        EndSwitch
        If $timeout > 0 And TimerDiff($timer) >= $timeout*1000 Then
            $input = ""
            $error = 2
            ExitLoop
        EndIf
    WEnd
    If $PARENT_EXISTS Then WinSetState($ParentWindowHandle_Or_GuiIcon, "", @SW_ENABLE)
    ;
    GUIDelete($GUIInputBox)
    Opt("GUIOnEventMode", $optbak)
    If $error Then Return SetError($error, 0, $input)
    Return $input
EndFunc

; #STRINGSIZE# =====================================================================================
; Name...........: _StringSize
; Description ...: Returns the size (in pixels) of an string.
; Syntax.........: _StringSize( "string" [, size [, weight [, fontname ]]] )
; Parameters ....: string - The string to evaluate the size.
; Size - [Optional] Fontsize (default is 9).
; Weight - [Optional] Font weight (default 400 = normal).
; FontName - [Optional] Font to use (OS default GUI font is used if the font is "" or is not found).
; Requirement(s).:
; Return values .: Success - Returns a 2-element array containing the following information:
; $array[0] = Width
; $array[1] = Height
; Failure - Returns the same array with 0 and sets @error to 1.
; Author ........: jscript
; Example .......: _StringSize( "Text" )
; ==================================================================================================
Func _StringSize($sString, $iSize = 8, $iWeight = 400, $sFontName = "")
    Local $hWnd, $hGuiSwitch, $iCtrlID, $aCtrlSize, $aRetSize[2] = [0, 0]
    $hWnd = GUICreate("StringExInternalWin", 0, 0, 0, 0, BitOR(0x80000000, 0x20000000), BitOR(0x00000080, 0x00000020))
    $hGuiSwitch = GUISwitch($hWnd)
    If $iSize = 65535 Then ; Used by _ImageSize
        $iCtrlID = GUICtrlCreatePic($sString, 0, 0, 0, 0)
    Else
        GUISetFont($iSize, $iWeight, -1, $sFontName, $hWnd)
        $iCtrlID = GUICtrlCreateLabel($sString, 0, 0)
    EndIf
    $aCtrlSize = ControlGetPos($hWnd, "", $iCtrlID)
    GUIDelete($hWnd)
    GUISwitch($hGuiSwitch)
    If IsArray($aCtrlSize) Then
        $aRetSize[0] = $aCtrlSize[2]; Width
        $aRetSize[1] = $aCtrlSize[3]; Height
        Return SetError(0, 0, $aRetSize)
    EndIf
    Return SetError(1, 0, $aRetSize)
EndFunc ;==>_StringSize

 

Edited by shuaua
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...