Sign in to follow this  
Followers 0
guinness

GUIEvents

4 posts in this topic

How I use WM_MESSAGES efficiently. If someone wants a better example then let me know. Thanks.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Enum $GUI_ACTION_LCLIENT, $GUI_ACTION_RCLIENT, $GUI_ACTION_LTITLE ; An enumeration of possible actions.
Global $g_iActionCommand = -1 ; Create a single global variable to store a dummy control.

Example()

Func Example()
    ; When using WM_MESSAGES users tend to either create a ton of dummy controls and/or global variables that change a state from true to false.
    ; This example demonstrates that everything can be contained in the main function, so it's then easy to keep control of data being passed around.
    ; There is no need for tons of global variables and functions.
    ; It's also a good way of not staying too long in the WM_MESSAGE, which can turn into a huge mess.
    Local $hGUI = GUICreate('GUIEvents', 500, 500)

    GUIRegisterMsg($WM_LBUTTONDOWN, 'WM_LBUTTONDOWN')
    GUIRegisterMsg($WM_NCLBUTTONDOWN, 'WM_NCLBUTTONDOWN')
    GUIRegisterMsg($WM_RBUTTONDOWN, 'WM_RBUTTONDOWN')

    $g_iActionCommand = GUICtrlCreateDummy() ; Assign the global dummy control variable with a dummy control id.

    GUISetState(@SW_SHOW, $hGUI) ; Display the AutoIt GUI.

    While 1
        Switch GUIGetMsg() ; Monitor the gui messages/ids.
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case $g_iActionCommand ; When a message is sent to the dummy control we have to find out what message was actually sent.
                $iActionCommand = GUICtrlRead($g_iActionCommand) ; Read the message assigned to the control's "state" parameter e.g. 1001.
                ; As MakeLong() was used in the event function, find the LoWord of the message that was sent. HiWord could be used for an array index or listview item.
                $iAction = _WinAPI_LoWord($iActionCommand)
                Switch $iAction
                    Case $GUI_ACTION_LCLIENT
                        ConsoleWrite('The left button was clicked in the client.' & @CRLF)
                    Case $GUI_ACTION_RCLIENT
                        ConsoleWrite('The right button was clicked in the client.' & @CRLF)
                    Case $GUI_ACTION_LTITLE
                        ConsoleWrite('The left button was clicked in the titlebar.' & @CRLF)

                EndSwitch
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_LCLIENT, 0)) ; $GUI_ACTION_LCLIENT can be swapped for an integer value, but I use an enum value for ease.
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_LBUTTONDOWN

Func WM_NCLBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_LTITLE, 0))
    ; You don't even need to use MakeLong(). You can just use GUICtrlSendToDummy($g_iActionCommand, $GUI_ACTION_LTITLE), but then the MakeLong() offers the choice to pass
    ; another integer value such as listview item/subitem in a message such as WM_NOTIFY.
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NCLBUTTONDOWN

Func WM_RBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_RCLIENT, 0))
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_RBUTTONDOWN
3 people like this

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites



A better approach would be to hide the global variable around function calls. So now the user only has to interact with the functions and not the global variable.

#include <GUIConstantsEx.au3>
#include <WinAPI.au3>
#include <WindowsConstants.au3>

Global Enum $GUI_ACTION_LCLIENT, $GUI_ACTION_RCLIENT, $GUI_ACTION_LTITLE ; An enumeration of possible actions.
Global $g_iActionCommand ; Create a single global variable to store a dummy control.
_SetDummyControl(-9999)

Example()

Func Example()
    ; When using WM_MESSAGES users tend to either create a ton of dummy controls and/or global variables that change a state from true to false.
    ; This example demonstrates that everything can be contained in the main function, so it's then easy to keep control of data being passed around.
    ; There is no need for tons of global variables and functions.
    ; It's also a good way of not staying too long in the WM_MESSAGE, which can turn into a huge mess.
    Local $hGUI = GUICreate('GUIEvents', 500, 500)

    GUIRegisterMsg($WM_LBUTTONDOWN, 'WM_LBUTTONDOWN')
    GUIRegisterMsg($WM_NCLBUTTONDOWN, 'WM_NCLBUTTONDOWN')
    GUIRegisterMsg($WM_RBUTTONDOWN, 'WM_RBUTTONDOWN')

    $g_iActionCommand = GUICtrlCreateDummy() ; Assign the global dummy control variable with a dummy control id.

    GUISetState(@SW_SHOW, $hGUI) ; Display the AutoIt GUI.

    While 1
        Switch GUIGetMsg() ; Monitor the gui messages/ids.
            Case $GUI_EVENT_CLOSE
                ExitLoop

            Case _GetDummyControl() ; When a message is sent to the dummy control we have to find out what message was actually sent.
                $iActionCommand = GUICtrlRead(_GetDummyControl()) ; Read the message assigned to the control's "state" parameter e.g. 1001.
                ; As MakeLong() was used in the event function, find the LoWord of the message that was sent. HiWord could be used for an array index or listview item.
                $iAction = _WinAPI_LoWord($iActionCommand)
                Switch $iAction
                    Case $GUI_ACTION_LCLIENT
                        ConsoleWrite('The left button was clicked in the client.' & @CRLF)
                    Case $GUI_ACTION_RCLIENT
                        ConsoleWrite('The right button was clicked in the client.' & @CRLF)
                    Case $GUI_ACTION_LTITLE
                        ConsoleWrite('The left button was clicked in the titlebar.' & @CRLF)

                EndSwitch
        EndSwitch
    WEnd
    GUIDelete($hGUI)
EndFunc   ;==>Example

Func _GetDummyControl() ; Getter for the dummy control.
    Return $g_iActionCommand
EndFunc   ;==>_GetDummyControl

Func _SetDummyControl($iDummy) ; Setter for the dummy control.
    $g_iActionCommand = $iDummy
EndFunc   ;==>_SetDummyControl

Func WM_LBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_LCLIENT, 0)) ; $GUI_ACTION_LCLIENT can be swapped for an integer value, but I use an enum value for ease.
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_LBUTTONDOWN

Func WM_NCLBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_LTITLE, 0))
    ; You don't even need to use MakeLong(). You can just use GUICtrlSendToDummy($g_iActionCommand, $GUI_ACTION_LTITLE), but then the MakeLong() offers the choice to pass
    ; another integer value such as listview item/subitem in a message such as WM_NOTIFY.
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_NCLBUTTONDOWN

Func WM_RBUTTONDOWN($hWnd, $iMsg, $wParam, $lParam)
    #forceref $hWnd, $iMsg, $wParam, $lParam
    GUICtrlSendToDummy($g_iActionCommand, _WinAPI_MakeLong($GUI_ACTION_RCLIENT, 0))
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_RBUTTONDOWN
1 person likes this

_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

Share this post


Link to post
Share on other sites

can it be combined with : _GUIRegisterMsgEx() ?


Signature beginning:   Wondering who uses AutoIT and what it can be used for ?
* GHAPI UDF - modest begining - comunication with GitHub REST API *
ADO.au3 UDF     POP3.au3 UDF     XML.au3 UDF    How to use IE.au3  UDF with  AutoIt v3.3.14.x  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 *

My contribution to others projects or UDF based on  others projects: * _sql.au3 UDF  * POP3.au3 UDF *  RTF Printer - UDF * XML.au3 - BETA * ADO.au3 UDF SMTP Mailer UDF *

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 * Best coding practices * 

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) * 

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 *

"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"
:naughty:  :ranting:, 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: 2017-06-04

Share this post


Link to post
Share on other sites

I don't think so. This is a design choice by the developer.


_AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_ConvertLHWebColor()/_ConvertSHWebColor()_DesktopDimensions()_DisplayPassword()_DotNet_Load()/_DotNet_Unload()_Fibonacci()_FileCompare()_FileCompareContents()_FileNameByHandle()_FilePrefix/SRE()_FindInFile()_GetBackgroundColor()/_SetBackgroundColor()_GetConrolID()_GetCtrlClass()_GetDirectoryFormat()_GetDriveMediaType()_GetFilename()/_GetFilenameExt()_GetHardwareID()_GetIP()_GetIP_Country()_GetOSLanguage()_GetSavedSource()_GetStringSize()_GetSystemPaths()_GetURLImage()_GIFImage()_GoogleWeather()_GUICtrlCreateGroup()_GUICtrlListBox_CreateArray()_GUICtrlListView_CreateArray()_GUICtrlListView_SaveCSV()_GUICtrlListView_SaveHTML()_GUICtrlListView_SaveTxt()_GUICtrlListView_SaveXML()_GUICtrlMenu_Recent()_GUICtrlMenu_SetItemImage()_GUICtrlTreeView_CreateArray()_GUIDisable()_GUIImageList_SetIconFromHandle()_GUIRegisterMsg()_GUISetIcon()_Icon_Clear()/_Icon_Set()_IdleTime()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_IsSystemDrive()_IsUPX()_IsValidType()_IsWebColor()_Language()_Log()_MicrosoftInternetConnectivity()_MSDNDataType()_PathFull/GetRelative/Split()_PathSplitEx()_PrintFromArray()_ProgressSetMarquee()_ReDim()_RockPaperScissors()/_RockPaperScissorsLizardSpock()_ScrollingCredits_SelfDelete()_SelfRename()_SelfUpdate()_SendTo()_ShellAll()_ShellFile()_ShellFolder()_SingletonHWID()_SingletonPID()_Startup()_StringCompact()_StringIsValid()_StringRegExpMetaCharacters()_StringReplaceWholeWord()_StringStripChars()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_Create_CTL_CODE()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()Au3 script parsingAutoIt SearchAutoIt3 PortableAutoIt3WrapperToPragmaAutoItWinGetTitle()/AutoItWinSetTitle()CodingDirToHTML5FileInstallrFileReadLastChars()GeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIEventsGUIGetBkColor()Int_Parse() & Int_TryParse()IsISBN()LockFile()Mapping CtrlIDsOOP in AutoItParseHeadersToSciTE()PasswordValidPasteBinPosts Per DayPreExpandProtect GlobalsQueue()Resource UpdateResourcesExSciTE JumpSettings INISHELLHOOKShunting-YardSignature CreatorStack()Stopwatch()StringAddLF()/StringStripLF()StringEOLToCRLF()VSCROLLWM_COPYDATAMore Examples...

Updated: 04/09/2015

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
Sign in to follow this  
Followers 0