MsgBoxEx() Gui Submit is hiding Owner Topic is solved

Old Topics related to the original "AutoGUI" ahk script editor.
User avatar
megnatar
Posts: 92
Joined: 27 Oct 2014, 20:49
Location: The Netherlands
Contact:

MsgBoxEx() Gui Submit is hiding Owner

Post by megnatar » 15 Apr 2020, 04:52

Hey folk,

While using MsgBoxEx in my script I noticed that the GUI was hiding it's owner when using timeout.
So I changed the line Gui Submit at the end in the function to the following.

Code: Select all

Gui Submit, % Owner ? "NoHide" : ""
I think it's preferable that a owned msgbox does not hide it's owner after submitting to the main GUI.

Have Fun,
Megnatar
Last edited by megnatar on 15 Apr 2020, 17:43, edited 1 time in total.

User avatar
megnatar
Posts: 92
Joined: 27 Oct 2014, 20:49
Location: The Netherlands
Contact:

Re: AutoGui: MsgBoxEx() hiding Gui:1

Post by megnatar » 15 Apr 2020, 16:52

Two working examples showing what happens when timeout period is used. The old code hides the first Gui after submitting changes to it.

I changed it in such a way that when the MsgBox is owned (Application model), hiding the main instance doesn't happen.
The MsgBox will continue hiding the main window when its not owned and timeout is used.

Added:
Shows countdown period in the titlebar if timeout is enabled.

Changed Code:

Code: Select all

#NoEnv 
#SingleInstance force
#KeyHistory 0 
SetBatchLines -1
Listlines off

OnTop := 1

GUI % "+LastFound " (OnTop ? "+" : "-") "AlwaysOnTop +OwnDialogs +hWndhScriptGui -Theme"
Gui add, button, w130, MsgBox Test
Gui, Show
Return

ButtonMsgBoxtest:
Text := "Work in progress`nNot Implemented yet."
MsgBoxEx(Text, "ToDo", "Close", [65, "imageres.dll"], "", "AlwaysOnTop", hScriptGui, 5)
Return

GuiClose:
GuiEscape:
Exitapp

MsgBoxEx(Text, Title := "", Buttons := "", Icon := "", ByRef CheckText := "", Styles := "", Owner := "", Timeout := "", FontOptions := "", FontName := "", BGColor := "", Callback := "") {
    Static hWnd, y2, p, px, pw, c, cw, cy, ch, f, o, gL, hBtn, lb, ww, Off, k, v, RetVal
    Static Sound := {2: "*48", 4: "*16", 5: "*64"}

    Gui New, hWndhWnd LabelMsgBoxEx -0xA0000
    Gui % (Owner) ? "+Owner" . Owner : ""
    Gui Font
    Gui Font, % (FontOptions) ? FontOptions : "s9", % (FontName) ? FontName : "Segoe UI"
    Gui Color, % (BGColor) ? BGColor : "White"
    Gui Margin, 10, 12

    If (IsObject(Icon)) {
        Gui Add, Picture, % "x20 y24 w32 h32 Icon" . Icon[1], % (Icon[2] != "") ? Icon[2] : "shell32.dll"
    } Else If (Icon + 0) {
        Gui Add, Picture, x20 y24 Icon%Icon% w32 h32, user32.dll
        SoundPlay % Sound[Icon]
    }

    Gui Add, Link, % "x" . (Icon ? 65 : 20) . " y" . (InStr(Text, "`n") ? 24 : 32) . " vc", %Text%
    GuicontrolGet c, Pos
    GuiControl Move, c, % "w" . (cw + 30)
    y2 := (cy + ch < 52) ? 90 : cy + ch + 34

    Gui Add, Text, vf -Background ; Footer

    Gui Font
    Gui Font, s9, Segoe UI
    px := 42
    If (CheckText != "") {
        CheckText := StrReplace(CheckText, "*",, ErrorLevel)
        Gui Add, CheckBox, vCheckText x12 y%y2% h26 -Wrap -Background AltSubmit Checked%ErrorLevel%, %CheckText%
        GuicontrolGet p, Pos, CheckText
        px := px + pw + 10
    }

    o := {}
    Loop Parse, Buttons, |, *
    {
        gL := (Callback != "" && InStr(A_LoopField, "...")) ? Callback : "MsgBoxExBUTTON"
        Gui Add, Button, hWndhBtn g%gL% x%px% w90 y%y2% h26 -Wrap, %A_Loopfield%
        lb := hBtn
        o[hBtn] := px
        px += 98
    }
    GuiControl +Default, % (RegExMatch(Buttons, "([^\*\|]*)\*", Match)) ? Match1 : StrSplit(Buttons, "|")[1]
    
    Gui Show, Autosize Center Hide
    DetectHiddenWindows % A_DetectHiddenWindows = "Off" ? "On" : A_DetectHiddenWindows 
    WinGetPos,,, ww,, ahk_id %hWnd%
    GuiControlGet p, Pos, %lb% ; Last button
    Off := ww - (((px + pw + 14) * A_ScreenDPI) // 96)
    For k, v in o {
        GuiControl Move, %k%, % "x" . (v + Off)
    }
    Guicontrol MoveDraw, f, % "x-1 y" . (y2 - 10) . " w" . ww . " h" . 48
    DetectHiddenWindows % A_DetectHiddenWindows = "Off" ? "On" : "Off"
    
    Gui Show,, % Timeout ? Title " : " Timeout : Title
    Gui +SysMenu %Styles%

    If (Owner) {
        WinSet Disable,, ahk_id %Owner%
    }

    If (Timeout) {
        CountDown := Round(Timeout) * 1000
        SetTimer MsgBoxExTIMEOUT, 1000
    }
    
    GuiControl Focus, f
    Gui Font
    WinWaitClose ahk_id %hWnd%
    Return RetVal
    
    MsgBoxExBUTTON:
    MsgBoxExESCAPE:
    MsgBoxExCLOSE:
    MsgBoxExTIMEOUT:
        If (A_ThisLabel == "MsgBoxExTIMEOUT") {
            if (CountDown := Round(CountDown) - 1000) {
                WinSetTitle ahk_id %hWnd%, , % Title " : " CountDown // 1000
                Return
            }
            SetTimer MsgBoxExTIMEOUT, Delete
        }
        
        If (A_ThisLabel == "MsgBoxExBUTTON") {
            RetVal := StrReplace(A_GuiControl, "&")
        } Else {
            RetVal := (A_ThisLabel == "MsgBoxExTIMEOUT") ? "Timeout" : "Cancel"
        }
        
        If (Owner) {
            WinSet Enable,, ahk_id %Owner%
        }
	
        ; Gui Submit, nohide 					; Never hide main GUI instance when submitting changes to it.
        Gui Submit, % Owner ? "NoHide" : ""		; Only hide when it's not owned and timeout is used.
        Gui %hWnd%: Destroy
    Return
}
Old Code:

Code: Select all

#NoEnv 
#SingleInstance force
#KeyHistory 0 
SetBatchLines -1
Listlines off

OnTop := 1

GUI % "+LastFound " (OnTop ? "+" : "-") "AlwaysOnTop +OwnDialogs +hWndhScriptGui -Theme"
Gui add, button, w130, MsgBox Test
Gui, Show
Return

ButtonMsgBoxtest:
Text := "Work in progress`nNot Implemented yet."
MsgBoxEx(Text, "ToDo" count, "Close", [65, "imageres.dll"], "", "AlwaysOnTop", hScriptGui, 5)
Return

GuiClose:
GuiEscape:
Exitapp

MsgBoxEx(Text, Title := "", Buttons := "", Icon := "", ByRef CheckText := "", Styles := "", Owner := "", Timeout := "", FontOptions := "", FontName := "", BGColor := "", Callback := "") {
    Static hWnd, y2, p, px, pw, c, cw, cy, ch, f, o, gL, hBtn, lb, DHW, ww, Off, k, v, RetVal
    Static Sound := {2: "*48", 4: "*16", 5: "*64"}

    Gui New, hWndhWnd LabelMsgBoxEx -0xA0000
    Gui % (Owner) ? "+Owner" . Owner : ""
    Gui Font
    Gui Font, % (FontOptions) ? FontOptions : "s9", % (FontName) ? FontName : "Segoe UI"
    Gui Color, % (BGColor) ? BGColor : "White"
    Gui Margin, 10, 12

    If (IsObject(Icon)) {
        Gui Add, Picture, % "x20 y24 w32 h32 Icon" . Icon[1], % (Icon[2] != "") ? Icon[2] : "shell32.dll"
    } Else If (Icon + 0) {
        Gui Add, Picture, x20 y24 Icon%Icon% w32 h32, user32.dll
        SoundPlay % Sound[Icon]
    }

    Gui Add, Link, % "x" . (Icon ? 65 : 20) . " y" . (InStr(Text, "`n") ? 24 : 32) . " vc", %Text%
    GuicontrolGet c, Pos
    GuiControl Move, c, % "w" . (cw + 30)
    y2 := (cy + ch < 52) ? 90 : cy + ch + 34

    Gui Add, Text, vf -Background ; Footer

    Gui Font
    Gui Font, s9, Segoe UI
    px := 42
    If (CheckText != "") {
        CheckText := StrReplace(CheckText, "*",, ErrorLevel)
        Gui Add, CheckBox, vCheckText x12 y%y2% h26 -Wrap -Background AltSubmit Checked%ErrorLevel%, %CheckText%
        GuicontrolGet p, Pos, CheckText
        px := px + pw + 10
    }

    o := {}
    Loop Parse, Buttons, |, *
    {
        gL := (Callback != "" && InStr(A_LoopField, "...")) ? Callback : "MsgBoxExBUTTON"
        Gui Add, Button, hWndhBtn g%gL% x%px% w90 y%y2% h26 -Wrap, %A_Loopfield%
        lb := hBtn
        o[hBtn] := px
        px += 98
    }
    GuiControl +Default, % (RegExMatch(Buttons, "([^\*\|]*)\*", Match)) ? Match1 : StrSplit(Buttons, "|")[1]

    Gui Show, Autosize Center Hide, %Title%
    DHW := A_DetectHiddenWindows
    DetectHiddenWindows On
    WinGetPos,,, ww,, ahk_id %hWnd%
    GuiControlGet p, Pos, %lb% ; Last button
    Off := ww - (((px + pw + 14) * A_ScreenDPI) // 96)
    For k, v in o {
        GuiControl Move, %k%, % "x" . (v + Off)
    }
    Guicontrol MoveDraw, f, % "x-1 y" . (y2 - 10) . " w" . ww . " h" . 48

    Gui Show
    Gui +SysMenu %Styles%
    DetectHiddenWindows %DHW%

    If (Timeout) {
        SetTimer MsgBoxExTIMEOUT, % Round(Timeout) * 1000
    }

    If (Owner) {
        WinSet Disable,, ahk_id %Owner%
    }

    GuiControl Focus, f
    Gui Font
    WinWaitClose ahk_id %hWnd%
    Return RetVal

    MsgBoxExESCAPE:
    MsgBoxExCLOSE:
    MsgBoxExTIMEOUT:
    MsgBoxExBUTTON:
        SetTimer MsgBoxExTIMEOUT, Delete

        If (A_ThisLabel == "MsgBoxExBUTTON") {
            RetVal := StrReplace(A_GuiControl, "&")
        } Else {
            RetVal := (A_ThisLabel == "MsgBoxExTIMEOUT") ? "Timeout" : "Cancel"
        }

        If (Owner) {
            WinSet Enable,, ahk_id %Owner%
        }

        Gui Submit
        Gui %hWnd%: Destroy
    Return
}

User avatar
Alguimist
Posts: 428
Joined: 05 Oct 2015, 16:41
Contact:

Re: MsgBoxEx() Gui Submit is hiding Owner  Topic is solved

Post by Alguimist » 27 Apr 2020, 10:01

@megnatar
Thanks for reporting, I will revise the code. Apparently, all that has to be done is to provide the hWnd when calling submit: Gui %hWnd%: Submit, and remove the Gui Destroy line.

User avatar
megnatar
Posts: 92
Joined: 27 Oct 2014, 20:49
Location: The Netherlands
Contact:

Re: MsgBoxEx() Gui Submit is hiding Owner

Post by megnatar » 06 May 2020, 04:38

Alguimist wrote:
27 Apr 2020, 10:01
@megnatar
Thanks for reporting, I will revise the code. Apparently, all that has to be done is to provide the hWnd when calling submit: Gui %hWnd%: Submit, and remove the Gui Destroy line.
You're welcome. Yes that crossed my mind to, but I was not sure "hiding" was a choice or something overlooked.

Have a nice day!
Megnatar

Post Reply

Return to “Old Topics”