Leaderboard
Popular Content
Showing content with the highest reputation on 12/09/2018 in all areas
-
Resize window using picture control
argumentum and one other reacted to UEZ for a topic
If this is what you are looking for ;coded by UEZ #include <Constants.au3> #include <GDIPlus.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> AutoItSetOption("GUICoordMode", 2) Global Const $STM_SETIMAGE = 0x0172 Global $sPathKey = "HKLM64\SOFTWARE\AutoIt v3\AutoIt\" If @OSArch = "x64" Then $sPathKey = "HKLM\SOFTWARE\Wow6432Node\AutoIt v3\AutoIt\" Global $sImage = RegRead($sPathKey, "InstallDir") & "\Examples\GUI\logo4.gif" _GDIPlus_Startup() Global $hBmp = _GDIPlus_BitmapCreateFromFile($sImage) Global $iW = _GDIPlus_ImageGetWidth($hBmp), $iH = _GDIPlus_ImageGetHeight($hBmp), $iWG = 600, $iHG = 300 Global $hGUI = GUICreate("Test", $iWG, $iHG, -1, -1, -1, $WS_EX_COMPOSITED) Global $idPic = GUICtrlCreatePic("", ($iWG - $iW) / 2, ($iHG - $iH) / 2, $iW, $iH, -1, $WS_EX_STATICEDGE) Global $hHBitmap = _GDIPlus_BitmapCreateHBITMAPFromBitmap($hBmp) Global $hB = GUICtrlSendMsg($idPic, $STM_SETIMAGE, $IMAGE_BITMAP, $hHBitmap) If $hB Then _WinAPI_DeleteObject($hB) GUISetState(@SW_SHOW) Global $aMPos, $aCPos = ControlGetPos($hGUI, "", $idPic) While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE _WinAPI_DeleteObject($hHBitmap) _GDIPlus_BitmapDispose($hBmp) _GDIPlus_Shutdown() GUIDelete() Exit Case $GUI_EVENT_RESTORE ControlMove($hGUI, "", $idPic, $aCPos[0], $aCPos[1], $aCPos[2], $aCPos[3]) EndSwitch $aMPos = GUIGetCursorInfo($hGUI) Switch $aMPos[4] Case $idPic $aCPos = ControlGetPos($hGUI, "", $idPic) If $aMPos[0] = $aCPos[0] Then ;border west GUISetCursor(13, 1, $hGUI) If $aMPos[2] Then While $aMPos[2] $aMPos = GUIGetCursorInfo($hGUI) ControlMove($hGUI, "", $idPic, $aMPos[0], $aCPos[1], $aCPos[2] + $aCPos[0] - $aMPos[0], $aCPos[3]) WEnd EndIf ElseIf $aMPos[0] = $aCPos[0] + $aCPos[2] Then ;border east GUISetCursor(13, 1, $hGUI) If $aMPos[2] Then While $aMPos[2] $aMPos = GUIGetCursorInfo($hGUI) ControlMove($hGUI, "", $idPic, $aCPos[0], $aCPos[1], $aMPos[0] - $aCPos[0], $aCPos[3]) WEnd EndIf ElseIf $aMPos[1] = $aCPos[1] + $aCPos[3] Then ;border north GUISetCursor(11, 1, $hGUI) If $aMPos[2] Then While $aMPos[2] $aMPos = GUIGetCursorInfo($hGUI) ControlMove($hGUI, "", $idPic, $aCPos[0], $aCPos[1], $aCPos[2], $aMPos[1] - $aCPos[1]) WEnd EndIf ElseIf $aMPos[1] = $aCPos[1] Then ;border south GUISetCursor(11, 1, $hGUI) If $aMPos[2] Then While $aMPos[2] $aMPos = GUIGetCursorInfo($hGUI) ControlMove($hGUI, "", $idPic, $aCPos[0], $aMPos[1], $aCPos[2], $aCPos[3] + $aCPos[1] - $aMPos[1]) WEnd EndIf Else GUISetCursor(2, 1, $hGUI) EndIf Case Else GUISetCursor(2, 1, $hGUI) EndSwitch WEnd then adding the 4 corners is homework for you.2 points -
Introduction JSON (Javascript Object Notation) is a popular data-interchange format and supported by a lot of script languages. On AutoIt, there is already a >JSON UDF written by Gabriel Boehme. It is good but too slow, and not supports unicode and control characters very well. So I write a new one (and of course, fast one as usual). I use a machine code version of JSON parser called "jsmn". jsmn not only supports standard JSON, but also accepts some non-strict JSON string. See below for example. Important Update!! I rename the library from jsmn.au3 to json.au3. All function names are changed, too. Decoding Function Json_Decode($Json) $Json can be a standard or non-standard JSON string. For example, it accepts: { server: example.com port: 80 message: "this looks like a config file" } The most JSON data type will be decoded into corresponding AutoIt variable, including 1D array, string, number, true, false, and null. JSON object will be decoded into "Windows Scripting Dictionary Object" retuned from ObjCreate("Scripting.Dictionary"). AutoIt build-in functions like IsArray, IsBool, etc. can be used to check the returned data type. But for Object and Null, Json_IsObject() and Json_IsNull() should be used. If the input JSON string is invalid, @Error will be set to $JSMN_ERROR_INVAL. And if the input JSON string is not finish (maybe read from stream?), @Error will be set to $JSMN_ERROR_PART. Encoding Function Json_Encode($Data, $Option = 0, $Indent = "\t", $ArraySep = ",\r\n", $ObjectSep = ",\r\n", $ColonSep = ": ") $Data can be a string, number, bool, keyword(default or null), 1D arrry, or "Scripting.Dictionary" COM object. Ptr will be converted to number, Binary will be converted to string in UTF8 encoding. Other unsupported types like 2D array, dllstruct or object will be encoded into null. $Option is bitmask consisting following constant: $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) $JSON_UNESCAPED_UNICODE ; Encode multibyte Unicode characters literally $JSON_UNESCAPED_SLASHES ; Don't escape / $JSON_HEX_TAG ; All < and > are converted to \u003C and \u003E $JSON_HEX_AMP ; All &amp;amp;amp;s are converted to \u0026 $JSON_HEX_APOS ; All ' are converted to \u0027 $JSON_HEX_QUOT ; All " are converted to \u0022 $JSON_PRETTY_PRINT ; Use whitespace in returned data to format it $JSON_STRICT_PRINT ; Make sure returned JSON string is RFC4627 compliant $JSON_UNQUOTED_STRING ; Output unquoted string if possible (conflicting with $JSMN_STRICT_PRINT) Most encoding option have the same means like PHP's json_enocde() function. When $JSON_PRETTY_PRINT is set, output format can be change by other 4 parameters ($Indent, $ArraySep, $ObjectSep, and $ColonSep). Because these 4 output format parameters will be checked inside Jsmn_Encode() function, returned string will be always accepted by Jsmn_Decode(). $JSON_UNQUOTED_STRING can be used to output unquoted string that also accetped by Jsmn_Decode(). $JSON_STRICT_PRINT is used to check output format setting and avoid non-standard JSON output. So this option is conflicting with $JSON_UNQUOTED_STRING. Get and Put Functions Json_Put(ByRef $Var, $Notation, $Data, $CheckExists = False) Json_Get(ByRef $Var, $Notation) These functions helps user to access object or array more easily. Both dot notation and square bracket notation can be supported. Json_Put() by default will create non-exists objects and arrays. For example: Local $Obj Json_Put($Obj, ".foo", "foo") Json_Put($Obj, ".bar[0]", "bar") Json_Put($Obj, ".test[1].foo.bar[2].foo.bar", "Test") Local $Test = Json_Get($Obj, '["test"][1]["foo"]["bar"][2]["foo"]["bar"]') ; "Test" Object Help Functions Json_ObjCreate() Json_ObjPut(ByRef $Object, $Key, $Value) Json_ObjGet(ByRef $Object, $Key) Json_ObjDelete(ByRef $Object, $Key) Json_ObjExists(ByRef $Object, $Key) Json_ObjGetCount(ByRef $Object) Json_ObjGetKeys(ByRef $Object) Json_ObjClear(ByRef $Object) These functions are just warps of "Scripting.Dictionary" COM object. You can use these functions if you are not already familiar with it. == Update 2013/05/19 == * Add Jsmn_Encode() option "$JSMN_UNESCAPED_ASCII". Now the default output of Json_Encode() is exactly the same as PHP's json_encode() function (for example, chr(1) will be encoded into u0001). $JSON_UNESCAPED_ASCII ; Don't escape ascii charcters between chr(1) ~ chr(0x1f) == Update 2015/01/08 == * Rename the library from jsmn.au3 to json.au3. All function names are changed, too. * Add Json_Put() and Json_Get() * Add Null support * Using BinaryCall.au3 to loading the machine code. == Update 2018/01/13== (Jos) * Add JsonDump() to list all Json Keys and their values to easily figure out what they are. == Update 2018/10/01== (Jos) * Fixed JsonDump() some fields and values were not showing as discussed here - tnx @TheXman . == Update 2018/10/01b== (Jos) * Added Json_ObjGetItems, Tidied source and fixed au3check warnings - tnx @TheXman . == Update 2018/10/28== (Jos) * Added declaration for $value to avoid au3check warning - tnx @DerPensionist == Update 2018/12/16== (Jos) * Added another declaration for $value to avoid au3check warning and updated the version at the top - tnx @maniootek == Update 2018/12/29== (Jos) * Changed Json_ObjGet() and Json_ObjExists() to allow for multilevel object in string. == Update 2019/01/17== (Jos) * Added support for DOT notation in JSON functions. == Update 2019/07/15== (Jos) * Added support for reading keys with a dot inside when using a dot as separator (updated) == Update 2021/11/18== (TheXman) * Update details in below post: == Update 2021/11/20== (TheXman) * Minor RegEx update, no change to the functionality or result._Json(2021.11.20).zip1 point
-
AutoIt Snippets
coffeeturtle reacted to wolflake for a topic
This is probably more a tool than a function but should be helpful. This script will take rows and columns that you copy to the clipboard from Excel and produce autoit code to create a one or two dimensional array. I was doing a mock-up of a menu system in Excel and once I had what I liked I thought it would be nice if I could just copy and paste it into Autoit as dim array code. To use the script go in Excel highlight the data you want to use and copy it while you still have dashed lines around the data run the script and it will produce autoit code to define an array in the console and copied to the clipboard. Then you can paste the array code into your program. Note that Excel only has the data on the clipboard while there are dotted lines around the data. If you don't get what you expect check and see that there are dashes around the Excel data. A matrix can be looked at as either rows and columns or columns and rows (2,5) or (5,2). Because of what I was working on the system defaults to columns and rows but if you want to see it the other way there's a string variable called $transpose that's defined as True if you change that to False you will get code with rows and columns. Another nice use I found was I had display array up with about 20 files showing in it and I needed about 8 of them so I control clicked on the 8 and pressed copy data then ran my program can it produced a 1d array code. I hope someone uses this (actually a lot of people) because my girlfriend says it will never save me the time I put into it. Of course she doesn't understand that it was fun doing it. For some reason my code colors didn't come out right when I pasted it below.... Scratch that the colors are fine now that I left the page and went back to it. ;Excel matrix to Autoit array code #include <array.au3> #include <string.au3> $transpose = True $sClipt = ClipGet() $sClipt = StringReplace($sClipt, Chr(10), "") ;remove those pesky line feeds If StringInStr($sClipt, @TAB) = 0 Then make_code_1D() $sClipt = StringReplace($sClipt, @TAB, "|") ;save the tabs $aE = StringSplit($sClipt, @CR, $STR_NOCOUNT) _ArrayDelete($aE, UBound($aE) - 1) ;remove the last row $aE3 = StringSplit($aE[0], "|", $STR_NOCOUNT) Dim $aE3[0][UBound($aE3)] ;Build up the $aE3 array For $i = 0 To UBound($aE) - 1 ;ConsoleWrite($i &" "& $aE[$i] & @CRLF) $aE2 = StringSplit($aE[$i], "|", $STR_NOCOUNT) _ArrayTranspose($aE2) _ArrayAdd($aE3, $aE2) Next If $transpose Then _ArrayTranspose($aE3) ;_ArrayDisplay($aE3, "Before save") make_code_2D($aE3) Func make_code_2D($aD) Local $q = '"' Local $sLine = "dim $aAr[" & UBound($aD, 2) & "][" & UBound($aD) & "] = [[" For $j = 0 To UBound($aD, 2) - 1 For $i = 0 To UBound($aD) - 1 $sLine = $sLine & $q & $aD[$i][$j] & $q & ", " Next $sLine = StringTrimRight($sLine, 2) & "], [" Next $sLine = StringTrimRight($sLine, 3) & "]" ConsoleWrite($sLine & @CRLF) ClipPut($sLine) EndFunc ;==>make_code_2D Func make_code_1D() Local $q = '"' Local $aD = StringSplit($sClipt, @CR, $STR_NOCOUNT) _ArrayDelete($aD, UBound($aD) - 1) ;remove the last row Local $sLine = "dim $aAr[" & UBound($aD) & "] = [" For $i = 0 To UBound($aD) - 1 $sLine = $sLine & $q & $aD[$i] & $q & ", " Next $sLine = StringTrimRight($sLine, 2) & "]" ConsoleWrite($sLine & @CRLF) ClipPut($sLine) Exit EndFunc ;==>make_code_1D1 point -
Or you can go the adlib route if your code is too complicated to be in a event loop or if you are extra lazy: AdlibRegister(ThreeMinuteExit, 60 * 3 * 1000) ; 3 minutes in milliseconds Func ThreeMinuteExit() MsgBox(0, "3 minutes", "Hey mate your time is up, now it is time to quit") Exit EndFunc @Xandy You used 3 seconds instead of the 3 minutes OP asked for by the way1 point
-
Ah ok, well this is originally not my code and really only added the JSON_DUMP() function to it as I thought it would be very useful to others. I am fine when we also clean up the code further and make an update for that. I honestly think these UDF's should be added to the standard AutoIt3 distribution but that means somebody needs to build the helpfilepages and examples. Jos1 point
-
Thanks. I didn't realize that the JSON Arrays were "just arrays." Solution provided below for others. #Include "JSon.au3" Local $Json = '{"name":"John","cars":[ "Ford", "BMW", "Fiat", "Chevy" ]}' ;Correctly returns a count of 2 elements (name and cars) Local $Obj = JSon_Decode($Json) ConsoleWrite("count root: " & Json_ObjGetCount($Obj) & @CRLF) ;Returns error Variable must be of type "Object" ;Local $Cars = Json_ObjGet($Obj, '["cars"][0]') ;ConsoleWrite("count cars: " & Json_ObjGetCount($aCars) & @CRLF) ;Solution from TheDcoder Local $aCars = Json_Get($Obj, '["cars"]') ConsoleWrite("count cars: " & UBound($aCars) & @CRLF) ;Walking the array For $iCurrentCar = 0 To UBound($aCars) - 1 ;zero-based array ConsoleWrite("Car '" & $iCurrentCar & "' of '" & UBound($aCars) & "' is '" & _ Json_Get($Obj, '["cars"][' & $iCurrentCar & ']') & "'" & @CRLF) Next1 point
-
My func: Func _URIEncode($sData) Local $aData = StringSplit(BinaryToString(StringToBinary($sData, 4), 1), "") Local $nChar $sData = "" For $i = 1 To $aData[0] $nChar = Asc($aData[$i]) Switch $nChar Case 45, 46, 48 To 57, 65 To 90, 95, 97 To 122, 126 $sData &= $aData[$i] Case 32 $sData &= "+" Case Else $sData &= "%" & Hex($nChar, 2) EndSwitch Next Return $sData EndFunc ;==>_URIEncode Func _GetTranslateBaidu($sText) Local $oHTTP = ObjCreate("winhttp.winhttprequest.5.1") $oHTTP.Open("Post", "http://translate.baidu.com/v2transapi", False) $oHTTP.SetRequestHeader("Content-Type", "application/x-www-form-urlencoded; charset=UTF-8") $oHTTP.SetRequestHeader("Host", 'translate.baidu.com') $oHTTP.SetRequestHeader("User-Agent", "Mozilla/5.0 (Windows NT 6.1; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/58.0.3029.110 Safari/537.36") $oHTTP.SetRequestHeader("Connection", "keep-alive") ConsoleWrite('from=en&to=zh&query=' & _URIEncode($sText) & '&transtype=translang&simple_means_flag=3' & @CRLF) $oHTTP.Send('from=en&to=zh&query=' & _URIEncode($sText) & '&transtype=translang&simple_means_flag=3') If ($oHTTP.Status == 200) Then $sJSON = $oHTTP.ResponseText $aJSON = Json_Decode($sJSON, 10000) ConsoleWrite($aJSON['trans_result']['data'][0]['dst'] & @CRLF) EndIf EndFunc ;==>_GetTranslateBaidu Include: jsonMap.au31 point
-
Cheers. I was searching for something like that on msdn but could not find it. Got my answer ... http://stackoverflow.com/questions/6952084/excel-cell-alignments-numerical-values-for-e-g-xlleft-xlright-or-xlcenter1 point
-
Yeah, I now see what it is...... - you are trying too hard! Not sure what all that Array stuff is - you don't need it when working with JSON - it, by definition, is an array-decrypter (of sorts) - whatever you call it, JSON is a method in/of itself and you are trying to tear it apart yourself, then looking for the tools to do the work. Don't try so hard! Basically, all you need when using this UDF: 1. take the incoming JSON code (in your case, coming from a .txt file - it might come from a result or anywhere... - but LEAVE IT ALONE AS JSON) 2. JSMN_DECODE it to an object. 3. use JSMN_GET. That's it! This works (using the demo from JSMN and your data to show you the full results) - notice you only need ONE include, which is this UDF! #include "JSMN.au3" local $Json1 $Json1 = FileRead("TestHistory.txt") Local $Data1 = Jsmn_Decode($Json1) ; ************************************************************************************ ; these lines are NOT NEEDED in your live system, just here to show the output Local $Json2 = Jsmn_Encode($Data1, $JSMN_UNQUOTED_STRING) Local $Data2 = Jsmn_Decode($Json2) Local $Json3 = Jsmn_Encode($Data2, $JSMN_PRETTY_PRINT, " ", "\n", "\n", "") ConsoleWrite("Test3 Unquoted Result: " & $Json2 & @CRLF) ConsoleWrite("Test3 Pretty Result: " & $Json3 & @CRLF) ; the above lines are not needed in a live system ; ************************************************************************************ $resault = Jsmn_Get($Data1, '[0]["Sysinfo"][0]["Properties"][0]["Name"]') ConsoleWrite($resault & @CRLF) and, for future reference, when you post code - go ahead and put in the #includes you are using (makes it simpler for someone testing your code, as well as know what you are using - it might be that you have the wrong includes sometimes....) So, end result - let the tool do the work. (BTW, my 'tip' on going step at a time didn't work on this - because it starts off with an array - the data I have been working with is a bit different and that stepping through worked for me - guess it all depends on the data However, the 'replace [{ with [0] works on your data as well as mine, so that seems a good 'rule'... )1 point
-
Right to Left texts in menus
argumentum reacted to Mat for a topic
1) To create sub menus, you first create the sub menu with _GUICtrlMenu_CreateMenu, and then add it using this $hSubMenu argument of _GUICtrlMenu_AddMenuItem. 2) This seems to be a problem which occurs if the window passed to TrackPopupMenu is not visible. A quick workaround is to create a visible window, that is positioned off screen: #include <GUIMenu.au3> $hSubMenu = _GUICtrlMenu_CreateMenu() _GUICtrlMenu_AddMenuItem($hSubMenu, "Submenu 1") _GUICtrlMenu_AddMenuItem($hSubMenu, "Submenu 2") _GUICtrlMenu_AddMenuItem($hSubMenu, "Submenu 3") $hMenu = _GUICtrlMenu_CreatePopup() _GUICtrlMenu_AddMenuItem($hMenu, "Menu 1") _GUICtrlMenu_AddMenuItem($hMenu, "Submenu", 0, $hSubMenu) _GUICtrlMenu_AddMenuItem($hMenu, "Menu 2") $hGUI = GUICreate("Test", 1, 1, -100, -100) GUISetState() _GUICtrlMenu_TrackPopupMenu($hMenu, $hGUI)1 point