Jump to content

WinControlSetEvent UDF


MrCreatoR
 Share

Recommended Posts

This UDF allows to catch an event (pressed/released) from specific window's control.

Header:

; #FUNCTION# ====================================================================================================


; Name...........: _WinControlSetEvent
; Description ...: Sets an event for Window control.
; Syntax.........: Func(hWindow, nCtrlID, $sCallOnHoldFunc [, $sOnHoldParams [, $sCallOnReleaseFunc [, $sOnReleaseParams]]])
;
; Parameters ....: $hWindow         - Parent window handle (or class name) that have the control to set event for.
;                 $nCtrlID          - CtrlID (or control text) of the control to set the event for.
;                 $sCallOnHoldFunc  - Function name to call when event is fired (control is pressed *down*).
;                 $sOnHoldParams      - [Optional] Extra parameters to pass to the called function ($sCallOnHoldFunc).
;                 $sCallOnReleaseFunc - [Optional] Function to call when event is fired (control is released after pressing down).
;                 $sOnReleaseParams   - [Optional] Extra parameters to pass to the called function ($sCallOnReleaseFunc).
;
; Return values .: Always return 1.
;
; Author ........: G.Sandler (a.k.a MrCreatoR).
; Modified.......: 22.01.2009, 20:00
;
; Remarks .......: 1) This UDF includes OnAutoItExit function to release the callback/hooks resources.
;                 2) Blocking of $sCallOnHoldFunc/$sCallOnReleaseFunc function by window messages with commands
;                    such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible!
;
; Related .......: _WinCtrlEvents_Handler, _ControlGetHoveredID, _WinGetHoveredHandle, _OnAutoItExit
; Link ..........; 
; Example .......; Yes
; ====================================================================================================

Example:

#include <WinControlSetEvent.au3>

;============== Calc Demo ==============
;In this example we use the main loop to avoid blocking of mouse processing from inside CalcGetResult_Proc() function.

HotKeySet("^q", "_Quit")

$iGetCalcResults = False

Run("Calc.exe")

For $i = 1 To 9
    _WinControlSetEvent("[CLASS:SciCalc]", $i, "", "", "CalcGetResult_Proc", "'" & $i & "' Button has been pressed")
Next

_WinControlSetEvent("[CLASS:SciCalc]", "=", "", "", "CalcGetResult_Proc", "'=' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "/", "", "", "CalcGetResult_Proc", "'/' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "*", "", "", "CalcGetResult_Proc", "'*' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "-", "", "", "CalcGetResult_Proc", "'-' Button has been pressed")
_WinControlSetEvent("[CLASS:SciCalc]", "+", "", "", "CalcGetResult_Proc", "'+' Button has been pressed")

_WinControlSetEvent("[CLASS:SciCalc]", "Button28", "", "", "CalcGetResult_Proc", "'X' Button has been pressed")

While 1
    Sleep(10)
    
    If $iGetCalcResults <> False Then
        $iParam = $iGetCalcResults
        $iGetCalcResults = False
        
        If $iParam = 168 Then Exit ;Calc closed
        
        $sCtrl_Data = ControlGetText("[CLASS:SciCalc]", "", $iParam)
        
        If $sCtrl_Data = "=" Then
            Local $sResult = StringStripWS(ControlGetText("[CLASS:SciCalc]", "", "Edit1"), 3)
            If StringRight($sResult, 1) = "," Then $sResult = StringTrimRight($sResult, 1)
        
            ToolTip("The result has been calculated: " & $sResult, Default, Default, "Calc Info", 1, 5)
        ElseIf $sCtrl_Data <> "" Then
            ToolTip("Calc Button Pressed: " & $sCtrl_Data, Default, Default, "Calc Info", 1, 5)
        EndIf
    EndIf
WEnd

;Warning: blocking of this function by window messages with commands such as "Msgbox()" can lead to unexpected behavior,
;the return to the system should be as fast as possible !!!
Func CalcGetResult_Proc($sParams, $hWnd, $nCtrlID)
    ConsoleWrite("Passed params: " & $sParams & @CRLF)
    
    $iGetCalcResults = $nCtrlID
EndFunc

Func _Quit()
    Exit
EndFunc

WinControlSetEvent.zip

Edited by MrCreatoR

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

Nice UDF.

I came up with another way to trigger actions while not blocking the message queue and not using the main loop. I used it for my MD5 gui script. The basic idea is to create a hidden GUI with a dummy control (GuiCtrlCreateDummy()), then when you want to do something, you send it a message with GuiCtrlSendToDummy() and pass it some identifying data. That data can be read in the dummy's OnEvent function with GuiCtrlRead() to determine what it needs to do. Sending the event is the last thing in your message loop function before returning to the main script. This approach worked great for me, since I obviously didn't want to be hashing huge files and have the script stuck in the message loop.

Link to comment
Share on other sites

Nice UDF.

Thanks.

I came up with another way to trigger actions while not blocking the message queue and not using the main loop. I used it for my MD5 gui script. The basic idea is to create a hidden GUI with a dummy control (GuiCtrlCreateDummy()), then when you want to do something, you send it a message with GuiCtrlSendToDummy() and pass it some identifying data. That data can be read in the dummy's OnEvent function with GuiCtrlRead() to determine what it needs to do. Sending the event is the last thing in your message loop function before returning to the main script. This approach worked great for me, since I obviously didn't want to be hashing huge files and have the script stuck in the message loop.

But it's required OnEvent mode enabled, not all of us (and not for all our scripts) using this mode.

And in a matter of fact, this can be done seperately by you (as user), the event function just give you the needed trigger, what will happend next it's up to you.

P.S

To avoid the main loop inolving, we can use Adlib*:

Func _Event_Trigger_Proc($sParams, $hWnd, $nCtrlID)
    ConsoleWrite("Passed params: " & $sParams & @CRLF)
    
    $iGetCalcResults = $nCtrlID
    AdlibEnable("_Event_Main_Proc", 1)
EndFunc

Func _Event_Main_Proc()
    AdlibDisable()
    ;Here we can do whatever we need when event is triggered
EndFunc

 

Spoiler

Using OS: Win 7 Professional, Using AutoIt Ver(s): 3.3.6.1 / 3.3.8.1

AutoIt_Rus_Community.png AutoIt Russian Community

My Work...

Spoiler

AutoIt_Icon_small.pngProjects: ATT - Application Translate Tool {new}| BlockIt - Block files & folders {new}| SIP - Selected Image Preview {new}| SISCABMAN - SciTE Abbreviations Manager {new}| AutoIt Path Switcher | AutoIt Menu for Opera! | YouTube Download Center! | Desktop Icons Restorator | Math Tasks | KeyBoard & Mouse Cleaner | CaptureIt - Capture Images Utility | CheckFileSize Program

AutoIt_Icon_small.pngUDFs: OnAutoItErrorRegister - Handle AutoIt critical errors {new}| AutoIt Syntax Highlight {new}| Opera Library! | Winamp Library | GetFolderToMenu | Custom_InputBox()! | _FileRun UDF | _CheckInput() UDF | _GUIInputSetOnlyNumbers() UDF | _FileGetValidName() UDF | _GUICtrlCreateRadioCBox UDF | _GuiCreateGrid() | _PathSplitByRegExp() | _GUICtrlListView_MoveItems - UDF | GUICtrlSetOnHover_UDF! | _ControlTab UDF! | _MouseSetOnEvent() UDF! | _ProcessListEx - UDF | GUICtrl_SetResizing - UDF! | Mod. for _IniString UDFs | _StringStripChars UDF | _ColorIsDarkShade UDF | _ColorConvertValue UDF | _GUICtrlTab_CoverBackground | CUI_App_UDF | _IncludeScripts UDF | _AutoIt3ExecuteCode | _DragList UDF | Mod. for _ListView_Progress | _ListView_SysLink | _GenerateRandomNumbers | _BlockInputEx | _IsPressedEx | OnAutoItExit Handler | _GUICtrlCreateTFLabel UDF | WinControlSetEvent UDF | Mod. for _DirGetSizeEx UDF
 
AutoIt_Icon_small.pngExamples: 
ScreenSaver Demo - Matrix included | Gui Drag Without pause the script | _WinAttach()! | Turn Off/On Monitor | ComboBox Handler Example | Mod. for "Thinking Box" | Cool "About" Box | TasksBar Imitation Demo

Like the Projects/UDFs/Examples? Please rate the topic (up-right corner of the post header: Rating AutoIt_Rating.gif)

* === My topics === *

==================================================
My_Userbar.gif
==================================================

 

 

 

AutoIt is simple, subtle, elegant. © AutoIt Team

Link to comment
Share on other sites

  • 5 months later...

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...