Pausing a Gui control

Get help with using AutoHotkey (v1.1 and older) and its commands and hotkeys
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Pausing a Gui control

15 Dec 2018, 09:37

I have a really nice little function to display an image that I modified from somewhere else.
It works great, but without the MsgBox line, the image will appear and then disappear instantly.
I get that the Gui, Destroy line is doing that, but what I want to do is pause processing until the user closes the gui (either by pressing the top right 'X' close button, or by a button on the gui control if anyone knows how to do that?).
Does anyone know how to do some kind of pause-processing option so as not to do the Gui, Destroy until the user presses a button to proceed?

Code: Select all

ShowImage(imagepath, imagetitle)
{
    Gui, +Resize
    Gui, Add, Pic, xm vPic
    if imagepath =
        return
    Height := A_ScreenHeight - 100
    Width  := -1   ; -1 is "Keep aspect ratio"
    GuiControl, , Pic, *w%width% *h%height% %imagepath%  ; Load the image.
    Gui, Show, x0 y0 AutoSize, %imagetitle%   ; Resize the window to match the picture size. ; xCenter puts in middle screen!
    MsgBox, , Press OK to close image, Press OK or Enter to leave this image.
    Gui, Destroy
    WinWaitClose
    ; ;-----------------------------------
    ; ; event handlers
    ; ;-----------------------------------
    ; ButtonOK: ; "OK" button, {Enter} pressed
    ;     Gui, Destroy
    ; Return
    ;
    ; ButtonCancel: ; "Cancel" button
    ; GuiClose:     ; {Alt+F4} pressed, [X] clicked
    ; GuiEscape:    ; {Esc} pressed
    ;     Index := 0
    ;     Gui, Destroy
    ; Return
}
User avatar
Hellbent
Posts: 2109
Joined: 23 Sep 2017, 13:34

Re: Pausing a Gui control

15 Dec 2018, 16:24

Add these.

Code: Select all

;add this button

Gui,Add,Button,gGuiClose,EXIT




;then add this label 

GuiClose:
  	ExitApp
   
   ; or this one
   
   GuiClose:
   	Gui,Destroy
   	return
	
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Pausing a Gui control

15 Dec 2018, 20:05

Try swapping these two lines:

Code: Select all

    Gui, Destroy
    WinWaitClose
and take out the MsgBox
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Pausing a Gui control

17 Dec 2018, 11:04

I tried both of these solutions, and unfortunately, neither worked! :-(
In both cases, it just does the same as before, the image opens then closes after a few milliseconds.
Any idea what is missing here?
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Pausing a Gui control

17 Dec 2018, 11:16

An example that demonstrates your problem would be nice.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Pausing a Gui control

17 Dec 2018, 11:47

Try this:

Code: Select all

#NoEnv
#SingleInstance, Force

    PictureBox("Test 1", "Lara.jpg")

ExitApp



;-------------------------------------------------------------------------------
PictureBox(Title := "", Picture := "") {
;-------------------------------------------------------------------------------
    ; show a picture
    ; wait until user dismisses
    ;
    ; Title is the title for the GUI
    ; Picture is the picture to display

    ; create GUI
    Gui, PictureBox: New, +LastFound, %Title%
    Gui, -MinimizeBox
    Gui, Margin, 30, 18
    Gui, Add, Picture,, %Picture%

    ; main loop
    Gui, Show
    WinWaitClose

return ; end of function

    ;-----------------------------------
    ; event handlers
    ;-----------------------------------

    PictureBoxGuiClose:     ; {Alt+F4} pressed, [X] clicked
    PictureBoxGuiEscape:    ; {Esc} pressed
        Gui, Destroy
    return
}
I hope that helps.
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Pausing a Gui control

17 Dec 2018, 11:52

Maybe a missing Gui, +LastFound was your problem :?:
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Pausing a Gui control

18 Dec 2018, 07:11

I've got a lot to learn here, but your one works perfectly wolf_II, thanks.
One very small thing, I see your event handler for GuiClose and GuiEscape, and these work great, but I tried to also make the gui close on {Enter} being pressed, but PictureBoxGuiEnter or PictureBoxGuiReturn fail. Do you know how to make {Enter} trigger a gui close?

PictureBoxGuiClose: ; {Alt+F4} pressed, [X] clicked
PictureBoxGuiEscape: ; {Esc} pressed
wolf_II
Posts: 2688
Joined: 08 Feb 2015, 20:55

Re: Pausing a Gui control

18 Dec 2018, 07:30

Try a (hidden) Button, and make that Default, so it will fire on {Enter}-key
then add an event to handle ( exactly as you did :thumbup: )
like this for example:

Code: Select all

#NoEnv
#SingleInstance, Force

    PictureBox("Test 1", "Lara.jpg")

ExitApp



;-------------------------------------------------------------------------------
PictureBox(Title := "", Picture := "") {
;-------------------------------------------------------------------------------
    ; show a picture
    ; wait until user dismisses
    ;
    ; Title is the title for the GUI
    ; Picture is the picture to display

    ; create GUI
    Gui, PictureBox: New, +LastFound, %Title%
    Gui, -MinimizeBox
    Gui, Margin, 30, 18
    Gui, Add, Picture,, %Picture%
    Gui, Add, Button, w0 h0 Hidden Default, Enter

    ; main loop
    Gui, Show
    WinWaitClose

return ; end of function

    ;-----------------------------------
    ; event handlers
    ;-----------------------------------

    PictureBoxGuiClose:     ; {Alt+F4} pressed, [X] clicked
    PictureBoxGuiEscape:    ; {Esc} pressed
    PictureBoxButtonEnter:  ; "Enter" button pressed
        Gui, Destroy
    return
}
I hope that helps.
roysubs
Posts: 428
Joined: 29 Sep 2018, 16:37

Re: Pausing a Gui control

18 Dec 2018, 08:18

Absolutely perfect. Works like a dream. Thanks. :-)

Return to “Ask for Help (v1)”

Who is online

Users browsing this forum: Bing [Bot], Chunjee and 141 guests