noellarkin Posted January 16, 2023 Share Posted January 16, 2023 1 hour ago, SOLVE-SMART said: like well defined names for variables, functions (methods), classes etc yep you're right it makes a world of difference in terms of code readability, especially in conditional statements, especially for someone relatively new to coding (little over a year in). I have a whole bunch of small snippets just for readability: Func _ArrayEnd(ByRef $thisarray) Local $ReturnValue = 0 $ReturnValue = UBound($thisarray) - 1 Return $ReturnValue EndFunc Func _ArraySize(ByRef $thisarray) Local $ReturnValue = 0 $ReturnValue = UBound($thisarray) Return $ReturnValue EndFunc Func _ArrayDeleteFirstRow(ByRef $thisarray) _ArrayDelete($thisarray, 0) EndFunc Func _ArraySearchFound($arraysearchresult) Local $ReturnValue = 0 If $arraysearchresult <> -1 Then $ReturnValue = 1 Return $ReturnValue EndFunc Func _ArraySearchNotFound($arraysearchresult) Local $ReturnValue = 0 If $arraysearchresult = -1 Then $ReturnValue = 1 Return $ReturnValue EndFunc Func _ProcessExists($thisprocessid) Local $ReturnValue = 0 If $thisprocessid <> -1 Then $ReturnValue = 1 Return $ReturnValue EndFunc Func _ProcessDoesntExist($thisprocessid) Local $ReturnValue = 0 If $thisprocessid = -1 Then $ReturnValue = 1 Return $ReturnValue EndFunc etc etc :) oh and I know I have those extra lines $ReturnValue = 0 etc that's because I've been forcing myself to remember to use SESE whenever possible :) SOLVE-SMART 1 Link to comment Share on other sites More sharing options...
noellarkin Posted February 9, 2023 Share Posted February 9, 2023 Async HTTP Requests I couldn't find the thread for "Async HTTP requests" and the corresponding code snippet, so I rewrote it. This code snippet sents Async GET requests to an array of URLs, and returns response code, response headers and response body as an array. #include-once #include <Array.au3> Local $HTTP_METHOD = "GET" Local $ASYNC_REQUEST = True Local $aURLs = ["https://en.wikipedia.org", "https://en.wikipedia.org/wiki/Forest_raven", "https://en.wikipedia.org/wiki/Stanley_Bruce", "https://en.wikipedia.org/wiki/Beatlemania"] Local $nTotalThreads = UBound($aURLs) Local $aRequestHandles[$nTotalThreads] Local $aThreadStatus[$nTotalThreads] Local $nCompletedThreads = 0 _ArrayColInsert($aURLs, 1) ; stores response code _ArrayColInsert($aURLs, 1) ; stores response headers _ArrayColInsert($aURLs, 1) ; stores response text For $i = 0 To $nTotalThreads - 1 $aRequestHandles[$i] = ObjCreate("WinHttp.WinHttpRequest.5.1") $aRequestHandles[$i].Open($HTTP_METHOD, $aURLs[$i][0], $ASYNC_REQUEST) $aRequestHandles[$i].Send() Next Do For $i = 0 To $nTotalThreads - 1 If $aThreadStatus[$i] = 0 And $aRequestHandles[$i].WaitForResponse(0) = True Then $aURLs[$i][1] = $aRequestHandles[$i].Status $aURLs[$i][2] = $aRequestHandles[$i].GetAllResponseHeaders $aURLs[$i][3] = $aRequestHandles[$i].ResponseText $aThreadStatus[$i] = 1 $nCompletedThreads += 1 EndIf Next Until $nCompletedThreads = $nTotalThreads _ArrayDisplay($aURLs) Explanation: First, we define variables: $HTTP_METHOD is set to "GET" as the request method. $ASYNC_REQUEST is set to True for asynchronous processing. $aURLs is an array of 4 URLs to be requested. $nTotalThreads is set to the upper bound of $aURLs, which is the number of elements in the array. $aRequestHandles is an array to store the request handles, with a length of $nTotalThreads. $aThreadStatus is an array to store the status of each thread, with a length of $nTotalThreads. $nCompletedThreads is a counter for the completed threads, which is initialized to 0. The next 3 lines insert 3 new columns in $aURLs to store the response status code, headers, and text. The next block of code performs a loop from 0 to $nTotalThreads - 1. Inside the loop, an object is created from the "WinHttp.WinHttpRequest.5.1" class and assigned to $aRequestHandles[$i]. The object's Open method is then called, with arguments $HTTP_METHOD, $aURLs[$i][0], and $ASYNC_REQUEST, to initiate the request. The object's Send method is then called to send the request. The next block of code is a do-until loop that waits until all threads have completed. Inside the loop, a nested for loop is performed from 0 to $nTotalThreads - 1. If a thread is not yet completed (indicated by $aThreadStatus[$i] being equal to 0) and has received a response (indicated by $aRequestHandles[$i].WaitForResponse(0) being equal to True), then the response status code, headers, and text are stored in $aURLs[$i][1], $aURLs[$i][2], and $aURLs[$i][3] respectively. The thread status is then set to completed (indicated by $aThreadStatus[$i] being equal to 1) and the $nCompletedThreads counter is incremented. The last line calls the _ArrayDisplay function with $aURLs as an argument, which displays the contents of the array. SOLVE-SMART, robertocm and argumentum 3 Link to comment Share on other sites More sharing options...
kut0 Posted February 16, 2023 Share Posted February 16, 2023 Thank you very much, Chimaera Link to comment Share on other sites More sharing options...
mLipok Posted March 2, 2023 Share Posted March 2, 2023 (edited) I very offen wonders which values gives particular macro. For example: do I should use @AppDataDir or @LocalAppDataDir or @UserProfileDir Here is my Show_Macro_Values.au3script: expandcollapse popupConsoleWrite('@AppDataCommonDir : ' & @AppDataCommonDir & @CRLF) ConsoleWrite('@AppDataDir : ' & @AppDataDir & @CRLF) ConsoleWrite('@AutoItExe : ' & @AutoItExe & @CRLF) ConsoleWrite('@AutoItPID : ' & @AutoItPID & @CRLF) ConsoleWrite('@AutoItVersion : ' & @AutoItVersion & @CRLF) ConsoleWrite('@AutoItX64 : ' & @AutoItX64 & @CRLF) ConsoleWrite('@CommonFilesDir : ' & @CommonFilesDir & @CRLF) ConsoleWrite('@Compiled : ' & @Compiled & @CRLF) ConsoleWrite('@ComputerName : ' & @ComputerName & @CRLF) ConsoleWrite('@ComSpec : ' & @ComSpec & @CRLF) ConsoleWrite('@CPUArch : ' & @CPUArch & @CRLF) ConsoleWrite('@DesktopCommonDir : ' & @DesktopCommonDir & @CRLF) ConsoleWrite('@DesktopDepth : ' & @DesktopDepth & @CRLF) ConsoleWrite('@DesktopDir : ' & @DesktopDir & @CRLF) ConsoleWrite('@DesktopHeight : ' & @DesktopHeight & @CRLF) ConsoleWrite('@DesktopRefresh : ' & @DesktopRefresh & @CRLF) ConsoleWrite('@DesktopWidth : ' & @DesktopWidth & @CRLF) ConsoleWrite('@DocumentsCommonDir : ' & @DocumentsCommonDir & @CRLF) ConsoleWrite('@FavoritesCommonDir : ' & @FavoritesCommonDir & @CRLF) ConsoleWrite('@FavoritesDir : ' & @FavoritesDir & @CRLF) ConsoleWrite('@HomeDrive : ' & @HomeDrive & @CRLF) ConsoleWrite('@HomePath : ' & @HomePath & @CRLF) ConsoleWrite('@HomeDrive : ' & @HomeDrive & @CRLF) ConsoleWrite('@HomeShare : ' & @HomeShare & @CRLF) ConsoleWrite('@LocalAppDataDir : ' & @LocalAppDataDir & @CRLF) ConsoleWrite('@LogonDNSDomain : ' & @LogonDNSDomain & @CRLF) ConsoleWrite('@LogonDomain : ' & @LogonDomain & @CRLF) ConsoleWrite('@LogonServer : ' & @LogonServer & @CRLF) ConsoleWrite('@MyDocumentsDir : ' & @MyDocumentsDir & @CRLF) ConsoleWrite('@OSArch : ' & @OSArch & @CRLF) ConsoleWrite('@OSBuild : ' & @OSBuild & @CRLF) ConsoleWrite('@OSLang : ' & @OSLang & @CRLF) ConsoleWrite('@OSServicePack : ' & @OSServicePack & @CRLF) ConsoleWrite('@OSType : ' & @OSType & @CRLF) ConsoleWrite('@OSVersion : ' & @OSVersion & @CRLF) ConsoleWrite('@ProgramFilesDir : ' & @ProgramFilesDir & @CRLF) ConsoleWrite('@ProgramsCommonDir : ' & @ProgramsCommonDir & @CRLF) ConsoleWrite('@ProgramsDir : ' & @ProgramsDir & @CRLF) ConsoleWrite('@ScriptFullPath : ' & @ScriptFullPath & @CRLF) ConsoleWrite('@ScriptDir : ' & @ScriptDir & @CRLF) ConsoleWrite('@ScriptName : ' & @ScriptName & @CRLF) ConsoleWrite('@ScriptLineNumber : ' & @ScriptLineNumber & @CRLF) ConsoleWrite('@StartMenuCommonDir : ' & @StartMenuCommonDir & @CRLF) ConsoleWrite('@StartMenuDir : ' & @StartMenuDir & @CRLF) ConsoleWrite('@StartupCommonDir : ' & @StartupCommonDir & @CRLF) ConsoleWrite('@StartupDir : ' & @StartupDir & @CRLF) ConsoleWrite('@SystemDir : ' & @SystemDir & @CRLF) ConsoleWrite('@TempDir : ' & @TempDir & @CRLF) ConsoleWrite('@UserName : ' & @UserName & @CRLF) ConsoleWrite('@UserProfileDir : ' & @UserProfileDir & @CRLF) ConsoleWrite('@WindowsDir : ' & @WindowsDir & @CRLF) Edited March 2, 2023 by mLipok noellarkin, pixelsearch, web2win and 1 other 4 Signature beginning:* Please remember: "AutoIt"..... * Wondering who uses AutoIt and what it can be used for ? * Forum Rules ** ADO.au3 UDF * POP3.au3 UDF * XML.au3 UDF * IE on Windows 11 * How to ask ChatGPT for AutoIt Code * for other useful stuff click the following button: Spoiler Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind. My contribution (my own projects): * Debenu Quick PDF Library - UDF * Debenu PDF Viewer SDK - UDF * Acrobat Reader - ActiveX Viewer * UDF for PDFCreator v1.x.x * XZip - UDF * AppCompatFlags UDF * CrowdinAPI UDF * _WinMergeCompare2Files() * _JavaExceptionAdd() * _IsBeta() * Writing DPI Awareness App - workaround * _AutoIt_RequiredVersion() * Chilkatsoft.au3 UDF * TeamViewer.au3 UDF * JavaManagement UDF * VIES over SOAP * WinSCP UDF * GHAPI UDF - modest begining - comunication with GitHub REST API * ErrorLog.au3 UDF - A logging Library * Include Dependency Tree (Tool for analyzing script relations) * Show_Macro_Values.au3 * My contribution to others projects or UDF based on others projects: * _sql.au3 UDF * POP3.au3 UDF * RTF Printer - UDF * XML.au3 UDF * ADO.au3 UDF * SMTP Mailer UDF * Dual Monitor resolution detection * * 2GUI on Dual Monitor System * _SciLexer.au3 UDF * SciTE - Lexer for console pane * Useful links: * Forum Rules * Forum etiquette * Forum Information and FAQs * How to post code on the forum * AutoIt Online Documentation * AutoIt Online Beta Documentation * SciTE4AutoIt3 getting started * Convert text blocks to AutoIt code * Games made in Autoit * Programming related sites * Polish AutoIt Tutorial * DllCall Code Generator * Wiki: * Expand your knowledge - AutoIt Wiki * Collection of User Defined Functions * How to use HelpFile * Good coding practices in AutoIt * OpenOffice/LibreOffice/XLS Related: WriterDemo.au3 * XLS/MDB from scratch with ADOX IE Related: * How to use IE.au3 UDF with AutoIt v3.3.14.x * Why isn't Autoit able to click a Javascript Dialog? * Clicking javascript button with no ID * IE document >> save as MHT file * IETab Switcher (by LarsJ ) * HTML Entities * _IEquerySelectorAll() (by uncommon) * IE in TaskScheduler * IE Embedded Control Versioning (use IE9+ and HTML5 in a GUI) * PDF Related: * How to get reference to PDF object embeded in IE * IE on Windows 11 * I encourage you to read: * Global Vars * Best Coding Practices * Please explain code used in Help file for several File functions * OOP-like approach in AutoIt * UDF-Spec Questions * EXAMPLE: How To Catch ConsoleWrite() output to a file or to CMD *I also encourage you to check awesome @trancexx code: * Create COM objects from modules without any demand on user to register anything. * Another COM object registering stuff * OnHungApp handler * Avoid "AutoIt Error" message box in unknown errors * HTML editor * winhttp.au3 related : * https://www.autoitscript.com/forum/topic/206771-winhttpau3-download-problem-youre-speaking-plain-http-to-an-ssl-enabled-server-port/ "Homo sum; humani nil a me alienum puto" - Publius Terentius Afer"Program are meant to be read by humans and only incidentally for computers and execute" - Donald Knuth, "The Art of Computer Programming" , be and \\//_. Anticipating Errors : "Any program that accepts data from a user must include code to validate that data before sending it to the data store. You cannot rely on the data store, ...., or even your programming language to notify you of problems. You must check every byte entered by your users, making sure that data is the correct type for its field and that required fields are not empty." Signature last update: 2023-04-24 Link to comment Share on other sites More sharing options...
noellarkin Posted March 2, 2023 Share Posted March 2, 2023 @mLipok Love this, thank you for sharing! Link to comment Share on other sites More sharing options...
pixelsearch Posted March 2, 2023 Share Posted March 2, 2023 @mLipok well done ! Some comments if I may : * @HomeDrive is found twice in the script. * @KBLayout, @MUILang could both be added. After all, you choosed to indicate @OSLang and these 2 are a bit related. * @WorkingDir should be added. Of course it may change, but it's also the case with the existing @ScriptDir etc... * I like your idea, not to follow the alphabetic order for the @Script... macros : Help file : @ScriptDir, @ScriptFullPath, @ScriptLineNumber, @ScriptName (alphabetically) mLipok : @ScriptFullPath, @ScriptDir, @ScriptName, @ScriptLineNumber ("matryoshka dolls way") Great job starting this, bravo Link to comment Share on other sites More sharing options...
Popular Post OJBakker Posted March 2, 2023 Popular Post Share Posted March 2, 2023 This is my Showmacros.au3 script. It is one of the first scripts I have made with AutoIt , but I have enhanced it a few times. It uses an array with all the macro names in the same order as the helpfile and reports all macro's with their values (if available) with arraydisplay. If you don't like this order, just rearrange the array and be happy! expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7 #include <Array.au3> AutoItSetOption("MustDeclareVars", 1) Local $asMacroNames = [ _ "@AppDataCommonDir", "@AppDataDir", "@AutoItExe", "@AutoItPID", "@AutoItVersion", "@AutoItX64", "@COM_EventObj", "@CommonFilesDir", _ "@Compiled", "@ComputerName", "@ComSpec", "@CPUArch", "@CR", "@CRLF", "@DesktopCommonDir", "@DesktopDepth", "@DesktopDir", "@DesktopHeight", _ "@DesktopRefresh", "@DesktopWidth", "@DocumentsCommonDir", "@error", "@exitCode", "@exitMethod", "@extended", _ "@FavoritesCommonDir", "@FavoritesDir", "@GUI_CtrlHandle", "@GUI_CtrlId", "@GUI_DragFile", "@GUI_DragId", "@GUI_DropId", "@GUI_WinHandle", _ "@HomeDrive", "@HomePath", "@HomeShare", "@HotKeyPressed", "@HOUR", "@IPAddress1", "@IPAddress2", "@IPAddress3", "@IPAddress4", "@KBLayout", "@LF", _ "@LocalAppDataDir", "@LogonDNSDomain", "@LogonDomain", "@LogonServer", "@MDAY", "@MIN", "@MON", "@MSEC", "@MUILang", "@MyDocumentsDir", _ "@NumParams", "@OSArch", "@OSBuild", "@OSLang", "@OSServicePack", "@OSType", "@OSVersion", "@ProgramFilesDir", "@ProgramsCommonDir", "@ProgramsDir", _ "@ScriptDir", "@ScriptFullPath", "@ScriptLineNumber", "@ScriptName", "@SEC", "@StartMenuCommonDir", "@StartMenuDir", "@StartupCommonDir", "@StartupDir", _ "@SW_DISABLE", "@SW_ENABLE", "@SW_HIDE", "@SW_LOCK", "@SW_MAXIMIZE", "@SW_MINIMIZE", "@SW_RESTORE", "@SW_SHOW", "@SW_SHOWDEFAULT", "@SW_SHOWMAXIMIZED", _ "@SW_SHOWMINIMIZED", "@SW_SHOWMINNOACTIVE", "@SW_SHOWNA", "@SW_SHOWNOACTIVATE", "@SW_SHOWNORMAL", "@SW_UNLOCK", "@SystemDir", "@TAB", "@TempDir", "@TRAY_ID", _ "@TrayIconFlashing", "@TrayIconVisible", "@UserName", "@UserProfileDir", "@WDAY", "@WindowsDir", "@WorkingDir", "@YDAY", "@YEAR", "@NoMacro"] ; above are the macro's from autoit v3.3.16.1 These can also be found in file au3check.dat but than a compiled script (exe) would need to know where to find this file. Local $sMacroValue Local $aMacroArray[UBound($asMacroNames)][4] For $k = 0 To UBound($asMacroNames) - 1 $aMacroArray[$k][0] = $asMacroNames[$k] $aMacroArray[$k][1] = "" $aMacroArray[$k][2] = Execute($asMacroNames[$k]) If @error Then $aMacroArray[$k][1] = "N/A" $aMacroArray[$k][3] = "" $sMacroValue = " " Switch $asMacroNames[$k] Case "@COM_EventObj" $sMacroValue &= "Only valid in a COM even function" Case "@GUI_CtrlId", "@GUI_CtrlHandle", "@GUI_WinHandle" $sMacroValue &= "Only valid in an event function" Case "@GUI_DragFile", "@GUI_DragId", "@GUI_DropId" $sMacroValue &= "Only valid in a drop event function" Case "@exitCode", "@exitMethod" $sMacroValue &= "Only valid in a function in OnAutoItExitRegister" Case "@TRAY_ID" $sMacroValue &= "Only valid in Tray event function" Case Else $sMacroValue &= "Not a valid macro" EndSwitch $aMacroArray[$k][2] = $sMacroValue Else ; hex/dec view for non-visual macro's $aMacroArray[$k][3] = VarGetType($aMacroArray[$k][2]) Switch $asMacroNames[$k] Case "@CR", "@CRLF", "@LF", "@TAB" $aMacroArray[$k][2] = "Hex: " & StringToBinary($aMacroArray[$k][2]) & " Dec: " & _ArrayToString(StringToASCIIArray($aMacroArray[$k][2]), " ") Case "@HOUR", "@MDAY", "@MIN", "@MON", "@MSEC", "@SEC", "@WDAY", "@YDAY", "@YEAR" $aMacroArray[$k][2] = " now: " & $aMacroArray[$k][2] ; group these in view sort on macrovalue EndSwitch EndIf Next _ArrayDisplay($aMacroArray, "AutoIt Macro's Overview. Type = VarGetType(Execute('@Macro'))", Default, $ARRAYDISPLAY_COLALIGNLEFT + $ARRAYDISPLAY_NOROW, Default, "Macro Name|Avail.|Macro Value|Type") ShowMacros.au3 SOLVE-SMART, mLipok, TheDcoder and 2 others 4 1 Link to comment Share on other sites More sharing options...
ioa747 Posted March 31, 2023 Share Posted March 31, 2023 (edited) Very often we need to measure execution speed, thus came the idea of FuncSpeedTest($sExecute) Func FuncSpeedTest($sExecute) Local $hTimer = TimerInit() Execute($sExecute) ConsoleWrite($sExecute & " processed in: " & Round(TimerDiff($hTimer) / 1000, 3) & " seconds " & @LF) EndFunc ;==>FuncSpeedTest Example1: #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 FuncSpeedTest("_IfSingle()") FuncSpeedTest("_IfMulti()") ;---------------------------------------------------------------------------------------- Func _IfSingle() Local $iVariable = 1 For $i = 1 To 1000000 If $iVariable Then $iVariable = 1 Next EndFunc ;==>_IfSingle ;---------------------------------------------------------------------------------------- Func _IfMulti() Local $iVariable = 1 For $i = 1 To 1000000 If $iVariable Then $iVariable = 1 EndIf Next EndFunc ;==>_IfMulti ;---------------------------------------------------------------------------------------- Func FuncSpeedTest($sExecute) Local $hTimer = TimerInit() Execute($sExecute) ConsoleWrite($sExecute & " processed in: " & Round(TimerDiff($hTimer) / 1000, 3) & " seconds " & @LF) EndFunc ;==>FuncSpeedTest ;---------------------------------------------------------------------------------------- from: 133961-if-then-else-single-line Example2: expandcollapse popup#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 #include <WinAPISys.au3> For $i = 1 To 4 FuncSpeedTest("_test" & $i & "()") ConsoleWrite("" & @CRLF) Next ;---------------------------------------------------------------------------------------- Func _test1() For $i = 1 To 1000000 _WinAPI_GetTickCount64() Next ConsoleWrite(_WinAPI_GetTickCount64() & @CRLF) EndFunc ;==>_test1 ;---------------------------------------------------------------------------------------- Func _test2() For $i = 1 To 1000000 DllCall('kernel32.dll', 'uint64', 'GetTickCount64') Next ConsoleWrite(DllCall('kernel32.dll', 'uint64', 'GetTickCount64')[0] & @CRLF) EndFunc ;==>_test2 ;---------------------------------------------------------------------------------------- Func _test3() Local $dll = DllOpen("kernel32.dll") For $i = 1 To 1000000 DllCall($dll, 'uint64', 'GetTickCount64') Next ConsoleWrite(DllCall($dll, 'uint64', 'GetTickCount64')[0] & @CRLF) DllClose($dll) EndFunc ;==>_test3 ;---------------------------------------------------------------------------------------- Func _test4() Local $hInstance_Kernel32_dll = _WinAPI_GetModuleHandle("kernel32.dll") Local $hAddress_Kernel32_dll_GetTickCount64 = _WinAPI_GetProcAddress($hInstance_Kernel32_dll, "GetTickCount64") For $i = 1 To 1000000 DllCallAddress('uint64', $hAddress_Kernel32_dll_GetTickCount64) Next ConsoleWrite(DllCallAddress('uint64', $hAddress_Kernel32_dll_GetTickCount64)[0] & @CRLF) EndFunc ;==>_test4 ;---------------------------------------------------------------------------------------- Func FuncSpeedTest($sExecute) Local $hTimer = TimerInit() Execute($sExecute) ConsoleWrite($sExecute & " processed in: " & Round(TimerDiff($hTimer) / 1000, 3) & " seconds " & @LF) EndFunc ;==>FuncSpeedTest ;---------------------------------------------------------------------------------------- from: 209984-dllclose-is-still-a-good-idea Edited March 31, 2023 by ioa747 robertocm 1 I know that I know nothing Link to comment Share on other sites More sharing options...
Gianni Posted April 8, 2023 Share Posted April 8, 2023 (edited) _WorkDay() Returns the date that is the specified number of working days before or after the start date This is a variation of a function from this post that implements more features, namely, the function allows 3 parameters: $StartDate: Date representing the start date of the count. (no check is made on the correctness of the date provided) $iDays: Number of working days before or after the start_date. A positive value indicates a future date; a negative value indicates a past date; a zero value indicates the start_date itself. (Indicating 0 the departure date is returned even if this corresponds to a non-working day, but in this case the @error flag is set to 1) $sHolidays: Optional (default 1,7). Indicate the days of the week considered non-working (not to be considered in the count). $sHolidays represents a string with any combination of digits from 1 to 7 (the comma between the digits is optional) which indicates the days of the week to be considered holidays which correspond as follows: 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday. if you want to consider working all days of the week pass an empty string then. (if, by contradiction, all seven days of the week are indicated as holidays, the day pointed to by $Days is returned anyway, but the @error flag is also set to 1) I hope there are no bugs. Have fun. expandcollapse popup#include <date.au3> _Example() ; 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday Func _Example() Local $Start = _NowCalcDate() Local $count = 25 MsgBox(0, 'example 1', "The " & $count & "th working days past " & $Start & " is " & _WorkDay($Start, $count)) $count = -9 MsgBox(0, 'example 2', $count & " workdays ago: " & _WorkDay($Start, $count)) $Start = "2022/12/31" $count = 33 $NoCountWeekDays = '234567' ; <-- all holidays except Sunday (count only the Sunday) MsgBox(0, 'example 3', "The " & $count & "th Sunday of the 2023 is on " & _WorkDay($Start, $count, $NoCountWeekDays)) $Start = _NowCalcDate() $count = -15 ; negative $NoCountWeekDays = '134567' ; <-- all holidays except Monday (count only the Monday) MsgBox(0, 'example 4', $count & " Mondays ago was " & _WorkDay($Start, $count, $NoCountWeekDays)) EndFunc ;==>_Example ; #FUNCTION# ==================================================================================================================== ; Name ..........: _WorkDay ; Description ...: ; Syntax ........: _WorkDay([$StartDate = _NowCalcDate([, $iDays = 0[, $sHolidays = "17"]]]) ; Parameters ....: $StartDate - Start day. ; $iDays - number of WorkDays to count (positive or negative) ; $sHolidays - a string of digits indicating not working days of week. Default is "17". ; Return values .: Target date ; Author ........: Gianni ; Modified ......: ; Remarks .......: ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _WorkDay($StartDate = _NowCalcDate(), $iDays = 0, $sHolidays = "17") Local $iDirection = 1 * ($iDays < 0 ? -1 : $iDays > 0), $iWeekdays = 0 Local $iWorkDays = StringRegExpReplace("1234567", "[" & StringRegExpReplace($sHolidays, "[^1234567]", "") & "]", "") Local $aYMD = StringSplit($StartDate, "/", 2) Local $nJulianDate = _DateToDayValue($aYMD[0], $aYMD[1], $aYMD[2]) If Not $iDirection Then Return SetError(False = StringInStr($iWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2), 0, $StartDate) If $iWorkDays = '' Then Return SetError(1, 0, _DateAdd('D', $iDays, $StartDate)) $iDays = Abs($iDays) Do $nJulianDate += $iDirection _DayValueToDate($nJulianDate, $aYMD[0], $aYMD[1], $aYMD[2]) $iWeekdays += (StringInStr($iWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2) ? 1 : 0) Until $iWeekdays = $iDays Return SetError(0, 0, $aYMD[0] & '/' & $aYMD[1] & '/' & $aYMD[2]) EndFunc ;==>_WorkDay Edited April 8, 2023 by Gianni noellarkin, robertocm and Skeletor 3 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
Gianni Posted April 11, 2023 Share Posted April 11, 2023 This function has the same functionality as the one in the above post, The only difference is that the one above counts the days in sequence until it reaches the required number of days (in a kind of brute force), while the latter calculates the target week and then only counts the last few days left. This way it is much faster especially for dates far from the initial one. (I also changed the function name from _WorkDay() to _DateAddWorkDay) I copied the calculation from <this post> by @Nine (thanks) if you find any bugs please report. expandcollapse popup#include <date.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateAddWorkDay ; Description ...: Calculates a new date by adding/subtracting a specified number of Days from an initial date, excluding from the count ; those days corresponding to specified weekdays. (By defaul Sundays and Saturdays are excluded from the count). ; You can freely choose the days of the week to be considered as non-working days. See the third parameter for this. ; Syntax ........: _DateAddWorkDay([$StartDate = _NowCalcDate([, $iDays = 0[, $sHolidays = "17"]]]) ; Parameters ....: $StartDate - Initial date in the format YYYY/MM/DD ; (no check is made on the correctness of the date provided) ; ; $iDays - Number of business days to add/subtract (use negative number to count back) ; a zero value indicates that the StartDate itself will be returned. ; (Indicating 0 the StartDate is returned even if this corresponds to a non-working day, ; but in this case the @error flag is set to 1. This can be useful if you need to check ; if a specified date is a working or not working day) ; ; $sHolidays - a string of digits indicating not working days of week. Default is "17". ; Indicate the days of the week to be considered as non-working days. ; (not to be considered in the count). $sHolidays represents a string with any combination ; of digits from 1 to 7 (the comma between the digits is optional). Digit correspond as follows: ; 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday. ; if you want to consider working all days of the week pass an empty string. ; (if, by contradiction, all seven days of the week are indicated as holidays, then the day ; pointed to by $Days is returned anyway, but the @error flag is also set to 1) ; ; Return values .: Target date in the "YYYY/MM/DD" format ; Author ........: Gianni ; Modified ......: ; ; Remarks .......: It is possible to force the function to return a date even if it belongs to a non-working day. ; This is possible by passing 0 as the second parameter and setting the start date to point to a non working day of the week. ; In this case, even if a holiday, that date is returned but the @error flag is set to 1. ; This could be used to check in one shot if a date belongs or not to a day of the week included in the third parameter. ; (also, by setting all seven days as holidays in the third parameter you will be rturned with the date pointed to by the ; second parameter, even if it's holiday, but also in this case the @error flag is set to 1) ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateAddWorkDay($StartDate = _NowCalcDate(), $iDays = 0, $sHolidays = "17") Local $iDirection = 1 * ($iDays < 0 ? -1 : $iDays > 0) Local $sWorkDays = StringRegExpReplace("1234567", "[" & StringRegExpReplace($sHolidays, "[^1234567]", "") & "]", "") Local $iWorkDays = StringLen($sWorkDays) If $iWorkDays = 7 Then Return SetError(1, 0, _DateAdd('D', $iDays, $StartDate)) Local $aYMD = StringSplit($StartDate, "/", 2) If Not $iDirection Then Return SetError(False = StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2), 0, $StartDate) Local $iNumWeek = Int(($iDays - $iDirection) / $iWorkDays) Local $iDaysRemainder = Mod($iDays - $iDirection, $iWorkDays) + $iDirection $iDays -= $iDaysRemainder $StartDate = _DateAdd("w", $iNumWeek, $StartDate) $aYMD = StringSplit($StartDate, "/", 2) Local $nJulianDate = _DateToDayValue($aYMD[0], $aYMD[1], $aYMD[2]) While $iDaysRemainder $nJulianDate += $iDirection _DayValueToDate($nJulianDate, $aYMD[0], $aYMD[1], $aYMD[2]) $iDaysRemainder -= (StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2) ? 1 : 0) * $iDirection WEnd Return SetError(0, 0, $aYMD[0] & '/' & $aYMD[1] & '/' & $aYMD[2]) EndFunc ;==>_DateAddWorkDay robertocm, Skeletor and noellarkin 3 Chimp small minds discuss people average minds discuss events great minds discuss ideas.... and use AutoIt.... Link to comment Share on other sites More sharing options...
argumentum Posted June 24, 2023 Share Posted June 24, 2023 (edited) ConsoleWrite('- IsDarkMode: ' & IsDarkMode() & @CRLF) Func IsDarkMode() Local $aCall = DllCall("user32.dll", "INT", "GetSysColor", "int", 15) ; $COLOR_3DFACE ; _WinAPI_GetSysColor() If @error Then Return SetError(@error, @extended, 0) If "0x" & StringMid(Hex($aCall[0], 6), 3, 2) < 0xC0 Then Return 1 EndFunc Not researched but a guesstimation. Works ok on my PC. Edit: use the next version bellow. Is better researched. Edited June 27, 2023 by argumentum Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
seadoggie01 Posted June 26, 2023 Share Posted June 26, 2023 On Windows 11, your function returns false for me, despite having dark mode on and my "custom accent color" set to #404040... though GetSysColor returns 00F0F0F0 🤨 HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\Personalize has SystemUsesLightTheme and AppsUseLightTheme, which are both set to 0 for me. Perhaps this would be a better check? Alternately, this works on Win11, not sure about 10 or previous. The theme path comes from HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Themes\CurrentTheme Func AppMode() return IniRead(@LocalAppDataDir & "\Microsoft\Windows\Themes\Custom.theme","VisualStyles", "AppMode", "Unknown") EndFunc argumentum 1 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
argumentum Posted June 26, 2023 Share Posted June 26, 2023 4 hours ago, seadoggie01 said: despite having dark mode ..we use win32 controls ( from Win95 onwards ) on our GUIs. The code I presented is to be used by other themes ( not quite welcomed by the OS ). So the declaration of windows itself for it's own use was not my idea, but rather to figure if the current theme as the colors that could be considered as dark, or dark enough. Had a battle with a user using these themes and I myself had a go at it without having to patch/alter the OS. But do thank you for your clarification. Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
seadoggie01 Posted June 26, 2023 Share Posted June 26, 2023 Perhaps then the brightness calculation here (specifically, the IsColorLight function) would be helpful? Essentially (((5 * Green) + (2 * Red) + Blue) > (8 * 128)); I've got no idea why it works, but stumbled on it earlier when I was looking for a "better" way to calculate color since your method didn't work for me I think it's a cool idea either way argumentum and TheDcoder 2 All my code provided is Public Domain... but it may not work. Use it, change it, break it, whatever you want. Spoiler My Humble Contributions:Personal Function Documentation - A personal HelpFile for your functionsAcro.au3 UDF - Automating Acrobat ProToDo Finder - Find #ToDo: lines in your scriptsUI-SimpleWrappers UDF - Use UI Automation more Simply-erKeePass UDF - Automate KeePass, a password managerInputBoxes - Simple Input boxes for various variable types Link to comment Share on other sites More sharing options...
argumentum Posted June 27, 2023 Share Posted June 27, 2023 (edited) #include-once ; if used as include #include <WinAPISysWin.au3> #include <WindowsConstants.au3> ConsoleWrite('- IsDarkMode: ' & IsDarkMode() & @CRLF) Func IsDarkMode() ; https://www.autoitscript.com/forum/topic/139260-autoit-snippets/?do=findComment&comment=1520332 ; https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getsyscolor Return (PerceivedBrightnessOfColor(_WinAPI_GetSysColor($COLOR_WINDOWTEXT)) _ > PerceivedBrightnessOfColor(_WinAPI_GetSysColor($COLOR_WINDOW))) ; if the text is perceived as brighter than the background then Return True EndFunc ;==>IsDarkMode Func PerceivedBrightnessOfColor($color) ; https://learn.microsoft.com/en-us/windows/apps/desktop/modernize/apply-windows-themes#know-when-dark-mode-is-enabled ; This function performs a quick calculation of the perceived brightness of a color, and takes into consideration ; ways that different channels in an RGB color value contribute to how bright it looks to the human eye. Local $a = StringSplit(Hex($color, 6), ""), $r = 2 * Dec($a[1] & $a[2]), $g = 5 * Dec($a[3] & $a[4]), $b = 1 * Dec($a[5] & $a[6]) Return ($r + $g + $b) EndFunc ;==>PerceivedBrightnessOfColor ..yes. If the perceived color is brighter than the background then it can be said to be in dark mode. Based on the link you shared, I cooked the above and it makes sense. The 5 + 2 + 1 = 8 makes sense too. All in all the above is a better approach than my 1st attempt at determining dark mode. Thanks for sharing @seadoggie01 Edited May 30 by argumentum seadoggie01 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
t0nZ Posted July 14, 2023 Share Posted July 14, 2023 On 4/11/2023 at 10:09 PM, Gianni said: This function has the same functionality as the one in the above post, The only difference is that the one above counts the days in sequence until it reaches the required number of days (in a kind of brute force), while the latter calculates the target week and then only counts the last few days left. This way it is much faster especially for dates far from the initial one. (I also changed the function name from _WorkDay() to _DateAddWorkDay) I copied the calculation from <this post> by @Nine (thanks) if you find any bugs please report. expandcollapse popup#include <date.au3> ; #FUNCTION# ==================================================================================================================== ; Name ..........: _DateAddWorkDay ; Description ...: Calculates a new date by adding/subtracting a specified number of Days from an initial date, excluding from the count ; those days corresponding to specified weekdays. (By defaul Sundays and Saturdays are excluded from the count). ; You can freely choose the days of the week to be considered as non-working days. See the third parameter for this. ; Syntax ........: _DateAddWorkDay([$StartDate = _NowCalcDate([, $iDays = 0[, $sHolidays = "17"]]]) ; Parameters ....: $StartDate - Initial date in the format YYYY/MM/DD ; (no check is made on the correctness of the date provided) ; ; $iDays - Number of business days to add/subtract (use negative number to count back) ; a zero value indicates that the StartDate itself will be returned. ; (Indicating 0 the StartDate is returned even if this corresponds to a non-working day, ; but in this case the @error flag is set to 1. This can be useful if you need to check ; if a specified date is a working or not working day) ; ; $sHolidays - a string of digits indicating not working days of week. Default is "17". ; Indicate the days of the week to be considered as non-working days. ; (not to be considered in the count). $sHolidays represents a string with any combination ; of digits from 1 to 7 (the comma between the digits is optional). Digit correspond as follows: ; 1=Sunday, 2 =Monday , 3=Tuesday, 4=Wednesday, 5=Thursday, 6=Friday, 7=Saturday. ; if you want to consider working all days of the week pass an empty string. ; (if, by contradiction, all seven days of the week are indicated as holidays, then the day ; pointed to by $Days is returned anyway, but the @error flag is also set to 1) ; ; Return values .: Target date in the "YYYY/MM/DD" format ; Author ........: Gianni ; Modified ......: ; ; Remarks .......: It is possible to force the function to return a date even if it belongs to a non-working day. ; This is possible by passing 0 as the second parameter and setting the start date to point to a non working day of the week. ; In this case, even if a holiday, that date is returned but the @error flag is set to 1. ; This could be used to check in one shot if a date belongs or not to a day of the week included in the third parameter. ; (also, by setting all seven days as holidays in the third parameter you will be rturned with the date pointed to by the ; second parameter, even if it's holiday, but also in this case the @error flag is set to 1) ; Related .......: ; Link ..........: ; Example .......: No ; =============================================================================================================================== Func _DateAddWorkDay($StartDate = _NowCalcDate(), $iDays = 0, $sHolidays = "17") Local $iDirection = 1 * ($iDays < 0 ? -1 : $iDays > 0) Local $sWorkDays = StringRegExpReplace("1234567", "[" & StringRegExpReplace($sHolidays, "[^1234567]", "") & "]", "") Local $iWorkDays = StringLen($sWorkDays) If $iWorkDays = 7 Then Return SetError(1, 0, _DateAdd('D', $iDays, $StartDate)) Local $aYMD = StringSplit($StartDate, "/", 2) If Not $iDirection Then Return SetError(False = StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2), 0, $StartDate) Local $iNumWeek = Int(($iDays - $iDirection) / $iWorkDays) Local $iDaysRemainder = Mod($iDays - $iDirection, $iWorkDays) + $iDirection $iDays -= $iDaysRemainder $StartDate = _DateAdd("w", $iNumWeek, $StartDate) $aYMD = StringSplit($StartDate, "/", 2) Local $nJulianDate = _DateToDayValue($aYMD[0], $aYMD[1], $aYMD[2]) While $iDaysRemainder $nJulianDate += $iDirection _DayValueToDate($nJulianDate, $aYMD[0], $aYMD[1], $aYMD[2]) $iDaysRemainder -= (StringInStr($sWorkDays, _DateToDayOfWeek($aYMD[0], $aYMD[1], $aYMD[2]), 2) ? 1 : 0) * $iDirection WEnd Return SetError(0, 0, $aYMD[0] & '/' & $aYMD[1] & '/' & $aYMD[2]) EndFunc ;==>_DateAddWorkDay Very interesting, I think we can add also national holidays to the count, excluding these days. To obtain a list of updated national holidays there are some services online, but for a quick test I asked chatgpt: 20230101,Capodanno 20230106,Epifania 20230409,Domenica di Pasqua 20230416,Lunedì dell'Angelo 20230425,Festa della Liberazione 20230501,Festa dei Lavoratori 20230602,Festa della Repubblica 20230815,Ferragosto 20231101,Ognissanti 20231208,Immacolata Concezione 20231225,Natale I am thinking to work on this... robertocm 1 Link to comment Share on other sites More sharing options...
Popular Post UEZ Posted July 24, 2023 Popular Post Share Posted July 24, 2023 (edited) _WinAPI_SetDPIAwareness.au3 expandcollapse popup;Coded by UEZ build 2023-07-25 beta ;To get it working properly, please compile script first and start the exe afterwards #AutoIt3Wrapper_Res_HiDpi=y #AutoIt3Wrapper_UseX64=n #AutoIt3Wrapper_Change2CUI=y #include <GUIConstantsEx.au3> #include <Misc.au3> #include <WinAPIGdiDC.au3> #include <WinAPISysWin.au3> #include <WindowsConstants.au3> #Region _WinAPI_SetDPIAwareness ;https://learn.microsoft.com/en-us/windows/win32/api/windef/ne-windef-dpi_awareness Global Enum $DPI_AWARENESS_INVALID = -1, $DPI_AWARENESS_UNAWARE = 0, $DPI_AWARENESS_SYSTEM_AWARE = 1, $DPI_AWARENESS_PER_MONITOR_AWARE = 2 ;https://learn.microsoft.com/en-us/windows/win32/hidpi/dpi-awareness-context Global Const $DPI_AWARENESS_CONTEXT_UNAWARE = $DPI_AWARENESS_UNAWARE - 1 Global Const $DPI_AWARENESS_CONTEXT_SYSTEM_AWARE = $DPI_AWARENESS_UNAWARE - 2 Global Const $DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE = $DPI_AWARENESS_UNAWARE - 3 Global Const $DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2 = $DPI_AWARENESS_UNAWARE - 4 Global Const $DPI_AWARENESS_CONTEXT_UNAWARE_GDISCALED = $DPI_AWARENESS_UNAWARE - 5 ; enum _MONITOR_DPI_TYPE Global Enum $MDT_EFFECTIVE_DPI = 0, $MDT_ANGULAR_DPI, $MDT_RAW_DPI Global Const $MDT_DEFAULT = $MDT_EFFECTIVE_DPI Global Const $WM_DPICHANGED = 0x02E0 ;https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-adjustwindowrectexfordpi Func _WinAPI_AdjustWindowRectExForDpi($dpi, $dwStyle, $dwExStyle, $bMenu = False) Local $tRECT = DllStructCreate($tagRECT) Local $aResult = DllCall("user32.dll", "bool", "AdjustWindowRectExForDpi", "struct*", $tRECT, "dword", $dwStyle, "bool", $bMenu, "dword", $dwExStyle, "uint", $dpi) ;requires Win10 v1607+ / no server support If @error Then Return SetError(@error, @extended, 0) Return $tRECT EndFunc ;==>_WinAPI_AdjustWindowRectExForDpi Func _WinAPI_GetDPI($hWnd = _WinAPI_GetDesktopWindow()) Local Const $hDC = _WinAPI_GetDC($hWnd) If @error Then Return SetError(1, 0, 0) Local Const $iDPI = _WinAPI_GetDeviceCaps($hDC, $LOGPIXELSX) If @error Or Not $iDPI Then _WinAPI_ReleaseDC($hWnd, $hDC) Return SetError(2, 0, 0) EndIf _WinAPI_ReleaseDC($hWnd, $hDC) Return $iDPI EndFunc ;==>_WinAPI_GetDPI ;https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-getdpiforwindow Func _WinAPI_GetDpiForWindow($hWnd) Local $aResult = DllCall("user32.dll", "uint", "GetDpiForWindow", "hwnd", $hWnd) ;requires Win10 v1607+ / no server support If @error Then Return SetError(@error, @extended, 0) Return $aResult[0] EndFunc ;==>_WinAPI_GetDpiForWindow ;https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setprocessdpiawarenesscontext Func _WinAPI_SetProcessDpiAwarenessContext($DPI_AWARENESS_CONTEXT_value) Local $aResult = DllCall("user32.dll", "bool", "SetProcessDpiAwarenessContext", "int", $DPI_AWARENESS_CONTEXT_value) ;requires Win10 v1703+ / Windows Server 2016+ If @error Then Return SetError(@error, @extended, 0) Return $aResult[0] EndFunc ;==>_WinAPI_SetProcessDpiAwarenessContext ;https://learn.microsoft.com/en-us/windows/win32/api/winuser/nf-winuser-setthreaddpiawarenesscontext Func _WinAPI_SetThreadDpiAwarenessContext($DPI_AWARENESS_CONTEXT_value) Local $aResult = DllCall("user32.dll", "bool", "SetThreadDpiAwarenessContext", "int", $DPI_AWARENESS_CONTEXT_value) ;requires Win10 v1703+ / Windows Server 2016+ If @error Then Return SetError(@error, @extended, 0) Return $aResult[0] EndFunc ;==>_WinAPI_SetThreadDpiAwarenessContext ;https://learn.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-setprocessdpiawareness Func _WinAPI_SetProcessDpiAwareness($PROCESS_DPI_AWARENESS = $DPI_AWARENESS_PER_MONITOR_AWARE) Local $iResult = DllCall("Shcore.dll", "long", "SetProcessDpiAwareness", "int", $PROCESS_DPI_AWARENESS) ;requires Win 8.1+ / Server 2012 R2+ If @error Or $iResult Then Return SetError(1, 0, 0) Return 1 EndFunc ;==>_WinAPI_SetProcessDpiAwareness Func _WinAPI_SetDPIAwareness($DPIAwareContext = $DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE, $iMode = 1) Switch @OSBuild Case 6000 To 9199 If Not DllCall("user32.dll", "bool", "SetProcessDPIAware") Then Return SetError(1, 0, 0) Case 9200 To 13999 $DPIAwareContext = ($DPIAwareContext < 0) ? 0 : ($DPIAwareContext > 2) ? 2 : $DPIAwareContext _WinAPI_SetProcessDpiAwareness($DPIAwareContext) If @error Then Return SetError(2, @error, 0) Case @OSBuild > 13999 $DPIAwareContext = ($DPIAwareContext < -5) ? -5 : ($DPIAwareContext > -1) ? -1 : $DPIAwareContext $iMode = ($iMode < 1) ? 1 : ($iMode > 2) ? 2 : $iMode Switch $iMode Case 1 _WinAPI_SetProcessDpiAwarenessContext($DPIAwareContext) If @error Then Return SetError(3, 0, 0) Case 2 _WinAPI_SetThreadDpiAwarenessContext($DPIAwareContext) If @error Then Return SetError(4, 0, 0) EndSwitch EndSwitch If @OSBuild < 14393 Then $iDPI = _WinAPI_GetDPI() If @error Then Return SetError(5, 0, 0) Else Local Const $hGUI_dummy = GUICreate("", 1, 1, 0, 0) $iDPI = _WinAPI_GetDpiForWindow($hGUI_dummy) GUIDelete($hGUI_dummy) If @error Then Return SetError(6, 0, 0) EndIf Return $iDPI EndFunc ;==>_WinAPI_SetDPIAwareness #EndRegion Global $dpiScaledX, $dpiScaledY, $aCtrlFS[5][2], $hGUI, $hGUI_child, $g_iDPI_ratio2, $iLable_child, $iPic_child, _ $sImage = "c:\Program Files (x86)\AutoIt3\Examples\GUI\Merlin.gif" Example1() Func Example1() Local $iDPI = _WinAPI_SetDPIAwareness($DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2) If @error Or Not $iDPI Then Exit MsgBox($MB_ICONERROR, "ERROR", "Cannot set DPI awareness!", 10) ConsoleWrite("DPI read: " & $iDPI & @CRLF) Local $iDPI_ratio = $iDPI / 96 $g_iDPI_ratio2 = 96 / $iDPI $hGUI = GUICreate("Example 1", 314 * $iDPI_ratio, 130 * $iDPI_ratio, -1, 10) GUISetFont(12 * $iDPI_ratio, 400, 0, "Times New Roman") $aCtrlFS[0][0] = GUICtrlCreateLabel("Label1", 16 * $iDPI_ratio, 16 * $iDPI_ratio, 40 * $iDPI_ratio, 21 * $iDPI_ratio) $aCtrlFS[0][1] = 10 GUICtrlSetBkColor(-1, 0x3399FF) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $aCtrlFS[1][0] = GUICtrlCreateLabel("Label2", 64 * $iDPI_ratio, 16 * $iDPI_ratio, 40 * $iDPI_ratio, 21 * $iDPI_ratio) $aCtrlFS[1][1] = 10 GUICtrlSetBkColor(-1, 0x3399FF) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $aCtrlFS[2][0] = GUICtrlCreateLabel("Label3", 112 * $iDPI_ratio, 16 * $iDPI_ratio, 40 * $iDPI_ratio, 21 * $iDPI_ratio) $aCtrlFS[2][1] = 10 GUICtrlSetBkColor(-1, 0x3399FF) GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $aCtrlFS[3][0] = GUICtrlCreateInput("Input1", 160 * $iDPI_ratio, 16 * $iDPI_ratio, 137 * $iDPI_ratio, 22 * $iDPI_ratio) $aCtrlFS[3][1] = 10 GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $aCtrlFS[4][0] = GUICtrlCreateButton("Close", 16 * $iDPI_ratio, 48 * $iDPI_ratio, 283 * $iDPI_ratio, 65 * $iDPI_ratio) $aCtrlFS[4][1] = 16 GUICtrlSetResizing(-1, $GUI_DOCKAUTO) $hGUI_child = GUICreate("Child", 320 * $iDPI_ratio, 260 * $iDPI_ratio, -1, -1, -1, -1, $hGUI) GUISetBkColor(0xFFFFFF) $iLable_child = GUICtrlCreateLabel("Label11", 16, 16, 288 * $iDPI_ratio, 168 * $iDPI_ratio) GUICtrlSetFont(-1, 65, 400, 0, "Times New Roman", 5) $iPic_child = GUICtrlCreatePic($sImage, 0, 160 * $iDPI_ratio, 68 * $iDPI_ratio, 71 * $iDPI_ratio) ResizeFont($hGUI) GUISetState(@SW_SHOW, $hGUI) GUISetState(@SW_SHOW, $hGUI_child) GUIRegisterMsg($WM_DPICHANGED, "WM_DPICHANGED") While True Switch GUIGetMsg() Case $GUI_EVENT_CLOSE, $aCtrlFS[4][0] GUIDelete($hGUI_child) GUIDelete($hGUI) ExitLoop EndSwitch WEnd EndFunc ;==>Example1 Func ResizeFont($hWnd) Local $iDPI = _WinAPI_GetDpiForWindow($hWnd), $dpi_ratio = $iDPI / 96 Switch $hWnd Case $hGUI Local $i For $i = 0 To UBound($aCtrlFS) - 1 GUICtrlSetFont($aCtrlFS[$i][0], $aCtrlFS[$i][1] * $dpi_ratio * $g_iDPI_ratio2, 400, 0, "Times New Roman", 5) Next Case $hGUI_child GUICtrlSetFont($iLable_child, 65 * $dpi_ratio * $g_iDPI_ratio2, 400, 0, "Times New Roman", 5) EndSwitch EndFunc ;==>ResizeFont Func WM_DPICHANGED($hWnd, $iMsg, $wParam, $lParam) #forceref $iMsg, $wParam Local $dpi_ratio2 = 96 / _WinAPI_LoWord($wParam) Local $dpi_ratio = _WinAPI_LoWord($wParam) / 96 Local $tRECT = DllStructCreate($tagRECT, $lParam) Local $iX = $tRECT.left, $iY = $tRECT.top, $iW = $tRECT.right - $iX, $iH = $tRECT.bottom - $iY _WinAPI_AdjustWindowRectExForDpi(_WinAPI_HiWord($wParam), BitOR($WS_BORDER, $WS_CAPTION, $WS_CLIPSIBLINGS, $WS_GROUP, $WS_POPUP, $WS_SYSMENU, $WS_VISIBLE), $WS_EX_WINDOWEDGE) _WinAPI_SetWindowPos($hWnd, 0, $iX, $iY, $iW, $iH, BitOR($SWP_NOZORDER, $SWP_NOACTIVATE)) ResizeFont($hWnd) Local $aPos = ControlGetPos($hWnd, "", $iPic_child) If UBound($aPos) = 4 Then _WinAPI_MoveWindow(GUICtrlGetHandle($iPic_child), $aPos[0] * $dpi_ratio2 * $g_iDPI_ratio2, $aPos[1] * $dpi_ratio2 * $g_iDPI_ratio2, $aPos[2] * $dpi_ratio * $g_iDPI_ratio2, $aPos[3] * $dpi_ratio * $g_iDPI_ratio2) GUICtrlSetImage($iPic_child, $sImage) EndIf $tRECT = 0 ConsoleWrite("DPI change to " & _WinAPI_LoWord($wParam) & " detected and applied to the GUI elements" & @CRLF) Return 1 EndFunc ;==>WM_DPICHANGED Edited July 25, 2023 by UEZ Changed to a single call robertocm, ioa747, kurtykurtyboy and 2 others 3 2 Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
TheDcoder Posted July 24, 2023 Share Posted July 24, 2023 @UEZ Aren't these in AutoIt's standard UDFs? If not, maybe you should make a ticket to get those added in EasyCodeIt - A cross-platform AutoIt implementation - Fund the development! (GitHub will double your donations for a limited time) DcodingTheWeb Forum - Follow for updates and Join for discussion Link to comment Share on other sites More sharing options...
UEZ Posted July 25, 2023 Share Posted July 25, 2023 11 hours ago, TheDcoder said: @UEZ Aren't these in AutoIt's standard UDFs? If not, maybe you should make a ticket to get those added in Obviously not. 😉 I tested it only on my Win11 notebook. I don't know if the test script will work properly on other operating systems. Please don't send me any personal message and ask for support! I will not reply! Selection of finest graphical examples at Codepen.io The own fart smells best! ✌Her 'sikim hıyar' diyene bir avuç tuz alıp koşma!¯\_(ツ)_/¯ ٩(●̮̮̃•̃)۶ ٩(-̮̮̃-̃)۶ૐ Link to comment Share on other sites More sharing options...
argumentum Posted July 25, 2023 Share Posted July 25, 2023 (edited) ..on my side, I'll be putting something like this: Spoiler <removed by user> Edit: The code got cleaned up and posted as an example at: https://www.autoitscript.com/forum/topic/210613-guictrls_hidpi-udf-in-progress/ to simply add water a prefix _HiDpi_GUICtrlCreateSomething() and make it instantly coffee HiDpi ready ...unless @TheDcoder jumps the gun and gets it done. It'll take me time to get this functional. But the easier the addendum ( so to call it ), the more testers we'll have to know if it ( @UEZ's code ) works everywhere Edited July 28, 2023 by argumentum better code TheDcoder 1 Follow the link to my code contribution ( and other things too ). FAQ - Please Read Before Posting. Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now