Chimaera

AutoIt Snippets

330 posts in this topic

#321 ·  Posted (edited)

reducing it to 2 passes with no regex and accounting for ridiculous edge cases. I am oddly proud of this one, but I am disappointed in the lack of competition. Come get some, you are being judged on simplicity and understandability (Dcoder's rules).

 

edit:  meant to write that on the last post, but cant delete stuff so...

Edited by iamtheky

,-. .--. ________ .-. .-. ,---. ,-. .-. .-. .-.
|(| / /\ \ |\ /| |__ __||| | | || .-' | |/ / \ \_/ )/
(_) / /__\ \ |(\ / | )| | | `-' | | `-. | | / __ \ (_)
| | | __ | (_)\/ | (_) | | .-. | | .-' | | \ |__| ) (
| | | | |)| | \ / | | | | | |)| | `--. | |) \ | |
`-' |_| (_) | |\/| | `-' /( (_)/( __.' |((_)-' /(_|
'-' '-' (__) (__) (_) (__)

Share this post


Link to post
Share on other sites



I was busy so didn't bother, I will play around with your code today (if I have the time)


AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#323 ·  Posted (edited)

load a theme/deskthemepack in Win10 from a command line is a pain an unfriendly affair, so I resorted to this:

If Int(FileGetVersion("winver.exe")) = 10 Then ; this is for Win10
;~  ShellExecute("shell:::{ED834ED6-4B5A-4bfe-8F11-A626DCB6A921}")
    ShellExecute("\\WPACS\share$\[w10customize]\defTheme.deskthemepack") ; your path
    If WinWait("[CLASS:CabinetWClass;]","",10) Then WinClose("[CLASS:CabinetWClass;]")
EndIf

Shared in hopes it will help someone.

Edited by argumentum
nicer code

Share this post


Link to post
Share on other sites

#324 ·  Posted (edited)

Implode or explode your GUI from anywhere on your screen:  holding the mouse pressed on the GUI while dragging will resize the GUI preserving it's dimension
 

#include <GuiconstantsEx.au3>
#include <WinAPISys.au3>
#include <WinAPIGdi.au3>
#include <Misc.au3>

Opt("GUIResizeMode", 904)

$hDLL = DllOpen("user32.dll")
OnAutoItExitRegister("On_Exit")

Global $iWidth = 380, $iHeight = 180

Global $hGUI = GUICreate("X", $iWidth, $iHeight, -1, -1)
GUISetBkColor(0X5c6e8c, $hGUI)

$iWidth = _WinAPI_GetClientWidth($hGUI)
$iHeight = _WinAPI_GetClientHeight($hGUI)
$ButtonWidth = 40
$ButtonHeight = 20
$idnew = GUICtrlCreateButton("Change Dimension", ($iWidth / 2) - (3 * $ButtonWidth / 2), ($iHeight / 2) - (4 * $ButtonHeight / 2), 3 * $ButtonWidth, $ButtonHeight)
$idCenter = GUICtrlCreateButton("x", ($iWidth / 2) - ($ButtonWidth / 2), ($iHeight / 2) - ($ButtonHeight / 2), $ButtonWidth, $ButtonHeight)
GUICtrlSetFont($idCenter, 10, 200)

Global $PM = _WinAPI_MonitorFromWindow($hGUI)

DllCall("user32.dll", "int", "AnimateWindow", "hwnd", $hGUI, "int", 500, "long", 0x00040010)
GUISetState()

$aGPos = WinGetPos($hGUI)
$iWidth = $aGPos[2]
$iHeight = $aGPos[3]

While 1
    Switch GUIGetMsg()
        Case $GUI_EVENT_close
            Exit
        Case $idnew
            $iWidth = Random(150, 550, 1)
            $iHeight = Random(150, 550, 1)
            _NewDimension($iWidth, $iHeight)
        Case $idCenter
            _CenterToScreen()
        Case $GUI_EVENT_PRIMARYDOWN
            OnDrag()
    EndSwitch
WEnd

Func OnDrag()
    Local $aPos, $iRoll, $X, $Y, $width, $height, $PH, $PW = 0, $aCurInfo = GUIGetCursorInfo($hGUI)
    If $aCurInfo[4] <> 0 Then Return ; Mouse is over a control
    Local $aGPos = WinGetPos($hGUI)
    Local $aMPos = MouseGetPos()
    Local $MON = _WinAPI_MonitorFromWindow($hGUI)
    Local $aMax = MonitorGetRect($MON)
    If $PM <> $MON Then
        $PM = $MON
        Return _CenterToScreen(1)
    EndIf

    If _SnapToScreen($aGPos, $aMax) Then Return WinMove($hGUI, "", $aGPos[0], $aGPos[1], $aGPos[2], $aGPos[3])

    $width = $aGPos[2]
    $height = $aGPos[3]

    While _IsPressed("01", $hDLL)
        $aPos = MouseGetPos()
        $iRoll = (($aMPos[1] - $aPos[1]) - ($aMPos[0] - $aPos[0])) * 100 / 50

        $height = ($aGPos[3] / $aGPos[2]) * ($aGPos[2] + $iRoll)
        If $height < $iHeight Then ExitLoop
        If $height >= $aMax[3] Then
            Do
                $iRoll -= 1
                $height = Round(($aGPos[3] / $aGPos[2]) * ($aGPos[2] + $iRoll))
            Until $height <= $aMax[3]
        EndIf

        $width = ($aGPos[2] / $aGPos[3]) * $height
        If $width >= ($aMax[2] - $aMax[0]) Then
            Do
                $iRoll -= 1
                $height = Round(($aGPos[3] / $aGPos[2]) * ($aGPos[2] + $iRoll))
                $width = ($aGPos[2] / $aGPos[3]) * $height
            Until $width <= ($aMax[2] - $aMax[0])
        EndIf

        ;Store as Previous Width & Height
        $PW = $width
        $PH = $height

        $X = Round((($aGPos[2] / 2) + $aGPos[0]) - ($width / 2))
        $Y = Round((($aGPos[3] / 2) + $aGPos[1]) - ($height / 2))

        If $X <= $aMax[0] Then
            $X = $aMax[0]
        ElseIf ($X + $width) >= $aMax[2] Then
            $X = ($aMax[2] - $width)
        EndIf

        If $Y <= $aMax[1] Then
            $Y = $aMax[1]
        ElseIf ($Y + $height) >= $aMax[3] Then
            $Y = ($aMax[3] - $height)
        EndIf

        WinMove($hGUI, "", $X, $Y, $width, $height)

        Sleep(20)
    WEnd
    If $height < $iHeight And $PW <> 0 Then
        $tRect = _WinAPI_GetWindowRect($hGUI)
        Return WinMove($hGUI, "", (($PW / 2) + DllStructGetData($tRect, "Left")) - ($iWidth / 2), Ceiling(($PH / 2) + DllStructGetData($tRect, "Top")) - ($iHeight / 2), $iWidth, $iHeight)
    EndIf
EndFunc   ;==>OnDrag

Func _SnapToScreen(ByRef $aGPos, ByRef $aMax) ; Snap the gui back to its full view when moved off screen
    Local $Move = False
    If $aGPos[0] < $aMax[0] Then
        $aGPos[0] = $aMax[0]
        $Move = True
    ElseIf ($aGPos[0] + $aGPos[2]) > $aMax[2] Then
        $aGPos[0] = ($aMax[2] - $aGPos[2])
        $Move = True
    EndIf
    If $aGPos[1] < $aMax[1] Then
        $aGPos[1] = $aMax[1]
        $Move = True
    ElseIf ($aGPos[1] + $aGPos[3]) > $aMax[3] Then
        $aGPos[1] = ($aMax[3] - $aGPos[3])
        $Move = True
    EndIf
    If $Move Then Return True
EndFunc   ;==>_SnapToScreen

Func _NewDimension($width, $height)
    Local $aPos = WinGetPos($hGUI)
    Return WinMove($hGUI, "", ($aPos[2] / 2) + $aPos[0] - ($width / 2), ($aPos[3] / 2) + $aPos[1] - ($height / 2), $width, $height)
EndFunc   ;==>_NewDimension

Func _CenterToScreen($Move =False)
    Local $aMax = MonitorGetRect(_WinAPI_MonitorFromWindow($hGUI)) ; Center & Resize the gui dimension if off screen
    Local $aGPos = WinGetPos($hGUI)
    Local $check = $aGPos[2]
    If $aGPos[3] > ($aMax[3] - $aMax[1]) Then
        $aGPos[2] = Round(($aGPos[2] / $aGPos[3]) * ($aMax[3] - $aMax[1])) ;New Width
        $aGPos[3] = $aMax[3] - $aMax[1] ;New Height
    EndIf
    If $aGPos[2] > ($aMax[2] - $aMax[0]) Then
        $aGPos[3] = Round(($aGPos[3] / $aGPos[2]) * ($aMax[2] - $aMax[0])) ;New Height
        $aGPos[2] = $aMax[2] - $aMax[0] ;New Width
    EndIf
    If $Move and $check = $aGPos[2] then Return
    Return WinMove($hGUI, "", (($aMax[2] + $aMax[0]) / 2) - ($aGPos[2] / 2), (($aMax[1] + $aMax[3]) / 2) - ($aGPos[3] / 2), $aGPos[2], $aGPos[3])
EndFunc   ;==>_CenterToScreen

Func MonitorGetRect($hMonitor)
    Local $aData = _WinAPI_GetMonitorInfo($hMonitor)
    Local $s = DllStructGetData($aData[1], 1) & ',' & DllStructGetData($aData[1], 2) & ',' & DllStructGetData($aData[1], 3) & ',' & DllStructGetData($aData[1], 4)
    Local $a = StringSplit($s, ',', 2)
    Return $a
EndFunc   ;==>MonitorGetRect

Func On_Exit()
    DllClose($hDLL)
EndFunc   ;==>On_Exit

 

Edited by Deye
Updated

Share this post


Link to post
Share on other sites

#325 ·  Posted (edited)

<snip>

Here try this method

Edited by JLogan3o13
Removed YouTube Video

Share this post


Link to post
Share on other sites

@Namber1 this thread is to share your own snippets, not random Non-English videos from YouTube.


√-1 2^3 ∑ π, and it was delicious!

Share this post


Link to post
Share on other sites

@Namber1,I have actually already removed 3 of those video posts in other threads so YES, stop plugging the video or else will impose a posting ban.

Jos 


Visit the SciTE4AutoIt3 Download page for the latest versions  - Beta files                                How to post scriptsource        Forum Rules
 
Live for the present,
Dream of the future,
Learn from the past.
  :)

Share this post


Link to post
Share on other sites

#328 ·  Posted (edited)

This functions lets you check if a window is responding or not. Equal to checking if a Window goes white after a while using your eye :P

#include <WinAPISys.au3>

; #FUNCTION# ====================================================================================================================
; Name ..........: IsWindowNotResponding
; Description ...: Checks if a Window is not responding
; Syntax ........: IsWindowNotResponding($hWindow[, $iTimeout = 5000])
; Parameters ....: $hWindow             - A window handle.
;                  $iTimeout            - [optional] Shouldn't matter, Timeout in milliseconds. Default is 5000.
; Return values .: @error set by _WinAPI_SendMessageTimeout
; Author ........: Damon Harris (TheDcoder)
; Remarks .......: The way it works is that it exploits SendMessageTimeout's SMTO_ABORTIFHUNG option.
;                  Do more research on Process.Responding and how it works (C# function for checking if a window is responding)
; Link ..........: https://git.io/vbcvJ
; Example .......: If IsWindowNotResponding($hWindow) Then DoSomething()
; ===============================================================================================================================
Func IsWindowNotResponding($hWindow, $iTimeout = 5000)
    _WinAPI_SendMessageTimeout($hWindow, 0, 0, 0, $iTimeout, $SMTO_ABORTIFHUNG)
    Return @error
EndFunc
Edited by TheDcoder
1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

I have a hack this time, not a function: Generate custom/fake/pseudo events in OnEvent mode in AutoIt

; This snippet will show you a "hack" that you can use in GUI OnEvent mode in AutoIt
; With this hack you can create fake/pseudo events for a control in a OnEvent "handler"
; function which is registered with multiple controls.

; Usually you can get around this by using a unique function for every control
; but I like to use a single function that I use as a handler for a group of related
; controls. Looks neat and organized :)

; Let's get started!

; Include the important stuff
#include <AutoItConstants.au3>
#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>

Opt("GUIOnEventMode", 1)

; This function will create the GUI and make it visible
Func CreateGUI()
    GUICreate("Example", 60, 60)
    ; Functions with parameters will not be called, unless all of their parameters are optional (undocumented)
    GUISetOnEvent($GUI_EVENT_CLOSE, HandleGUI)
    Global $idToggleButton = GUICtrlCreateButton("", 5, 05, 50, 50)
    GUICtrlSetOnEvent($idToggleButton, HandleGUI)
EndFunc

; This is our most important piece of code, the Handler function which handles
; any events generated by the GUI!
Func HandleGUI($iCtrlID = Default)
    ; As mentioned previously, when a function is called by AutoIt's OnEvent handler,
    ; the optional parameters are NOT defined at all! So their default values are meaningless
    ; Therefore we can use IsDeclared to see if a parameter is declared, effective way to know
    ; if it was called by AutoIt's OnEvent handler.
    ;
    ; The switch expression is a ternary operation, it first checkes if $iCtrlID is defined,
    ; then if it is declared locally, the expression is evaluated to $iCtrlID's value. This
    ; would be the case when a psedo/fake event has been generated by the script manually
    ;
    ; If it was called by AutoIt, then the expression would evaluate to @GUI_CtrlID
    ;
    ; This allows for seamless integration with the code, you won't have to use ControlClick to
    ; create a mouse press or something similar!
    Switch (IsDeclared("iCtrlID") = $DECLARED_LOCAL ? $iCtrlID : @GUI_CtrlId)
        Case $GUI_EVENT_CLOSE
            Exit
        Case $idToggleButton
            Local Static $bOn = True
            If $bOn Then
                GUICtrlSetData($idToggleButton, "Off")
                $bOn = False
            Else
                GUICtrlSetData($idToggleButton, "On")
                $bOn = True
            EndIf
    EndSwitch
EndFunc

; A practical use case:
; Often when there is a button which toggles/switches between values, the default value will have
; to be repeated when creating the control, this is fine for small things but is a good way to do
; if you have something more that needs to be done. This is just a simple example of a button which
; toggles between On and Off. The speciality is that those values will not be repeated in the code :)
;
; This is not very easy to read or as simple as setting the value of the button to the default state
; during the creation of the GUI, but it is a good practise that helps you avoid replicating text

CreateGUI()

HandleGUI($idToggleButton) ; This will generate a fake event which will trigger the same action as clicking on the button

GUISetState() ; Show the GUI

While True
    Sleep(10)
WEnd

; Link to this code snippet on Gist: https://git.io/vbRQ5

 

1 person likes this

AutoIt.4.Life Clubrooms - Life is like a Donut (secret key)

Spoiler

My contributions to the AutoIt Community

Some messages & Apologizes:

If I hurt you, Please accept my apologies, I never (regardless of the situation) mean to hurt anybody!!!

Also, I am very busy with my project so I will appear in the last row of the online list, if you want to contact me: Email@TheDcoder.xyz

Or you can have a nice chat with me in freenode, I use the same nick on freenode too!

3fHNZJ.gif

PLEASE JOIN ##AutoIt AND HELP THE IRC AUTOIT COMMUNITY!

Share this post


Link to post
Share on other sites

#330 ·  Posted (edited)

This function offers a simple way to roughly calibrate the speed of animations on various computers and OSes. It calculates the number of loops iterated within a given number of milliseconds.

#AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w- 4 -w 5 -w 6 -w- 7

Global $iAnimSpeed
_SetAnimSpeed()
AdlibRegister("_SetAnimSpeed", 300000) ;wait 5 minutes for Windows startup to finish, then every 5 minutes, recalibrate animation speed

Func _SetAnimSpeed()
    $iAnimSpeed = _CalibrateLoopTime(1000, 1) ;get the number of loops within a second with 1 Sleep()
    ;AdlibUnRegister("_SetAnimSpeed") ;uncomment this line if you only want to recalibrate once
EndFunc   ;==>_SetAnimSpeed

ConsoleWrite("On this computer, 1 second of animation = " & $iAnimSpeed & " for-next loops, including 1 sleep." & @CRLF)

; #FUNCTION# ====================================================================================================================
; Name ..........: _CalibrateLoopTime
; Description ...: Calculates the number of loops iterated within a given number of milliseconds. A simple way to *roughly*
;                   calibrate the speed of animations on various computers and OSes.
; Syntax ........: _CalibrateLoopTime($iMilliseconds[, $iSleep = 0])
; Parameters ....: $iMilliseconds       - number of milliseconds to calibrate. Longer is more accurate, shorter is less disruptive.
;                   Remember to divide or multiply to get the length of the animation.
;                  $iSleep              - [optional] manually adjust this to correct for the how fast animation gets generated.
;                   Default is 0.
; Return value ..: integer number of loops within given milliseconds
; Author ........: Tim Curran (tim/at/timcurran/dot/com)
; Modified ......:
; Remarks .......: The longer the Sleep() adjustment, the less accurate this calibration, as computer speed performing the task
;                   (e.g. building the animation) becomes a greater factor.
; Example .......: See above.
; ===============================================================================================================================

Func _CalibrateLoopTime($iMilliseconds, $iSleep = 0)
    Local $iLoopCounter, $hCaliTimer
    $hCaliTimer = TimerInit()
    Do
        $iLoopCounter += 1
        Sleep($iSleep)
    Until TimerDiff($hCaliTimer) >= $iMilliseconds
    Return $iLoopCounter
EndFunc   ;==>_CalibrateLoopTime

 

Edited by tcurran
1 person likes this

Share this post


Link to post
Share on other sites

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 account

Sign in

Already have an account? Sign in here.


Sign In Now