Jump to content

Leaderboard

Popular Content

Showing content with the highest reputation on 11/03/2017 in all areas

  1. #include <GuiComboBox.au3> #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <WinAPI.au3> #include <Array.au3> #include <WinAPITheme.au3> #include <GDIPlus.au3> #Region GUICtrlComboSetColors UDF Global $g__aWM_CTLCOLORLISTBOX[1][16] = [[0, 0, 0]] ; init. the Global array ; #FUNCTION# ==================================================================================================================== ; Name...........: GUICtrlComboSetColors ; Description ...: Change the colors and position/size of a ComboBox ; Syntax.........: GUICtrlComboSetColors ( $idCombo [, $iBgColor = Default] [, $iFgColor = Default] [, $iExtendLeft = Default] ) ; Parameters ....: $idCombo - GUICtrlCreateCombo() ControlID / [ ArrayIndex ] ; $iBgColor - Background RGB color ; - or "-1" to use prior color declared ; - or to remove a control by ControlID, "-2" ; - or to remove a control by ArrayIndex, "-3" ; $iFgColor - Foreground RGB color ; - or "-1" to use prior color declared ; - or "-2" to use sytem color and leave theme default ; $iExtendLeft - pixels to extend the dropdown list ; - or "-1" to use prior width declared ; - or "1" auto size, extending left ( see Remarks/AutoSize ) ; - or "2" auto size, extending right ( see Remarks/AutoSize ) ; Return values .: Success - index position in the array ; Failure - 0 ; @error - 1 : Control handle = 0 ; - 2 : GetComboBoxInfo failed ; - 3 : Control for removal not found ; @extended - 2 : Success on Control removal ; Author ........: argumentum ; Modified.......: v0.0.0.5 ; Remarks .......: this UDF is in its a work in progress, will expand if needed. ; AutoSize...: use the pertinent parameters from GUICtrlComboSetColors_SetAutoSize() ; minus the CtrlID as semicolon separated to initialize. Ex: "2;Arial;8.5;0" ; Related .......: ; Link ..........: https://www.autoitscript.com/forum/topic/191035-combobox-set-dropdownlist-bgcolor/ ; Example .......: Yes, at the end of the file ; =============================================================================================================================== Func GUICtrlComboSetColors($idCombo = 0, $iBgColor = Default, $iFgColor = Default, $iExtendLeft = Default) If Not $idCombo Then Return SetError(1, 0, 0) Local $n, $tInfo, $i = 0 If $iBgColor = -2 Or $iBgColor = -3 Then Local $m For $n = 1 To $g__aWM_CTLCOLORLISTBOX[0][0] If ($g__aWM_CTLCOLORLISTBOX[$n][0] = $idCombo And $iBgColor = -2) Or ($g__aWM_CTLCOLORLISTBOX[$n][9] = $idCombo And $iBgColor = -3) Then _ArrayDelete($g__aWM_CTLCOLORLISTBOX, $n) $g__aWM_CTLCOLORLISTBOX[0][0] -= 1 Return SetError(0, 2, $n) EndIf Next Return SetError(3, 0, 0) EndIf For $n = 1 To $g__aWM_CTLCOLORLISTBOX[0][0] If $g__aWM_CTLCOLORLISTBOX[$n][0] = $idCombo Then $i = $n ExitLoop EndIf Next If Not $i Then $g__aWM_CTLCOLORLISTBOX[0][0] += 1 $i = $g__aWM_CTLCOLORLISTBOX[0][0] ; If $i >= UBound($g__aWM_CTLCOLORLISTBOX) Then ReDim $g__aWM_CTLCOLORLISTBOX[$i + 1][16] ; add extra "slots" EndIf EndIf Local $sStr = GUICtrlRead($idCombo) Local $iSetWindowTheme = 1 If $iBgColor = Default And $iFgColor = Default Then $iSetWindowTheme = 0 If $iBgColor = Default Then $iBgColor = _WinAPI_GetSysColor($COLOR_WINDOW) If $iFgColor = Default Then $iFgColor = _WinAPI_GetSysColor($COLOR_WINDOWTEXT) If $iBgColor = -1 Then $iBgColor = $g__aWM_CTLCOLORLISTBOX[$i][10] If $iFgColor = -1 Then $iFgColor = $g__aWM_CTLCOLORLISTBOX[$i][11] $g__aWM_CTLCOLORLISTBOX[$i][11] = $iFgColor $g__aWM_CTLCOLORLISTBOX[$i][10] = $iBgColor If $iExtendLeft = Default Then $iExtendLeft = 0 $g__aWM_CTLCOLORLISTBOX[$i][12] = 0 EndIf If $iExtendLeft = -1 Then $iExtendLeft = $g__aWM_CTLCOLORLISTBOX[$i][8] ElseIf Int($iExtendLeft) = 1 Then $g__aWM_CTLCOLORLISTBOX[$i][12] = 1 ElseIf Int($iExtendLeft) = 2 Then $g__aWM_CTLCOLORLISTBOX[$i][12] = 2 Else $g__aWM_CTLCOLORLISTBOX[$i][12] = 0 EndIf $g__aWM_CTLCOLORLISTBOX[$i][8] = Int($iExtendLeft) $g__aWM_CTLCOLORLISTBOX[$i][0] = $idCombo $g__aWM_CTLCOLORLISTBOX[$i][1] = GUICtrlGetHandle($idCombo) $g__aWM_CTLCOLORLISTBOX[$i][13] = "Arial" ; default $sFont $g__aWM_CTLCOLORLISTBOX[$i][14] = 8.5 ; default $fSize $g__aWM_CTLCOLORLISTBOX[$i][15] = 0 ; default $iStyle If $g__aWM_CTLCOLORLISTBOX[$i][12] Then $f = StringSplit($iExtendLeft, ";") If UBound($f) > 1 Then $g__aWM_CTLCOLORLISTBOX[$i][8] = Int($f[1]) If UBound($f) > 2 Then $g__aWM_CTLCOLORLISTBOX[$i][13] = $f[2] If UBound($f) > 3 Then $g__aWM_CTLCOLORLISTBOX[$i][14] = Int($f[3]) If UBound($f) > 4 Then $g__aWM_CTLCOLORLISTBOX[$i][15] = Int($f[4]) $t = TimerInit() GUICtrlComboSetColors_SetAutoSize(Int("-" & $i), $g__aWM_CTLCOLORLISTBOX[$i][12], $g__aWM_CTLCOLORLISTBOX[$i][13], $g__aWM_CTLCOLORLISTBOX[$i][14], $g__aWM_CTLCOLORLISTBOX[$i][15]) ConsoleWrite(TimerDiff($t) & @CRLF) EndIf If _GUICtrlComboBox_GetComboBoxInfo($idCombo, $tInfo) Then If $iSetWindowTheme Then If $g__aWM_CTLCOLORLISTBOX[$i][11] <> -2 Then _WinAPI_SetWindowTheme($g__aWM_CTLCOLORLISTBOX[$i][1], "", "") If $g__aWM_CTLCOLORLISTBOX[$i][11] <> -2 Then GUICtrlSetColor($g__aWM_CTLCOLORLISTBOX[$i][0], $iFgColor) GUICtrlSetBkColor($g__aWM_CTLCOLORLISTBOX[$i][0], $iBgColor) Else GUICtrlSetBkColor($g__aWM_CTLCOLORLISTBOX[$i][0], _WinAPI_GetSysColor($COLOR_HOTLIGHT)) _WinAPI_SetWindowTheme($g__aWM_CTLCOLORLISTBOX[$i][1], 0, 0) EndIf $g__aWM_CTLCOLORLISTBOX[$i][2] = DllStructGetData($tInfo, "hCombo") $g__aWM_CTLCOLORLISTBOX[$i][3] = DllStructGetData($tInfo, "hEdit") $g__aWM_CTLCOLORLISTBOX[$i][4] = DllStructGetData($tInfo, "hList") ; this is what is colored Else $g__aWM_CTLCOLORLISTBOX[0][0] -= 1 Return SetError(2, 0, 0) EndIf If Int($g__aWM_CTLCOLORLISTBOX[$i][5]) Then _WinAPI_DeleteObject($g__aWM_CTLCOLORLISTBOX[$i][5]) $g__aWM_CTLCOLORLISTBOX[$i][5] = 0 ; holder for "_WinAPI_CreateSolidBrush()" return value $g__aWM_CTLCOLORLISTBOX[$i][6] = BitOR(BitAND($iBgColor, 0x00FF00), BitShift(BitAND($iBgColor, 0x0000FF), -16), BitShift(BitAND($iBgColor, 0xFF0000), 16)) If $g__aWM_CTLCOLORLISTBOX[$i][11] = -2 Then $iFgColor = _WinAPI_GetSysColor($COLOR_WINDOWTEXT) $g__aWM_CTLCOLORLISTBOX[$i][7] = BitOR(BitAND($iFgColor, 0x00FF00), BitShift(BitAND($iFgColor, 0x0000FF), -16), BitShift(BitAND($iFgColor, 0xFF0000), 16)) If Not $g__aWM_CTLCOLORLISTBOX[0][1] Then If $g__aWM_CTLCOLORLISTBOX[$i][4] Then $g__aWM_CTLCOLORLISTBOX[0][1] = GUIRegisterMsg($WM_CTLCOLORLISTBOX, "UDF_WM_CTLCOLORLISTBOX") If $g__aWM_CTLCOLORLISTBOX[0][1] Then OnAutoItExitRegister("OnAutoItExit_UDF_WM_CTLCOLORLISTBOX") EndIf EndIf $g__aWM_CTLCOLORLISTBOX[0][2] += 1 $g__aWM_CTLCOLORLISTBOX[$i][9] = $g__aWM_CTLCOLORLISTBOX[0][2] ; internal ID $g__aWM_CTLCOLORLISTBOX[0][3] = TimerInit() ; to use in UDF_WM_CTLCOLORLISTBOX() $g__aWM_CTLCOLORLISTBOX[0][4] = 0 ; to use in UDF_WM_CTLCOLORLISTBOX() If $sStr Then GUICtrlSetData($idCombo, $sStr) Return SetError(0, 0, $g__aWM_CTLCOLORLISTBOX[0][2]) EndFunc ;==>GUICtrlComboSetColors Func UDF_WM_CTLCOLORLISTBOX($hWnd, $Msg, $wParam, $lParam) ConsoleWrite('+ Func UDF_WM_CTLCOLORLISTBOX(' & $hWnd & ', ' & $Msg & ', ' & $wParam & ', ' & $lParam & ')' & @CRLF) For $i = 1 To $g__aWM_CTLCOLORLISTBOX[0][0] If $g__aWM_CTLCOLORLISTBOX[$i][4] = $lParam Then If TimerDiff($g__aWM_CTLCOLORLISTBOX[0][3]) > 500 Or $g__aWM_CTLCOLORLISTBOX[0][4] <> $lParam Then If $g__aWM_CTLCOLORLISTBOX[$i][12] Then GUICtrlComboSetColors_SetAutoSize("-" & $i) EndIf $g__aWM_CTLCOLORLISTBOX[0][3] = TimerInit() $g__aWM_CTLCOLORLISTBOX[0][4] = $lParam If $g__aWM_CTLCOLORLISTBOX[$i][8] > 0 Then Local $aWPos = WinGetPos($g__aWM_CTLCOLORLISTBOX[$i][2]) WinMove($lParam, "", $aWPos[0] - $g__aWM_CTLCOLORLISTBOX[$i][8], $aWPos[1] + $aWPos[3], $aWPos[2] + $g__aWM_CTLCOLORLISTBOX[$i][8]) ElseIf $g__aWM_CTLCOLORLISTBOX[$i][8] < 0 Then Local $aWPos = WinGetPos($g__aWM_CTLCOLORLISTBOX[$i][2]) WinMove($lParam, "", $aWPos[0], $aWPos[1] + $aWPos[3], $aWPos[2] - $g__aWM_CTLCOLORLISTBOX[$i][8]) EndIf If $g__aWM_CTLCOLORLISTBOX[$i][7] >= 0 Then _WinAPI_SetTextColor($wParam, $g__aWM_CTLCOLORLISTBOX[$i][7]) EndIf If $g__aWM_CTLCOLORLISTBOX[$i][6] >= 0 Then _WinAPI_SetBkColor($wParam, $g__aWM_CTLCOLORLISTBOX[$i][6]) If Not $g__aWM_CTLCOLORLISTBOX[$i][5] Then $g__aWM_CTLCOLORLISTBOX[$i][5] = _WinAPI_CreateSolidBrush($g__aWM_CTLCOLORLISTBOX[$i][6]) Return $g__aWM_CTLCOLORLISTBOX[$i][5] EndIf Return 0 EndIf Next EndFunc ;==>UDF_WM_CTLCOLORLISTBOX ; #FUNCTION# ==================================================================================================================== ; Name...........: GUICtrlComboSetColors_SetAutoSize ; Description ...: Set autosize for a ComboBox initialized in GUICtrlComboSetColors() ; Syntax.........: GUICtrlComboSetColors ( $idCombo [, $iExtendLeft = Default] [, $sFont = Default] [, $fSize = Default] [, $iStyle = Default] ) ; Parameters ....: $idCombo - GUICtrlCreateCombo() ControlID / [ ArrayIndex ] ; $iExtendLeft - 1 = Left, 2 = Right, 0 = disable auto-sizing ; $sFont - Font name ; $fSize - Font size ; $iStyle - Font style ; Return values .: Success - widthest string in pixels ; Failure - -1 ; @error - look at the comments in the function ; Author ........: argumentum ; Modified.......: v0.0.0.5 ; Remarks .......: this UDF is in its a work in progress, will expand if needed. ; Related .......: GUICtrlComboSetColors() ; Link ..........: https://www.autoitscript.com/forum/topic/191035-combobox-set-dropdownlist-bgcolor/ ; Example .......: Yes, at the end of the file ; =============================================================================================================================== Func GUICtrlComboSetColors_SetAutoSize($idCombo, $iExtendLeft = Default, $sFont = Default, $fSize = Default, $iStyle = Default) ConsoleWrite('+ Func GUICtrlComboSetColors_AutoSizeSet("' & $idCombo & '", "' & $iExtendLeft & '", "' & $sFont & '", "' & $fSize & '", "' & $iStyle & '")' & @CRLF) $idCombo = Int($idCombo) ; just in case the value is a string Local $n, $iArrayIndex = 0, $iCtrl = 0 If $idCombo > 0 Then For $n = 1 To $g__aWM_CTLCOLORLISTBOX[0][0] If $g__aWM_CTLCOLORLISTBOX[$n][0] = $idCombo Then ; the expected value, is the ControlID $iArrayIndex = $n ExitLoop EndIf Next Return SetError(4, 0, -1) ; $iArrayIndex not found ElseIf $idCombo < 0 Then ; the expected value, is a negative of array's index .. $iArrayIndex = Int(StringTrimLeft(StringStripWS($idCombo, 8), 1)) ; .. so now is a positive value .. If $iArrayIndex < 1 Then Return SetError(3, 0, -1) ; .. else, error .. If $iArrayIndex > $g__aWM_CTLCOLORLISTBOX[0][0] Then Return SetError(2, 0, -1) ; .. as long as is not greater than expected Else Return SetError(1, 0, -1) ; could not find a usable value EndIf Switch $iExtendLeft Case 0, 1, 2 $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][12] = $iExtendLeft EndSwitch Local $aCtrlPos = WinGetPos($g__aWM_CTLCOLORLISTBOX[$iArrayIndex][1]) If UBound($aCtrlPos) <> 4 Then Return SetError(5, 0, -1) ; could not get a usable value Local $sString = StringReplace(_GUICtrlComboBox_GetList($g__aWM_CTLCOLORLISTBOX[$iArrayIndex][0]), "|", @CRLF) Local $aStrWidth = _GDIPlus_MeasureString($sString, $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][13], $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][14], $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][15]) If UBound($aStrWidth) <> 2 Then Return SetError(6, 0, -1) ; could not get a usable value If $aStrWidth[0] < $aCtrlPos[2] Then $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][8] = 0 Else $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][8] = $aStrWidth[0] - $aCtrlPos[2] If $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][12] = 2 Then $g__aWM_CTLCOLORLISTBOX[$iArrayIndex][8] = Int("-" & $aStrWidth[0] - $aCtrlPos[2]) EndIf Return $aStrWidth[0] EndFunc ;==>GUICtrlComboSetColors_SetAutoSize Func _GDIPlus_MeasureString($sString, $sFont = "Arial", $fSize = 12, $iStyle = 0, $bRound = True) ConsoleWrite('Func _GDIPlus_MeasureString("' & $sString & '", "' & $sFont & '", "' & $fSize & '", "' & $iStyle & '", "' & $bRound & '")' & @CRLF) ; original code @ https://www.autoitscript.com/forum/topic/150736-gdi-wrapping-text/?do=findComment&comment=1077210 If Not $__g_iGDIPRef Then _GDIPlus_Startup() ; added by argumentum for this UDF's implementation ( AutoIt v3.3.14 ) due to the way the function is written ;~ Func _GDIPlus_Startup($sGDIPDLL = Default, $bRetDllHandle = False) ;~ $__g_iGDIPRef += 1 <-- I believe this aspect should be coded differently in "GDIPlus.au3" ;~ If $__g_iGDIPRef > 1 Then Return True Local $aSize[2] Local Const $hFamily = _GDIPlus_FontFamilyCreate($sFont) If Not $hFamily Then Return SetError(1, 0, $aSize) Local Const $hFormat = _GDIPlus_StringFormatCreate() Local Const $hFont = _GDIPlus_FontCreate($hFamily, $fSize, $iStyle) Local Const $tLayout = _GDIPlus_RectFCreate(0, 0, 0, 0) Local Const $hGraphic = _GDIPlus_GraphicsCreateFromHWND(0) Local $aInfo = _GDIPlus_GraphicsMeasureString($hGraphic, $sString, $hFont, $tLayout, $hFormat) $aSize[0] = $bRound ? Round($aInfo[0].Width, 0) : $aInfo[0].Width $aSize[1] = $bRound ? Round($aInfo[0].Height, 0) : $aInfo[0].Height _GDIPlus_FontDispose($hFont) _GDIPlus_FontFamilyDispose($hFamily) _GDIPlus_StringFormatDispose($hFormat) _GDIPlus_GraphicsDispose($hGraphic) Return $aSize EndFunc ;==>_GDIPlus_MeasureString Func OnAutoItExit_UDF_WM_CTLCOLORLISTBOX() For $i = 1 To $g__aWM_CTLCOLORLISTBOX[0][0] If Int($g__aWM_CTLCOLORLISTBOX[$i][5]) Then _WinAPI_DeleteObject($g__aWM_CTLCOLORLISTBOX[$i][5]) Next If $__g_iGDIPRef Then _GDIPlus_Shutdown() EndFunc ;==>OnAutoItExit_UDF_WM_CTLCOLORLISTBOX #EndRegion GUICtrlComboSetColors UDF Example() Func Example() ; Create GUI GUICreate("ComboBox Set DROPDOWNLIST BgColor", 640, 300) Local $a_idCombo[7] = [6] $a_idCombo[1] = GUICtrlCreateCombo("", 2, 2, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlComboSetColors($a_idCombo[1], 0xEEEEEE, -2, Default) Example_FillTheCombo($a_idCombo[1]) GUICtrlCreateLabel("<<< change BG color, default theme && size ", 400, 4, 396, 296) $a_idCombo[2] = GUICtrlCreateCombo("", 2, 32, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlComboSetColors($a_idCombo[2], 0x0000FF, 0xFFFF00, 0) Example_FillTheCombo($a_idCombo[2]) GUICtrlCreateLabel("<<< change colors", 400, 34, 396, 296) $a_idCombo[3] = GUICtrlCreateCombo("", 2, 62, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlComboSetColors($a_idCombo[3], 0xdddddd, Default, 100) Example_FillTheCombo($a_idCombo[3]) GUICtrlCreateLabel("<<< change BG color, resize 100px. left", 400, 64, 396, 296) $a_idCombo[4] = GUICtrlCreateCombo("", 2, 92, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlComboSetColors($a_idCombo[4], Default, 0x0000FF, -100) Example_FillTheCombo($a_idCombo[4]) GUICtrlCreateLabel("<<< change FG color, resize 100px. right", 400, 94, 396, 296) $a_idCombo[5] = GUICtrlCreateCombo("", 2, 122, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlComboSetColors($a_idCombo[5], 0x00FFFF, 0x0000FF, 1) Example_FillTheCombo($a_idCombo[5]) GUICtrlCreateLabel("<<< change colors, resize auto left", 400, 124, 396, 296) $a_idCombo[6] = GUICtrlCreateCombo("", 2, 152, 390, 296, BitOR($CBS_DROPDOWNLIST, $WS_HSCROLL, $WS_VSCROLL)) GUICtrlSetFont($a_idCombo[6], 10, 400, 0, "Courier New") GUICtrlComboSetColors($a_idCombo[6], Default, Default, "2;Courier New;10") Example_FillTheCombo($a_idCombo[6]) GUICtrlCreateLabel("<<< default colors, resize auto right", 400, 154, 396, 296) Local $bttnArrayShow = GUICtrlCreateButton("Show array", 2, 296 - 27, 75, 25) Local $bttnStrMore = GUICtrlCreateButton("Longer str.", 102, 296 - 27, 75, 25) Local $idLorem = GUICtrlCreateLabel("", 195, 296 - 27, 50, 25) Local $bttnStrLess = GUICtrlCreateButton("Shorter str.", 252, 296 - 27, 75, 25) GUISetState(@SW_SHOW) WinActivate("ComboBox Set DROPDOWNLIST BgColor") ;~ Sleep(3500) ; you can reassign colors, size, or restore default ;~ GUICtrlComboSetColors($idCombo5, Default, Default, 300) ; this resets the Control back to default and changes $iExtendLeft ;~ GUICtrlComboSetColors($idCombo5, 0x0000FF, 0x00FFFF, -1) ; this changes the colors and keeps $iExtendLeft as it was ;~ GUICtrlComboSetColors($idCombo5, -1, -1, 300) ; using "-1" will keep the existing colors ;~ ; so in this case, only the $iExtendLeft is declared ;~ Example_FillTheCombo($idCombo5) ;~ Sleep(500) ; after removal, it will not repaint "hList", but then again, you're deleteing the control ;~ GUICtrlComboSetColors($idColors, -3) ;~ GUICtrlDelete($idCombo2) Local $iLorem = 5, $sLorem = "" Example_LoremStr($iLorem, $sLorem, $a_idCombo, $idLorem) ; Loop until the user exits. While 1 Switch GUIGetMsg() Case $GUI_EVENT_CLOSE GUIDelete() Return Case $bttnArrayShow _ArrayDisplay($g__aWM_CTLCOLORLISTBOX, "$g__aWM_CTLCOLORLISTBOX") Case $bttnStrMore $iLorem += 5 Example_LoremStr($iLorem, $sLorem, $a_idCombo, $idLorem) Case $bttnStrLess $iLorem -= 5 Example_LoremStr($iLorem, $sLorem, $a_idCombo, $idLorem) EndSwitch WEnd EndFunc ;==>Example Func Example_FillTheCombo(ByRef $idComboCtrl) GUICtrlSetData($idComboCtrl, "") _GUICtrlComboBox_AddString($idComboCtrl, "something") _GUICtrlComboBox_AddString($idComboCtrl, "something else") _GUICtrlComboBox_AddString($idComboCtrl, "blah, blah, blah, blah") _GUICtrlComboBox_AddString($idComboCtrl, "Lorem will change") Local $a = _GUICtrlComboBox_GetListArray($idComboCtrl) GUICtrlSetData($idComboCtrl, $a[1]) EndFunc ;==>Example_FillTheCombo Func Example_LoremStr(ByRef $iLorem, ByRef $sLorem, ByRef $a_idCombo, ByRef $idLorem) Local Static $s = "Lorem ipsum dolor sit amet, consectetuer adipiscing elit, sed diam nonummy nibh euismod tincidunt ut laoreet dolore magna aliquam erat volutpat." $s &= " Ut wisi enim ad minim veniam, quis nostrud exerci tation ullamcorper suscipit lobortis nisl ut aliquip ex ea commodo consequat." Local Static $i = 5, $a = StringSplit($s, " ") If $iLorem < 1 Then $iLorem = 1 If $iLorem > $a[0] Then $iLorem = $a[0] Local $x, $iLastEntry $sLorem = "" GUICtrlSetData($idLorem, $iLorem & ' words') For $x = 1 To $iLorem $sLorem &= $a[$x] & " " Next For $x = 1 To $a_idCombo[0] $iLastEntry = _GUICtrlComboBox_GetCount($a_idCombo[$x]) - 1 _GUICtrlComboBox_DeleteString($a_idCombo[$x], $iLastEntry) _GUICtrlComboBox_AddString($a_idCombo[$x], $sLorem) Next EndFunc ;==>Example_LoremStr new in v0.0.0.5: auto size the dropdown. You may also wanna take a look at ComboBox Color Background/Text subclass approach.
    1 point
  2. 1 point
  3. mLipok

    System restore UDF

    I just modified your UDF, a little No breaking Changes (I hope). SystemRestore.au3
    1 point
  4. Bert

    Google Speech API

    here you are: http://lmgtfy.com/?q=speech+API+from+Google+code+examples
    1 point
  5. Define and declare ALL your global variables in the beginning of your master script.
    1 point
  6. HAA, I missed it the first time, but StringRegExp with the $STR_REGEXPARRAYMATCH returns an array of matches - so just using the array name will not work. You need to define which element of that array you need - probably something like this: FileWriteToLine("Q:\test\assi.config", 102, ' <setting name="PhoneInterfaceServiceProvider">' & $result[0] & ' (Aastra2380ip)</setting>', True) M23
    1 point
  7. I will check it out later. Oh damn the automated handling of the control buttons keeps causing problems.. The first v5 fullscreen was working fine. It must have sneaked in after the bugfixes, i haven't noticed as I am handling it manually for my programs. I will fix it with the next update, for now you can do this: Global $ControlBtnsAutoMode = False;Set automode off ;Handle the click manually. Case $GUI_FULLSCREEN_BUTTON, $GUI_FSRestore_BUTTON _Metro_FullscreenToggle($Form1) ConsoleWrite("Fullscreen toggled" & @CRLF)
    1 point
  8. I finally got around to making a new tutorial on ini files https://www.youtube.com/watch?v=0_cPWYQ6iIs&index=20&list=PL4Jcq5zn02jKpjX0nqI1_fS7mEEb5tw6z I had several requests to do the video on the additional way to pull multiple unknown things from ini files, so I have now done the video.
    1 point
  9. Optimizing C# and VB code Index based sorting The examples in post 4 is an introduction to threading. The examples here shows how VB code can be optimized using multithreading. The examples are a continuation of the code in post 2 regarding index based sorting. Multithreaded sorting The idea is that if 4 threads are used to sort 100,000 rows, the first thread can sort the first 25,000 rows, the next thread can sort the next 25,000 rows, etc. It results in 4 indexes that finally can be combined into one big index. This is the code in Sort2DArrayOptEx.vb: Imports System Imports System.Threading Imports System.Collections Class SortArrayClass Dim aObjects, aCompare As Object(,) Dim iPartRows, iRestRows, iCmps As Integer Dim iThreads = 4, aIndex(iThreads)(), aRows(iThreads) As Integer Public Function Sort2DArray( aObjPar As Object(,), aCompPar As Object(,) ) As Integer() aObjects = aObjPar : aCompare = aCompPar Dim iRows As Integer = aObjects.GetUpperBound(1) + 1 iRestRows = iRows Mod iThreads : iPartRows = ( iRows - iRestRows ) / iThreads iCmps = aCompare.GetUpperBound(1) + 1 'Multithreaded index based sorting Dim th0 As New Thread( AddressOf Sort2DArraySub ) : th0.Start(0) Dim th1 As New Thread( AddressOf Sort2DArraySub ) : th1.Start(1) Dim th2 As New Thread( AddressOf Sort2DArraySub ) : th2.Start(2) Dim th3 As New Thread( AddressOf Sort2DArraySub ) : th3.Start(3) 'Main thread sleeps until sorting is done While aRows(0) = 0 Or aRows(1) = 0 Or aRows(2) = 0 Or aRows(3) = 0 Thread.Sleep(20) End While 'Combine four indexes into one Dim aIndexAll(iRows-1) As Integer Dim aIndexIdx(iThreads), iLeft, iRight, r, j As Integer For i As Integer = 0 To iRows - 1 iLeft = If( aIndexIdx(0) < aRows(0), 0, If( aIndexIdx(1) < aRows(1), 1, If( aIndexIdx(2) < aRows(2), 2, 3 ) ) ) iRight = iLeft While iRight < iThreads iRight += 1 Select iRight Case 1 iRight = If( aIndexIdx(1) < aRows(1), 1, If( aIndexIdx(2) < aRows(2), 2, If( aIndexIdx(3) < aRows(3), 3, iThreads ) ) ) Case 2 iRight = If( aIndexIdx(2) < aRows(2), 2, If( aIndexIdx(3) < aRows(3), 3, iThreads ) ) Case 3 iRight = If( aIndexIdx(3) < aRows(3), 3, iThreads ) End Select If iRight = iThreads Then Exit While r = 0 'Compare result (-1,0,1) j = 0 'Index in $aCompare array While r = 0 And j < iCmps r = If( aCompare(1,j), String.Compare( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))), aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))) ), If( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))) < aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))), -1, If( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))) > aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))), 1, 0 ) ) ) * aCompare(2,j) j += 1 End While Select r Case 1 iLeft = iRight Case 0 If aIndex(iLeft)(aIndexIdx(iLeft)) > aIndex(iRight)(aIndexIdx(iRight)) Then iLeft = iRight End Select End While aIndexAll(i) = aIndex(iLeft)(aIndexIdx(iLeft)) aIndexIdx(iLeft) += 1 Next Return aIndexAll End Function 'Multithreaded index based sorting Private Sub Sort2DArraySub( oThread As Object ) Dim iThread As Integer = CInt( oThread ) Dim iFirstRow As Integer = If( iThread < iRestRows, iPartRows * iThread + iThread, iPartRows * iThread + iRestRows ) Dim iLastRow As Integer = iFirstRow + If( iThread < iRestRows, iPartRows, iPartRows - 1 ) Dim iRows As Integer = iLastRow - iFirstRow + 1 Dim MyList As New Generic.List( Of Integer ) MyList.Capacity = iRows MyList.Add(iFirstRow) 'Sorting by multiple columns Dim lo, hi, mi, r, j As Integer For i As Integer = iFirstRow + 1 To iLastRow lo = iFirstRow hi = i - 1 Do r = 0 'Compare result (-1,0,1) j = 0 'Index in $aCompare array mi = ( lo + hi ) / 2 While r = 0 And j < iCmps r = If( aCompare(1,j), String.Compare( aObjects(aCompare(0,j),i), aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)) ), If( aObjects(aCompare(0,j),i) < aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)), -1, If( aObjects(aCompare(0,j),i) > aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)), 1, 0 ) ) ) * aCompare(2,j) j += 1 End While Select r Case -1 hi = mi - 1 Case 1 lo = mi + 1 Case 0 Exit Do End Select Loop Until lo > hi MyList.Insert( If(lo=mi+1,mi+1,mi)-iFirstRow, i ) Next aIndex(iThread) = MyList.ToArray() aRows(iThread) = iRows End Sub End Class In Runtimes.au3 the execution times for sorting arrays with different number of rows are measuered for pure AutoIt code, for optimized VB code and for multithreaded VB code. This is the results on my PC: Code executed as 32 bit code Code executed as 64 bit code ============================ ============================ $iRows = 100, 6 columns $iRows = 100, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 4.3340 Pure AutoIt code: 3.7655 Optimized VB code: 1.2553 Optimized VB code: 1.4281 Multithreaded VB code: 19.0067 Multithreaded VB code: 33.5323 $iRows = 500, 6 columns $iRows = 500, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 27.5916 Pure AutoIt code: 25.5753 Optimized VB code: 2.6330 Optimized VB code: 2.8050 Multithreaded VB code: 31.2496 Multithreaded VB code: 36.4108 $iRows = 1000, 6 columns $iRows = 1000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 106.4464 Pure AutoIt code: 90.3135 Optimized VB code: 35.7356 Optimized VB code: 27.4234 Multithreaded VB code: 64.3406 Multithreaded VB code: 57.4474 $iRows = 2000, 6 columns $iRows = 2000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 142.1940 Pure AutoIt code: 134.2208 Optimized VB code: 11.6813 Optimized VB code: 10.9643 Multithreaded VB code: 30.2622 Multithreaded VB code: 33.4628 $iRows = 5000, 6 columns $iRows = 5000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 401.1128 Pure AutoIt code: 365.6661 Optimized VB code: 38.3885 Optimized VB code: 31.2551 Multithreaded VB code: 79.6997 Multithreaded VB code: 89.7060 $iRows = 10000, 6 columns $iRows = 10000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 826.2889 Pure AutoIt code: 691.7514 Optimized VB code: 70.5041 Optimized VB code: 65.1964 Multithreaded VB code: 89.2076 Multithreaded VB code: 60.4300 $iRows = 20000, 6 columns $iRows = 20000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 1816.3940 Pure AutoIt code: 1517.2513 Optimized VB code: 183.4977 Optimized VB code: 151.7480 Multithreaded VB code: 150.1049 Multithreaded VB code: 135.3445 $iRows = 50000, 6 columns $iRows = 50000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 5386.5884 Pure AutoIt code: 4239.2327 Optimized VB code: 602.9203 Optimized VB code: 471.3676 Multithreaded VB code: 373.1342 Multithreaded VB code: 371.5393 $iRows = 100000, 6 columns $iRows = 100000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 12798.0606 Pure AutoIt code: 9489.1929 Optimized VB code: 1590.7280 Optimized VB code: 1273.4829 Multithreaded VB code: 800.1118 Multithreaded VB code: 684.8815 $iRows = 250000, 6 columns $iRows = 250000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 44781.3723 Pure AutoIt code: 27407.9705 Optimized VB code: 7217.6899 Optimized VB code: 5222.3106 Multithreaded VB code: 2330.1729 Multithreaded VB code: 1914.2506 $iRows = 500000, 6 columns $iRows = 500000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 128479.2462 Pure AutoIt code: 69438.5295 Optimized VB code: 25016.8543 Optimized VB code: 16692.2426 Multithreaded VB code: 5949.0161 Multithreaded VB code: 4565.7862 $iRows = 750000, 6 columns $iRows = 750000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 250176.5552 Pure AutoIt code: 107785.2414 Optimized VB code: 54193.4737 Optimized VB code: 35217.0613 Multithreaded VB code: 10827.3308 Multithreaded VB code: 8186.0242 $iRows = 1000000, 6 columns $iRows = 1000000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 408457.7649 Pure AutoIt code: 162282.8254 Optimized VB code: 96291.4960 Optimized VB code: 62412.9602 Multithreaded VB code: 17399.9110 Multithreaded VB code: 12690.8546 $iRows = 2000000, 6 columns $iRows = 2000000, 6 columns Sort array by columns 0, 1, 2 Sort array by columns 0, 1, 2 ----------------------------------- ----------------------------------- Pure AutoIt code: 1445235.5246 Pure AutoIt code: 498648.9821 Optimized VB code: 409872.8400 Optimized VB code: 282116.6518 Multithreaded VB code: 63527.4207 Multithreaded VB code: 51087.1290 Code is added to "Examples\5) Optimizing C# and VB code\1) Index based sorting\" in zip-file in bottom of first post. Implementing a progress bar Another advantage of performing the sorting in 4 worker threads is that the main thread can be released by the VB code and thereby making the AutoIt code responsive while the sorting is going on. This makes it possible to implement a progress bar in the AutoIt code. The code in Sort2DArrayOptEx2.vb is changed a little bit: Imports System Imports System.Threading Imports System.Collections Class SortArrayClass Dim aObjects, aCompare As Object(,) Dim iRows, iPartRows, iRestRows, iCmps As Integer Dim iThreads = 4, aIndex(iThreads)(), aRows(iThreads) As Integer Public Sub Sort2DArray( aObjPar As Object(,), aCompPar As Object(,) ) aObjects = aObjPar : aCompare = aCompPar : iRows = aObjects.GetUpperBound(1) + 1 iRestRows = iRows Mod iThreads : iPartRows = ( iRows - iRestRows ) / iThreads iCmps = aCompare.GetUpperBound(1) + 1 'Multithreaded index based sorting Dim th0 As New Thread( AddressOf Sort2DArraySub ) : th0.Start(0) Dim th1 As New Thread( AddressOf Sort2DArraySub ) : th1.Start(1) Dim th2 As New Thread( AddressOf Sort2DArraySub ) : th2.Start(2) Dim th3 As New Thread( AddressOf Sort2DArraySub ) : th3.Start(3) End Sub 'Multithreaded index based sorting Private Sub Sort2DArraySub( oThread As Object ) Dim iThread As Integer = CInt( oThread ) Dim iFirstRow As Integer = If( iThread < iRestRows, iPartRows * iThread + iThread, iPartRows * iThread + iRestRows ) Dim iLastRow As Integer = iFirstRow + If( iThread < iRestRows, iPartRows, iPartRows - 1 ) Dim iSubRows As Integer = iLastRow - iFirstRow + 1 Dim MyList As New Generic.List( Of Integer ) MyList.Capacity = iSubRows MyList.Add(iFirstRow) 'Sorting by multiple columns Dim lo, hi, mi, r, j As Integer For i As Integer = iFirstRow + 1 To iLastRow lo = iFirstRow hi = i - 1 Do r = 0 'Compare result (-1,0,1) j = 0 'Index in $aCompare array mi = ( lo + hi ) / 2 While r = 0 And j < iCmps r = If( aCompare(1,j), String.Compare( aObjects(aCompare(0,j),i), aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)) ), If( aObjects(aCompare(0,j),i) < aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)), -1, If( aObjects(aCompare(0,j),i) > aObjects(aCompare(0,j),MyList.Item(mi-iFirstRow)), 1, 0 ) ) ) * aCompare(2,j) j += 1 End While Select r Case -1 hi = mi - 1 Case 1 lo = mi + 1 Case 0 Exit Do End Select Loop Until lo > hi MyList.Insert( If(lo=mi+1,mi+1,mi)-iFirstRow, i ) If ( i - iFirstRow ) Mod 1000 = 0 Then aRows(iThread) = i - iFirstRow Next aIndex(iThread) = MyList.ToArray() aRows(iThread) = iSubRows End Sub Public Function Sort2DArrayGetCount() As Integer Return aRows(0) + aRows(1) + aRows(2) + aRows(3) End Function Public Function Sort2DArrayGetIndex() As Integer() 'Combine four indexes into one Dim aIndexAll(iRows-1) As Integer Dim aIndexIdx(iThreads), iLeft, iRight, r, j As Integer For i As Integer = 0 To iRows - 1 iLeft = If( aIndexIdx(0) < aRows(0), 0, If( aIndexIdx(1) < aRows(1), 1, If( aIndexIdx(2) < aRows(2), 2, 3 ) ) ) iRight = iLeft While iRight < iThreads iRight += 1 Select iRight Case 1 iRight = If( aIndexIdx(1) < aRows(1), 1, If( aIndexIdx(2) < aRows(2), 2, If( aIndexIdx(3) < aRows(3), 3, iThreads ) ) ) Case 2 iRight = If( aIndexIdx(2) < aRows(2), 2, If( aIndexIdx(3) < aRows(3), 3, iThreads ) ) Case 3 iRight = If( aIndexIdx(3) < aRows(3), 3, iThreads ) End Select If iRight = iThreads Then Exit While r = 0 'Compare result (-1,0,1) j = 0 'Index in $aCompare array While r = 0 And j < iCmps r = If( aCompare(1,j), String.Compare( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))), aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))) ), If( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))) < aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))), -1, If( aObjects(aCompare(0,j),aIndex(iLeft)(aIndexIdx(iLeft))) > aObjects(aCompare(0,j),aIndex(iRight)(aIndexIdx(iRight))), 1, 0 ) ) ) * aCompare(2,j) j += 1 End While Select r Case 1 iLeft = iRight Case 0 If aIndex(iLeft)(aIndexIdx(iLeft)) > aIndex(iRight)(aIndexIdx(iRight)) Then iLeft = iRight End Select End While aIndexAll(i) = aIndex(iLeft)(aIndexIdx(iLeft)) aIndexIdx(iLeft) += 1 Next Return aIndexAll End Function End Class In Progressbar.au3 the execution times are measuered for the new code. This is the results on my PC when the code is run as 64 bit code: 250,000 rows Generate array Array generated 782.829209100278 Start sorting ... Sorting started 1052.33601879453 Sorting finished 3112.24138122098 Get index ... Index received 628.809605602899 500,000 rows Generate array Array generated 1692.53510123118 Start sorting ... Sorting started 1736.08362238057 Sorting finished 5300.57901794125 Get index ... Index received 987.373807333858 750,000 rows Generate array Array generated 2701.3300790124 Start sorting ... Sorting started 2846.97747093829 Sorting finished 8147.51853411496 Get index ... Index received 1427.32770753222 1,000,000 rows Generate array Array generated 3671.26491871585 Start sorting ... Sorting started 3360.98083976994 Sorting finished 11515.8238123206 Get index ... Index received 1913.89753875819 2,000,000 rows Generate array Array generated 7789.33304890347 Start sorting ... Sorting started 6773.98630303971 Sorting finished 48250.9211649065 Get index ... Index received 3852.50335220913 Note the lines titled "Sorting started". These are the lines that measure the time it takes to execute this code: Public Sub Sort2DArray( aObjPar As Object(,), aCompPar As Object(,) ) aObjects = aObjPar : aCompare = aCompPar : iRows = aObjects.GetUpperBound(1) + 1 iRestRows = iRows Mod iThreads : iPartRows = ( iRows - iRestRows ) / iThreads iCmps = aCompare.GetUpperBound(1) + 1 'Multithreaded index based sorting Dim th0 As New Thread( AddressOf Sort2DArraySub ) : th0.Start(0) Dim th1 As New Thread( AddressOf Sort2DArraySub ) : th1.Start(1) Dim th2 As New Thread( AddressOf Sort2DArraySub ) : th2.Start(2) Dim th3 As New Thread( AddressOf Sort2DArraySub ) : th3.Start(3) End Sub Most of the time is spent by passing the array ($aArray) as a parameter from the AutoIt code to the VB method (in aObjPar). Code is added to "Examples\5) Optimizing C# and VB code\1) Index based sorting\" in zip-file in bottom of first post. Global array variable Post 2 is a UDF version of four examples about generating a 2D array of random data, sorting the array by one or more columns through an index, converting the 2D array to a 1D array in CSV format, and finally saving the 1D array as a CSV file. The code is stored in "Examples\3) UDF version of examples\" in zip-file in bottom of first post. I've added a new example, ExampleOpt2.au3, to the Examples folder, where the execution times and the total execution time for these 4 procedures are measured for an array with 250,000 rows. This is the results: Code executed as 32 bit code Code executed as 64 bit code ============================ ============================ 250000 rows 250000 rows Generate array: 995.724559926393 Generate array: 797.652719123923 Sort array: 7236.27375117504 Sort array: 5291.30258795446 Convert array: 1426.65439887983 Convert array: 1224.09933028633 Save array: 450.983570086659 Save array: 383.220670832949 Total time: 10109.6362800679 Total time: 7696.27530819767 At the bottom of the example above we saw that quite a lot of time is being used to transfer arrays between the AutoIt code and the VB code. One way to avoid the time used to transfer arrays between AutoIt code and VB code is to store the array ($aArray/aObjects) as a global variable in the VB code. This is implemented in "Examples\5) Optimizing C# and VB code\2) Global array variable\Includes\ArrayFuncsOpt.vb". There is also used a multithreaded version of the sorting code. And the two procedures to convert and save the array as a CSV file is combined into one procedure to avoid transfering the array of strings back and forth. This is the time measurement for the new code: Code executed as 32 bit code Code executed as 64 bit code ============================ ============================ 250000 rows 250000 rows Generate array: 696.800872597593 Generate array: 516.643380589801 Sort array: 1542.99252546023 Sort array: 1465.04082474547 Save array: 446.520844017085 Save array: 418.329314718878 Total time: 2686.31424207491 Total time: 2400.01352005415 Code in "Examples\5) Optimizing C# and VB code\2) Global array variable\" in zip-file in bottom of first post. Delete global variables To delete global variables used in the VB code simply delete the object. The objects are solely handled in Includes\ArrayFuncsOpt.au3 UDF (there are no objects directly in the user code). The $oArrayClass object which is an instance of the ArrayClass in the VB code is created in ArrayFuncsOptInit() in this way: Func ArrayFuncsOptInit( $bDelObj = False ) Static $oNetCode = 0, $oArrayClass = 0 If $bDelObj Then $oArrayClass = 0 $oArrayClass = DotNet_CreateObject( $oNetCode, "ArrayClass" ) Return EndIf If IsObj( $oArrayClass ) Then Return $oArrayClass ; Compile and load VB code, create ArrayClass object $oNetCode = DotNet_LoadVBcode( FileRead( "..\Includes\ArrayFuncsOpt.vb" ), "System.dll" ) $oArrayClass = DotNet_CreateObject( $oNetCode, "ArrayClass" ) Return $oArrayClass EndFunc And the object is deleted in ArrayFuncsDelObject() by calling ArrayFuncsOptInit() with the $bDelObj parameter set to True: Func ArrayFuncsDelObject() ArrayFuncsOptInit( True ) EndFunc When the old object is deleted, a new object is created. ArrayFuncsDelObject() is called in bottom of ExampleOpt2.au3. Similar procedures are used in the example with the progress bar above to delete the old object and create a new.
    1 point
  10. A few updates to the examples in post 2 "2) Real C# and VB examples\1) Index based sorting\Includes\Sort2DArrayOpt.au3" System.Collections.dll is not needed in DotNet_LoadVBcode(). "2) Real C# and VB examples\2) Convert array\Includes\Conv2DArrayOpt.vb" CType( aObjects(j,i), String ) replaced with CStr( aObjects(j,i) ). "2) Real C# and VB examples\3) Save array\Includes\Save1DArrayOpt.au3" System.IO.dll and System.Text.Encoding.dll are not needed in DotNet_LoadVBcode(). "2) Real C# and VB examples\3) Save array\Examples\ExampleOpt.au3" In the previous version (ExampleOpt-a.au3), I had forgotten to initialize the VB code with Save1DArrayOptInit() in top of the script. This meant that the initialization instead was performed at the top of Save1DArrayOpt() function, and that the runtime for the initialization (about 100 milliseconds) was included in the runtime for Save1DArrayOpt(). That made the total runtime for Save1DArrayOpt() about 100 milliseconds too high. I've updated ExampleOpt.au3 and included Save1DArrayOptInit() in top of the script. This makes Save1DArrayOpt() about 100 milliseconds faster. And now, the optimized AutoIt/VB code is a bit faster than the pure AutoIt code. In the old version, the pure AutoIt code was fastest. I've updated the conclusion about the runtimes in "Save array" section in bottom of post 2. Post 3 UDF version of examples in post 2 Post 4 Threading
    1 point
  11. Real VB examples added to post 2. New zip in bottom of first post.
    1 point
  12. Adv. C# and VB examples Threading Perform a lengthy calculation of prime numbers in a worker thread in VB code while the AutoIt script is still responsive in the main thread. Code in "Examples\4) Adv. C# and VB examples\Threading" in zip-file in bottom of first post. Copy code We'll start by copying TemplateVB.au3 and TemplateVB.vb from "Examples\1) Introductory C# and VB examples\0) Code templates". The files are renamed to tst00.au3 and tst00.vb. tst00.vb: Imports System Class Au3Class Public Sub MyMethod() Console.WriteLine( "Hello world from VB!" ) End Sub End Class tst00.au3: #include "..\..\..\Includes\DotNetAll.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst00.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) $oAu3Class.MyMethod() EndFunc #cs Hello world from VB! #ce Copy tst00.au3/tst00.vb to tst01.au3/tst01.vb. Copy this VB code from Microsoft documentation to tst01.vb. tst01.vb must look like this: 'https://msdn.microsoft.com/en-us/library/system.threading.thread(v=vs.110).aspx?cs-save-lang=1&cs-lang=vb#Starting Imports System Imports System.Threading Imports System.Diagnostics Class Au3Class Public Sub MyMethod() Dim th As New Thread(AddressOf ExecuteInForeground) th.Start() Thread.Sleep(1000) Console.WriteLine("Main thread ({0}) exiting...", Thread.CurrentThread.ManagedThreadId) End Sub Private Sub ExecuteInForeground() Dim start As DateTime = DateTime.Now Dim sw As Stopwatch = Stopwatch.StartNew() Console.WriteLine("Thread {0}: {1}, Priority {2}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.ThreadState, Thread.CurrentThread.Priority) Do Console.WriteLine("Thread {0}: Elapsed {1:N2} seconds", Thread.CurrentThread.ManagedThreadId, sw.ElapsedMilliseconds / 1000) Thread.Sleep(500) Loop While sw.ElapsedMilliseconds <= 5000 sw.Stop() End Sub End Class tst01.au3: #include "..\..\..\Includes\DotNetAll.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst01.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) $oAu3Class.MyMethod() EndFunc #cs Thread 3: Running, Priority Normal Thread 3: Elapsed 0,00 seconds Thread 3: Elapsed 0,50 seconds Main thread (1) exiting... Thread 3: Elapsed 1,02 seconds #ce When we run the code in tst01.au3 there seems to be something wrong. Some of the console output is missing. The problem is that there is no code in tst01.au3 to keep the script running. tst01.au3 exits as soon as $oAu3Class.MyMethod() has been executed. And when tst01.au3 exits, the .NET Framework host exits, the VB code exits and the worker thread that was created within the VB code exits. Lets add a Sleep(5000) to keep the script alive: tst02.au3: #include "..\..\..\Includes\DotNetAll.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst01.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) $oAu3Class.MyMethod() Sleep(5000) EndFunc #cs Thread 3: Running, Priority Normal Thread 3: Elapsed 0,00 seconds Thread 3: Elapsed 0,51 seconds Main thread (1) exiting... Thread 3: Elapsed 1,03 seconds Thread 3: Elapsed 1,54 seconds Thread 3: Elapsed 2,06 seconds Thread 3: Elapsed 2,57 seconds Thread 3: Elapsed 3,09 seconds Thread 3: Elapsed 3,60 seconds Thread 3: Elapsed 4,12 seconds Thread 3: Elapsed 4,63 seconds #ce Now the output is similar to the output of the Microsoft example. Add a ConsoleWrite before and after the Sleep. tst03.au3: #include "..\..\..\Includes\DotNetAll.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst01.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) $oAu3Class.MyMethod() ConsoleWrite( "Sleep starts" & @CRLF ) Sleep(5000) ConsoleWrite( "Sleep stops" & @CRLF ) EndFunc #cs Thread 3: Running, Priority Normal Thread 3: Elapsed 0,00 seconds Thread 3: Elapsed 0,50 seconds Main thread (1) exiting... Sleep starts Thread 3: Elapsed 1,01 seconds Thread 3: Elapsed 1,53 seconds Thread 3: Elapsed 2,04 seconds Thread 3: Elapsed 2,56 seconds Thread 3: Elapsed 3,07 seconds Thread 3: Elapsed 3,59 seconds Thread 3: Elapsed 4,10 seconds Thread 3: Elapsed 4,62 seconds Sleep stops #ce The VB code in the worker thread is clearly running while the AutoIt code in the main thread is sleeping. Use counter Add a counter to the VB code. tst04.vb: Imports System Imports System.Threading Imports System.Diagnostics Class Au3Class Dim iCounter As Integer = 0 Public Function GetCounter() As Integer Return iCounter End Function Public Sub MyMethod() Dim th As New Thread(AddressOf ExecuteInForeground) th.Start() Thread.Sleep(1000) Console.WriteLine("Main thread ({0}) exiting...", Thread.CurrentThread.ManagedThreadId) End Sub Private Sub ExecuteInForeground() Dim start As DateTime = DateTime.Now Dim sw As Stopwatch = Stopwatch.StartNew() Console.WriteLine("Thread {0}: {1}, Priority {2}", Thread.CurrentThread.ManagedThreadId, Thread.CurrentThread.ThreadState, Thread.CurrentThread.Priority) Do Console.WriteLine("Thread {0}: Elapsed {1:N2} seconds", Thread.CurrentThread.ManagedThreadId, sw.ElapsedMilliseconds / 1000) iCounter += 1 Thread.Sleep(500) Loop While sw.ElapsedMilliseconds <= 5000 sw.Stop() End Sub End Class Replace the Sleep with a While loop in the AutoIt code. tst04.au3: #include "..\..\..\Includes\DotNetAll.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst04.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) $oAu3Class.MyMethod() Local $iCounter = 0 While $iCounter < 10 $iCounter = $oAu3Class.GetCounter() ConsoleWrite( "$iCounter = " & $iCounter & @CRLF ) Sleep( 250 ) WEnd EndFunc #cs Thread 3: Running, Priority Normal Thread 3: Elapsed 0,00 seconds Thread 3: Elapsed 0,51 seconds Main thread (1) exiting... $iCounter = 2 Thread 3: Elapsed 1,02 seconds $iCounter = 3 $iCounter = 3 Thread 3: Elapsed 1,54 seconds $iCounter = 4 $iCounter = 4 Thread 3: Elapsed 2,05 seconds $iCounter = 5 $iCounter = 5 Thread 3: Elapsed 2,57 seconds $iCounter = 6 $iCounter = 6 Thread 3: Elapsed 3,08 seconds $iCounter = 7 $iCounter = 7 Thread 3: Elapsed 3,60 seconds $iCounter = 8 Thread 3: Elapsed 4,11 seconds $iCounter = 9 $iCounter = 9 Thread 3: Elapsed 4,63 seconds $iCounter = 10 #ce Prime numbers At this point we can insert the VB code and AutoIt code to calculate prime numbers. tst05.vb: Imports System Imports System.Threading Class Au3Class Dim aPrimes(1) As Integer Dim iCntPrimes As Integer = 0 Public Function GetNumberOfPrimes() As Integer Return iCntPrimes End Function Public Function GetArrayOfPrimes() As Integer() Return aPrimes End Function Public Sub CalcPrimes( nPrimes As Integer ) Dim th As New Thread( AddressOf CalcPrimesSub ) th.Start( nPrimes ) End Sub Private Sub CalcPrimesSub( oPrimes As Object ) Dim nPrimes As Integer = CInt( oPrimes ) : ReDim aPrimes(nPrimes-1) Dim iPrime As Integer = 2, iPrimes As Integer = 0, i As Integer If nPrimes <= 100 Then Console.WriteLine( iPrime ) 'Store first prime aPrimes(iPrimes) = iPrime iPrimes += 1 iPrime += 1 'Loop to calculate primes While iPrimes < nPrimes For i = 0 To iPrimes - 1 If iPrime Mod aPrimes(i) = 0 Then Exit For Next If i = iPrimes Then If nPrimes <= 100 Then Console.WriteLine( iPrime ) aPrimes(iPrimes) = iPrime iPrimes += 1 If iPrimes Mod 1000 = 0 Then iCntPrimes = iPrimes End If iPrime += 1 End While iCntPrimes = iPrimes End Sub End Class tst05.au3: #include "..\..\..\Includes\DotNetAll.au3" #include "..\..\..\Display\Functions\ArrayDisplayEx\ArrayDisplayEx.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst05.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) Local $iPrimes = 100000 $oAu3Class.CalcPrimes( $iPrimes ) Local $iCntPrimes = 0 While $iCntPrimes < $iPrimes $iCntPrimes = $oAu3Class.GetNumberOfPrimes() ConsoleWrite( "$iCntPrimes = " & $iCntPrimes & @CRLF ) Sleep( 1000 ) WEnd Local $aPrimes = $oAu3Class.GetArrayOfPrimes() _ArrayDisplayEx( $aPrimes ) EndFunc #cs $iCntPrimes = 0 $iCntPrimes = 24000 $iCntPrimes = 34000 $iCntPrimes = 42000 $iCntPrimes = 49000 $iCntPrimes = 55000 $iCntPrimes = 60000 $iCntPrimes = 65000 $iCntPrimes = 69000 $iCntPrimes = 74000 $iCntPrimes = 78000 $iCntPrimes = 81000 $iCntPrimes = 85000 $iCntPrimes = 88000 $iCntPrimes = 92000 $iCntPrimes = 95000 $iCntPrimes = 98000 $iCntPrimes = 100000 #ce AutoIt GUI Finally we can make a GUI. tst06.au3: #AutoIt3Wrapper_Au3Check_Parameters=-d -w- 1 -w 2 -w 3 -w 4 -w 5 -w 6 #include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include "..\..\..\Includes\DotNetAll.au3" #include "..\..\..\Display\Functions\ArrayDisplayEx\ArrayDisplayEx.au3" Opt( "MustDeclareVars", 1 ) Example() Func Example() Local $oNetCode = DotNet_LoadVBcode( FileRead( "tst05.vb" ), "System.dll" ) Local $oAu3Class = DotNet_CreateObject( $oNetCode, "Au3Class" ) Local $hGui1 = GUICreate( "Prime numbers", 300, 100, 250, 250 ) Local $idLabel = GUICtrlCreateLabel( "Primes:", 20, 22, 35, 18 ) Local $idCombo = GUICtrlCreateCombo( "", 60, 20, 80, 20 ) GUICtrlSetData( $idCombo, "50000|100000|150000|200000|250000", "50000" ) Local $idPrimes = GUICtrlCreateButton( "Calculate primes", 160, 20, 120, 20 ) Local $idButton1 = GUICtrlCreateButton( "Button1", 20, 60, 120, 20 ) Local $idButton2 = GUICtrlCreateButton( "Button2", 160, 60, 120, 20 ) GUISetState() Local $s, $hGui2, $idProgress, $idPrgPct, $hTimer = 0, $iTimes, $nPrimes, $iPrimes While 1 Switch GUIGetMsg() Case $idPrimes GUICtrlSetState( $idPrimes, $GUI_DISABLE ) $nPrimes = Int( GUICtrlRead( $idCombo ) ) $oAu3Class.CalcPrimes( $nPrimes ) $s = StringRegExpReplace( $nPrimes, "(\d{1,3}(?=(\d{3})+\z)|\d{3}(?=\d))", "\1,") $hGui2 = GUICreate( "Calculating " & $s & " primes ...", 340, 90, 350, 400, $WS_OVERLAPPED ) $idProgress = GUICtrlCreateProgress( 20, 20, 260, 20 ) $idPrgPct = GUICtrlCreateLabel( "0%", 290, 23, 25, 18 ) $hTimer = TimerInit() $iTimes = 1 $iPrimes = 0 GUISetState() Case $idButton1 MsgBox( 0, "Button1", "Button1", 1, $hGui1 ) Case $idButton2 MsgBox( 0, "Button2", "Button2", 1, $hGui1 ) Case $GUI_EVENT_CLOSE ExitLoop Case Else If $hTimer And TimerDiff( $hTimer ) > $iTimes * 1000 Then $iTimes += 1 $iPrimes = $oAu3Class.GetNumberOfPrimes() GUICtrlSetData( $idProgress, 100*$iPrimes/$nPrimes ) GUICtrlSetData( $idPrgPct, Int( 100*$iPrimes/$nPrimes ) & "%" ) If $iPrimes = $nPrimes Then Sleep(1000) $hTimer = 0 GUIDelete( $hGui2 ) GUICtrlSetState( $idPrimes, $GUI_ENABLE ) _ArrayDisplayEx( $oAu3Class.GetArrayOfPrimes() ) EndIf EndIf EndSwitch WEnd GUIDelete( $hGui2 ) GUIDelete( $hGui1 ) #forceref $idLabel EndFunc You can click Button1 and Button2 while the prime numbers are calculated. The MsgBoxes closes automatically after one second.
    1 point
  13. 10 years later and post is still helping people. Thanks!!!
    1 point
  14. I don't think it would if the drop would be justified. Why supporting something that doesn't work properly (if it really doesn't). My thinking is that no sane person would have anything against such healthy approach in development. You shouldn't be hiding behind (boring) "drama" argument, it makes you look fruity. However, could you explain a bit what's exactly weird about it? Also, are you sure the headaches are similar (or you said that just because)? May I ask that you answer the first question not as AutoIt developer, but rather as advanced AutoIt user - if possible. Thanks in advance Jon.
    1 point
  15. During my first video's covering crucial and frequently used components of Autoit scripting, do you have any recommendations in case I have missed anything? So far the only notes I have left on my todo list are: I am open to other things I have not yet thought of to include in the "heres what you need to know first" kind of section of videos. So far my list is not complete, its just where my noted ideas are up to right now.
    1 point
  16. zfisherdrums

    LazyReader(c)

    Useless Humanity, Inc. 2000 Awesome Hair Drive Nashville, TN 37211 Re: Problems with the use of LazyReader© Dear BERT; Thank you for your interest in LazyReader©. We're sure you'll agree that our product will help you realize huge gains in the areas of personal productivity and knowledge advancement. I know it is hard for me to believe that we're practically GIVING this away for $19.95 free! In regards to your inquiry, our R&D department has provided the following response: "Looks ok, but do you really need to clear the clipboard at the start??" Yes. Otherwise LazyReader© will read whatever is currently in the clipboard when it starts up. Our testers discovered problems when large amounts of text resided in the clipboard upon startup. LazyReader© would begin rambling and because this version has not introduced our proprietary multi-threaded speech synthesis, the process must be killed via the Task Manager. This also is a problem in normal use if you copy a large area of text by accident. We hope to have that updated version released in tandem with our .NET port. Keep a look out for updates! "and I also get an error" You WOULD get an error! Er...sorry. We're kind of sensitive in the R&D department. Late nights, early mornings, coffee might as well be in an IV. You get the picture. LazyReader© has been programmed to patently refuse to read certain text it deems kinda boring. Perhaps you could highlight something more fascinating like a Wiki on moon rocks, Oprah's Book club, or the UFC web page. We've never had any problems with LazyReader© reading about BJ Penn. Another possible workaround would be to download the Microsoft Speech API from their web-site. We'd send you the link , but we cannot remember it. And our IT guy is a horrible speller, so our corporate firewall prevents access to any site with '.com' in it. Sorry. You might also try to remove that offending line. It isn't necessary for the use of LazyReader©. It just gives you that Stephen Hawkings crooning that the kids are going for these days. We hope this answers your concerns with the use of LazyReader©. Please feel free to contact me should you have any other questions or concerns. Sincerely, Bob MacNimara Customer Support/Company Friend Maker/Crowd Pleaser Useless Humanity, Inc.
    1 point
×
×
  • Create New...