Jump to content

Possible to delete a lot of controls in one command?


Spiff59
 Share

Recommended Posts

I have a complex GUI, in which a large portion of it needs to be drawn in a different manner depending on the value of some input data. In the interest of saving a lot of code... Rather than naming each control, then making a "Clear_Controls()" function containing a hundred "GUICtrlDelete" statements, is there a way to group different types of controls (labels, buttons, checkboxes, graphics) and delete them in a single statement? Or a command to delete every control within certain screen coordinates?

Thanks.

Edited by Spiff59
Link to comment
Share on other sites

Kind of. What you can do is make all the labels in an array. $Label[0], $Label[1], etc. Then, to delete all the labels, you just use a loop.

For $i = 1 To 100
     GuiCtrlDelete($Label[$i])
Next

I hadn't gotten a quick reply, so already started coding that.

A mix of one and two dimension control arrays and some nested loops kept the code down to about 20 lines.

The odd thing now is, when I call the routine to delete the labels/checkboxes/graphics, it caused an unrelated listview elsewhere in the GUI to flicker like crazy.

Link to comment
Share on other sites

Post the script and we can see what's wrong. Sometimes, nothing is wrong with the script, you just have to do it a different way.

Well, here's an example of the listview flickering. I just added a loop to the helpfile example to turn a graphic box on and off while there was a listview on the screen. You can execute the GUICtrlCreateGraphic all day long with no problems. It's the GUICtrlDelete of a graphic control that is messing with the listview.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $G1

    GUICreate("listview items", 420, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    $listview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
    $item2 = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    $item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping


    GUISetState()
    GUICtrlSetData($item2, "ITEM1")
    GUICtrlSetData($item3, "||COL33")
    GUICtrlDelete($item1)

; added code
    For $x = 1 to 40
        $G1 = GUICtrlCreateGraphic(300, 100, 60, 60)
        GUICtrlSetColor(-1, 0x808080)
        Sleep(100)
        Guictrldelete($g1)
        Sleep(100)
    Next
; end of added code

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Edit: Technically, I see the button control, as well as the input control, flashing in this example. So, I guess the GUICtrlDelete on a graphic type control is causing multiple types of controls, in addition to the listview, to be repainted.

Edited by Spiff59
Link to comment
Share on other sites

The GuiCtrlCreateGraphic generates 1 WM_PAINT message and the GuiCtrlDelete generates another one. If you do it with creating a label, colouring it and deleting you will get an extra WM_PAINT for the colouring that doesn't occur with the graphic, but would if you updated the graphic ($GUI_GR_REFRESH). All that repainting is what is causing the flickering.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)
Global $ip=0,$nop=0
Example()

Func Example()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $G1

    GUICreate("listview items", 420, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF)  ; will change background color
guiregistermsg($WM_PAINT,"Paint")
    $listview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
    $item2 = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    $item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping


    GUISetState()
    GUICtrlSetData($item2, "ITEM1")
    GUICtrlSetData($item3, "||COL33")
    GUICtrlDelete($item1)

; added code
$nop=1
    For $x = 1 to 40
        $G1 = GUICtrlCreateGraphic(300, 100, 60, 60)
        ;GUICtrlSetColor($G1, 0x808080)
        Sleep(100)
        Guictrldelete($g1)
        Sleep(100)
    Next
    $nop=0
; end of added code

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Func paint($hW, $iM,$wP,$lP)
    $ip += 1
    ConsoleWrite("painting " & $ip & @CRLF)
EndFunc

I don't know how to avoid it but here is a rather dumb way to get round it, though it means you can't do anything with your gui while it's deleting things.

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#include <screencapture.au3>

Opt('MustDeclareVars', 1)
Global $ip = 0, $nop = 0
Example()

Func Example()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $G1, $gui

    $gui = GUICreate("listview items", 420, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    GUISetBkColor(0x00E0FFFF) ; will change background color
    GUIRegisterMsg($WM_PAINT, "Paint")
    $listview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
    $item2 = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    $item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED) ; to allow drag and dropping


    GUISetState()
    GUICtrlSetData($item2, "ITEM1")
    GUICtrlSetData($item3, "||COL33")
    GUICtrlDelete($item1)
sleep(3000)
    ; added code
    #Region hide flicker??
    Local $hidew, $gp = WinGetPos($gui)

    _ScreenCapture_CaptureWnd("tempCover.bmp",$gui, 0,0,-1,-1,false)
    Local $hidew = GUICreate("", $gp[2], $gp[3], $gp[0], $gp[1], $WS_POPUP)
    GUISetCursor(15, 1)
    Local $coverpic = GUICtrlCreatePic("tempCover.bmp", 0, 0, $gp[2], $gp[3])
    ;show but without flickering

    ;WinSetTrans($hidew, "", 0)
    GUISetState()
    WinSetOnTop($hidew, "", 1)
   ; WinSetTrans($hidew, "", 252)
    Sleep(3000)
    GUISwitch($gui)
    #EndRegion hide flicker

    $nop = 1
    For $x = 1 To 40
        $G1 = GUICtrlCreateGraphic(300, 100, 60, 60)
        GUICtrlSetColor($G1, 0x808080)
        ;Sleep(100)
        GUICtrlDelete($G1)
       ; Sleep(100)
    Next
    $nop = 0
    ; end of added code
    GUIDelete($hidew)
    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Func paint($hW, $iM, $wP, $lP)
    $ip += 1
    ConsoleWrite("painting " & $ip & @CRLF)
EndFunc   ;==>paint
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

You can use another GUI child window.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $G1
    Local $hGUI1, $hGUI2

    $hGUI1 = GUICreate("listview items", 420, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    $hGUI2 = GUICreate("", 60, 60, 300, 100, $WS_CHILD, -1, $hGUI1)
    
    GUISetBkColor(0x00E0FFFF)
    GUISwitch($hGUI1)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    $listview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
    $item2 = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    $item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping


    GUISetState(@SW_SHOW, $hGUI1)
    GUISetState(@SW_SHOW, $hGUI2)
    
    GUICtrlSetData($item2, "ITEM1")
    GUICtrlSetData($item3, "||COL33")
    GUICtrlDelete($item1)

; added code

    GUISwitch($hGUI2)
    For $x = 1 to 20
        $G1 = GUICtrlCreateGraphic(0, 0, 60, 60)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFF00, 0xFFFF00)
        GUICtrlSetGraphic(-1, $GUI_GR_RECT, 0, 0, 60, 60)
        _WinAPI_InvalidateRect($hGUI2, 0, True)
        Sleep(200)
        Guictrldelete($g1)
        Sleep(200)
    Next
    ConsoleWrite('Here' & @CRLF)

; end of added code

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example
Link to comment
Share on other sites

You can use another GUI child window.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Opt('MustDeclareVars', 1)

Example()

Func Example()
    Local $listview, $button, $item1, $item2, $item3, $input1, $msg, $G1
    Local $hGUI1, $hGUI2

    $hGUI1 = GUICreate("listview items", 420, 250, 100, 200, -1, $WS_EX_ACCEPTFILES)
    $hGUI2 = GUICreate("", 60, 60, 300, 100, $WS_CHILD, -1, $hGUI1)
    
    GUISetBkColor(0x00E0FFFF)
    GUISwitch($hGUI1)
    GUISetBkColor(0x00E0FFFF)  ; will change background color

    $listview = GUICtrlCreateListView("col1  |col2|col3  ", 10, 10, 200, 150);,$LVS_SORTDESCENDING)
    $button = GUICtrlCreateButton("Value?", 75, 170, 70, 20)
    $item1 = GUICtrlCreateListViewItem("item2|col22|col23", $listview)
    $item2 = GUICtrlCreateListViewItem("item1|col12|col13", $listview)
    $item3 = GUICtrlCreateListViewItem("item3|col32|col33", $listview)
    $input1 = GUICtrlCreateInput("", 20, 200, 150)
    GUICtrlSetState(-1, $GUI_DROPACCEPTED)   ; to allow drag and dropping


    GUISetState(@SW_SHOW, $hGUI1)
    GUISetState(@SW_SHOW, $hGUI2)
    
    GUICtrlSetData($item2, "ITEM1")
    GUICtrlSetData($item3, "||COL33")
    GUICtrlDelete($item1)

; added code

    GUISwitch($hGUI2)
    For $x = 1 to 20
        $G1 = GUICtrlCreateGraphic(0, 0, 60, 60)
        GUICtrlSetGraphic(-1, $GUI_GR_COLOR, 0xFFFF00, 0xFFFF00)
        GUICtrlSetGraphic(-1, $GUI_GR_RECT, 0, 0, 60, 60)
        _WinAPI_InvalidateRect($hGUI2, 0, True)
        Sleep(200)
        Guictrldelete($g1)
        Sleep(200)
    Next
    ConsoleWrite('Here' & @CRLF)

; end of added code

    Do
        $msg = GUIGetMsg()

        Select
            Case $msg = $button
                MsgBox(0, "listview item", GUICtrlRead(GUICtrlRead($listview)), 2)
            Case $msg = $listview
                MsgBox(0, "listview", "clicked=" & GUICtrlGetState($listview), 2)
        EndSelect
    Until $msg = $GUI_EVENT_CLOSE
EndFunc   ;==>Example

Ah...

That not only solves the secondary flickering issue that came up in this thread, but also provides an easy way to zap a whole flock of controls in one fell swoop (my initial question in this thread).

Thank you, Sir.

Link to comment
Share on other sites

I hadn't gotten a quick reply, so already started coding that.

A mix of one and two dimension control arrays and some nested loops kept the code down to about 20 lines.

The odd thing now is, when I call the routine to delete the labels/checkboxes/graphics, it caused an unrelated listview elsewhere in the GUI to flicker like crazy.

Correct, each update will be drawn. Use GUISetState(@SW_LOCK) before starting and GUISetState(@SW_UNLOCK) after finishing. This will remove the flicker and also make your code run much faster.

jacQues

Link to comment
Share on other sites

  • 4 months later...

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