Micahs
Joined: 01 Dec 2006 Posts: 460
|
Posted: Mon Dec 21, 2009 5:26 am Post subject: Snapshot |
|
|
Take snapshots of existing controls (Not related to images.)
I noticed a question a while ago asking if it is possible to detect which tab a control is on. Recently, I needed to do just that. I wanted to easily be able to tell which controls were on a certain tab and disable/enable them as a group. This was my solution.
The 'Snapshot' function makes a comma separated list of existing controls each time it is run. It compares the list generated on previous runs to the current list and will return the difference; any controls that are new since the last run.
In this case, I get the list of controls on tab2 and disable them. Simple!
This example also includes a function called 'DisableControls' which uses the comma sep list to enable/disable specific controls.
| Code: |
Gui, Add, Tab2, , Tab1|Tab2|Tab3
Gui, Tab, 1
Gui, Add, Button, Section, Tab1, Button1
Gui, Add, Button, xp y+3, Tab1, Button2
Gui, Add, Button, xp y+3, Tab1, Button3
Gui, Add, Button, xp y+3, Tab1, Button4
Gui, Add, Button, xp y+3, Tab1, Button5
Gui, Add, Button, xp y+3, Tab1, Button6
snapshot() ;get baseline of existing controls
Gui, Tab, 2
Gui, Add, Button, xs ys, Tab2, Button1
Gui, Add, Button, xp y+3, Tab2, Button2
Gui, Add, Button, xp y+3, Tab2, Button3
Gui, Add, Button, xp y+3, Tab2, Button4
Gui, Add, Button, xp y+3, Tab2, Button5
Gui, Add, Button, xp y+3, Tab2, Button6
tab2CTRLS := snapshot() ;get only controls that have been added since the baseline (these will be controls only on tab 2)
DisableControls(0, tab2CTRLS) ;disable ctrls on tab2, gui1
cDisable=0 ;set flag for the state of these controls (0 is disabled)
Gui, Add, Button, xp y+8 gtoggleButtons, Toggle Buttons
Gui, Tab, 3
Gui, Add, Button, xs ys, Tab3, Button1
Gui, Add, Button, xp y+3, Tab3, Button2
Gui, Add, Button, xp y+3, Tab3, Button3
Gui, Add, Button, xp y+3, Tab3, Button4
Gui, Add, Button, xp y+3, Tab3, Button5
Gui, Add, Button, xp y+3, Tab3, Button6
Gui, Show, +Autosize +AlwaysOnTop X50 YCenter, Snapshot Example
Return
toggleButtons:
cDisable := !cDisable
DisableControls(cDisable, tab2CTRLS) ;disable ctrls on tab2, gui1
Return
GuiClose:
ExitApp
Return
/*
'snapshot_all' is the running list of all the controls
'snapshot_prevrun' is the list of controls that were introduced during the prev run of the func
'snapshot_diff' is the list of controls that are newly detected from the latest run
'g' is the gui number (default is 1)
*/
snapshot(g=1)
{
global snapshot_all, snapshot_prevrun, snapshot_diff
Gui, %g%:+LastFound
WinGet, snapshot_new, ControlList
StringReplace, snapshot_new, snapshot_new, `n, `,, All
snapshot_prevrun := snapshot_diff ;save prev results
If(snapshot_all) ;If this is not the first time
{ snapshot_diff := snapshot_new
Loop, Parse, snapshot_all, CSV ;remove all that were already there
{ snapshot_diff := RegExReplace(snapshot_diff, "\b" . A_LoopField . "\b")
}
snapshot_diff := RegExReplace(snapshot_diff, ",,+") ;remove multiple commas
snapshot_diff := RegExReplace(snapshot_diff, "^,") ;remove commas at beginning
snapshot_diff := RegExReplace(snapshot_diff, ",$") ;remove commas at end
}
Else ;if not, first run
{ snapshot_diff := snapshot_new
}
snapshot_all := snapshot_new ;save latest complete list
Return, snapshot_diff ;return only new controls from this time
}
/*
'which' is 0 or 1 (0 disables, 1 enables)
'list' is a comma separated list of controls (classnn, not hwnd) if not given, all controls are used
'g' is gui number (defaults to 1)
'restrictedCtrls' is a comma separated list of controls that are exempt from being enabled or disabled
*/
DisableControls(which, list="", g=1)
{ ;this is set up to use classnn, not the hwnds because disabling controls with hwnds did not work with any controls on tabs
global restrictedCtrls
Gui, %g%:+LastFound
Gui, %g%:Default
If(!list)
{ WinGet, list, ControlList
StringReplace, list, list, `n, `,, All
}
If(which = 0)
{ w = Disable
}
Else
{ w = Enable
}
Loop, Parse, list, CSV
{ If A_Loopfield Not In %restrictedCtrls%
{ GuiControl, %w%, %A_LoopField%
}
}
}
|
_________________
 |
|