Leaderboard
Popular Content
Showing content with the highest reputation on 03/15/2014 in all areas
-
Screensaver, Sleep, Workstation Lock, and Power-Save Disabling Since I see this question asked again and again, and the simple answer isn't always given (or at least, only half of it is), I'm posting this for reference. To disable Power-saving, Workstation Locking, Screensavers, etc., all that's needed is a call to SetThreadExecutionState. No need for timers, nor for emulating mouse or keyboard input. Just make a call to that API once to disable any locking/sleeping/screensaverin'. When you're done, make another call to it with the proper parameters (this part is important), and everything will be restored. NOTE: The 'execution state' should really only matter while the program that made the call is running (its supposed to be per-application). Once it is terminated, the execution state should be restored. However, there have been some unusual reports regarding this, especially when it is called by more than one process. The main functions in my module are _PowerKeepAlive() and _PowerResetState(). One keeps everything 'awake', the other reenables the default state of Windows power settings (including screensavers and workstation locking). The primary reason I've used this myself is for games that forget to call that API function, and after playing with the joystick for a while, a screensaver or lock-screen will pop up. Using these functions will workaround that problem. Also!: If you want to save and restore the current power-savings 'execution state', just pass the return value from _PowerKeepAlive() as the first argument to 'SetThreadExecutionState'. Anyway, here's the main module I use (example use is below): #include-once ; =============================================================================================================================== ; <_PowerKeepAlive.au3> ; ; Functions to prevent/disable sleep/power-savings modes (AND screensaver) ; ; Functions: ; _PowerKeepAlive() ; _PowerResetState() ; ; See also: ; <_ScreenSaverFunctions.au3> ; query, change, enable & disable screensaver. ; ; Author: Ascend4nt ; =============================================================================================================================== ; ========================================================================================================================== ; Func _PowerKeepAlive() ; ; Function to Prevent the Screensaver and Sleep/Power-savings modes from kicking in. ; NOTE: Be sure to reset this state on exit! ; ; Returns: ; Success: @error=0 & previous state as # (typically 0x80000000 [-2147483648]) ; Failure: @error set (returns 0x80000000, but thats just the normal state) ; @error = 2 = DLLCall error. @extended = DLLCall error code (see AutoIt Help) ; ; Author: Ascend4nt ; ========================================================================================================================== Func _PowerKeepAlive() #cs ; Flags: ; ES_SYSTEM_REQUIRED (0x01) -> Resets system Idle timer ; ES_DISPLAY_REQUIRED (0x02) -> Resets display Idle timer ; ES_CONTINUOUS (0x80000000) -> Forces 'continuous mode' -> the above 2 will not need to continuously be reset #ce Local $aRet=DllCall('kernel32.dll','long','SetThreadExecutionState','long',0x80000003) If @error Then Return SetError(2,@error,0x80000000) Return $aRet[0] ; Previous state (typically 0x80000000 [-2147483648]) EndFunc ; ========================================================================================================================== ; Func _PowerResetState() ; ; Function to Reset the Screensaver and Sleep/Power-savings modes to defaults. ; NOTE: The timer is reset on each call to this! ; ; Returns: ; Success: @error=0 & previous state as # ; Failure: @error set (returns 0x80000000, but thats just the normal state) ; @error = 2 = DLLCall error. @extended = DLLCall error code (see AutoIt Help) ; ; Author: Ascend4nt ; ========================================================================================================================== Func _PowerResetState() ; Flag: ES_CONTINUOUS (0x80000000) -> (default) -> used alone, it resets timers & allows regular sleep/power-savings mode Local $aRet=DllCall('kernel32.dll','long','SetThreadExecutionState','long',0x80000000) If @error Then Return SetError(2,@error,0x80000000) Return $aRet[0] ; Previous state EndFunc Example usage: #NoTrayIcon #include "_PowerKeepAlive.au3" ; Singleton code: If WinExists("SA_0bc53fe0-59c2-11e2-bcfd-0800200c9a66_SA") Then Exit AutoItWinSetTitle("SA_0bc53fe0-59c2-11e2-bcfd-0800200c9a66_SA") Opt("TrayOnEventMode", 0) Opt("TrayMenuMode", 1+2) TraySetClick(8+1) Local $iTrayExit = TrayCreateItem("Exit + Reenable Sleep") ; Disable screensaver, power-save, etc _PowerKeepAlive() ; Be sure to register this to reenable power-saving, screensaver, etc OnAutoItExitRegister("_PowerResetState") ; Now we're ready to accept messages TraySetState() While TrayGetMsg() <> $iTrayExit ; No need for sleep WEnd _PowerKeepAlive.au31 point
-
1 point
-
Here is what I came up with, it supports MouseClick, MouseEnter, MouseLeave, MouseDown and MouseUp events. I tried to implement this in a clean way, in both coding and its logic so I followed how C#.net handles this events for a control and tried to simulate them (Guess Microsoft knows better than me!). It has an external function which you can use and a single internal function which you shouldn't call. Also there is only 1 global variable, an array which holds everything about the registred controls and their events. It could be used with GUIOnEventMode 1 and 0, and no matter if you switch GUIOnEventMode's value during the runtime. Let me know how is it going for you. Comments for improvements and suggestions are greatly welcomed. #include-once #AutoIt3Wrapper_Au3Check_Parameters=-d -w 1 -w 2 -w 3 -w 4 -w 5 -w 6 -w 7 Global $GUICtrlSetOnEventEx_ahEvents[1][1] Func GUICtrlSetOnEventEx($iCtrlId, $MouseClick = Null, $MouseEnter = Null, $MouseLeave = Null, $MouseDown = Null, $MouseUp = Null) ; ;Validate the control identifier ; Local $hCtrl = GUICtrlGetHandle($iCtrlId) If ($hCtrl = 0) Then Return False EndIf ; ;Validate the event functions, they must be a function, or Null ; If ((Not IsFunc($MouseClick)) And ($MouseClick <> Null)) Then Return False ElseIf ((Not IsFunc($MouseEnter)) And ($MouseEnter <> Null)) Then Return False ElseIf ((Not IsFunc($MouseLeave)) And ($MouseLeave <> Null)) Then Return False ElseIf ((Not IsFunc($MouseDown)) And ($MouseDown <> Null)) Then Return False ElseIf ((Not IsFunc($MouseUp)) And ($MouseUp <> Null)) Then Return False EndIf ; ;Delete $hCtrl and its events ; If (@NumParams = 1) Then ; ;There is no event to delete ; If ($GUICtrlSetOnEventEx_ahEvents[0][0] = 0) Then Return False EndIf Local $ahEvents[1][1] For $i = 1 To $GUICtrlSetOnEventEx_ahEvents[0][0] ; ;Skip $hCtrl as we don't want its events be listened any more ; If ($GUICtrlSetOnEventEx_ahEvents[$i][0] = $hCtrl) Then ContinueLoop EndIf $ahEvents[0][0] += 1 ReDim $ahEvents[$ahEvents[0][0] + 1][6] ; ;Control identifier ; $ahEvents[$ahEvents[0][0]][0] = $GUICtrlSetOnEventEx_ahEvents[$i][0] ; ;MouseClick event ; $ahEvents[$ahEvents[0][0]][1] = $GUICtrlSetOnEventEx_ahEvents[$i][1] ; ;MouseEnter event ; $ahEvents[$ahEvents[0][0]][2] = $GUICtrlSetOnEventEx_ahEvents[$i][2] ; ;MouseDown event ; $ahEvents[$ahEvents[0][0]][3] = $GUICtrlSetOnEventEx_ahEvents[$i][3] ; ;MouseUp event ; $ahEvents[$ahEvents[0][0]][4] = $GUICtrlSetOnEventEx_ahEvents[$i][4] ; ;MouseLeave event ; $ahEvents[$ahEvents[0][0]][5] = $GUICtrlSetOnEventEx_ahEvents[$i][5] Next ; ;If no matches found for $hCtrl ; If ($GUICtrlSetOnEventEx_ahEvents[0][0] = $ahEvents[0][0]) Then Return False EndIf ; ;Uninitialize the event listener if $hCtrl was the last one ; If ($ahEvents[0][0] = 0) Then AdlibUnRegister(GUICtrlSetOnEventEx_Proc) $GUICtrlSetOnEventEx_ahEvents = 0 Dim $GUICtrlSetOnEventEx_ahEvents[1][1] Else $GUICtrlSetOnEventEx_ahEvents = $ahEvents EndIf Return True EndIf ; ;Initialize the event listener if it's not already initialized ; If ($GUICtrlSetOnEventEx_ahEvents[0][0] = 0) Then If (AdlibRegister(GUICtrlSetOnEventEx_Proc, 10) = 0) Then Return False EndIf EndIf ; ;Update $hCtrl's events if it already exists in the event listener ; For $i = 1 To $GUICtrlSetOnEventEx_ahEvents[0][0] If ($GUICtrlSetOnEventEx_ahEvents[$i][0] = $hCtrl) Then $GUICtrlSetOnEventEx_ahEvents[$i][1] = $MouseClick $GUICtrlSetOnEventEx_ahEvents[$i][2] = $MouseEnter $GUICtrlSetOnEventEx_ahEvents[$i][3] = $MouseLeave $GUICtrlSetOnEventEx_ahEvents[$i][4] = $MouseDown $GUICtrlSetOnEventEx_ahEvents[$i][5] = $MouseUp Return True EndIf Next ; ;Add $hCtrl and its events to the event listener ; $GUICtrlSetOnEventEx_ahEvents[0][0] += 1 ReDim $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0] + 1][6] $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][0] = $hCtrl $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][1] = $MouseClick $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][2] = $MouseEnter $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][3] = $MouseLeave $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][4] = $MouseDown $GUICtrlSetOnEventEx_ahEvents[$GUICtrlSetOnEventEx_ahEvents[0][0]][5] = $MouseUp Return True EndFunc Func GUICtrlSetOnEventEx_Proc() ; ;These variables "must" be defined as static as we need their values to be available in the next calls ; Local Static $WasPrimaryButtonDown = False Local Static $LastHoveredCtrlId = 0 Local Static $LastHoveredCtrl = 0x0 Local Static $LastPressedCtrl = 0x0 Local Static $LastMouseUpFunction = Null Local Static $LastMouseLeaveFunction = Null ; ;Nonzero if the meanings of the left and right mouse buttons are swapped ; Local $AreMouseButtonsSwapped = DllCall("user32.dll", "int", "GetSystemMetrics", _ "int", 23)[0] ;SM_SWAPBUTTON ; ;Define the primary mouse button's value ; Local $PrimaryMouseButton = (($AreMouseButtonsSwapped = 0) ? (0x01) : (0x02)) ;VK_LBUTTON : VK_RBUTTON ; ;"<> 0" is not really necessary, but let it remain there, trust me ; Local $IsPrimaryButtonDown = (DllCall("user32.dll", "short", "GetAsyncKeyState", _ "int", $PrimaryMouseButton)[0] <> 0) ; ;Retrieves the position of the mouse cursor, in screen coordinates ; Local $tPOINT = DllStructCreate("long x;long y") DllCall("user32.dll", "BOOL", "GetCursorPos", _ "ptr", DllStructGetPtr($tPOINT)) ; ;Retrieves a handle to the window that contains the specified point ; Local $tCAST = DllStructCreate("INT64", DllStructGetPtr($tPOINT)) Local $hCtrl = DllCall("user32.dll", "HWND", "WindowFromPoint", _ "INT64", DllStructGetData($tCAST, 1))[0] ; ;Convert the control handle to control identifier ; Local $iCtrlId = DllCall("user32.dll", "int", "GetDlgCtrlID", _ "HWND", $hCtrl)[0] ; ;Wait while the primary mouse button is pressed and the cursor is not on the pressed control ; If (($hCtrl <> $LastPressedCtrl) And ($IsPrimaryButtonDown) And ($WasPrimaryButtonDown)) Then Return EndIf ; ;Return if none of the $hCtrl and $IsPrimaryButtonDown are changed ; If (($hCtrl = $LastHoveredCtrl) And ($IsPrimaryButtonDown = $WasPrimaryButtonDown)) Then Return EndIf ; ;Primary mouse button is pressed on a new control ; If (($IsPrimaryButtonDown <> $WasPrimaryButtonDown) And ($IsPrimaryButtonDown)) Then $LastPressedCtrl = $hCtrl EndIf ; ;If the current hovered control is not the previous hovered control ; If ($hCtrl <> $LastHoveredCtrl) Then ; ;Trigger previous pressed control's MouseUp event if it has one and it's not hovered anymore ; If (($LastMouseUpFunction <> Null) And (Not $IsPrimaryButtonDown) And ($WasPrimaryButtonDown) And ($LastHoveredCtrl <> 0)) Then $LastMouseUpFunction($LastHoveredCtrlId) $LastMouseUpFunction = Null EndIf ; ;Trigger previous hovered control's MouseLeave event if it has one ; If ($LastMouseLeaveFunction <> Null) Then $LastMouseLeaveFunction($LastHoveredCtrlId) $LastMouseLeaveFunction = Null EndIf EndIf ; ;Is there any event registerd for $hCtrl? Let's see! ; For $i = 1 To $GUICtrlSetOnEventEx_ahEvents[0][0] If ($GUICtrlSetOnEventEx_ahEvents[$i][0] = $hCtrl) Then ; ;If the primary mouse button is and was pressed while the cursor was on the current hovered control ; If (($IsPrimaryButtonDown) And ($hCtrl = $LastPressedCtrl)) Then ; ;Trigger MouseDown event if it has one ; If ($GUICtrlSetOnEventEx_ahEvents[$i][4] <> Null) Then $GUICtrlSetOnEventEx_ahEvents[$i][4] ($iCtrlId) Else ; ;If a new control is hovered and/or the primary mouse button state has changed ; If (($hCtrl <> $LastHoveredCtrl) Or ($IsPrimaryButtonDown <> $WasPrimaryButtonDown)) Then ; ;Current hovered control is the previous pressed control and the primary mouse button is not pressed now, but it was ; If (($hCtrl = $LastPressedCtrl) And (Not $IsPrimaryButtonDown) And ($WasPrimaryButtonDown)) Then ; ;Trigger MouseClick event if it has one ; If ($GUICtrlSetOnEventEx_ahEvents[$i][1] <> Null) Then $GUICtrlSetOnEventEx_ahEvents[$i][1] ($iCtrlId) ; ;Trigger MouseUp event if it has one ; If ($GUICtrlSetOnEventEx_ahEvents[$i][5] <> Null) Then $GUICtrlSetOnEventEx_ahEvents[$i][5] ($iCtrlId) ElseIf ($hCtrl <> $LastHoveredCtrl) Then ; ;Trigger MouseEnter event if it has one ; If ($GUICtrlSetOnEventEx_ahEvents[$i][2] <> Null) Then $GUICtrlSetOnEventEx_ahEvents[$i][2] ($iCtrlId) EndIf EndIf EndIf $LastMouseUpFunction = $GUICtrlSetOnEventEx_ahEvents[$i][5] $LastMouseLeaveFunction = $GUICtrlSetOnEventEx_ahEvents[$i][3] ExitLoop EndIf Next $WasPrimaryButtonDown = $IsPrimaryButtonDown $LastHoveredCtrlId = $iCtrlId $LastHoveredCtrl = $hCtrl EndFunc Example: #include <StaticConstants.au3> #include <GUIConstantsEx.au3> #include "GUICtrlSetOnEventEx.au3" Global $hWnd = GUICreate("GUICtrlSetOnEventEx Example", 600, 400) GUICtrlCreateButton("Button!", 25, 25, 100, 25) GUICtrlSetOnEventEx(-1, btnMouseClick, btnMouseEnter, btnMouseLeave, btnMouseDown, btnMouseUp) GUICtrlCreateLabel("LABEL", 25, 75, 100, 20, BitOR($SS_CENTER, $SS_CENTERIMAGE)) GUICtrlSetColor(-1, 0x1E1E1E) GUICtrlSetOnEventEx(-1, Null, lblMouseEnter, lblMouseLeave, lblMouseDown, lblMouseUp) Global $chkA = GUICtrlCreateCheckbox("Checkbox A", 25, 120, -1, 15) GUICtrlSetOnEventEx(-1, Null, chkMouseEnter) Global $chkB = GUICtrlCreateCheckbox("Checkbox B", 25, 140, -1, 15) GUICtrlSetOnEventEx(-1, Null, chkMouseEnter) GUISetState() Do Until (GUIGetMsg() = $GUI_EVENT_CLOSE) Func btnMouseClick($iCtrlId) ConsoleWrite("Button clicked..." & @CRLF) EndFunc Func btnMouseEnter($iCtrlId) GUICtrlSetData($iCtrlId, "Enter") EndFunc Func btnMouseLeave($iCtrlId) GUICtrlSetData($iCtrlId, "Leave") EndFunc Func btnMouseDown($iCtrlId) GUICtrlSetData($iCtrlId, "Down") EndFunc Func btnMouseUp($iCtrlId) GUICtrlSetData($iCtrlId, "Button!") EndFunc Func lblMouseEnter($iCtrlId) GUICtrlSetBkColor($iCtrlId, 0xFEFEFE) EndFunc Func lblMouseLeave($iCtrlId) GUICtrlSetBkColor($iCtrlId, 0xEFEFF2) EndFunc Func lblMouseDown($iCtrlId) GUICtrlSetData($iCtrlId, "DOWN") EndFunc Func lblMouseUp($iCtrlId) GUICtrlSetData($iCtrlId, "LABEL") EndFunc Func chkMouseEnter($iCtrlId) Switch ($iCtrlId) Case $chkA ConsoleWrite("Checkbox A hovered...!" & @CRLF) case $chkB ConsoleWrite("Checkbox B hovered...!" & @CRLF) EndSwitch EndFunc1 point
-
Why window info Class alway change
rikimaru110011 reacted to FireFox for a topic
Of course it can, do you see TITLE in all the examples?1 point -
Why window info Class alway change
rikimaru110011 reacted to AdamUL for a topic
Look at Advanced Window Descriptions under Window Titles and Text (Advanced) in the Help File for the REGEXPCLASS as JohnOne mentioned. Adam1 point