philpw99 Posted January 17, 2023 Share Posted January 17, 2023 (edited) I have been playing with maps and realize that, if you need some simple object manipulation or something similar to class, maps can be of great help. Here is an example script to realize a simple class: myClass. The testObj below is a map declared by myClassNew(), and it comes with "mSet" and "mShow" 2 methods, and "status" as the only property. It's pretty easy to call the methods on this testObj, just use the "method" function. The properties are directly accessible like a real object. So this way is quite a simple and lightweight way to write your own class, which is actually a map. expandcollapse popupexample() Func example() $testObj = myClass_New() c("Original status:" & $testObj.status) $testObj.status = "Set directly" c( "You can get or set the properties directly:" & @CRLF & "status:" & $testObj.status) c( "Or you can set the value by a method:" ) ; Run the methods for this class. method($testObj, "mSet", "the status set by mSet") c( "Status after mSet:" & $testObj.status) c( "And you can use a method to show the value:") $testObj.status = "status from method" method($testObj, "mShow") EndFunc Func method(ByRef $obj, $method, $para = "") $m = $obj[$method] if $m = "" or Not IsFunc($m) then Return SetError(1) If $para = "" Then Return call( $obj[$method], $obj ) ; No parameters return call( $obj[$method], $obj, $para ) ; with some parameters. EndFunc Func myClass_SetStatus(ByRef $obj , $text = "") $obj.status = $text Return EndFunc Func myClass_showStatus(ByRef $obj) c("Show value in a method:" & $obj.status ) Return EndFunc Func myClass_New() Local $obj[] $obj.mSet = myClass_SetStatus $obj.mShow = myClass_showStatus $obj.status = "start" Return $obj EndFunc Func c($str) ConsoleWrite($str & @CRLF) EndFunc Edited January 17, 2023 by philpw99 RTFC 1 Link to comment Share on other sites More sharing options...
philpw99 Posted January 17, 2023 Author Share Posted January 17, 2023 (edited) Here is an example of this new way of declaring a GUI class. Notice that it's much easier and intuitive to set the styles in the example() function. expandcollapse popup#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <Array.au3> example() Func example() $mWin = guiClass_New( "My Window", 600, 500) $mWin.resizible = True $mWin.hasMinBox = False $mWin.popup = True method( $mWin, "mSetStyle") method( $mWin, "mShow") $aList = method( $mWin, "mList") _ArrayDisplay($aList, "My Gui class properties and methods list") while True switch GUIGetMsg() Case $GUI_EVENT_CLOSE exit EndSwitch sleep(1) Wend EndFunc Func method(ByRef $obj, $method, $para = "") $m = $obj[$method] if $m = "" or Not IsFunc($m) then Return SetError(1) If $para = "" Then Return call( $obj[$method], $obj ) ; No parameters return call( $obj[$method], $obj, $para ) ; with some parameters. EndFunc Func guiClass_move( ByRef $obj , $iSpeed = 0) ; First, set the left, top, or/and width, heigh properties, ; The call this method $hWnd = WinMove( $obj.gui, "", $obj.left, $obj.top, $obj.width, $obj.height, $iSpeed) Return SetError(@error, @extended, $hWnd) EndFunc Func guiClass_hide(ByRef $obj) WinSetState($obj.gui, "", @SW_HIDE) $obj.state = WinGetState($obj.gui) EndFunc Func guiClass_show(ByRef $obj) WinSetState($obj.gui, "", @SW_SHOW) $obj.state = WinGetState($obj.gui) EndFunc Func guiClass_close(ByRef $obj) WinClose($obj.gui) $obj = Null EndFunc Func guiClass_listClass(ByRef $obj) ; list all properties and methods as 2D array ; $a[0] is the name, $a[1] is the value, $a[2] is the var type $aKeys = MapKeys($obj) $iCount = UBound($aKeys) Local $aList[$iCount][3] For $i = 0 to $iCount -1 $aList[$i][0] = $aKeys[$i] $aList[$i][1] = $obj[ $aKeys[$i] ] $aList[$i][2] = VarGetType( $aList[$i][1] ) Next Return $aList EndFunc Func guiClass_updateStatus(ByRef $obj) if Not WinExists($obj.gui) Then Return SetError(1) ; Set current status $aPos = WinGetPos($obj.gui) if @error Then Return SetError(2) $obj.left = $aPos[0] $obj.top = $aPos[1] $obj.width = $aPos[2] $obj.height = $aPos[3] $obj.state = WinGetState($obj.gui) $obj.title = WinGetTitle($obj.gui) $aStyle = GUIGetStyle($obj.gui) $obj.style = $aStyle[0] $obj.exstyle = $aStyle[1] EndFunc Func guiClass_flash(ByRef $obj) WinFlash($obj.gui) EndFunc Func guiClass_setStyle(ByRef $obj) $iStyle = $obj.style $iStyle = BitOR( _ $obj.resizible ? $WS_SIZEBOX : 0, _ $obj.thinBorder ? $WS_BORDER : 0, _ $obj.popup ? $WS_POPUP : 0, _ $obj.hasTitle ? $WS_DLGFRAME : 0, _ $obj.hasMaxBox ? $WS_MAXIMIZEBOX : 0, _ $obj.hasMinBox ? $WS_MINIMIZEBOX : 0, _ $obj.hasSysMenu ? $WS_SYSMENU : 0 _ ) $obj.style = $iStyle GUISetStyle($iStyle) EndFunc Func guiClass_New($sTitle = "", $iWidth = 800, $iHeight = 450, $iLeft = -1, $iTop = -1, $iStyle = $GUI_SS_DEFAULT_GUI, $iExStyle = 0, $hParent = 0) Local $obj[] ; Initialize all default values. $obj.title = $sTitle $obj.width = $iWidth $obj.height = $iHeight $obj.left = $iLeft $obj.top = $iTop $obj.style = $iStyle $obj.exstyle = $iExStyle $obj.parentGui = $hParent ; Main Styles $obj.resizible = BitAND( $iStyle, $WS_SIZEBOX) = $WS_SIZEBOX $obj.thinBorder = BitAND($iStyle, $WS_BORDER) = $WS_BORDER $obj.popup = BitAND($iStyle, $WS_POPUP) = $WS_POPUP $obj.hasTitle = BitAND($iStyle, $WS_DLGFRAME) = $WS_DLGFRAME $obj.hasMaxBox = BitAND($iStyle, $WS_MAXIMIZEBOX) = $WS_MAXIMIZEBOX $obj.hasMinBox = BitAND($iStyle, $WS_MINIMIZEBOX) = $WS_MINIMIZEBOX $obj.hasSysMenu = BitAND($iStyle, $WS_SYSMENU) = $WS_SYSMENU ; Methods $obj.mMove = guiClass_move $obj.mHide = guiClass_hide $obj.mShow = guiClass_show $obj.mClose = guiClass_close $obj.mList = guiClass_listClass $obj.mFlash = guiClass_flash $obj.mSetStyle = guiClass_setStyle ; Create the gui local $hGui = GUICreate($obj.title, $obj.width, $obj.height, $obj.left, $obj.top, $obj.style, $obj.exstyle, $obj.parentGui) if @error then Return SetError(1) $obj.gui = $hGui $obj.pid = WinGetProcess($hGui) $obj.state = WinGetState($hGui) Return $obj EndFunc Func c($str) ConsoleWrite($str & @CRLF) EndFunc Edited January 17, 2023 by philpw99 SOLVE-SMART 1 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now