Leaderboard
Popular Content
Showing content with the highest reputation on 02/05/2015 in all areas
-
_Base64Encode, _Base64Decode
Colduction reacted to trancexx for a topic
I guess there is no need to explain what Base64 encoding is and what is used for. What is special about this way. Well, I did some testing and it turns that it's fast. About three times faster than the fastest way posted here on this forum (machine code implementation). It's calling functions "CryptBinaryToString" and "CryptStringToBinary" in Crypt32.dll I'm in the phase when discovering functions DllCall(), DllStructCreate(),... and related, so please be gentle to me, lol There is no error checking mainly because of the sentence before this one. If someone is willing to add that to the code below (or correct possible mistakes), I'm willing to learn out of that. So, please do. Here's the script: Updated 29th October 2008 - error checking added and some code optimization $encoded = _Base64Encode("Testing") MsgBox(0, 'Base64 Encoded', $encoded) $decoded = _Base64Decode($encoded) MsgBox(0, 'Base64 Decoded - binary', $decoded) MsgBox(0, 'Base64 Decoded - string', BinaryToString($decoded)) Func _Base64Encode($input) $input = Binary($input) Local $struct = DllStructCreate("byte[" & BinaryLen($input) & "]") DllStructSetData($struct, 1, $input) Local $strc = DllStructCreate("int") Local $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _ "ptr", DllStructGetPtr($struct), _ "int", DllStructGetSize($struct), _ "int", 1, _ "ptr", 0, _ "ptr", DllStructGetPtr($strc)) If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating the length of the buffer needed EndIf Local $a = DllStructCreate("char[" & DllStructGetData($strc, 1) & "]") $a_Call = DllCall("Crypt32.dll", "int", "CryptBinaryToString", _ "ptr", DllStructGetPtr($struct), _ "int", DllStructGetSize($struct), _ "int", 1, _ "ptr", DllStructGetPtr($a), _ "ptr", DllStructGetPtr($strc)) If @error Or Not $a_Call[0] Then Return SetError(2, 0, ""); error encoding EndIf Return DllStructGetData($a, 1) EndFunc ;==>_Base64Encode Func _Base64Decode($input_string) Local $struct = DllStructCreate("int") $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", 0, _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(1, 0, "") ; error calculating the length of the buffer needed EndIf Local $a = DllStructCreate("byte[" & DllStructGetData($struct, 1) & "]") $a_Call = DllCall("Crypt32.dll", "int", "CryptStringToBinary", _ "str", $input_string, _ "int", 0, _ "int", 1, _ "ptr", DllStructGetPtr($a), _ "ptr", DllStructGetPtr($struct, 1), _ "ptr", 0, _ "ptr", 0) If @error Or Not $a_Call[0] Then Return SetError(2, 0, ""); error decoding EndIf Return DllStructGetData($a, 1) EndFunc ;==>_Base64Decode There is @CRLF every 64 chars in return of _Base64Encode(). _Base64Decode() will return binary data. That is intentional to avoid Chr(0) issue. Convert it to string using BinaryToString() Microsoft about requirements: Client - Requires Windows Vista or Windows XP. Server - Requires Windows Server 2008 or Windows Server 2003.1 point -
Add to your SciTEUser.properties file a line like this. That creates a context menu item that reads Add as Snippet, and that item is linked to command #16. Here's how an actual right click menu is set up in my installation for commands I've created in SciTE. These two commands, #s 40 and 41, are linked to the context menu items so that when selected will run the command. Command #45 is linked to a command that runs a lua script saved in PersonalTools.lua by this command.1 point
-
nassausky, That is not a question for this UDF thread - please post your question in the General Help section of the forums. M231 point
-
JakeKenmode, When I need to resize non-native controls (which is what child GUIs essentially are) I often create a native label which can automatically resize and then resize the control to match the label once the resizing is finished - this thread shows an example with ListViews. M231 point
-
Damn it, there are times when you rely on your brain to tell you things, and it inevitably lets you down, especially as you get older. It has only occurred to me now (why damn it?), after having shutdown my programming PC and about to wind down watching a Midsummer Murders episode with the wife, that I should have done something about favorites per user too, and undoubtedly there may be one or two other things. Obviously I'm tired, which is not a good recipe when programming. So, I recommend you hang off (yet again) getting the latest update, until I give it a good thorough check tomorrow and adjust everything that needs doing. I really didn't feel like doing that tonight, at that stage of the evening, and so relied on memory. When tired, you don't think straight either.1 point
-
vyperhand, Here you go. - 1. Declare Global variables in the main script. These variables are visible to every function in the script so declare them early to make sure that they are visible when needed. AutoIt does not recognise variables that have not yet been seen by the interpreter, so the declaration line has to have been parsed at run-time for the declaration to be valid - if the function has not yet been called then the variables within are not considered to have been declared as AutoIt cannot read your mind. Au3Check (the syntax checker) will throw a warning if you declare them inside a function, but will not error. The Variables - using Global, Local, Static and ByRef tutorial in the Wiki offers further information. - 2. Idle loop in main script. While the script is inside a function AutoIt will not, in general, run another function - so putting the idle loop inside a function (meaning you never actually leave it) will prevent most other functions from running. Obviously this is not "a good thing" - you might find the Interrupting a running function tutorial in the Wiki useful reading to understand in even more detail. - 3. Recursion. I mentioned that you were calling the MainGUI function from inside itself and that this would cause problems. Before you say "But I call the function from within the SelectItem function" I would point out that you actually call that function from within the MainGUI function so there is an unbroken chain. Calling functions again before they have finished is known as recursion (and yes there is a Wiki tutorial: Recursion) which can lead to major crashes. As explained above, you want to get out of functions cleanly so that AutoIt can run other ones without problem. In this case we just hide the initial choice GUI and then either reshow or destroy it depending on the MsgBox return - that way we do not get involved in recursion at all. - 4. Magic numbers. Although it can appear to be a good idea to use a single integer to define the MsgBox parameters, will you remember what that value represents in a few days, weeks, months time when you next take a look at the code. Much better to use the constants which explain (pretty well) what is intended. There are a number of utilities available which can reduce the execution time of the code by replacing these constants with their numeric values before compiling - guinness' PreExpand is the best I have found. I hope that helps. M231 point
-
Updated 2014-01-17: - New _LineTraverser UDF with floating point math to replace the old (now renamed _LineTraverserB) - Added Pacman examples 2014-01-17 'Hotfix': - Fixed majorly borked Atan2() code Lakes, thanks but I've about had enough of the PacGUI! haha.. Ah, and here's the final Pacman animated GUI example. The 'final' dot doesn't seem to always line up, and squares happen to draw better than ellipses so you'll have to make do - or improve it as you wish *Edit: Note, this example will eat up memory quickly. This is due to a bug in Windows where it doesn't properly dispose of previous Regions that have been applied to a Window. Unfortunately, there's no way to get at the region once Window takes over it either... In the future I may create a versoin with the 3 different 'animations' as 3 different GUI's and try swapping them in and out. It should alleviate the memory leak problem.. Pacman (Animated) Line-Traversing GUI, The Final Act #include "_LineTraverser.au3" #include "_GUIShapes.au3" #include <WinAPI.au3> ; ======================================================================================================== ; <LineTraverserPacmanExample.au3> ; ; Simple Example of using the <_LineTraverser.au3> and <_GuiShapes.au3> UDF's ; ; A little red-ball displays, while a 'Pacman' GUI moves towards it along a line ; An actual line is drawn onscreen to show the path from 'Pacman' to the red-ball target GUI, ; and then the PacGUI moves to it in $iStep increments. ; ; Additional Functions: ; _DrawPlainOldLine() ; Draws a line on the screen ; _DrawPacmanDottedLine() ; Draws a special Pacman-Dotted line ; _GUIApplyPacman() ; Converts a square GUI into a Pacman-shaped GUI. Regions are recreated ; ; each time, based on the input ; Atan2_Radians() ; An 'atan2()' vfunction ; ; Author: Ascend4nt ; ======================================================================================================== Global Const $iStep = 2 Local $hPacMan, $hDestCircle, $iExt, $iStepsTaken = 0, $iMode = 0, $fLineAngleRads = 0 Local $iXTarget, $iYTarget, $aLineTraverser ; Create the Pacman GUI and apply shape to it $hPacMan = GUICreate("", 81, 81, Random(0, @DesktopWidth - 20, 1), Random(0, @DesktopHeight - 20, 1), 0x80000000, 0x08000080 + 0x20) GUISetBkColor(Random(0x111111, 0xFFFFFF, 1), $hPacMan) _GUIApplyPacman($hPacMan, 0) ; Pink-dot GUI $hDestCircle = _CircleGUICreate(1, 1, 17, 0, 17, 0xDE8394) ; Set initial target point $iXTarget = Random(0, @DesktopWidth - 20, 1) $iYTarget = Random(0, @DesktopHeight - 20, 1) ; Source/Target are same to start off with $aLineTraverser = _LineTraverserCreate($iXTarget, $iYTarget, $iXTarget, $iYTarget) ; Move windows to start positions WinMove($hPacMan, '', $aLineTraverser[0], $aLineTraverser[1]) ; + Center of hollow circle, - half of target circle WinMove($hDestCircle, '', $iXTarget + 40 - 8, $iYTarget + 40 - 8) ; Transparency on 'seeker' circle WinSetTrans($hPacMan, '', 150) ; Show both GUIs, and put on top of all windows WinSetState($hDestCircle, '', @SW_SHOWNOACTIVATE) WinSetState($hPacMan, '', @SW_SHOWNOACTIVATE) WinSetOnTop($hPacMan, '', 1) WinSetOnTop($hDestCircle, '', 1) While 1 ; Exit on 'ESC' keypress (in down state) If BitAND(_WinAPI_GetAsyncKeyState(0x1B), 0x8000) Then ExitLoop ; < 10 ms sleep with an API call ;DllCall("kernel32.dll",'none','Sleep','dword',4) ; Slower allows to see mouth transitions better Sleep(10) If Not _LineTraverserStep($aLineTraverser, $iStep) Then $iExt = @extended ; Was there movement? Then moooove If $iExt Then WinMove($hPacMan, '', $aLineTraverser[0], $aLineTraverser[1]) EndIf $aPos = WinGetPos($hPacMan) ; Debug check. Should never be hit, so long as Window is moved to each step (including any last steps - see @extended) If $iXTarget <> $aPos[0] Or $iYTarget <> $aPos[1] Then ConsoleWrite("Mismatch: TargetX:" & $iXTarget & ", TraverserX:" & $aLineTraverser[0] & ", Current X:" & $aPos[0] & _ ", TargetY:" & $iYTarget & ", TraverserY:" & $aLineTraverser[1] & ", Current Y:" & $aPos[1] & ", @extended:" & $iExt & @CRLF) EndIf ; A little extra sleep to make it clear we've reached our destination. DllCall("kernel32.dll", 'none', 'Sleep', 'dword', 6) ; Now we'll set a new destination (with a visible line) Dim $iXTarget = Random(0, @DesktopHeight - 20, 1), $iYTarget = Random(0, @DesktopHeight - 20, 1) ;ConsoleWrite("New target: X: " & $iXTarget & ", Y: " & $iYTarget & ", FROM X: " & $aLineTraverser[0] & ", Y: " & $aLineTraverser[1] & @CRLF) ; Create a new Line-Traverser (no need to explicitly destroy the last one, it was just an array of numbers) $aLineTraverser = _LineTraverserCreate($aLineTraverser[0], $aLineTraverser[1], $iXTarget, $iYTarget) ; + Center of hollow circle, - center of target circle WinMove($hDestCircle, '', $iXTarget + 40 - 8, $iYTarget + 40 - 8) ; Apply Pacman GUI with calculated angle of line it should point towards $fLineAngleRads = Atan2_Radians($iYTarget - $aLineTraverser[1], $iXTarget - $aLineTraverser[0]) _GUIApplyPacman($hPacMan, $fLineAngleRads) ; Draw the line on-screen to give a visual indicator of the path that the hollow circle should take ; (note that the line will be overwritten by ANY screen activity, but that's fine for the example) ; Add 40 for the center of the hollow circle GUI _DrawPacmanDottedLine($aPos[0] + 40 - 1, $aPos[1] + 40 - 1, $iXTarget + 40 - 1, $iYTarget + 40 - 1) ; Or ;~ _DrawPlainOldLine($aPos[0] + 40 - 1, $aPos[1] + 40 - 1, $iXTarget + 40 - 1, $iYTarget + 40 - 1) Else $iStepsTaken += 1 If $iStepsTaken > 7 Then $iMode += 1 ;If $iMode > 2 Then $iMode = 0 ; Function masks off the needed bits _GUIApplyPacman($hPacMan, $fLineAngleRads, $iMode) $iStepsTaken = 0 EndIf WinMove($hPacMan, '', $aLineTraverser[0], $aLineTraverser[1]) EndIf WEnd Exit ; ================================================================================================================= ; Func _DrawPlainOldLine($iX1, $iY1, $iX2, $iY2) ; ; Draws a white line on the screen ; ; Author: Ascend4nt ; ================================================================================================================= Func _DrawPlainOldLine($iX1, $iY1, $iX2, $iY2) Local $hDC, $hPen, $hPenOld, $hBrush, $hBrushOld ; Get DC to screen $hDC = _WinAPI_GetDC(0) ; Create Pen (outline) [PS_SOLID = 0, PS_DASH = 1, PS_DOT = 2, PS_DASHDOT = 3, PS_DASHDOTDOT = 4, PS_NULL = 5] ; Seems no styles other than PS_SOLID work even if Width is <= 1, at least on the desktop DC in Win7 $hPen = _WinAPI_CreatePen(0, 3, 0xFFFFFF) ; Alternatively, get a Stock Pen.. (width 1 unfortunately) ; WHITE_PEN = 6, BLACK_PEN = 7, NULL_PEN = 8 ;$hPen = _WinAPI_GetStockObject(6) ; Create (or Get) Brush (fill) ; Doesn't seem to make a difference for Lines actually ; WHITE_BRUSH = 0, BLACK_BRUSH = 4, HOLLOW_BRUSH = 5 $hBrush = _WinAPI_GetStockObject(0) ; BS_SOLID = 0, BS_HOLLOW = 1, BS_HATCHED = 2 ;~ $hBrush = _WinAPI_CreateBrushIndirect(1, 0xFFFFFF, 0) ; Select Pen and Brush (saving old) $hPenOld = _WinAPI_SelectObject($hDC, $hPen) $hBrushOld = _WinAPI_SelectObject($hDC, $hBrush) _WinAPI_DrawLine($hDC, $iX1, $iY1, $iX2, $iY2) ; Select the old Brush and Pen back _WinAPI_SelectObject($hDC, $hBrushOld) _WinAPI_SelectObject($hDC, $hPenOld) ; Clean up pen & brush and then release DC _WinAPI_DeleteObject($hPen) ; _WinAPI_DeleteObject($hBrush) ; No need to delete Stock Objects _WinAPI_ReleaseDC(0, $hDC) Return True EndFunc ;==>_DrawPlainOldLine ; ================================================================================================================= ; Func _DrawPacmanDottedLine($iX1, $iY1, $iX2, $iY2) ; ; Draws a series of 'dots' on a given line. ; ; Unfortunately, in my tests, the Ellipse creates a bunch of artifacts, so the Rectangle is used to plot ; the dots. Some versions of Pacman look like they use squares anyway.. ; ; Author: Ascend4nt ; ================================================================================================================= Func _DrawPacmanDottedLine($iX1, $iY1, $iX2, $iY2) Local $hDC, $hPen, $hPenOld, $hBrush, $hBrushOld Local $aLineTraverser ; Line traverser for plotting Pac dots.. $aLineTraverser = _LineTraverserCreate($iX1, $iY1, $iX2, $iY2) ; Get DC to screen $hDC = _WinAPI_GetDC(0) ; Create Pen (outline) [PS_SOLID = 0, PS_DASH = 1, PS_DOT = 2, PS_DASHDOT = 3, PS_DASHDOTDOT = 4, PS_NULL = 5] $hPen = _WinAPI_CreatePen(5, 3, 0x00FFFF) ; Create (or Get) Brush (fill) ; [BS_SOLID = 0, BS_HOLLOW = 1, BS_HATCHED = 2] ; {Hatch Styles} HS_HORIZONTAL = 0, HS_VERTICAL = 1, HS_FDIAGONAL = 2, ; HS_BDIAGONAL = 3, HS_CROSS = 4, HS_DIAGCROSS = 5, HS_API_MAX = 12 $hBrush = _WinAPI_CreateBrushIndirect(0, 0xFFFF00, 0) ; Select Pen and Brush (saving old) $hPenOld = _WinAPI_SelectObject($hDC, $hPen) $hBrushOld = _WinAPI_SelectObject($hDC, $hBrush) ; We try to avoid drawing on the target circle While _LineTraverserStepsRemaining($aLineTraverser) > 30 ; Steps of 20 _LineTraverserStep($aLineTraverser, 20) ;~ _WinAPI_Ellipse($hDC, _WinAPI_CreateRect($aLineTraverser[0] - 5, $aLineTraverser[1] - 5, $aLineTraverser[0] + 5, $aLineTraverser[1] + 5)) _WinAPI_Rectangle($hDC, _WinAPI_CreateRect($aLineTraverser[0] - 5, $aLineTraverser[1] - 5, $aLineTraverser[0] + 5, $aLineTraverser[1] + 5)) WEnd ; Select the old Brush and Pen back _WinAPI_SelectObject($hDC, $hBrushOld) _WinAPI_SelectObject($hDC, $hPenOld) ; Clean up pen & brush and then release DC _WinAPI_DeleteObject($hPen) _WinAPI_DeleteObject($hBrush) _WinAPI_ReleaseDC(0, $hDC) Return True EndFunc ;==>_DrawPacmanDottedLine ; ================================================================================================================= ; Func _GUIApplyPacman($hGUI, $fAngleInRads, $nMouthMode = 0) ; ; Takes a GUI and applies a 'Pacman' shape to it. The angle of Pacman's mouth is based upon the $fAngleinRads ; variable. It also has 3 modes - mouth closed, partially open, or open to 90 degrees (rotated to face given angle) ; ; Parameters: ; $hGUI = GUI that will have a 'Pacman' Region applied to it ; $fAngleInRads = The Angle which the 'mouth' should center on, given in Radians (Degrees * PI / 180) ; $iMouthMode = 'Mode' of mouth: 0 (default) = 90 degree span, 1 = 40 degree span, 2 = closed (basic circle Region) ; ; Returns: ; Success: 1, @error = 0 ; Failure: 0, @error set ; ; Author: Ascend4nt ; ================================================================================================================= Func _GUIApplyPacman($hGUI, $fAngleInRads, $iMouthMode = 0) Local $aPos, $aVertices, $hEllipseRgn = 0, $hPieRegion = 0 Local $iCenterXY, $fMouthAngleRad $aPos = WinGetPos($hGUI) If @error Then Return SetError(1, 0, 0) $iCenterXY = $aPos[2] / 2 $iMouthMode = BitAND($iMouthMode, 3) Switch $iMouthMode Case 1 ; 20 Degrees $fMouthAngleRad = 0.34906585 Case 2 ; No adjustment necessary, we're using a full circle $fMouthAngleRad = 0 Case Else ; 45 Degrees $fMouthAngleRad = 0.78539816 EndSwitch Do ; Create a Basic Elliptical region $hEllipseRgn = _WinAPI_CreateEllipticRgn(_WinAPI_CreateRect(0, 0, $aPos[2], $aPos[3])) If $hEllipseRgn = 0 Then SetError(10) ExitLoop EndIf ; Everything OTHER than Mode 2 gets a 'mouth' applied If $iMouthMode <> 2 Then #cs ;; -------------- ; Vertices array: ; --------------- ; These vertices make up the mouth. ; Note that the multipler should typically be Radius, not $aPos[2] as here, if we were creating a line ; to the circumference of the circle. However, that would cause a weird triangle-inside-a-circle effect ; when combining Regions (leaving whats known as a 'Chord' behind). So we overextend it and let ; Windows clip the line at the GUI borders, which does its job of covering up the outer parts of the circle ; ----------------------------------------------------------------------------------------------------- #ce Dim $aVertices[3][2] = [ _ [$iCenterXY, $iCenterXY], _ ; We over-extend the lines here ($aPos[2] is way longer than the radius) [$iCenterXY + $aPos[2] * Cos($fAngleInRads - $fMouthAngleRad), $iCenterXY + $aPos[2] * Sin($fAngleInRads - $fMouthAngleRad)], _ [$iCenterXY + $aPos[2] * Cos($fAngleInRads + $fMouthAngleRad), $iCenterXY + $aPos[2] * Sin($fAngleInRads + $fMouthAngleRad)] _ ] ; Create the Pacman Pie area $hPieRegion = _WinAPI_CreatePolygonRgn($aVertices, 0, -1, 1) If $hPieRegion = 0 Then SetError(12) ExitLoop EndIf ; Combine, put resulting region in $hEllipseRgn. (RGN_AND = 1, RGN_OR = 2, RGN_XOR = 3, RGN_DIFF = 4) If Not _WinAPI_CombineRgn($hEllipseRgn, $hEllipseRgn, $hPieRegion, 4) Then SetError(14) ExitLoop EndIf ; Don't need this anymore (already combined with the Region injected into the GUI) _WinAPI_DeleteObject($hPieRegion) EndIf ; Set the region into the GUI. (GUI will then own it so there's no need to delete it) If Not _WinAPI_SetWindowRgn($hGUI, $hEllipseRgn, True) Then SetError(16) ExitLoop EndIf Return 1 Until 1 Local $iErr = @error, $iExt = @extended ; Cleanup _WinAPI_DeleteObject($hEllipseRgn) _WinAPI_DeleteObject($hPieRegion) Return SetError($iErr, $iExt, 0) EndFunc ;==>_GUIApplyPacman ; ================================================================================================================= ; Func Atan2_Radians($fY, $fX) ; ; Atan2() implementation, based on Wikipedia description. See notes ; ; atan2() differs from atan() in that it maps atan(Y/X) to correct Quadrants ; ; References: ; https://en.wikipedia.org/wiki/Atan2 ; https://en.wikipedia.org/wiki/Radian ; https://en.wikipedia.org/wiki/Cosine#Unit-circle_definitions ; ; Returns: Atan2() result in radians (for the flipped counterclockwise system) ; ; Author: Ascend4nt ; ================================================================================================================= Func Atan2_Radians($fY, $fX) Local Const $f_PI = 3.14159265 Local Const $f_PIHalf = $f_PI / 2 ;~ ConsoleWrite("Atan2 entry, $fY = " & $fY & ", $fX = " & $fX & @CRLF) If $fX = 0 Then ; Technically 'undefined' If $fY = 0 Then Return 0.0 If $fY < 0 Then Return -$f_PIHalf Else ; $fY >= 0 Return $f_PIHalf EndIf EndIf Local $fTmp = ATan($fY / $fX) If $fX < 0 Then If $fY < 0 Then Return $fTmp - $f_PI Else Return $fTmp + $f_PI EndIf Else ; $fX > 0 ; already checked == 0 at start Return $fTmp EndIf EndFunc ;==>Atan2_Radians1 point
-
Here are some UDFs that help when dealing with encoded mailbody and mail headers http://autoit.de/index.php?page=Thread&postID=87721#post877211 point