Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 10/25/2023 in all areas

  1. For a project I had to use FOLDERID_Documents I was suprised that this variable was missing from : APIShellExConstants.au3 So I have edited : APIShellExConstants.au3 to add below line 623 : and of course it's works as expected : perfectly without any other modification ! https://learn.microsoft.com/en-us/windows/win32/shell/knownfolderid?redirectedfrom=MSDN say : This modification allow to do example: Is this quick "fix" could being added to the next AutoIT version ? Regards Edit: as requested I have done on trac a request feature : https://www.autoitscript.com/trac/autoit/ticket/3979
    3 points
  2. It's good that you got your answer. I am just concerned when I see hard coded values like +/- 110. Maybe you can calculate these values with respect to your bitmap/bubble size but if these won't change in different context then never mind, use them as they are.
    1 point
  3. yes it does exactly that Thanks
    1 point
  4. This is a basic start but this script can be improved. #include <GDIPlus.au3> $hMain = GUICreate('Test', 400, 400) $cPic = GUICtrlCreatePic('', 0, 0, 400, 400) GUISetState(@SW_SHOW, $hMain) _GDIPlus_Startup() $hBubble = SpeechBubble(400, 400, 'Yayyy!' & @CRLF & 'This is my bubble.') BitmapRotate($hBubble, 45) BitmapToCtrl($hBubble, $cPic) Do Until GUIGetMsg() = -3 _GDIPlus_BitmapDispose($hBubble) _GDIPlus_Shutdown() Func SpeechBubble($iW, $iH, $sString, $iBubbleColor = 0xFF1D3557, $iTextColor = 0xFFFFFFFF, $sFontFamily = 'Arial', $iFontSize = 15, $iPadding = 25) Local $aTriangle[4][2] = [[3, 0], [10, Int($iH/2)], [40, Int($iH/2) - 20], [40, Int($iH/2) + 20]] Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hFamily = _GDIPlus_FontFamilyCreate($sFontFamily) Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hBrush = _GDIPlus_BrushCreateSolid($iBubbleColor) Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH) Local $hPath = _GDIPlus_PathCreate(1) _GDIPlus_StringFormatSetAlign($hFormat, 1) _GDIPlus_StringFormatSetLineAlign($hFormat, 1) _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2) _GDIPlus_PathAddEllipse($hPath, $iPadding, $iPadding, $iW - 2 * $iPadding, $iH - 2 * $iPadding) _GDIPlus_PathAddPolygon($hPath, $aTriangle) _GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush) _GDIPlus_BrushSetSolidColor($hBrush, $iTextColor) _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $tLayout, $hFormat, $hBrush) _GDIPlus_PathDispose($hPath) _GDIPlus_BrushDispose($hBrush) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_GraphicsDispose($hGraphics) Return $hBitmap EndFunc Func BitmapRotate(ByRef $hBitmap, $iAngle) Local $iW = _GDIPlus_ImageGetWidth($hBitmap) Local $iH = _GDIPlus_ImageGetHeight($hBitmap) Local $hNew = _GDIPlus_BitmapCreateFromScan0($iW, $iH) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hNew) Local $hMatrix = _GDIPlus_MatrixCreate() _GDIPlus_MatrixTranslate($hMatrix, $iW / 2, $iH / 2) _GDIPlus_MatrixRotate($hMatrix, $iAngle) _GDIPlus_GraphicsSetTransform($hGraphics, $hMatrix) _GDIPlus_MatrixTranslate($hMatrix, -$iW / 2, -$iH / 2) _GDIPlus_GraphicsSetTransform($hGraphics, $hMatrix) _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) $hBitmap = $hNew EndFunc Func BitmapToCtrl($hBitmap, $cCtrl) Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP)) _WinAPI_DeleteObject($hHBITMAP) EndFunc Or something like this if you don't want to keep tracking your bitmaps for further release of resources: #include <GDIPlus.au3> $hMain = GUICreate('Test', 400, 400) $cPic = GUICtrlCreatePic('', 0, 0, 400, 400) GUISetState(@SW_SHOW, $hMain) SpeechBubble($cPic, 400, 400, 'Yayyy!' & @CRLF & 'This is my bubble.', 45) Do Until GUIGetMsg() = -3 Func SpeechBubble($cCtrl, $iW, $iH, $sString, $iAngle = 0, $iBubbleColor = 0xFF1D3557, $iTextColor = 0xFFFFFFFF, $sFontFamily = 'Arial', $iFontSize = 15, $iPadding = 25) Local $aTriangle[4][2] = [[3, 0], [10, Int($iH/2)], [40, Int($iH/2) - 20], [40, Int($iH/2) + 20]] _GDIPlus_Startup() Local $hBitmap = _GDIPlus_BitmapCreateFromScan0($iW, $iH) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hBitmap) Local $hFamily = _GDIPlus_FontFamilyCreate($sFontFamily) Local $hFont = _GDIPlus_FontCreate($hFamily, $iFontSize) Local $hFormat = _GDIPlus_StringFormatCreate() Local $hBrush = _GDIPlus_BrushCreateSolid($iBubbleColor) Local $tLayout = _GDIPlus_RectFCreate(0, 0, $iW, $iH) Local $hPath = _GDIPlus_PathCreate(1) _GDIPlus_StringFormatSetAlign($hFormat, 1) _GDIPlus_StringFormatSetLineAlign($hFormat, 1) _GDIPlus_GraphicsSetSmoothingMode($hGraphics, 2) _GDIPlus_PathAddEllipse($hPath, $iPadding, $iPadding, $iW - 2 * $iPadding, $iH - 2 * $iPadding) _GDIPlus_PathAddPolygon($hPath, $aTriangle) _GDIPlus_GraphicsFillPath($hGraphics, $hPath, $hBrush) _GDIPlus_BrushSetSolidColor($hBrush, $iTextColor) _GDIPlus_GraphicsDrawStringEx($hGraphics, $sString, $hFont, $tLayout, $hFormat, $hBrush) BitmapRotate($hBitmap, $iAngle) BitmapToCtrl($hBitmap, $cCtrl) _GDIPlus_PathDispose($hPath) _GDIPlus_BrushDispose($hBrush) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) _GDIPlus_Shutdown() EndFunc Func BitmapRotate(ByRef $hBitmap, $iAngle) Local $iW = _GDIPlus_ImageGetWidth($hBitmap) Local $iH = _GDIPlus_ImageGetHeight($hBitmap) Local $hNew = _GDIPlus_BitmapCreateFromScan0($iW, $iH) Local $hGraphics = _GDIPlus_ImageGetGraphicsContext($hNew) Local $hMatrix = _GDIPlus_MatrixCreate() _GDIPlus_MatrixTranslate($hMatrix, $iW / 2, $iH / 2) _GDIPlus_MatrixRotate($hMatrix, $iAngle) _GDIPlus_GraphicsSetTransform($hGraphics, $hMatrix) _GDIPlus_MatrixTranslate($hMatrix, -$iW / 2, -$iH / 2) _GDIPlus_GraphicsSetTransform($hGraphics, $hMatrix) _GDIPlus_GraphicsDrawImage($hGraphics, $hBitmap, 0, 0) _GDIPlus_GraphicsDispose($hGraphics) _GDIPlus_BitmapDispose($hBitmap) $hBitmap = $hNew EndFunc Func BitmapToCtrl($hBitmap, $cCtrl) Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP)) _WinAPI_DeleteObject($hHBITMAP) EndFunc
    1 point
  5. or this ;$string = "bob smith cima, phd, ma" $string = "brian o'connor cima, phd, ma" $res = StringRegExpReplace($string, '^([[:alpha:]'']+) ((?1)).*', "$2, $1") MsgBox(0,'', $res)
    1 point
  6. here is my approach, with out rotate ; https://www.autoitscript.com/forum/topic/211009-create-a-custom-angled-speech-bubble/?do=findComment&comment=1525867 #include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <WinAPIHObj.au3> ; For _GDIPlus_MatrixCreate Opt('MustDeclareVars', 1) _Main() Func _Main() Global $hGUI, $hWnd, $hImage, $hGraphic, $cPic, $idButton Global $outputImage = "SpeechBubble.png" Global $fAngle = 0 Global $nImgWidth = 220 FileDelete($outputImage) ; Create GUI $hGUI = GUICreate("GDI+", $nImgWidth, 240) $idButton = GUICtrlCreateButton("Angle", 0, 0, 100, 20) $cPic = GUICtrlCreatePic('', 0, 20, $nImgWidth, $nImgWidth) $hWnd = WinGetHandle("GDI+") GUISetState() ; Create a blank image _GDIPlus_Startup() $hImage = _GDIPlus_BitmapCreateFromScan0($nImgWidth, $nImgWidth) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) ; Create fill brushes and draw pens Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00) ; Light green color Local $hBrush2 = _GDIPlus_BrushCreateSolid(0xFF0000FF) ; Light blue color Global $hPen = _GDIPlus_PenCreate(0xFF8800AA, 2) Global $hPen2 = _GDIPlus_PenCreate(0xFFFF0000, 2) DrawBubble($hGraphic, $hBrush, $hPen, 0) BitmapToCtrl($hImage, $cPic) Do Switch GUIGetMsg() Case $GUI_EVENT_CLOSE ExitLoop Case $idButton Global $fAngle = InputBox("Enter Angle", "Please enter the angle of the speech bubble pointer", 45) ConsoleWrite("Required Angle: " & $fAngle & @CRLF) SpeechBubbleRedraw($hGraphic, $hImage, $hBrush, $hPen, $fAngle) EndSwitch Until False ;GUIGetMsg() = $GUI_EVENT_CLOSE ; Save the image to $outputImage _GDIPlus_ImageSaveToFile($hImage, $outputImage) _GDIPlus_BrushDispose($hBrush2) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_BitmapDispose($hImage) _GDIPlus_Shutdown() If FileExists($outputImage) Then ShellExecute($outputImage) ;If FileExists($outputImage2) Then ShellExecute($outputImage2) EndFunc ;==>_Main ;---------------------------------------------------------------------------------------- Func BitmapToCtrl($hBitmap, $cCtrl) Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP)) _WinAPI_DeleteObject($hHBITMAP) EndFunc ;==>BitmapToCtrl ;---------------------------------------------------------------------------------------- Func DrawBubble($hGraphicT, $hBrushT, $hPenT, $iAngle = 135) ; Draw a Triangle Local Const $nPI = 3.1415926535897932384626433832795 Local $Deg, $R, $iX1, $iY1, $iRadius Local $iCenter = $nImgWidth / 2 Local $aPoints[4][2] ; Create Points Array (Row 0 Col 0 holds row count) $aPoints[0][0] = 3 ;Number of points ;b $iRadius = $nImgWidth / 2 $Deg = $iAngle $R = ($nPI / 2) - ($Deg * ($nPI / 180)) $iX1 = $iCenter + Cos($R) * $iRadius $iY1 = $iCenter - Sin($R) * $iRadius $aPoints[2][0] = $iX1 $aPoints[2][1] = $iY1 ;ConsoleWrite("b=" & $aPoints[2][0] & ", " & $aPoints[2][1] & @CRLF) ;c $iRadius = 25 $Deg = $Deg + 90 $R = ($nPI / 2) - ($Deg * ($nPI / 180)) $iX1 = $iCenter + Cos($R) * $iRadius $iY1 = $iCenter - Sin($R) * $iRadius $aPoints[3][0] = $iX1 $aPoints[3][1] = $iY1 ;ConsoleWrite("c=" & $aPoints[3][0] & ", " & $aPoints[3][1] & @CRLF) ;a $iRadius = 25 $Deg = $Deg + 180 $R = ($nPI / 2) - ($Deg * ($nPI / 180)) $iX1 = $iCenter + Cos($R) * $iRadius $iY1 = $iCenter - Sin($R) * $iRadius $aPoints[1][0] = $iX1 $aPoints[1][1] = $iY1 ;ConsoleWrite("a=" & $aPoints[1][0] & ", " & $aPoints[1][1] & @CRLF) _GDIPlus_GraphicsFillPolygon($hGraphicT, $aPoints, $hBrushT) _GDIPlus_GraphicsDrawPolygon($hGraphicT, $aPoints, $hPenT) ; Draw a circle Local $CircleDiameter = 150 Local $CircleX = ($nImgWidth - $CircleDiameter) / 2 Local $CircleY = ($nImgWidth - $CircleDiameter) / 2 _GDIPlus_GraphicsFillEllipse($hGraphicT, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hBrushT) _GDIPlus_GraphicsDrawEllipse($hGraphicT, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hPenT) ;hide tail $iRadius = ($CircleDiameter / 2) + 1 $Deg = $iAngle $R = ($nPI / 2) - ($Deg * ($nPI / 180)) $iX1 = $iCenter + Cos($R) * $iRadius $iY1 = $iCenter - Sin($R) * $iRadius $hPenT = _GDIPlus_PenCreate(0xFF00FF00, 14) _GDIPlus_GraphicsDrawLine($hGraphicT, $iCenter, $iCenter, $iX1, $iY1, $hPenT) EndFunc ;==>DrawBubble ;---------------------------------------------------------------------------------------- Func SpeechBubbleRedraw($hGraphicT, $hImageT, $hBrushT, $hPenT, $iAngle = 135) _GDIPlus_GraphicsClear($hGraphicT, 0x00FFFFFF) ; Clear with white (0xFFFFFFFF) or any other desired color DrawBubble($hGraphicT, $hBrushT, $hPenT, $iAngle) GUICtrlDelete($cPic) $cPic = GUICtrlCreatePic('', 0, 20, $nImgWidth, $nImgWidth) GUISetState() BitmapToCtrl($hImageT, $cPic) _GDIPlus_ImageSaveToFile($hImageT, $outputImage) EndFunc ;==>SpeechBubbleRedraw
    1 point
  7. Here is your code modified from first post #include <GuiConstantsEx.au3> #include <GDIPlus.au3> #include <WinAPIHObj.au3> ; For _GDIPlus_MatrixCreate Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hWnd, $hImage, $hGraphic, $cPic Local $outputImage = "SpeechBubble.png" FileDelete($outputImage) Local $fAngle = InputBox( "Enter Angle", "Please enter the angle of the speech bubble pointer",10) ConsoleWrite("Required Angle: " & $fAngle & @CRLF) ; Create GUI $hGUI = GUICreate("GDI+", 220, 220) $cPic = GUICtrlCreatePic('', 0, 0, 220, 220) $hWnd = WinGetHandle("GDI+") GUISetState() ; Create a blank image _GDIPlus_Startup() $hImage = _GDIPlus_BitmapCreateFromScan0(220, 220) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) ; Create fill brushes and draw pens Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00) ; Light green color Local $hBrush2 = _GDIPlus_BrushCreateSolid(0xFF0000FF) ; Light blue color Global $hPen = _GDIPlus_PenCreate(0xFF8800AA, 2) Global $hPen2 = _GDIPlus_PenCreate(0xFFFF0000, 2) ; Draw a Triangle Global $tWidth = 220 Global $tHeight = 150 Global $TriangleBase = 60 Global $TriangleHeight = 60 Global $TriangleX = 110 - $TriangleBase / 2 Global $TriangleY = 65 ; $CircleY + $CircleDiameter / 2 Local $aPoints[4][2] ; Create Points Array (Row 0 Col 0 holds row count) $aPoints[0][0] = 3 ;Number of points $aPoints[1][0] = $TriangleX $aPoints[1][1] = $TriangleY $aPoints[2][0] = $TriangleX + $TriangleBase $aPoints[2][1] = $TriangleY $aPoints[3][0] = $TriangleX + $TriangleBase / 2 $aPoints[3][1] = $TriangleY - $TriangleHeight _GDIPlus_GraphicsFillPolygon($hGraphic, $aPoints, $hBrush) _GDIPlus_GraphicsDrawPolygon($hGraphic, $aPoints, $hPen) ; Draw a circle Local $CircleDiameter = 150 Local $CircleX = (220 - $CircleDiameter) / 2 Local $CircleY = (220 - $CircleDiameter) / 2 _GDIPlus_GraphicsFillEllipse ( $hGraphic, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hBrush) _GDIPlus_GraphicsDrawEllipse($hGraphic, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hPen) ;This in new - pen for drawing axes lines Local $hPenArrow = _GDIPlus_PenCreate(0xFF000000, 1) _GDIPlus_PenSetEndCap($hPenArrow, $GDIP_LINECAPARROWANCHOR) Local $hPenArrow_ForRotated = _GDIPlus_PenCreate(0xFFF00F00, 1);yellow line - rotated _GDIPlus_PenSetEndCap($hPenArrow_ForRotated, $GDIP_LINECAPARROWANCHOR) Local $fAxesCenterX=$CircleX+$CircleDiameter/2, $fAxesCenterY=$CircleY+$CircleDiameter/2;center coordinates ConsoleWrite("X=" & $fAxesCenterX & "; Y=" & $fAxesCenterY & @CRLF) _GDIPlus_GraphicsDrawLine($hGraphic,$fAxesCenterX,$fAxesCenterY,$fAxesCenterX+100,$fAxesCenterY,$hPenArrow);notice value of center coordinates ; Create a matrix and rotate the image by $fAngle degrees ;Rotation Matrix Local $hMatrix = _GDIPlus_MatrixCreate() _GDIPlus_MatrixTranslate($hMatrix,+$fAxesCenterX,+$fAxesCenterY);https://www.vbforums.com/showthread.php?841185-GDIPLUS-how-rotate-an-image-by-a-center&s=f20f7a20e35573308599859c5bb9c70d&p=5123549&viewfull=1#post5123549 _GDIPlus_MatrixRotate($hMatrix, $fAngle, False) _GDIPlus_MatrixTranslate($hMatrix,-$fAxesCenterX,-$fAxesCenterY) _GDIPlus_GraphicsSetTransform($hGraphic, $hMatrix);https://www.vbforums.com/showthread.php?841185-GDIPLUS-how-rotate-an-image-by-a-center&s=f20f7a20e35573308599859c5bb9c70d&p=5123549&viewfull=1#post5123549 _GDIPlus_GraphicsDrawImage($hGraphic, $hImage, 0, 0);"Origin display rotated" - comment this line to see axes BitmapToCtrl($hImage, $cPic) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Save the image to $outputImage ;_GDIPlus_ImageSaveToFile($hImage, $outputImage) _GDIPlus_PenDispose($hPenArrow) _GDIPlus_BrushDispose($hBrush2) _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_BitmapDispose($hImage) _GDIPlus_Shutdown() ;If FileExists($outputImage) Then ShellExecute($outputImage) EndFunc ;==>_Main Func BitmapToCtrl($hBitmap, $cCtrl) Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP)) _WinAPI_DeleteObject($hHBITMAP) EndFunc Rotation about center taken from here.
    1 point
  8. Why don't you tell us what are you trying to achieve so we can help you better? Why don't you create a bitmap and display it properly in a picture control? Something like this: #include <GuiConstantsEx.au3> #include <GDIPlus.au3> Opt('MustDeclareVars', 1) _Main() Func _Main() Local $hGUI, $hWnd, $hImage, $hGraphic, $cPic ; Create GUI $hGUI = GUICreate("GDI+", 400, 300) $cPic = GUICtrlCreatePic('', 0, 0, 400, 300) $hWnd = WinGetHandle("GDI+") GUISetState() ; Create a blank image _GDIPlus_Startup() $hImage = _GDIPlus_BitmapCreateFromScan0(400, 300) $hGraphic = _GDIPlus_ImageGetGraphicsContext($hImage) ; Draw a circle Local $CircleDiameter = 150 Local $CircleX = (400 - $CircleDiameter) / 2 Local $CircleY = (300 - $CircleDiameter) / 2 Local $hBrush = _GDIPlus_BrushCreateSolid(0xFF00FF00) ; Light green color _GDIPlus_GraphicsFillEllipse ( $hGraphic, $CircleX, $CircleY, $CircleDiameter, $CircleDiameter, $hBrush) BitmapToCtrl($hImage, $cPic) Do Until GUIGetMsg() = $GUI_EVENT_CLOSE ; Save the image to "circle.png" _GDIPlus_ImageSaveToFile($hImage, "circle.png") _GDIPlus_BrushDispose($hBrush) _GDIPlus_GraphicsDispose($hGraphic) _GDIPlus_BitmapDispose($hImage) _GDIPlus_Shutdown() If FileExists("circle.png") Then ShellExecute("circle.png") EndFunc ;==>_Main Func BitmapToCtrl($hBitmap, $cCtrl) Local $hHBITMAP = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBitmap) _WinAPI_DeleteObject(GUICtrlSendMsg($cCtrl, 0x0172, 0, $hHBITMAP)) _WinAPI_DeleteObject($hHBITMAP) EndFunc
    1 point
  9. I searched for "_datediff years months days"
    1 point
  10. Andreik

    LIBDEVP vulnerability

    It's not that important. The fact it's still valid that libwebp it's an independent library and has nothing to do with AutoIt core.
    1 point
  11. Andreik

    LIBDEVP vulnerability

    @TheDcoder that's what I said in a previous post but seems someone delete it. There is no native support for libwebp in AutoIt so there is no vulnerability related to AutoIt.
    1 point
  12. TheDcoder

    LIBDEVP vulnerability

    AutoIt does not link with libwebp so I don't see how it could be effected.
    1 point
  13. If you want the difference between two dates expressed in years, months, days, hours, minutes and seconds, this little _ElapsedTime() function can be useful for you. It returns an array with those results in its elements, that you can then format as you better like... (maybe it can be shorten a bit) (something similar already diascussed here.... https://www.autoitscript.com/forum/topic/206014-autoit-calculates-date/?do=findComment&comment=1483698) #include <Date.au3> Local $Start = "2020/01/01 08:00:00" ; "1962/05/02" & " " & "00:00:00" ; _NowTime(); start date Local $Stop = "2021/09/05 15:58:00" ; _NowCalc() ; _NowCalcDate() ; end date Local $aElapsed = _ElapsedTime($Start, $Stop) ; MsgBox(0, "Elapsed Time", $aElapsed[0] & " years, " & $aElapsed[1] & " months, " & $aElapsed[2] & " days, " & $aElapsed[3] & " hours, " & $aElapsed[4] & " minutes, " & $aElapsed[5] & " seconds") ConsoleWrite("Elapsed Time from " & $Start & " to " & $Stop & " : " & $aElapsed[0] & " years, " & $aElapsed[1] & " months, " & $aElapsed[2] & " days, " & $aElapsed[3] & " hours, " & $aElapsed[4] & " minutes, " & $aElapsed[5] & " seconds" & @CRLF) ; returns an array with the elapsed time between 2 dates expressed in years, months, days, hours, minutes, seconds Func _ElapsedTime($sStartDate, $sEndDate) Local Enum $iYears, $iMonths, $iDays, $iHours, $iMinutes, $iSeconds Local $aResult[6] ; $aResult[$iYears] = _DateDiff('Y', $sStartDate, $sEndDate) $aResult[$iMonths] = _DateDiff('M', _DateAdd('Y', $aResult[$iYears], $sStartDate), $sEndDate) $aResult[$iDays] = _DateDiff('D', _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate)), $sEndDate) $aResult[$iHours] = _DateDiff('h', _DateAdd('D', $aResult[$iDays], _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate))), $sEndDate) $aResult[$iMinutes] = _DateDiff('n', _DateAdd('h', $aResult[$iHours], _DateAdd('D', $aResult[$iDays], _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate)))), $sEndDate) $aResult[$iSeconds] = _DateDiff('s', _DateAdd('n', $aResult[$iMinutes], _DateAdd('h', $aResult[$iHours], _DateAdd('D', $aResult[$iDays], _DateAdd('M', $aResult[$iMonths], _DateAdd('Y', $aResult[$iYears], $sStartDate))))), $sEndDate) Return $aResult ; [0] years; [1] months; [2] days; [3] hours; [4] minutes; [5] seconds EndFunc ;==>_ElapsedTime
    1 point
  14. I wrote this very simple functions to parse command line arguments. It can get: Simple key/value Example. The following code: #include "cmdline.au3" MsgBox(0, _CmdLine_Get('color')) Will return "white" if you run the script in one of these ways (quotes are optional but mandatory if you're going to use spaces): script.exe -color "white" script.exe --color white script.exe /color white Existence Example. The following code: #include "cmdline.au3" If _CmdLine_KeyExists('givemecoffee') Then ConsoleWrite('You want coffee.') Else ConsoleWrite('You do not want coffee.') EndIf Will return "You want coffee." if you run one of these: script.exe -givemecoffee script.exe --givemecoffee script.exe /givemecoffee And the following code: #include "cmdline.au3" If _CmdLine_ValueExists('givemecoffee') Then ConsoleWrite('You want coffee.') Else ConsoleWrite('You do not want coffee.') EndIf Will return "You want coffee." if you run one of these: script.exe givemecoffee script.exe "givemecoffee" Flags Example. This script: #include "cmdline.au3" ConsoleWrite("You want: ") If _CmdLine_FlagEnabled('C') Then ConsoleWrite("coffee ") EndIf If _CmdLine_FlagEnabled('B') Then ConsoleWrite("beer ") EndIf ConsoleWrite(" and you do not want: ") If _CmdLine_FlagDisabled('V') Then ConsoleWrite("vodka ") EndIf If _CmdLine_FlagDisabled('W') Then ConsoleWrite("wine ") EndIf ConsoleWrite(" but you did not tell me if you want: ") If Not _CmdLine_FlagExists('S') Then ConsoleWrite("soda ") EndIf If Not _CmdLine_FlagExists('J') Then ConsoleWrite("juice ") EndIf Will return "You want: coffee beer and you do not want: vodka wine but you did not tell me if you want: soda juice" if you run: script.exe +CB -VW Getting argument by its index You can also read the $CmdLine (1-based index) through this function. The advantage is that if the index does not exist (the user did not specify the argument), it won't break your script. It will just return the value you specify in the second function parameter. #include "cmdline.au3" $first_argument = _CmdLine_GetValByIndex(1, False) If Not $first_argument Then ConsoleWrite("You did not specify any argument.") Else ConsoleWrite("First argument is: " & $first_argument) EndIf Just a note: The second value of _CmdLine_GetValByIndex function can be an integer value, a string, a boolean value, an array or anything you want it to return if the index does not exist in $CmdLine array. This parameter is also available in _CmdLine_Get() function, also as a second function parameter. In this case, it will return this value if the key was not found. Example: #include "cmdline.au3" $user_wants = _CmdLine_Get("iwant", "nothing") ConsoleWrite("You want " & $user_wants) So, if you run: script.exe /iwant water It will return "You want water". But if you run just: script.exe It will return "You want nothing". Please note that, as these two are the only functions in this library meant to return strings, the second parameter is not available for the other functions. By default, if you do not specify any fallback value, it returns null if the wanted value could not be found. Also, please note that this UDF can NOT parse arguments in the format (key=value). Example: script.exe key=value IT WILL NOT WORK Here is the code: #include-once #comments-start CmdLine small UDF coder: Jefrey (jefrey[at]jefrey.ml) #comments-end Func _CmdLine_Get($sKey, $mDefault = Null) For $i = 1 To $CmdLine[0] If $CmdLine[$i] = "/" & $sKey OR $CmdLine[$i] = "-" & $sKey OR $CmdLine[$i] = "--" & $sKey Then If $CmdLine[0] >= $i+1 Then Return $CmdLine[$i+1] EndIf EndIf Next Return $mDefault EndFunc Func _CmdLine_KeyExists($sKey) For $i = 1 To $CmdLine[0] If $CmdLine[$i] = "/" & $sKey OR $CmdLine[$i] = "-" & $sKey OR $CmdLine[$i] = "--" & $sKey Then Return True EndIf Next Return False EndFunc Func _CmdLine_ValueExists($sValue) For $i = 1 To $CmdLine[0] If $CmdLine[$i] = $sValue Then Return True EndIf Next Return False EndFunc Func _CmdLine_FlagEnabled($sKey) For $i = 1 To $CmdLine[0] If StringRegExp($CmdLine[$i], "\+([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then Return True EndIf Next Return False EndFunc Func _CmdLine_FlagDisabled($sKey) For $i = 1 To $CmdLine[0] If StringRegExp($CmdLine[$i], "\-([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then Return True EndIf Next Return False EndFunc Func _CmdLine_FlagExists($sKey) For $i = 1 To $CmdLine[0] If StringRegExp($CmdLine[$i], "(\+|\-)([a-zA-Z]*)" & $sKey & "([a-zA-Z]*)") Then Return True EndIf Next Return False EndFunc Func _CmdLine_GetValByIndex($iIndex, $mDefault = Null) If $CmdLine[0] >= $iIndex Then Return $CmdLine[$iIndex] Else Return $mDefault EndIf EndFunc
    1 point
×
×
  • Create New...