Jump to content

Perfect modal dialog snippet


Recommended Posts

Throughout time, many people have asked how to code a modal dialog box, but after a quick search on the forum I could only find that snippet, that looks almost flawless.

#include <GuiConstants.au3>

$main = GUICreate("MyGUI", 392, 322)

$Button_1 = GUICtrlCreateButton("Input", 140, 190, 70, 30)

GUISetState()
While 1
    $msg = GUIGetMsg()
    Select
        Case $msg = $GUI_EVENT_CLOSE
            ExitLoop
        Case $msg = $Button_1
            GUISetState(@SW_DISABLE, $main)
            _Input()
            GUISetState(@SW_ENABLE, $main)
            GUISetState(@SW_SHOW)
        Case Else
        ;;;
    EndSelect
WEnd
Exit

Func _Input()
    $popup = GUICreate("PopUP", 191, 85, -1, -1, $WS_DLGFRAME, $WS_EX_TOPMOST)
    
    $Input_1 = GUICtrlCreateInput("Input1", 0, 0, 180, 20)
    $Button_2 = GUICtrlCreateButton("OK", 60, 40, 60, 20)
    
    GUISetState()
    While 1
        $msg2 = GUIGetMsg()
        Select
            Case $msg2 = $Button_2
                ExitLoop
            Case Else
            ;;;
        EndSelect
    WEnd
    GUIDelete($popup)
EndFunc  ;==>_Input

(note : some "include" are missing. I don't know if it's my version of AutoIT, but if I don't add other GUI-related includes it won't work. I assume you've added those includes yourself)

I can still see 2 issues with that code :

1. When you close the "modal" input window, the other window doesn't come back to the foreground. Does anyone know how to do that?

2. The modal dialog doesn't have a "close" button (red cross). How to add it and to handle the event?

Thanks for sharing your knowledge :-)

Link to comment
Share on other sites

...

I can still see 2 issues with that code :

1. When you close the "modal" input window, the other window doesn't come back to the foreground. Does anyone know how to do that?

2. The modal dialog doesn't have a "close" button (red cross). How to add it and to handle the event?

Thanks for sharing your knowledge :-)

1) You can do it by using WinActivate. If you want to be able to use it more globally, you can use this:

Func _Input ()
   $sWndActive = WinGetTitle ("[ACTIVE]", "")
   GUICreate (...)
   ; Code goes here
   GUIDelete ($popup)
   WinActivate ($sWndActive, "")
EndFunc   ;==>_Input

2) To get the cross, combine the $WS_DLGFRAME with $WS_SYSMENU and $WS_CAPTION

BitOR ($WS_DLGFRAME, $WS_SYSMENU, $WS_CAPTION)
You can handle the event in the same way as you would with a standard GUI.

And finally... Can't you make a msgbox modal?? 4096System modal (dialog has an icon)

MDiesel

Edited by mdiesel
Link to comment
Share on other sites

1) You can do it by using WinActivate.

2) BitOR ($WS_DLGFRAME, $WS_SYSMENU, $WS_CAPTION)

Thanks!

Can't you make a msgbox modal?? 4096System modal (dialog has an icon)

I'm not sure of your question. If you mean that I can use a built-in function to make a modal msgbox, then yes, it's easy to do. But the snipett above is meant to show how to make any custom window modal.

Thanks again.

=> SOLVED

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