Jump to content

MsgBox no more!


rbhkamal
 Share

Recommended Posts

I'm sure people here did this in different ways but anyway, here is what I always use

As long as its AutoIt you can modify it the way you want.

;Example
Opt( "GUIOnEventMode" , 1 )
$Handle = GUICreate( "Main" , 200 , 200 )
GUISetOnEvent( -3 , 'Quit' , $Handle)
GUICtrlCreateButton( "OK only" , 50 , 20 , 100 , 25 )
GUICtrlSetOnEvent( -1 , 'Event_1' )
GUICtrlCreateButton( "Yes and No" , 50 , 50 , 100 , 25 )
GUICtrlSetOnEvent( -1 , 'Event_2' )
GUICtrlCreateButton( "OK and Cancel with InputBox" , 30 , 100 , 150 , 25 )
GUICtrlSetOnEvent( -1 , 'Event_3' )
GUISetState()
While 1
    Sleep(10)
WEnd

Func Event_1()
    ConsoleWrite( @LF & " >> Return = "&message( "Message " , 'Will time out in 10 sec.'&@LF&'Parent GUI is still enabled.' , $Handle , 0 , 0 , 10000 )&" -- @error:"&@error )
EndFunc
Func Event_2()
    ConsoleWrite( @LF & " >> Return = "&message( "Message " , 'Will time out in 10 sec.'&@LF&'Parent GUI is still enabled.' , $Handle , 4 , 0 , 10000 )&" -- @error:"&@error )
EndFunc

Func Event_3()
    ConsoleWrite( @LF & " >> Return = "&message( "Message " , 'Will time out in 10 sec, parent GUI is still enabled.' , $Handle , 10 , 0 , 10000 )&" -- @error:"&@error )
EndFunc

Func Quit()
    If message( "Exiting.. " , 'Are you sure?' , $Handle , 4 ) = 6 Then Exit; Or use WinGetHandle( "Main") instead of $Handle.
EndFunc

    
;===============================================================================
;
; Function Name:    message()
; Parameter(s):     $P_title        - The Title of the message
;                   $P_Text         - Text of the message
;                   $P_parent       - The Window handle of the Parent GUI.
;                   $P_flag         - 0: will display "OK" button only.
;                                     4: Will display "Yes" and "No" buttons.
;                                     10: Will display "OK" and "Cancel" with InputBox
;                   $P_dis          - If = 1 then the Parent GUI will be disabled
;                   $P_timeOut      - Time out value in ms.
; Return Value(s):  On "Time Out": @error = 1 and return = -1
;                   If "Yes" clicked returns 6
;                   If "No" or "Cancel" returns 7 and sets @error=1
;                   If Input Box and pressed "OK" will return a string with value of the inputbox.
;
;===============================================================================
;   
Func message($P_title, $P_Text, $P_parent, $P_flag = 0, $P_dis = 1, $P_timeOut = 0)
    GUISetState(@SW_SHOW, $P_parent)
    GUISetState(@SW_RESTORE, $P_parent);;restoring the GUI.
    $L_mode_backup = Opt("GUIOnEventMode", 0)
    Local Const $L_char_width = 5, $L_button_width = 70, $L_button_hight = 23, $L_char_hight = 15
    Local $L_get_win_pos, $L_MSG_BAD = -123, $L_input = -124, $L_temp_read, $L_input_Width, $L_line_count, $L_TEMP_char_perLine
    Local $L_lable_width, $L_lable_hight, $L_temp, $L_get_win_pos, $L_temp_time, $L_first_button_left, $L_second_button_left
    $L_line_count = StringSplit($P_Text, @LF, 1)
    Dim $L_char_perLine[$L_line_count[0] + 1]
    If $P_dis = 1 Then
        GUISetState(@SW_DISABLE, $P_parent)
    ElseIf $P_dis = 2 Then
        GUISetState(@SW_DISABLE, $P_parent)
        GUISetState(@SW_HIDE, $P_parent)
    EndIf
    
    Select
        Case $L_line_count[0] > 1
            $L_char_perLine[0] = $L_line_count[0]
            For $L_a = 1 To $L_line_count[0]
                $L_TEMP_char_perLine = StringSplit($L_line_count[$L_a], '')
                $L_char_perLine[$L_a] = $L_TEMP_char_perLine[0]
            Next
        Case $L_line_count[0] = 1
            $L_char_perLine = StringSplit($P_Text, "")
            $L_char_perLine[1] = $L_char_perLine[0]
            $L_char_perLine[0] = 1
    EndSelect
    
    ;;Calculating the lable width...and Lable hight..
    Select
        Case $L_char_perLine[0] = 1
            $L_lable_hight = $L_char_hight
            $L_lable_width = ($L_char_perLine[1] * $L_char_width)
        Case $L_char_perLine[0] > 1
            $L_temp = $L_char_perLine[1]
            For $L_a = 2 To $L_char_perLine[0]
                If $L_temp < $L_char_perLine[$L_a] Then
                    $L_temp = $L_char_perLine[$L_a]
                EndIf
            Next
            ;setting the width
            $L_lable_width = ($L_temp * $L_char_width)
            ;setting the hight
            $L_lable_hight = ($L_char_hight * $L_char_perLine[0])
    EndSelect
    ;;Calculating the GUI width...
    Select
        Case $P_flag = 0 ; The OK button and the LABLE
            $L_GUI_Width = $L_lable_width + 30
            $L_GUI_hight = $L_lable_hight + 85
            
        Case $P_flag = 4  ; TWO BUTTONS and the LABLE
            $L_GUI_hight = $L_lable_hight + 85
            $L_GUI_Width = $L_lable_width + 30
            
        Case $P_flag = 10 ; TWO BUTTONS , INPUT and the LABLE
            $L_GUI_Width = $L_lable_width + 30
            $L_GUI_hight = $L_lable_hight + 85 + 30
    EndSelect
    
    ;;Calculating buffer space between yes and no buttons
    $L_first_button_left = (($L_GUI_Width / 4) - ($L_button_width / 2)) + ($L_GUI_Width / 10)
    If $L_first_button_left < 5 Then
        $L_first_button_left = 5
    EndIf
    
    $L_second_button_left = ((($L_GUI_Width / 4) * 3) - ($L_button_width / 2)) - ($L_GUI_Width / 10)
    If ($L_second_button_left) < ($L_first_button_left + $L_button_width + 4) Then
        $L_second_button_left = ($L_first_button_left + $L_button_width + 4)
    EndIf
    
    If ($L_second_button_left - $L_first_button_left) > 150 Then
        $L_buf_adjus = ($L_second_button_left - $L_first_button_left) / 4
        $L_first_button_left = $L_first_button_left + $L_buf_adjus
        $L_second_button_left = $L_second_button_left - $L_buf_adjus
    EndIf
    
    If $L_second_button_left + $L_button_width + 5 > $L_GUI_Width Then
        $L_GUI_Width = $L_second_button_left + $L_button_width + 10
    EndIf
    
    $L_get_win_pos = WinGetPos($P_parent)
    $L_MSG_GUI = GUICreate($P_title, $L_GUI_Width, $L_GUI_hight , ($L_get_win_pos[0] + ($L_get_win_pos[2] / 2)) - ($L_GUI_Width / 2) , ($L_get_win_pos[1] + ($L_get_win_pos[3] / 2)) - ($L_GUI_hight / 2), 0x00000001, -1, $P_parent)
    $L_MSG_lable = GUICtrlCreateLabel($P_Text, 10, 10, $L_lable_width + 10, $L_lable_hight)
    ;GUICtrlSetBkColor( -1 , 0x00ff00 )
    Select
        Case $P_flag = 0
            $L_MSG_GOOD = GUICtrlCreateButton( "  OK   " , (($L_GUI_Width / 2)) - ($L_button_width / 2) , ($L_GUI_hight - 30) - ($L_button_hight + 10), $L_button_width, $L_button_hight)
        Case $P_flag = 4
            $L_MSG_GOOD = GUICtrlCreateButton( "  Yes   ", $L_first_button_left , ($L_GUI_hight - 30) - ($L_button_hight + 10), $L_button_width, $L_button_hight)
            $L_MSG_BAD = GUICtrlCreateButton( "  No   ", $L_second_button_left , ($L_GUI_hight - 30) - ($L_button_hight + 10), $L_button_width, $L_button_hight)
        Case $P_flag = 10
            $L_MSG_GOOD = GUICtrlCreateButton( "  OK   ", $L_first_button_left, ($L_GUI_hight - 30) - ($L_button_hight + 10), $L_button_width, $L_button_hight)
            $L_MSG_BAD = GUICtrlCreateButton( "  Cancel   ", $L_second_button_left , ($L_GUI_hight - 30) - ($L_button_hight + 10), $L_button_width, $L_button_hight)
    EndSelect
    If $P_flag = 10 Then
        If $L_GUI_Width > 400 Then
            $L_input_Width = 340
        Else
            $L_input_Width = $L_GUI_Width - 50
        EndIf
        
        $L_input = GUICtrlCreateInput("" , (($L_GUI_Width / 2) - ($L_input_Width / 2)) , ($L_GUI_hight - ($L_button_hight + 10 + 60)), $L_input_Width, 20)
    EndIf
    
    GUISetState(@SW_SHOW, $L_MSG_GUI)
    If $P_timeOut <> 0 Then
        $L_temp_time = TimerInit()
    EndIf
    While 1
        If $P_timeOut <> 0 Then
            If (TimerDiff($L_temp_time) = $P_timeOut) or (TimerDiff($L_temp_time) > $P_timeOut) Then
                GUIDelete($L_MSG_GUI)
                If $P_dis = 2 Then
                    GUISetState(@SW_SHOW, $P_parent)
                    GUISetState(@SW_ENABLE, $P_parent)
                ElseIf $P_dis = 1 Then
                    GUISetState(@SW_ENABLE, $P_parent)
                EndIf
                Opt("GUIOnEventMode", $L_mode_backup)
                GUISetState(@SW_RESTORE, $P_parent)
                Return -1
            EndIf
        EndIf
        
        $L_MSG_MSG = GUIGetMsg(1)
        Select
            Case ($L_MSG_MSG[0] = $L_MSG_GOOD) And ($L_MSG_MSG[1] == $L_MSG_GUI) And ($P_flag <> 10)
                GUIDelete($L_MSG_GUI)
                If $P_dis = 2 Then
                    GUISetState(@SW_SHOW, $P_parent)
                    GUISetState(@SW_ENABLE, $P_parent)
                ElseIf $P_dis = 1 Then
                    GUISetState(@SW_ENABLE, $P_parent)
                EndIf
                Opt("GUIOnEventMode", $L_mode_backup)
                GUISetState(@SW_RESTORE, $P_parent)
                If $P_flag = 0 Then Return 0
                If $P_flag = 4 Then Return 6
                
            Case ($L_MSG_MSG[0] = $L_MSG_BAD) And ($L_MSG_MSG[1] == $L_MSG_GUI) And ($P_flag <> 0)
                GUIDelete($L_MSG_GUI)
                If $P_dis = 2 Then
                    GUISetState(@SW_SHOW, $P_parent)
                    GUISetState(@SW_ENABLE, $P_parent)
                ElseIf $P_dis = 1 Then
                    GUISetState(@SW_ENABLE, $P_parent)
                EndIf
                Opt("GUIOnEventMode", $L_mode_backup)
                GUISetState(@SW_RESTORE, $P_parent)
                SetError(1)
                Return 7
                
            Case ($L_MSG_MSG[0] = $L_MSG_GOOD) And ($L_MSG_MSG[1] == $L_MSG_GUI) And ($P_flag = 10)
                $L_temp_read = GUICtrlRead($L_input)
                GUIDelete($L_MSG_GUI)
                If $P_dis = 2 Then
                    GUISetState(@SW_SHOW, $P_parent)
                    GUISetState(@SW_ENABLE, $P_parent)
                ElseIf $P_dis = 1 Then
                    GUISetState(@SW_ENABLE, $P_parent)
                EndIf
                Opt("GUIOnEventMode", $L_mode_backup)
                GUISetState(@SW_RESTORE, $P_parent)
                Return $L_temp_read
        EndSelect
    WEnd
    GUISetState(@SW_ENABLE, $P_parent)
    GUISetState(@SW_RESTORE, $P_parent)
    Opt("GUIOnEventMode", $L_mode_backup)
EndFunc   ;==>message

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

Link to comment
Share on other sites

I do like, but can't you just use GUIGetMsg(1)?

Global $Handle = GUICreate( "Main" , 200 , 200 )
$Button1 = GUICtrlCreateButton( "OK only" , 50 , 20 , 100 , 25 )
$Button2 = GUICtrlCreateButton( "Yes and No" , 50 , 50 , 100 , 25 )
$Button3 = GUICtrlCreateButton( "OK and Cancel with InputBox" , 30 , 100 , 150 , 25 )
GUISetState()
While 1
    $msg = GUIGetMsg(1)
    If $msg[1] = $Handle Then
        Select
        Case $msg[0] = -3
            Exit
        Case $msg[0] = $Button1
            _OKGUI()
        Case $msg[0] = $Button2
            _YNGUI
        Case $msg[0] = $Button3
            _IBGUI
        EndSelect
    EndIf
    Sleep(10)
; If $msg[1] = $OKGUI such and such
WEnd
Exit
; GUI Functions...

Because I think you can. I also think it would be WAY easier then what you just did IMHO.

Edited by AutoIt Smith
Link to comment
Share on other sites

and... maybe

#include <GuiConstants.au3>

_msgbox(1, "Please Press OK")
_msgbox(2)
_msgbox(3, "Would you like an Input-Box")

Func _msgbox($xbox = 1, $mesg = "Please Respond")
Global $Handle = GUICreate( "Main" , 200 , 100 )
Local $Button1, $Button2, $Button3, $CButton
GUICtrlCreateLabel( $mesg, 10, 20, 180, 20, $SS_CENTER)
If $xbox = 1 Then $Button1 = GUICtrlCreateButton( "OK only" , 50 , 50 , 100 , 25 )
If $xbox = 2 Then $Button2 = GUICtrlCreateButton( "Yes" , 10 , 50 , 80 , 25 )
If $xbox = 2 Then $CButton = GUICtrlCreateButton( "No" , 100 , 50 , 80 , 25 )
If $xbox = 3 Then $Button3 = GUICtrlCreateButton( "OK" , 10 , 50 , 80 , 25 )
If $xbox = 3 Then $CButton = GUICtrlCreateButton( "Cancel" , 100 , 50 , 80 , 25 )
GUISetState()
While 1
    $msg = GUIGetMsg(1)
    If $msg[1] = $Handle Then
        Select
        Case $msg[0] = -3
            Exit
        Case $msg[0] = $Button1
           ;_OKGUI()
            Return
        Case $msg[0] = $Button2
           ;_YNGUI
            Return
        Case $msg[0] = $Button3
           ;_IBGUI
            Return
        Case $msg[0] = $CButton
            Return
        EndSelect
    EndIf
    Sleep(10)
; If $msg[1] = $OKGUI such and such
WEnd
EndFunc
; GUI Functions...

8)

NEWHeader1.png

Link to comment
Share on other sites

@AutoIt Smith and Valuater.

In the example I posted above I used GUIOnEventMode for the Parent Window, however if GUIOnEventMode

was disabled, GUIgetMsg (1) will capture all the events from both the Parent and the Child window.

for that reason I had to make sure that the events are actually coming from the child window not the Parent window.

so I used $MSG[1] and $MSG[0].

@All

I will try to clean it up today and make it simpler to use.

also will add icons.

Regards,

RK

"When the power of love overcomes the love of power, the world will know peace"-Jimi Hendrix

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...