Jump to content

GUIGetMsg() ... and Mouse movement.


MvGulik
 Share

Recommended Posts

Dug up some old code that also good in Showing the effect of mouse movements in relation to GUIGetMsg()'s behavior. (or TrayGetMsg() for that matter.)

#include <GUIConstants.au3>
#include <StaticConstants.au3>

Global Enum _
        $LED_COUNT_X, _
        $LED_COUNT_Y, _
        $LED_SIZE_X, _
        $LED_SIZE_Y, _
        $LED_MARGEN_X, _
        $LED_MARGEN_Y, _
        $WINDOW_MARGEN_XY, _
        $LEDGROUP_SIZE_X, _
        $LEDGROUP_SIZE_Y, _
        $WINDOW_SIZE_X, _
        $WINDOW_SIZE_Y, _
        $WINDOW_ID, _
        $Data_A_Size

Global $Data_A[$Data_A_Size]
$Data_A[$LED_COUNT_X] = 10
$Data_A[$LED_COUNT_Y] = 10
$Data_A[$LED_SIZE_X] = 20
$Data_A[$LED_SIZE_Y] = 20
$Data_A[$LED_MARGEN_X] = 4
$Data_A[$LED_MARGEN_Y] = 4
$Data_A[$WINDOW_MARGEN_XY] = 10

$Data_A[$LEDGROUP_SIZE_X] = _
        ($Data_A[$LED_SIZE_X] * $Data_A[$LED_COUNT_X]) + _
        ($Data_A[$LED_MARGEN_X] * ($Data_A[$LED_COUNT_X] + 1))
$Data_A[$LEDGROUP_SIZE_Y] = _
        ($Data_A[$LED_SIZE_Y] * $Data_A[$LED_COUNT_Y]) + _
        ($Data_A[$LED_MARGEN_Y] * ($Data_A[$LED_COUNT_Y] + 1))
$Data_A[$WINDOW_SIZE_X] = $Data_A[$LEDGROUP_SIZE_X] + ($Data_A[$WINDOW_MARGEN_XY] * 2)
$Data_A[$WINDOW_SIZE_Y] = $Data_A[$LEDGROUP_SIZE_Y] + ($Data_A[$WINDOW_MARGEN_XY] * 2)

Global Enum _
        $LED_ID, _
        $LED_COL, _
        $Led_A_Size

Global $Led_A[($Data_A[$LED_COUNT_X] * $Data_A[$LED_COUNT_Y]) + 1][$Led_A_Size]

SRandom(Mod(TimerInit(), 2 ^ 32) - (2 ^ 31)) ;; (SRandom,InputRange: -2^31..2^31-1)

MAIN()
Exit

Func MAIN()
    BuildGui()
    GuiWait()
EndFunc
Func BuildGui()
    $Data_A[$WINDOW_ID] = GUICreate('Colorful Led Test', $Data_A[$WINDOW_SIZE_X], $Data_A[$WINDOW_SIZE_Y], -1, -1)
    GUICtrlCreateLabel('', _
            $Data_A[$WINDOW_MARGEN_XY], $Data_A[$WINDOW_MARGEN_XY], _
            $Data_A[$LEDGROUP_SIZE_X], $Data_A[$LEDGROUP_SIZE_Y], _
            $SS_ETCHEDFRAME)

    Local $Style = $SS_SUNKEN
    If ($Data_A[$LED_MARGEN_X] < 2) Or ($Data_A[$LED_MARGEN_Y] < 2) Then $Style = 0

    Local $LedNr = 0
    Local $PosX, $PosY
    For $iy = 1 To $Data_A[$LED_COUNT_Y]
        For $ix = 1 To $Data_A[$LED_COUNT_X]
            $LedNr += 1
            $PosX = (($ix - 1) * ($Data_A[$LED_SIZE_X] + $Data_A[$LED_MARGEN_X])) + $Data_A[$LED_MARGEN_X] + $Data_A[$WINDOW_MARGEN_XY]
            $PosY = (($iy - 1) * ($Data_A[$LED_SIZE_Y] + $Data_A[$LED_MARGEN_Y])) + $Data_A[$LED_MARGEN_Y] + $Data_A[$WINDOW_MARGEN_XY]
            $Led_A[$LedNr][$LED_ID] = GUICtrlCreateLabel('', $PosX, $PosY, _
                    $Data_A[$LED_SIZE_X], $Data_A[$LED_SIZE_Y], _
                    $Style)
        Next
    Next
    GUISetState(@SW_SHOW, $Data_A[$WINDOW_ID])
EndFunc
Func GuiWait()
    Do
        Led_Set()
    Until GUIGetMsg() = $GUI_EVENT_CLOSE ;; yea, I know.
EndFunc
Func Led_Set()
    Local $LEdColor = RndColor()
    Local $LedNr = Random(1, UBound($Led_A, 1) - 1, 1)
    $Led_A[$LedNr][$LED_COL] = $LEdColor
    GUICtrlSetBkColor($Led_A[$LedNr][$LED_ID], $LEdColor)
EndFunc
Func RndColor()
    Return Floor(Random() * 16777216)
EndFunc

---

Its a little overkill on the GUI part, but that was a other test part.

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

Cool demostration, there is a way to avoid this behavior?

Other than using GUI OnEventMode instead of GUi MessageMode. Probably not. I at least don't know of any other way.

---

- see msg #6 from Martin for one way. (Adlib) :mellow:

Edited by iEvKI3gv9Wrkd41u

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
Share on other sites

So moving the mouse speeds things up? :S

Not really, it might look faster but you have a loop with a delay in it waiting for GuiGetMsg to return with an event. Creating an interrupt or event gives GuiGetMsg something to return so it doesn't wait for the the maximum time before returning. If you wanted the LED() function to operate quickly and continuously regardless of the mouse or other interupts you can do something like this:

#include <GUIConstants.au3>
#include <StaticConstants.au3>
#include <timers.au3>

Global Enum _
        $LED_COUNT_X, _
        $LED_COUNT_Y, _
        $LED_SIZE_X, _
        $LED_SIZE_Y, _
        $LED_MARGEN_X, _
        $LED_MARGEN_Y, _
        $WINDOW_MARGEN_XY, _
        $LEDGROUP_SIZE_X, _
        $LEDGROUP_SIZE_Y, _
        $WINDOW_SIZE_X, _
        $WINDOW_SIZE_Y, _
        $WINDOW_ID, _
        $Data_A_Size

Global $Data_A[$Data_A_Size]
$Data_A[$LED_COUNT_X] = 10
$Data_A[$LED_COUNT_Y] = 10
$Data_A[$LED_SIZE_X] = 20
$Data_A[$LED_SIZE_Y] = 20
$Data_A[$LED_MARGEN_X] = 4
$Data_A[$LED_MARGEN_Y] = 4
$Data_A[$WINDOW_MARGEN_XY] = 10

$Data_A[$LEDGROUP_SIZE_X] = _
        ($Data_A[$LED_SIZE_X] * $Data_A[$LED_COUNT_X]) + _
        ($Data_A[$LED_MARGEN_X] * ($Data_A[$LED_COUNT_X] + 1))
$Data_A[$LEDGROUP_SIZE_Y] = _
        ($Data_A[$LED_SIZE_Y] * $Data_A[$LED_COUNT_Y]) + _
        ($Data_A[$LED_MARGEN_Y] * ($Data_A[$LED_COUNT_Y] + 1))
$Data_A[$WINDOW_SIZE_X] = $Data_A[$LEDGROUP_SIZE_X] + ($Data_A[$WINDOW_MARGEN_XY] * 2)
$Data_A[$WINDOW_SIZE_Y] = $Data_A[$LEDGROUP_SIZE_Y] + ($Data_A[$WINDOW_MARGEN_XY] * 2)

Global Enum _
        $LED_ID, _
        $LED_COL, _
        $Led_A_Size

Global $Led_A[($Data_A[$LED_COUNT_X] * $Data_A[$LED_COUNT_Y]) + 1][$Led_A_Size]

SRandom(Mod(TimerInit(), 2 ^ 32) - (2 ^ 31)) ;; (SRandom,InputRange: -2^31..2^31-1)
Global $busy = false
MAIN()
Exit

Func MAIN()
    BuildGui()
    GuiWait()
EndFunc   ;==>MAIN
Func BuildGui()
    $Data_A[$WINDOW_ID] = GUICreate('Colorful Led Test', $Data_A[$WINDOW_SIZE_X], $Data_A[$WINDOW_SIZE_Y], -1, -1)
    GUICtrlCreateLabel('', _
            $Data_A[$WINDOW_MARGEN_XY], $Data_A[$WINDOW_MARGEN_XY], _
            $Data_A[$LEDGROUP_SIZE_X], $Data_A[$LEDGROUP_SIZE_Y], _
            $SS_ETCHEDFRAME)

    Local $Style = $SS_SUNKEN
    If ($Data_A[$LED_MARGEN_X] < 2) Or ($Data_A[$LED_MARGEN_Y] < 2) Then $Style = 0

    Local $LedNr = 0
    Local $PosX, $PosY
    For $iy = 1 To $Data_A[$LED_COUNT_Y]
        For $ix = 1 To $Data_A[$LED_COUNT_X]
            $LedNr += 1
            $PosX = (($ix - 1) * ($Data_A[$LED_SIZE_X] + $Data_A[$LED_MARGEN_X])) + $Data_A[$LED_MARGEN_X] + $Data_A[$WINDOW_MARGEN_XY]
            $PosY = (($iy - 1) * ($Data_A[$LED_SIZE_Y] + $Data_A[$LED_MARGEN_Y])) + $Data_A[$LED_MARGEN_Y] + $Data_A[$WINDOW_MARGEN_XY]
            $Led_A[$LedNr][$LED_ID] = GUICtrlCreateLabel('', $PosX, $PosY, _
                    $Data_A[$LED_SIZE_X], $Data_A[$LED_SIZE_Y], _
                    $Style)
        Next
    Next
    GUISetState(@SW_SHOW, $Data_A[$WINDOW_ID])
EndFunc   ;==>BuildGui
Func GuiWait()
   ;AdlibRegister("Led_SetB", 40)
  _Timer_SetTimer(0,40,"Led_SetB")
    Do
        ;Led_Set()
    Until GUIGetMsg() = $GUI_EVENT_CLOSE ;; yea, I know.
EndFunc   ;==>GuiWait

Func Led_SetB($a,$b,$c,$d)
    if $busy then Return
    $busy = true
    For $n = 1 To 10
        Led_Set()
    Next
    $busy = false
EndFunc   ;==>Led_SetB

Func Led_Set()
    For $n = 1 To 20
        Local $LEdColor = RndColor()
        Local $LedNr = Random(1, UBound($Led_A, 1) - 1, 1)
        $Led_A[$LedNr][$LED_COL] = $LEdColor
        GUICtrlSetBkColor($Led_A[$LedNr][$LED_ID], $LEdColor)
    Next
EndFunc   ;==>Led_Set
Func RndColor()
    Return Floor(Random() * 16777216)
EndFunc   ;==>RndColor
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Playing around a bit more with GUIGetMsg().

Some general trivia data.

- GUIGetMsg(), while in cpu lean mode, acts similar to a sleep(10).

- Cpu lean mode kicks in after 100 'no event' cases.

Used test code:

_GuiCreate()
_GuiWait()
Exit

Func _GuiCreate()
    GUICreate('GUIGetMsg', 120, 70)
    GUICtrlCreateLabel('-3-', 10, 10, 100, 20) ;; [3]
    GUICtrlCreateLabel('-4-', 10, 40, 100, 20) ;; [4]
    ;
;~  GUICtrlCreateLabel('', 10, 10, 100, 20, 0x07) ;; $SS_BLACKFRAME
;~  GUICtrlCreateLabel('', 10, 40, 100, 20, 0x07) ;; $SS_BLACKFRAME
    GUISetState()
EndFunc
Func _GuiWait()
;~  Local $iBailOut = 32
    Local $iMsg = 0, $iCount = 0, $iTimer = 0, $iTimer_old = $iTimer
    Do
;~      $iBailOut -= 1
;~      If Not $iBailOut Then ExitLoop

        _ShowTimePassed() ;; times the time running outside this function.
        ;; intended to be used with empty GUIGetMsg() loop. (less value variation jumping.)

        $iTimer_old = $iTimer
        $iTimer = TimerInit()
        $iMsg = GUIGetMsg()
        $iTimer = TimerDiff($iTimer)

        $iTimer = Round($iTimer*0.1)
        if $iMsg Then
;~          If $iCount > 10 Then DebugOut('$iCount', $iCount) ;### Debug DebugOut.
            $iCount = 0
        Else
            If $iTimer and Not $iTimer_old Then
;~              DebugOut('+ $iTimer', $iTimer) ;### Debug DebugOut.
                DebugOut('+ $iCount', $iCount) ;### Debug DebugOut.
            EndIf
            $iCount += 1
            GUICtrlSetData(4, $iCount)
        EndIf

    Until $iMsg = -3 ;; $GUI_EVENT_CLOSE
EndFunc
Func _ShowTimePassed()
    Local Static $iTimer = 0, $iTimeDiff = 0
    If $iTimer Then
        $iTimeDiff = TimerDiff($iTimer)
        GUICtrlSetData(3, StringFormat(' %07.3f ', $iTimeDiff))

        ;; mainly to slow down the call rate on GUIGetMsg(), or the change is a bit to fast to realy see.
        $iTimer = TimerInit()
        Sleep(11) ;; general sleep steps (local): 11,21,32,42,53, ... note: not absolute, 22 at this end likes to use/mixed two sleep times.
        $iTimeDiff = TimerDiff($iTimer)
;~      GUICtrlSetData(4, StringFormat(' %07.3f ', $iTimeDiff))
    EndIf
    $iTimer = TimerInit()
EndFunc
;; ---
Func DebugOut($1,$2='fOObAr')
    If not ($2 == 'fOObAr') Then $1 &= ' = ' & String($2)
    Return ConsoleWrite($1 & @CRLF)
EndFunc

"Straight_and_Crooked_Thinking" : A "classic guide to ferreting out untruths, half-truths, and other distortions of facts in political and social discussions."
"The Secrets of Quantum Physics" : New and excellent 2 part documentary on Quantum Physics by Jim Al-Khalili. (Dec 2014)

"Believing what you know ain't so" ...

Knock Knock ...
 

Link to comment
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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...