Jump to content
Sign in to follow this  
0Ethan0

How to achieve a responsive GUI while running a function with a loop?

Recommended Posts

0Ethan0

Ahoy Autoit Community!

After many trials and errors I am unable to solve a problem I am facing and would appreciate any kind of input or better yet a solution :)

The Premise: An embeded slideshow viewer that runs after double-clicking an item in a ListView (each item will generate a different slideshow images).

The Setup: GUI with a ListView Control and a simple exit button.

The Issue: Once double clicked the slide plays however the GUI "locks"/non responsive until the slide is over. Same thing if I click on the "Test" button.

The Culprit: I believe since it's in the images loop it can't accept any other commands until that loop is over.

The Wish: I want to be able to use the GUI functions (selecting other items, clicking on button etc.) while the slideshow plays.

The Code (stripped and simplified as much as I could):

#include <GuiListView.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

HotKeySet("{Esc}", "_Exit")

Global $c=0
Global $ssGDI[3], $ssGraphic[2], $ssImage
Global Const $bg_color = "000000"
Global Const $ssW = 480, $ssH = 320
Global $aFiles = _FileListToArrayRec("d:\testStage\", "*.jpg;*.png;*.bmp;*.gif;*.JPG;*.PNG;*.BMP;*.GIF", $FLTAR_FILES, $FLTAR_NORECUR ,$FLTAR_SORT ,$FLTAR_FULLPATH )


$guiW = 1200
$guiH = 726
$mainWindow = GUICreate("Slideshow Viewer", $guiW, $guiH, -1, -1, $WS_POPUP)
$Button1 = GUICtrlCreateButton("Exit", 0, 0, 50, 50)
GUICtrlSetOnEvent($Button1, "_Exit")
$Button1 = GUICtrlCreateButton("Test", 60, 0, 50, 50)
GUICtrlSetOnEvent($Button1, "Test")


Global $ListView = GUICtrlCreateListView("Entry Name|Category", 5, 75, 195, 280)
_GUICtrlListView_SetColumnWidth ($ListView, 0, 100)
_GUICtrlListView_SetColumnWidth ($ListView, 1, 100)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("Name 1|Category 1", $ListView)
GUICtrlCreateListViewItem("Name 2|Category 2", $ListView)

screenshotWidgetInit($ssW,$ssH, 690, 100)
GUISetState(@SW_SHOW, $mainWindow)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Exit()
    EndSwitch
WEnd

Func Test()
    For $k = 1 To UBound($aFiles) - 1
        screenshotWidgetTransition($aFiles[$k])
    Next
EndFunc

Func ListView_Click()
    ConsoleWrite("Left Click")
EndFunc

Func ListView_DoubleClick()
    ConsoleWrite("Double Left Click")
    Test()
EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
    Case $wParam = $ListView
        Select
            Case $event = $NM_CLICK
                ListView_Click ()
            Case $event = $NM_DBLCLK
                ListView_DoubleClick ()
            EndSelect
    EndSelect
    Return $GUI_RUNDEFMSG
EndFunc

Func screenshotWidgetTransition($image, $delay = 0, $speed = 1, $sleep = 2000)
    Local $a, $d = $c, $iX, $iY
    $ssImage = _GDIPlus_ImageLoadFromFile($image)
    $iX = _GDIPlus_ImageGetWidth($ssImage)
    $iY = _GDIPlus_ImageGetHeight($ssImage)

    $FDesktop=$ssH/$ssW
    $Fact =1
    If $iX > $ssW And $FDesktop > ($iY/$iX) Then
        $Fact=$ssW/$iX
    ElseIf $iY > $ssH Then
        $Fact=$ssH/$iY
    EndIf
    $H1 = Round(($Fact * $iY),0)
    $W1 = Round(($Fact * $iX),0)

    _GDIPlus_GraphicsDrawImageRect($ssGraphic[$d], $ssImage,($ssW - $W1)/2, ($ssH - $H1) / 2,$W1,$H1)

    WinSetTrans($ssGDI[$d], "", 0)
    WinSetOnTop($ssGDI[$d], "", 1)
    For $a = 0 To 254 Step $speed
        WinSetTrans($ssGDI[$d], "", $a)
        Sleep($delay)
    Next
    WinSetTrans($ssGDI[$d], "", 254)
    WinSetOnTop($ssGDI[Not ($d)], "", 0)
    WinSetTrans($ssGDI[Not ($d)], "", 0)
    _GDIPlus_GraphicsClear($ssGraphic[Not ($d)])
    $c = 1 - $d

    _GDIPlus_ImageDispose ($ssImage) ; very important to realease the pics
    Sleep($sleep)
EndFunc   ;==>screenshotWidgetTransition

Func screenshotWidgetInit($ssW,$ssH,$ssX,$ssY)
    $ssGDI[2] = GUICreate("", $ssW, $ssH, $ssX, $ssY, $WS_POPUP, $WS_EX_MDICHILD, $mainWindow)
    $ssGDI[0] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2])
    $ssGDI[1] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2])
;    GUISetBkColor("0x" & $bg_color, $ssGDI[2])
    GUISetState(@SW_SHOW, $ssGDI[2])
    GUISetState(@SW_SHOW, $ssGDI[0])
    GUISetState(@SW_SHOW, $ssGDI[1])
    WinSetTrans($ssGDI[0], "", 0)
    WinSetTrans($ssGDI[1], "", 0)
    _GDIPlus_Startup()
    $ssGraphic[0] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[0])
    $ssGraphic[1] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[1])
    _GDIPlus_GraphicsClear($ssGraphic[0], "0xFF" & $bg_color)
    _GDIPlus_GraphicsClear($ssGraphic[1], "0xFF" & $bg_color)
EndFunc   ;==>screenshotWidgetInit

Func _Exit()
    _GDIPlus_ImageDispose($ssImage)
    _GDIPlus_GraphicsDispose($ssGraphic[0])
    _GDIPlus_GraphicsDispose($ssGraphic[1])
    GUIDelete($ssGDI[0])
    GUIDelete($ssGDI[1])
    GUIDelete($ssGDI[2])
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Exit

I hope someone can shed light on this; perhaps a different approach is needed?

Thank you in advance!

P.S.
The script is patched from different scripts of different users in the forum - thank you again users! :)

Edited by 0Ethan0

Share this post


Link to post
Share on other sites
nend

You have some blocking function in it like sleep.
Also Guioneventmode give some problem in your case.

I fixed it, see the code.

#include <GuiListView.au3>
#include <File.au3>
#include <GDIPlus.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

HotKeySet("{Esc}", "_Exit")

Global $c=0
Global $ssGDI[3], $ssGraphic[2], $ssImage
Global Const $bg_color = "000000"
Global Const $ssW = 480, $ssH = 320
Global $aFiles = _FileListToArrayRec("d:\testStage\", "*.jpg;*.png;*.bmp;*.gif;*.JPG;*.PNG;*.BMP;*.GIF", $FLTAR_FILES, $FLTAR_NORECUR ,$FLTAR_SORT ,$FLTAR_FULLPATH )


$guiW = 1200
$guiH = 726
$mainWindow = GUICreate("Slideshow Viewer", $guiW, $guiH, -1, -1, $WS_POPUP)
$Button1 = GUICtrlCreateButton("Exit", 0, 0, 50, 50)
$Button2 = GUICtrlCreateButton("Test", 60, 0, 50, 50)


Global $ListView = GUICtrlCreateListView("Entry Name|Category", 5, 75, 195, 280)
_GUICtrlListView_SetColumnWidth ($ListView, 0, 100)
_GUICtrlListView_SetColumnWidth ($ListView, 1, 100)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_GRIDLINES, $LVS_EX_GRIDLINES)
GUICtrlSendMsg($ListView, $LVM_SETEXTENDEDLISTVIEWSTYLE, $LVS_EX_FULLROWSELECT, $LVS_EX_FULLROWSELECT)
GUICtrlCreateListViewItem("Name 1|Category 1", $ListView)
GUICtrlCreateListViewItem("Name 2|Category 2", $ListView)

screenshotWidgetInit($ssW,$ssH, 690, 100)
GUISetState(@SW_SHOW, $mainWindow)

GUIRegisterMsg($WM_NOTIFY, "WM_Notify_Events")

While 1
   _Check_MSG()
WEnd

Func _Check_MSG()
    $nMsg = GUIGetMsg()
    Switch $nMsg
        Case $GUI_EVENT_CLOSE
            _Exit()
        Case $Button1
            _Exit()
        Case $Button2
            Test()
    EndSwitch
EndFunc

Func Test()
    For $k = 1 To UBound($aFiles) - 1
        screenshotWidgetTransition($aFiles[$k])
    Next
EndFunc

Func ListView_Click()
    ConsoleWrite("Left Click")
EndFunc

Func ListView_DoubleClick()
    ConsoleWrite("Double Left Click")
    Test()
EndFunc

Func WM_Notify_Events($hWndGUI, $MsgID, $wParam, $lParam)
    #forceref $hWndGUI, $MsgID, $wParam
    Local $tagNMHDR, $event, $hwndFrom, $code
    $tagNMHDR = DllStructCreate("int;int;int", $lParam)
    If @error Then Return
    $event = DllStructGetData($tagNMHDR, 3)
    Select
    Case $wParam = $ListView
        Select
            Case $event = $NM_CLICK
                ListView_Click ()
            Case $event = $NM_DBLCLK
                ListView_DoubleClick ()
            EndSelect
    EndSelect
    Return $GUI_RUNDEFMSG
EndFunc

Func screenshotWidgetTransition($image, $delay = 0, $speed = 1, $sleep = 2000)
    Local $a, $d = $c, $iX, $iY
    $ssImage = _GDIPlus_ImageLoadFromFile($image)
    $iX = _GDIPlus_ImageGetWidth($ssImage)
    $iY = _GDIPlus_ImageGetHeight($ssImage)

    $FDesktop=$ssH/$ssW
    $Fact =1
    If $iX > $ssW And $FDesktop > ($iY/$iX) Then
        $Fact=$ssW/$iX
    ElseIf $iY > $ssH Then
        $Fact=$ssH/$iY
    EndIf
    $H1 = Round(($Fact * $iY),0)
    $W1 = Round(($Fact * $iX),0)

    _GDIPlus_GraphicsDrawImageRect($ssGraphic[$d], $ssImage,($ssW - $W1)/2, ($ssH - $H1) / 2,$W1,$H1)

    WinSetTrans($ssGDI[$d], "", 0)
    WinSetOnTop($ssGDI[$d], "", 1)
    For $a = 0 To 254 Step $speed
        WinSetTrans($ssGDI[$d], "", $a)
        _Check_MSG()
        Sleep($delay)
    Next
    WinSetTrans($ssGDI[$d], "", 254)
    WinSetOnTop($ssGDI[Not ($d)], "", 0)
    WinSetTrans($ssGDI[Not ($d)], "", 0)
    _GDIPlus_GraphicsClear($ssGraphic[Not ($d)])
    $c = 1 - $d

    _GDIPlus_ImageDispose ($ssImage) ; very important to realease the pics
    $timer = TimerInit()
    While TimerDiff($timer) < $sleep
        _Check_MSG()
    WEnd

EndFunc   ;==>screenshotWidgetTransition

Func screenshotWidgetInit($ssW,$ssH,$ssX,$ssY)
    $ssGDI[2] = GUICreate("", $ssW, $ssH, $ssX, $ssY, $WS_POPUP, $WS_EX_MDICHILD, $mainWindow)
    $ssGDI[0] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2])
    $ssGDI[1] = GUICreate("", $ssW, $ssH, 3, 3, $WS_POPUP, $WS_EX_MDICHILD, $ssGDI[2])
;    GUISetBkColor("0x" & $bg_color, $ssGDI[2])
    GUISetState(@SW_SHOW, $ssGDI[2])
    GUISetState(@SW_SHOW, $ssGDI[0])
    GUISetState(@SW_SHOW, $ssGDI[1])
    WinSetTrans($ssGDI[0], "", 0)
    WinSetTrans($ssGDI[1], "", 0)
    _GDIPlus_Startup()
    $ssGraphic[0] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[0])
    $ssGraphic[1] = _GDIPlus_GraphicsCreateFromHWND($ssGDI[1])
    _GDIPlus_GraphicsClear($ssGraphic[0], "0xFF" & $bg_color)
    _GDIPlus_GraphicsClear($ssGraphic[1], "0xFF" & $bg_color)
EndFunc   ;==>screenshotWidgetInit

Func _Exit()
    _GDIPlus_ImageDispose($ssImage)
    _GDIPlus_GraphicsDispose($ssGraphic[0])
    _GDIPlus_GraphicsDispose($ssGraphic[1])
    GUIDelete($ssGDI[0])
    GUIDelete($ssGDI[1])
    GUIDelete($ssGDI[2])
    _GDIPlus_Shutdown()
    Exit
EndFunc   ;==>_Exit

 

Edited by nend
  • Thanks 1

Share this post


Link to post
Share on other sites
0Ethan0

nend, Thank you so much for the reply and the code - it gave me a clearer concept of how things work behind the Autoit scene : )

I also read in the Wiki about the exact problem I was facing --> https://www.autoitscript.com/wiki/Interrupting_a_running_function

I hope it's OK to share the link here, just for others who stumble upon this topic can have a better understanding.

 

Share this post


Link to post
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
Sign in to follow this  

  • Similar Content

    • Jackized
      By Jackized
      Is there a way to loop back to the beginning and keep the already entered data in the GUI Input boxes? I can GUIDelete and start a new GUI but all the typed data has to be redone. Here is a sample.

       
      #Include <GuiConstants.au3> Dim $msg,$okbutton,$cancelbutton,$objOU,$intAccValue,$strGroup1,$strGroup2 Dim $strFirstName,$strLastName,$strUserName,$strUserName2,$strPassword,$strDescription,$strContainer Dim $Combo,$Duration Do $Form1 = GuiCreate("", 300, 300)          GUISetBkColor(0xA6CAF0)         GuiCtrlCreateLabel("First Name:", 50, 10)         $strFirstName = GuiCtrlCreateInput("", 50, 25, 200, 20)         GUICtrlSetState ( $okbutton, $GUI_FOCUS )         GuiSetState()     $okbutton = GUICtrlCreateButton ("OK",75,240,50,20)     $cancelbutton = GUICtrlCreateButton ("Cancel",175,240,50,20) While 1     $msg = GUIGetMsg()     Select     Case $msg = $GUI_EVENT_CLOSE Or $msg = $cancelbutton             Exit     Case $msg = $okbutton             $strFirstName = GUICtrlRead($strFirstName)             ExitLoop     EndSelect Wend $len = StringLen($strFirstName)     If $len > "20" then         MsgBox(0, "ERROR Name to long", $strFirstName & " can't be over 20 characters." & @CRLF & "Total: " & $len)         ;Exit         GUIDelete($Form1)     EndIf Until $len < "20" MsgBox(0, "Done", "")  
    • mrtgtr
      By mrtgtr
      I want to do something , after wait 2 min and again do same thing
      But I do do not want with sleep, it must be timer 
      How I do this ?
    • Emmhor1
      By Emmhor1
      Hi All,

      MAIN QUESTION:
      Is it possible to Call specific function within a GUI

      So I have a script with multiple functions although I don't want to use every function every time.
      My Idea is to create a simple GUI which allows me to select what functions I want to use then run the funtions by clicking a button.
      I have already made a GUI which allows me to select specific .exe's I would like to run after selection it runs the .exe one by one.
      This script is on my work laptops and cannot access it right now.
       
      Who can help me with this?
      GUIcreate
      Func1 
      Func2
      Func3
      Then have a boxes which allows me to select the specif Func.(I used GUIChecked and Unchecked in my other script)
      Then a button which executes/calls the selected functions
    • Skeletor
      By Skeletor
      Hi Guys,
      Is it possible to get a variable on your For..Next loop? 
       
      Local $Lines1 = _FileCountLines(C:\temp\test.txt) Local $linesToCount2 = $Lines1 + 2 $var = Number($linesToCount2) For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = $var To $count Next ;Code does stuff here Next Somehow my code doesn't work even though I thought I could convert the variable to a Integer / Number.
      This code I posted above does not move to the next value.
      But the code below does... why is that?
      For $count = 1 To _FileCountLines($FileRead2) Step 1 For $i = 2 To $count Next ;Code does stuff here Next  
      Why is the For loop resetting itself?
      Is it because the program does not cache the variable and needs to keep on acquiring this variable each time?
      If so , how would you make this variable static?

       
    • Blois
      By Blois
      Hi Guis,
       
      I need to create keyboard shortcut to navigate the tabs and activate them, but I could not use the code below:
      #include <TabConstants.au3> #include <WindowsConstants.au3> #include <GuiTab.au3> #include <GUIConstantsEx.au3> HotKeySet("^{TAB}", "_TabRight") HotKeySet("^+{TAB}", "_TabLeft") $gui = GUICreate("test",450, 300) $Tab1 = GUICtrlCreateTab(20, 24, 425, 201) $TabSheet1 = GUICtrlCreateTabItem("Tabsheet 1") $lbContagemGrupos2 = GUICtrlCreateLabel("aaaa", 50, 50) GUICtrlSetColor(-1, 0x0000FF) GUICtrlCreateTabItem("") $TabSheet2 = GUICtrlCreateTabItem(" ") $TabSheet3 = GUICtrlCreateTabItem(" ") GUISetState() While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop EndSwitch WEnd Func _TabRight() $CurPos = _GUICtrlTab_GetCurSel($Tab1) _GUICtrlTab_SetCurSel($Tab1, $CurPos + 1) $tab = "$TabSheet" & $CurPos GUICtrlSetState($TabSheet1 + $CurPos, $GUI_SHOW) Return EndFunc Func _TabLeft() $CurPos = _GUICtrlTab_GetCurSel($Tab1) _GUICtrlTab_SetCurSel($Tab1, $CurPos - 1) $tab = "$TabSheet" & $CurPos GUICtrlSetState($TabSheet1 + $CurPos, $GUI_SHOW) Return EndFunc Exit  
      can you help me?
×