jugador Posted April 20 Posted April 20 (edited) I haven't touched AutoIt in over a year, so I'm a bit rusty. Here is an attempt to incorporate the Get/Set property into @trancexx ObjectFromTag code, similar to the AutoItObject UDF. Example 1: expandcollapse popup#include "ObjFromTag_v1a UDF.au3" Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc") __Example1() Func __Example1() ConsoleWrite("> __Example4() " & @CRLF) Local $t_TestObj Local $o_TestObj = __ObjectFromTag("__MyInterface_", '', $t_TestObj, 'int Ln;char Msg[30];') __SetProperty($o_TestObj, "Ln", 555) __SetProperty($o_TestObj, "Msg", 'Is it working.') ConsoleWrite("+>>> Ln : " & __GetProperty($o_TestObj, "Ln") & @CRLF) ConsoleWrite("+>>> Msg : " & __GetProperty($o_TestObj, "Msg") & @CRLF) If IsObj($o_TestObj) Then $o_TestObj = 0 __DeleteObjectFromTag($t_TestObj) ConsoleWrite("----The End----" & @CRLF) EndFunc Func __MyInterface_QueryInterface($pSelf, $pRIID, $pObj) Return __QueryInterface($pSelf, $pRIID, $pObj) EndFunc Func __MyInterface_AddRef($pSelf) Return __AddRef($pSelf) EndFunc Func __MyInterface_Release($pSelf) Return __Release($pSelf) EndFunc Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) Return EndFunc ;==>_ErrFunc Example 2: #include "ObjFromTag_v1a UDF.au3" Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc") __Example2() Func __Example2() ConsoleWrite("> __Example2() " & @CRLF) Local $t_TestObj Local $o_TestObj = __ObjectFromTag("__MyInterface_", 'Open hresult();Close hresult();', $t_TestObj, 'handle WinHnd;', False) If Not IsObj($o_TestObj) Then Return $o_TestObj.Open() MsgBox(0, '', 'Clicked to closed Notepad....') $o_TestObj.Close() If IsObj($o_TestObj) Then $o_TestObj = 0 __DeleteObjectFromTag($t_TestObj) EndFunc Func __MyInterface_Open($pSelf) Run("notepad.exe") WinWait("[CLASS:Notepad]", "", 10) Local $hWnd = WinGetHandle("[CLASS:Notepad]") __SetProperty($pSelf, "WinHnd", $hWnd) Return 0 EndFunc Func __MyInterface_Close($pSelf) WinClose(__GetProperty($pSelf, "WinHnd")) Return 0 EndFunc Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) Return EndFunc ;==>_ErrFunc Example 3: #include "ObjFromTag_v1a UDF.au3" Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc") __Example3() Func __Example3() ConsoleWrite("> __Example3() " & @CRLF) Local $t_TestObj Local $o_TestObj = __ObjectFromTag("__MyInterface_", 'MsgBox hresult();', $t_TestObj, 'char Title[10];char Text[30];int Flag;', False) If Not IsObj($o_TestObj) Then Return __SetProperty($o_TestObj, "Title", "Something") __SetProperty($o_TestObj, "Text", 'Is it working.') __SetProperty($o_TestObj, "Flag", 64 + 262144) $o_TestObj.MsgBox() If IsObj($o_TestObj) Then $o_TestObj = 0 __DeleteObjectFromTag($t_TestObj) ConsoleWrite("----The End----" & @CRLF) EndFunc Func __MyInterface_MsgBox($pSelf) MsgBox(__GetProperty($pSelf, "Flag"), __GetProperty($pSelf, "Title"), __GetProperty($pSelf, "Text")) EndFunc Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) Return EndFunc ;==>_ErrFunc Example 4: #include "ObjFromTag_v1a UDF.au3" Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc") __Example3() Func __Example3() ConsoleWrite("> __Example3() " & @CRLF) Local $t_TestObj Local $o_TestObj = __ObjectFromTag("", 'DisplayBook str();', $t_TestObj, 'char book[50];char author[15];int year;', False) If Not IsObj($o_TestObj) Then Return __SetProperty($o_TestObj, "book", "Harry Potter and the Sorcerer's Stone") __SetProperty($o_TestObj, "author", 'J. K. Rowling') __SetProperty($o_TestObj, "year", 1997) MsgBox(0, 'Book', $o_TestObj.DisplayBook) If IsObj($o_TestObj) Then $o_TestObj = 0 __DeleteObjectFromTag($t_TestObj) ConsoleWrite("----The End----" & @CRLF) EndFunc Func DisplayBook($pSelf) Local $sString = 'The novel ' & __GetProperty($pSelf, "book") & ' ,' & _ ' written by author ' & __GetProperty($pSelf, "author") & _ ' and published in ' & __GetProperty($pSelf, "year") & '.' Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate! $tString.str = $sString Return DllStructGetPtr($tString) EndFunc Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) Return EndFunc ;==>_ErrFunc Example 5: expandcollapse popup#include "ObjFromTag_v1a UDF.au3" Global $oError = ObjEvent("AutoIt.Error", "_ErrFunc") __Example3() Func __Example3() ConsoleWrite("> __Example3() " & @CRLF) Local $t_BookObj, $t_AuthorObj Local $o_BookObj = __ObjectFromTag("", 'BookList hresult(bstr;bstr);', $t_BookObj, 'char book[50];int year;', False) Local $o_AuthorObj = __ObjectFromTag("", 'DisplayBook str(bstr);', $t_AuthorObj, 'char author[15];ptr oBook;', False) __SetProperty($o_AuthorObj, "oBook", $o_BookObj()) $o_BookObj.BookList("Harry Potter and the Sorcerer's Stone", 1997) MsgBox(0, 'Book', $o_AuthorObj.DisplayBook('J. K. Rowling')) If IsObj($o_AuthorObj) Then $o_AuthorObj = 0 If IsObj($o_BookObj) Then $o_BookObj = 0 __DeleteObjectFromTag($t_AuthorObj) __DeleteObjectFromTag($t_BookObj) ConsoleWrite("----The End----" & @CRLF) EndFunc Func BookList($pSelf, $pStrA, $pStrB) __SetProperty($pSelf, "book", __Bstr_ToString($pStrA)) __SetProperty($pSelf, "year", __Bstr_ToString($pStrB)) EndFunc Func DisplayBook($pSelf, $pStrA) __SetProperty($pSelf, "author", __Bstr_ToString($pStrA)) Local $pBookList = __GetProperty($pSelf, "oBook") Local $sString = 'The novel ' & __GetProperty($pBookList, "book") & ' ,' & _ ' written by author ' & __GetProperty($pSelf, "author") & _ ' and published in ' & __GetProperty($pBookList, "year") & '.' Local Static $tString = DllStructCreate(StringFormat("char str[%d]", StringLen($sString) + 1)) ;+1 to null terminate! $tString.str = $sString Return DllStructGetPtr($tString) EndFunc Func __Bstr_ToString($pString) Local $o_Data = DllStructGetData(DllStructCreate("wchar[" & _ DllStructGetData(DllStructCreate("dword", $pString - 4), 1) / 2 & "]", $pString), 1) Return $o_Data EndFunc Func _ErrFunc() ConsoleWrite("! COM Error ! Number: 0x" & Hex($oError.number, 8) & " ScriptLine: " & $oError.scriptline & " - " & $oError.windescription & @CRLF) Return EndFunc ;==>_ErrFunc for more about objectfromtag you can follow this thread created by @LarsJ https://www.autoitscript.com/forum/topic/205154-using-objcreateinterface-and-objectfromtag-functions/ and do check out code posted by @trancexx @ProgAndy @monoceres @wolf9228 @LarsJ @Danyfirex and @Bilgus. on this topic. Note: You can also register this object, created using __ObjectFromTag, within the Running Object Table (ROT). ObjFromTag_v1c.au3 Edited 16 hours ago by jugador Danyfirex 1
jugador Posted 16 hours ago Author Posted 16 hours ago (edited) Credit goes to @Numeric1 for his Ping Pong game using AutoItObject. @Gianni also converted the Ping Pong game using AutoItObject_Internal, coded by @genius257 Here’s my attempt using __ObjectFromTag. expandcollapse popup#include-once #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <GDIPlus.au3> #include <Misc.au3> #include "ObjFromTag_v1c.au3" Global $g__flag = False Global $oErrorHandler = ObjEvent("AutoIt.Error", "_ErrFunc") _GDIPlus_Startup() Const $COLOR_RED = 0xFFFF0000 Const $COLOR_GREEN = 0xFF00FF00 GamePanel() Func GamePanel() Local $hGUI = GUICreate("Ping Pong", 400, 300, -1, -1, $WS_SIZEBOX + $WS_SYSMENU) GUISetBkColor(0x000000) GUISetState() Local $aClient = WinGetClientSize($hGUI) If @error Then Return SetError(1, 0, 0) Local $iWidth = $aClient[0] Local $iHeight = $aClient[1] Local $t_GamePanel Local $GamePanel_Property = 'int iWidth;int iHeight;ptr ball;ptr paddle;ptr map[5];int speedLevel;' Local $GamePanel_Method = 'move hresult();drawStage hresult();cleanUpResources hresult();runGameLoop hresult();' Local $c_GamePanel = __ObjectFromTag('_', $GamePanel_Method, $t_GamePanel, $GamePanel_Property, False) If Not IsObj($c_GamePanel) Then Return Local $t_Ball Local $Ball_Property = 'int dx;int dy;int size;int X;int Y;' Local $Ball_Method = 'move hresult();' Local $c_Ball = __ObjectFromTag('_', $Ball_Method, $t_Ball, $Ball_Property, False) If Not IsObj($c_Ball) Then Return Local $t_Paddle Local $Paddle_Property = 'int X;int size;int dx;' Local $Paddle_Method = 'moveLeft hresult();moveRight hresult(int);' Local $c_Paddle = __ObjectFromTag('_', $Paddle_Method, $t_Paddle, $Paddle_Property, False) If Not IsObj($c_Paddle) Then Return Local $aGDIMap[5] $aGDIMap[0] = _GDIPlus_GraphicsCreateFromHWND($hGUI) $aGDIMap[1] = _GDIPlus_BitmapCreateFromGraphics($iWidth, $iHeight, $aGDIMap[0]) $aGDIMap[2] = _GDIPlus_ImageGetGraphicsContext($aGDIMap[1]) $aGDIMap[3] = _GDIPlus_BrushCreateSolid($COLOR_RED) $aGDIMap[4] = _GDIPlus_HatchBrushCreate(4, $COLOR_GREEN) Ball($c_Ball, 40, 40) Paddle($c_Paddle, 150, 100) __SetProperty($c_GamePanel, 'iWidth', $iWidth) __SetProperty($c_GamePanel, 'iHeight', $iHeight) __SetProperty($c_GamePanel, 'ball', $c_Ball()) __SetProperty($c_GamePanel, 'paddle', $c_Paddle()) __SetProperty($c_GamePanel, 'speedLevel', 100) __SetProperty($c_GamePanel, "map", $aGDIMap[0], 1) __SetProperty($c_GamePanel, "map", $aGDIMap[1], 2) __SetProperty($c_GamePanel, "map", $aGDIMap[2], 3) __SetProperty($c_GamePanel, "map", $aGDIMap[3], 4) __SetProperty($c_GamePanel, "map", $aGDIMap[4], 5) $c_GamePanel.runGameLoop() $c_GamePanel.cleanUpResources() $c_Paddle = 0 $c_Ball = 0 $c_GamePanel = 0 __DeleteObjectFromTag($t_Paddle) __DeleteObjectFromTag($t_Ball) __DeleteObjectFromTag($t_GamePanel) ConsoleWrite("< The End >" & @CRLF) EndFunc Func Ball(Byref $oSelf, $x = 0, $y = 0, $size = 5) __SetProperty($oSelf, 'dx', 10) __SetProperty($oSelf, 'dy', -10) __SetProperty($oSelf, 'size', $size) __SetProperty($oSelf, 'X', $y) __SetProperty($oSelf, 'Y', $x) EndFunc Func Paddle(Byref $oSelf, $x = 0, $size = 5) __SetProperty($oSelf, 'X', $x) __SetProperty($oSelf, 'size', $size) __SetProperty($oSelf, 'dx', 20) EndFunc Func _moveLeft($this) Local $paddleX = __GetProperty($this, "X") Local $paddledx = __GetProperty($this, "dx") $paddleX -= $paddledx If $paddleX < 0 Then __SetProperty($this, 'X', 0) Else __SetProperty($this, 'X', $paddleX) Endif EndFunc ;==>_moveLeft Func _moveRight($this, $maxX) Local $paddleWidth = __GetProperty($this, "size") Local $paddleX = __GetProperty($this, "X") Local $paddledx = __GetProperty($this, "dx") If $paddleX + $paddledx + $paddleWidth <= $maxX Then $paddleX += $paddledx Else $paddleX = $maxX - $paddledx EndIf __SetProperty($this, 'X', $paddleX) EndFunc ;==>_moveRight Func _move($this) Local $pBall = __GetProperty($this, "ball") Local $pPaddle = __GetProperty($this, "paddle") Local $x = __GetProperty($pBall, "X") Local $y = __GetProperty($pBall, "Y") Local $dx = __GetProperty($pBall, "dx") Local $dy = __GetProperty($pBall, "dy") Local $BallSize = __GetProperty($pBall, "size") Local $Width = __GetProperty($this, "iWidth") Local $Height = __GetProperty($this, "iHeight") If $y + $dy >= ($Height - 40) And $x + $BallSize >= __GetProperty($pPaddle, "X") And $x <= __GetProperty($pPaddle, "X") + __GetProperty($pPaddle, "size") Then $dy *= -1 EndIf If $y + $dy <= 0 Then $dy = Abs($dy) EndIf If $y + $dy >= $Height - $BallSize Then MsgBox(0, "Game Over", "You missed the ball! Game Over!") $g__flag = True Return EndIf If $x + $dx <= 0 Then $dx = Abs($dx) EndIf If $x + $dx >= $Width - $BallSize Then $dx = -Abs($dx) EndIf $x += $dx $y += $dy __SetProperty($pBall, 'dx', $dx) __SetProperty($pBall, 'dy', $dy) __SetProperty($pBall, 'X', $x) __SetProperty($pBall, 'Y', $y) Local $GamePanel_Method = 'move hresult();drawStage hresult();cleanUpResources hresult();runGameLoop hresult();' Local $c__GamePanel = __RestoreObjectFromPtr($this, $GamePanel_Method, False, Default) $c__GamePanel.drawStage() EndFunc ;==>_move Func _drawStage($this) Local $hGraphics = __GetProperty($this, "map", 1) Local $hBitmap = __GetProperty($this, "map", 2) Local $hGraphicsCtxt = __GetProperty($this, "map", 3) Local $hBrush = __GetProperty($this, "map", 4) Local $hHatchBrush = __GetProperty($this, "map", 5) Local $pBall = __GetProperty($this, "ball") Local $pPaddle = __GetProperty($this, "paddle") Local $iX = __GetProperty($pBall, "X") Local $iY = __GetProperty($pBall, "Y") Local $iRadius = __GetProperty($pBall, "size") Local $padX = __GetProperty($pPaddle, "X") Local $padH = __GetProperty($this, "iHeight") - 40 _GDIPlus_GraphicsClear($hGraphicsCtxt, 0xFF000000) _GDIPlus_GraphicsFillEllipse($hGraphicsCtxt, $iX - $iRadius, $iY - $iRadius, $iRadius * 2, $iRadius * 2, $hBrush) _GDIPlus_GraphicsFillRect($hGraphicsCtxt, $padX, $padH, __GetProperty($pPaddle, "size"), 10, $hHatchBrush) _GDIPlus_GraphicsDrawImageRect($hGraphics, $hBitmap, 0, 0, __GetProperty($this, "iWidth"), __GetProperty($this, "iHeight")) EndFunc ;==>_drawStage Func _cleanUpResources($this) ConsoleWrite("clean up ressources...." & @CRLF) _GDIPlus_GraphicsDispose(__GetProperty($this, "map", 1)) _GDIPlus_BitmapDispose(__GetProperty($this, "map", 2)) _GDIPlus_GraphicsDispose(__GetProperty($this, "map", 3)) _GDIPlus_BrushDispose(__GetProperty($this, "map", 4)) _GDIPlus_Shutdown() EndFunc ;==>_cleanUpResources Func _runGameLoop($this) Local $speedUpTime = 5000 Local $lastMoveTime = TimerInit() Local $maxX = __GetProperty($this, "iWidth") Local $GamePanel_Method = 'move hresult();drawStage hresult();cleanUpResources hresult();runGameLoop hresult();' Local $c__GamePanel = __RestoreObjectFromPtr($this, $GamePanel_Method, False, Default) Local $pPaddle = __GetProperty($this, "paddle") Local $Paddle_Method = 'moveLeft hresult();moveRight hresult(int);' Local $c__Paddle = __RestoreObjectFromPtr($pPaddle, $Paddle_Method, False, Default) While 1 If GUIGetMsg() = $GUI_EVENT_CLOSE Then ExitLoop If _IsPressed(25) Then $c__Paddle.moveLeft() If _IsPressed(27) Then $c__Paddle.moveRight($maxX) If _IsPressed(53) Then While 1 If _IsPressed(25) Or _IsPressed(27) Then ExitLoop Sleep(100) WEnd EndIf If TimerDiff($lastMoveTime) >= $speedUpTime Then __SetProperty($this, 'speedLevel', __GetProperty($this, "speedLevel") - 5) If __GetProperty($this, "speedLevel") < 0 Then __SetProperty($this, 'speedLevel', 0) $lastMoveTime = TimerInit() EndIf $c__GamePanel.move() If $g__flag Then ExitLoop Sleep(__GetProperty($this, "speedLevel")) WEnd EndFunc ;==>_runGameLoop I also updated the __ObjectFromTag UDF (ver: 1c) Edited 14 hours ago by jugador
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