Jump to content



Photo

GUIRegisterMsg replacement for GUICtrlSetOnEvent and GUIGetMsg


  • Please log in to reply
25 replies to this topic

#1 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 17 November 2011 - 08:20 PM

Here's a little snippet that I wrote up that demonstrates a way to eliminate the message loop and onevent modes of detecting control and GUI events. I was inspired to attempt this by this feature request and the answer that Valik gave when he rejected it. I wasn't sure how to write the wrapper that he said could be done to do this, so I dug into windows messages to see what I could come up with.

This snippet uses 3 different windows messages for various controls and for the GUI events. I have also included code that shows that you can use this in conjunction with GUIGetMsg, it should work just as well alongside OnEvent mode as well. You will probably have to play around with it to get it to work with every different type of control that AutoIt can make. I originally had a combo box in the GUI, but they work strangely because an event is fired every time the GUI gets focus and the combo is the last control actioned.

I hope this is informative to some and if there's a way of doing this easier, I'd love to know.

AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> #include <MenuConstants.au3> GUIRegisterMsg($WM_COMMAND, "_WM_EXTRACTOR") GUIRegisterMsg($WM_SYSCOMMAND, "_WM_EXTRACTOR") GUIRegisterMsg($WM_HSCROLL, "_WM_EXTRACTOR") $GUI = GUICreate("Test GUI", 200, 200, -1, -1, BitOR($WS_THICKFRAME, $gui_ss_default_gui)) $Button = GUICtrlCreateButton("Test", 20, 20) $Button2 = GUICtrlCreateButton(" Another Test ", 20, 100) $Slider = GUICtrlCreateSlider(20, 60, 150) $hSlider = GUICtrlGetHandle($Slider) GUISetState() While 1     $msg = GUIGetMsg()     Switch $msg         Case $Button             MsgBox(0, "", "You pressed the button marked TEST")         Case $Button2             MsgBox(0, "", "You pressed the button marked 'Another Test'")     EndSwitch WEnd Func _WM_EXTRACTOR($hWnd, $iMsg, $wParam, $lParam)     Local $nNotifyCode = BitShift($wParam, 16)     Local $nID = BitAND($wParam, 0x0000FFFF)     Local $hCtrl = $lParam     #forceref $hWnd, $iMsg, $wParam, $lParam     Switch $iMsg         Case $WM_HSCROLL             Switch $lParam                 Case $hSlider                     ConsoleWrite("+Slider moved to: " & GUICtrlRead($Slider) & @LF)             EndSwitch         Case $WM_VSCROLL         Case $WM_COMMAND             Switch $nID                 Case $Button                     ConsoleWrite(">Button Test pressed" & @LF)                 Case $Button2                     ConsoleWrite(">Button Another Test pressed" & @LF)                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch         Case $WM_SYSCOMMAND             Switch $wParam                 Case $SC_CLOSE                     ConsoleWrite("!Exit pressed" & @LF)                     Exit                 Case $SC_RESTORE                     ConsoleWrite("!Restore window" & @LF)                 Case $SC_MINIMIZE                     ConsoleWrite("!Minimize Window" & @LF)                 Case $SC_MOUSEMENU + 3                     ConsoleWrite("!System menu pressed" & @LF)                 Case $SC_MOVE                     ConsoleWrite("!System menu Move Option pressed" & @LF)                     Return 0                 Case $SC_SIZE                     ConsoleWrite("!System menu Size Option pressed" & @LF)                     Return 0                 Case $SC_MOUSEMENU + 2 ; This and the following case statements are only valid when the GUI is resizable                     ConsoleWrite("!Right side of GUI clicked" & @LF)                     Return 0                 Case $SC_MOUSEMENU + 1                     ConsoleWrite("!Left side of GUI clicked" & @LF)                     Return 0                 Case $SC_MOUSEMENU + 8                     ConsoleWrite("!Lower Right corner of GUI clicked" & @LF)                     Return 0                 Case $SC_MOUSEMENU + 7                     ConsoleWrite("!Lower Left corner of GUI clicked" & @LF)                     Return 0                 Case $SC_MOUSEMENU + 6                     ConsoleWrite("!Bottom side of GUI clicked" & @LF)                     Return 0                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch     EndSwitch     Return $GUI_RUNDEFMSG EndFunc   ;==>_WM_EXTRACTOR

Edited by BrewManNH, 17 December 2012 - 06:51 PM.

  • JScript likes this

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image








#2 Zedna

Zedna

    AutoIt rulez!

  • MVPs
  • 8,322 posts

Posted 17 November 2011 - 08:57 PM

Maybe this should be in Examples forum I think as it is solved solution only with question for optimizing.

Edited by Zedna, 17 November 2011 - 08:59 PM.


#3 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 18 November 2011 - 01:31 AM

You're probably right about that. I'll request for it to be moved, that makes more sense to have it in there.

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#4 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 18 November 2011 - 04:34 AM

Here's an updated version of the same code as above, but using a single function to handle all 3 types of Windows messages. I'm sure there's a Windows constant for the 2 numbers I used in the Switch statement for $iMsg, but I wasn't able to find out what it was. The 273 is used to activate the button control function, which also works on combobox controls, the 274 is the GUI messages for things like the sysmenu and the close button. If anyone knows the variables for those 2 numbers, please let me know.

AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> GUIRegisterMsg($WM_COMMAND, "_WM_EXTRACTOR") GUIRegisterMsg($WM_SYSCOMMAND, "_WM_EXTRACTOR") GUIRegisterMsg($WM_HSCROLL, "_WM_EXTRACTOR") $GUI = GUICreate("Test GUI", 200, 200, -1, -1, BitOR($WS_THICKFRAME, $gui_ss_default_gui)) $Button = GUICtrlCreateButton("Test", 20, 20) $Button2 = GUICtrlCreateButton(" Another Test ", 20, 100) $Slider = GUICtrlCreateSlider(20, 60, 150) $hSlider = GUICtrlGetHandle($Slider) GUISetState() While 1     $msg = GUIGetMsg()     Switch $msg         Case $Button             MsgBox(0, "", "You pressed the button marked TEST")         Case $Button2             MsgBox(0, "", "You pressed the button marked 'Another Test'")     EndSwitch WEnd Func _WM_EXTRACTOR($hWnd, $iMsg, $wParam, $lParam)     Local $nNotifyCode = BitShift($wParam, 16)     Local $nID = BitAND($wParam, 0x0000FFFF)     Local $hCtrl = $lParam     #forceref $hWnd, $iMsg, $wParam, $lParam     Switch $iMsg         Case $WM_HSCROLL             Switch $lParam                 Case $hSlider                     ConsoleWrite("+Slider moved to: " & GUICtrlRead($Slider) & @LF)             EndSwitch         Case $WM_VSCROLL         Case 273             Switch $nID                 Case $Button                     ConsoleWrite(">Button Test pressed" & @LF)                 Case $Button2                     ConsoleWrite(">Button Another Test pressed" & @LF)                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch         Case 274             Switch $wParam                 Case 0xf060                     ConsoleWrite("!Exit pressed" & @LF)                     Exit                 Case 0xF120                     ConsoleWrite("!Restore window" & @LF)                 Case 0xF020                     ConsoleWrite("!Minimize Window" & @LF)                 Case 0xF093                     ConsoleWrite("!System menu pressed" & @LF)                 Case 0xF010                     ConsoleWrite("!System menu Move Option pressed" & @LF)                     Return 0                 Case 0xF000                     ConsoleWrite("!System menu Size Option pressed" & @LF)                     Return 0                 Case 0xF002 ; This and the following case statements are only valid when the GUI is resizable                     ConsoleWrite("!Right side of GUI clicked" & @LF)                     Return 0                 Case 0xf001                     ConsoleWrite("!Left side of GUI clicked" & @LF)                     Return 0                 Case 0xF008                     ConsoleWrite("!Lower Right corner of GUI clicked" & @LF)                     Return 0                 Case 0xF007                     ConsoleWrite("!Lower Left corner of GUI clicked" & @LF)                     Return 0                 Case 0xF006                     ConsoleWrite("!Bottom side of GUI clicked" & @LF)                     Return 0                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch     EndSwitch     Return $GUI_RUNDEFMSG EndFunc   ;==>_WM_EXTRACTOR

  • Bowmore likes this

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#5 taietel

taietel

    I'm the third from the left...

  • Active Members
  • PipPipPipPipPipPip
  • 722 posts

Posted 18 November 2011 - 05:16 AM

Here?

#6 guinness

guinness

    guinness

  • MVPs
  • 10,433 posts

Posted 18 November 2011 - 07:16 AM

Nice idea BrewManNH and a good example for those who are keen to learn how GUIRegisterMsg works.

Example List: _AdapterConnections()_AlwaysRun()_AppMon()_AppMonEx()_BinaryBin()_CheckMsgBox()_CmdLineRaw()_ContextMenu()_DesktopDimensions()_DisplayPassword()_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()_GUISetIcon()_Icon_Clear()/_Icon_Set()_InetGet()_InetGetGUI()_InetGetProgress()_IPDetails()_IsFileOlder()_IsGUID()_IsHex()_IsPalindrome()_IsRegKey()_IsStringRegExp()_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()_StringIsValid()_StringReplaceWholeWord()_StringStripChar()_Temperature()_TrialPeriod()_UKToUSDate()/_USToUKDate()_WinAPI_CreateGUID()_WMIDateStringToDate()/_DateToWMIDateString()AutoIt SearchAutoIt3 PortableAutoItWinGetTitle()/AutoItWinSetTitle()CodingFileInstallrGeoIP databaseGUI - Only Close ButtonGUI ExamplesGUICtrlDeleteImage()GUICtrlGetBkColor()GUICtrlGetStyle()GUIGetBkColor()LockFile()PasteBinSciTE JumpSignature CreatorWM_COPYDATAMore Examples...Updated: 11/04/2013


#7 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 18 November 2011 - 06:23 PM

Here?

Yeah, that's the ones I was looking for. They're in WindowsConstants.au3 but in Hex so it threw me off. :D So, replace 274 with $WM_SYSCOMMAND and 273 with $WM_COMMAND, which makes perfect sense because that's what messages I am looking for at those points in the script.

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#8 WinkleDoodle

WinkleDoodle

    Seeker

  • Active Members
  • 23 posts

Posted 19 November 2011 - 12:52 AM

This is really cool.. now I won't have to cram *everything* in my main loop any more, functions are not blocking GUI messages e.g.

AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND") GUIRegisterMsg($WM_SYSCOMMAND, "MY_SYS_COMMAND") GUIRegisterMsg($WM_HSCROLL, "WM_HVSCROLL") $GUI = GUICreate("Test GUI", 200, 200, -1, -1, BitOR($WS_THICKFRAME, $gui_ss_default_gui)) $Button = GUICtrlCreateButton("Test", 20, 20) $Button2 = GUICtrlCreateButton(" Another Test ", 20, 100) $Slider = GUICtrlCreateSlider(20, 60, 150) $hSlider = GUICtrlGetHandle($Slider) GUISetState() Global $iStopTest = 0 _Test() While 1     Sleep(100) WEnd Func _Test()     While 1         If $iStopTest Then             ConsoleWrite('_test function stopped!' &@lf)             ExitLoop         EndIf         Sleep(1000)         ConsoleWrite(@HOUR&@MIN&@SEC& " working..." &@lf)     WEnd EndFunc Func MY_SYS_COMMAND($hWnd, $msg, $wParam, $lParam)     Local $nNotifyCode = BitShift($wParam, 16)     Local $nID = BitAND($wParam, 0x0000FFFF)     Local $hCtrl = $lParam     Switch $wParam         Case 0xf060             ConsoleWrite("!Exit pressed" & @LF)             Exit         Case 0xF120             ConsoleWrite("!Restore window" & @LF)         Case 0xF020             ConsoleWrite("!Minimize Window" & @LF)     EndSwitch     Return $GUI_RUNDEFMSG ; Only workout clicking on the button EndFunc   ;==>MY_SYS_COMMAND Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam)     Local $nNotifyCode = BitShift($wParam, 16)     Local $nID = BitAND($wParam, 0x0000FFFF)     Local $hCtrl = $lParam     Switch $nID         Case $Button             MsgBox(0, "", "You pressed the button marked TEST, stopping '_Test' function")             $iStopTest = 1         Case $Button2             MsgBox(0, "", "You pressed the button marked 'Another Test'")     EndSwitch     Return $GUI_RUNDEFMSG EndFunc   ;==>MY_WM_COMMAND Func WM_HVSCROLL($hWnd, $iMsg, $wParam, $lParam)     #forceref $hWnd, $iMsg, $wParam, $lParam     Switch $iMsg         Case $WM_HSCROLL             Switch $lParam                 Case $hSlider                     ConsoleWrite("+Slider moved to: " & GUICtrlRead($Slider) & @LF)             EndSwitch         Case $WM_VSCROLL     EndSwitch     Return $GUI_RUNDEFMSG EndFunc   ;==>WM_HVSCROLL


#9 Bowmore

Bowmore

    Feinschmecker

  • Active Members
  • PipPipPipPipPipPip
  • 781 posts

Posted 19 November 2011 - 12:58 PM

I have often used WM_COMMAND messages to add extra functionality to controls like edit and combo boxes. However this has inspired me to start and re-write some of my GUIs to give users a clean way to stop long blocking functions, by allowing them to click a button on the GUI which will exit the function in a controlled way.

Thank you BrewManNH
"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

#10 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 19 November 2011 - 04:39 PM

In case anyone was wondering how this method of reading controls could be used to stop blocking functions as mentioned by Bowmore above, try using the code below to simulate how this can be used to bypass long looping functions, or functions that take a long time to process. I've also added OnEvent mode functions so you can test it using both methods.

If you run this script and then press the Test button (with the GUIRegisterMsg for WM_SYSCOMMAND commented out) the script will loop for 10 seconds and you won't be able to exit the GUI until that loop has finished. With that message uncommented, it will exit immediately. Also, you can see that you can still action the other controls while the loop is running.

AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Opt("GUIOnEventMode", 1) ; <<<<<<<<<<< comment this out to use MessageLoop mode GUIRegisterMsg($WM_COMMAND, "_WM_EXTRACTOR") ;~ GUIRegisterMsg($WM_SYSCOMMAND, "_WM_EXTRACTOR") ; Try exiting the GUI after pressing the Test button, with this commented out, before 10 seconds has passed GUIRegisterMsg($WM_HSCROLL, "_WM_EXTRACTOR") $GUI = GUICreate("Test GUI", 200, 200, -1, -1, BitOR($WS_THICKFRAME, $gui_ss_default_gui)) GUISetOnEvent($GUI_EVENT_CLOSE, "_Exit") $Button = GUICtrlCreateButton("Test", 20, 20) GUICtrlSetOnEvent(-1, "_Button") $Button2 = GUICtrlCreateButton(" Another Test ", 20, 100) GUICtrlSetOnEvent(-1, "_Button2") $Slider = GUICtrlCreateSlider(20, 60, 150) $hSlider = GUICtrlGetHandle($Slider) GUISetState() While 1     $msg = GUIGetMsg()     Switch $msg         Case $Button             MsgBox(0, "", "You pressed the button marked TEST")             For $I = 1 To 1000                 Sleep(1)             Next         Case $Button2             MsgBox(0, "", "You pressed the button marked 'Another Test'")         Case $GUI_EVENT_CLOSE             Exit     EndSwitch WEnd Func _WM_EXTRACTOR($hWnd, $iMsg, $wParam, $lParam)     Local $nNotifyCode = BitShift($wParam, 16)     Local $nID = BitAND($wParam, 0x0000FFFF)     Local $hCtrl = $lParam     #forceref $hWnd, $iMsg, $wParam, $lParam     Switch $iMsg         Case $WM_HSCROLL             Switch $lParam                 Case $hSlider                     ConsoleWrite("+Slider moved to: " & GUICtrlRead($Slider) & @LF)             EndSwitch         Case $WM_VSCROLL         Case 273             Switch $nID                 Case $Button                     ConsoleWrite(">Button Test pressed" & @LF)                 Case $Button2                     ConsoleWrite(">Button Another Test pressed" & @LF)                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch         Case 274             Switch $wParam                 Case 0xf060                     ConsoleWrite("!Exit pressed" & @LF)                     Exit                 Case 0xF120                     ConsoleWrite("!Restore window" & @LF)                 Case 0xF020                     ConsoleWrite("!Minimize Window" & @LF)                 Case 0xF093                     ConsoleWrite("!System menu pressed" & @LF)                 Case 0xF010                     ConsoleWrite("!System menu Move Option pressed" & @LF)                     Return 0                 Case 0xF000                     ConsoleWrite("!System menu Size Option pressed" & @LF)                     Return 0                 Case 0xF002 ; This and the following case statements are only valid when the GUI is resizable                     ConsoleWrite("!Right side of GUI clicked" & @LF)                     Return 0                 Case 0xf001                     ConsoleWrite("!Left side of GUI clicked" & @LF)                     Return 0                 Case 0xF008                     ConsoleWrite("!Lower Right corner of GUI clicked" & @LF)                     Return 0                 Case 0xF007                     ConsoleWrite("!Lower Left corner of GUI clicked" & @LF)                     Return 0                 Case 0xF006                     ConsoleWrite("!Bottom side of GUI clicked" & @LF)                     Return 0                 Case Else                     MsgBox(0, "MY_WM_COMMAND", "GUIHWnd" & @TAB & ":" & $hWnd & @LF & _                             "MsgID" & @TAB & ":" & $msg & @LF & _                             "wParam" & @TAB & ":" & $wParam & @LF & _                             "lParam" & @TAB & ":" & $lParam & @LF & @LF & _                             "WM_COMMAND - Infos:" & @LF & _                             "-----------------------------" & @LF & _                             "Code" & @TAB & ":" & $nNotifyCode & @LF & _                             "CtrlID" & @TAB & ":" & $nID & @LF & _                             "CtrlHWnd" & @TAB & ":" & $hCtrl)             EndSwitch     EndSwitch     Return $GUI_RUNDEFMSG EndFunc   ;==>_WM_EXTRACTOR Func _Button()     MsgBox(0, "", "You pressed the button marked TEST")     For $I = 1 To 1000         Sleep(1)     Next EndFunc   ;==>_Button Func _Button2()     MsgBox(0, "", "You pressed the button marked 'Another Test'") EndFunc   ;==>_Button2 Func _Exit()     Exit EndFunc

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#11 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,397 posts

Posted 19 November 2011 - 04:41 PM

BrewManNH,

Have you ever seen the Interrupting a running function tutorial in the Wiki? :D

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#12 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 19 November 2011 - 04:52 PM

BrewManNH,

Have you ever seen the Interrupting a running function tutorial in the Wiki? :D

M23

I've read it many times. I was just explaining how this method could be done with the code I posted in case anyone else hasn't seen it. Also, this example deals with a lot more than just interrupting a function with Windows Messages, but Bowmore's comment prompted me to explain what he was referring to.

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#13 WinkleDoodle

WinkleDoodle

    Seeker

  • Active Members
  • 23 posts

Posted 19 November 2011 - 05:22 PM

@Bowmore

That's precisely what I was hinting at, albeit more eloquently put. :D

I've successfully updated a project of mine using this method. there's with one caveat though. Any long looping routines inside a WM_COMMAND message can deadlock the whole WM_COMMAND message, which in worse case scenario leads to application freezing, leaving one to wait till the routine finishes or killing the process.

But... of course, with GUICreateDummy() there's a nifty little work around. You'll need GUIOnEvent(), (I don't think this is possible with GUIGetMsg()..).

AutoIt         
#include <ButtonConstants.au3> #include <EditConstants.au3> #include <GUIConstantsEx.au3> #include <StaticConstants.au3> #include <WindowsConstants.au3> Global $hGUI, $edtData, $btnStart, $btnClose, $lbStatus, $dummyCallBlockingFunction ; this variable is checked inside our "BlockingFunction" ; if it is false then the routine stops Global $fRUNNING = False _Example_GUIOnEvent() Func _Example_GUIOnEvent()     ; OnEventMode because it's not possible to use GUIGetMsg() when a function is     Opt("GUIOnEventMode", 1)     $hGUI = GUICreate("WM_SYSCOMMAND/WM_COMMAND Example", 300, 141, 192, 204, BitOR($WS_THICKFRAME, $gui_ss_default_gui))     $edtData = GUICtrlCreateEdit("", 8, 16, 290, 57)     $btnStart = GUICtrlCreateButton("Start", 0, 88, 75, 25)     $btnClose = GUICtrlCreateButton("Close", 80, 88, 75, 25)     $lbStatus = GUICtrlCreateLabel("Ready", 0, 120, 196, 17, $SS_SUNKEN)     $dummyCallBlockingFunction = GUICtrlCreateDummy()     ; All "BlockingFunctions" e.g. functions that have long looping routines should have     ; a dummy control to pass the burden to this will allow other message to be processed     ; we don't need to set GUICtrlOnEvent for other controls because they will by processed     ; by WM_COMMAND and WM_SYSCOMMAND     GUICtrlSetOnEvent($dummyCallBlockingFunction, "BlockingFunction")     ; handles all GUICtrl*Create and _GUICtrl*Create events     GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")     ; handles GUI_EVENT_CLOSE, etc events     GUIRegisterMsg($WM_SYSCOMMAND, "WM_SYSCOMMAND")     GUISetState(@SW_SHOW)     While 1         Sleep(100)     WEnd EndFunc Func Close()     GUICtrlSetData($edtData, "Shutting down..." &@CRLF)     Sleep(1000)     Exit EndFunc Func BlockingFunction()     $fRUNNING = True     GUICtrlSetData($btnStart, "Stop")     GUICtrlSetData($lbStatus, "Started!" &@lf)     GUICtrlSetData($edtData, @HOUR &":"& @MIN &":"& @SEC & " Inializing test.." &@CRLF)     While 1         If $fRUNNING = False Then ExitLoop         GUICtrlSetData($edtData, @HOUR &":"& @MIN &":"& @SEC & " Running test: Quantum spacetime" &@CRLF)         Sleep(1000)         GUICtrlSetData($edtData, @HOUR &":"& @MIN &":"& @SEC & " Running test: Splitting atoms" &@CRLF)         Sleep(100)     WEnd     GUICtrlSetData($edtData, @HOUR &":"& @MIN &":"& @SEC & " Testing aborted by user" &@CRLF)     GUICtrlSetData($lbStatus, "Stopped!" &@lf)     $fRUNNING = False     GUICtrlSetData($btnStart ,"Start") EndFunc Func WM_SYSCOMMAND($hWnd, $iMsg, $iwParam, $ilParam)     Local $hWndFrom, $iIDFrom, $iCode     #forceref $hWnd, $iMsg, $iwParam, $ilParam, $hWndFrom, $iIDFrom, $iCode     $hWndFrom = $ilParam     $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word     $iCode = BitShift($iwParam, 16) ; Hi Word     Switch $iwParam         Case 0xF060 ;SC_CLOSE             ConsoleWrite("!Close window" & @LF)             Close()         Case 0xF120 ;SC_RESTORE             ConsoleWrite("!Restore window" & @LF)         Case 0xF020 ;SC_MAXIMIZE             ConsoleWrite("!Maximize window" & @LF)     EndSwitch     Return $GUI_RUNDEFMSG ; Only workout clicking on the button EndFunc   ;==>MY_SYS_COMMAND Func WM_COMMAND($hWnd, $iMsg, $iwParam, $ilParam)     Local $hWndFrom, $iIDFrom, $iCode     #forceref $hWnd, $iMsg, $iwParam, $ilParam, $hWndFrom, $iIDFrom, $iCode     $hWndFrom = $ilParam     $iIDFrom = BitAND($iwParam, 0xFFFF) ; Low Word     $iCode = BitShift($iwParam, 16) ; Hi Word     ; $hWndFrom is equal to @GUI_CtrlHandle     ; $iIDFrom is equal to  @GUI_CtrlId     Switch $iIDFrom         Case $btnStart             ; "Blocking functions" need to be passed onto a dummy control             ; so that we can process other messages             If Not $fRUNNING Then                 GUICtrlSendToDummy($dummyCallBlockingFunction, 1)             Else                 $fRUNNING = False             EndIf         Case $btnClose             ; Non blocking functions don't need a dummy control             Close()     EndSwitch     Return $GUI_RUNDEFMSG EndFunc


#14 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,397 posts

Posted 19 November 2011 - 05:31 PM

WinkleDoodle,

You should never put "long looping routines" inside any WM_ message handler. When the Help file for GUIRegisterMsg tells you:

"Warning: blocking of running user functions which executes window messages with commands such as "Msgbox()" can lead to unexpected behavior, the return to the system should be as fast as possible !!!"

it means what it says! :oops:

Windows relies on the free passage of these messages - you block the flow at your peril. :D

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#15 WinkleDoodle

WinkleDoodle

    Seeker

  • Active Members
  • 23 posts

Posted 19 November 2011 - 05:39 PM

Yes, but not everybody reads past "Warning:.." :D

#16 Melba23

Melba23

    Yes, me!

  • Moderators
  • 15,397 posts

Posted 19 November 2011 - 05:57 PM

WinkleDoodle,

Your funeral. :D

M23
StringSize - Automatically size controls to fit text - ExtMsgBox - A user customisable replacement for MsgBox

Toast - Small GUIs which pop out of the Systray - Marquee - Scrolling tickertape GUIs

Scrollbars - Automatically sized scrollbars with a single command - GUIFrame - Subdivide GUIs into many adjustable frames

GUIExtender - Extend and retract multiple sections within a GUI - NoFocusLines - Remove the dotted focus lines from buttons, sliders, radios and checkboxes

ChooseFileFolder - Single and multiple selections from specified path tree structure - - Notify - Small notifications on the edge of the display

RecFileListToArray - An alternative to _FileListToArray with user-defined include/exclude masks, maximum recursion level, sorting and displayed path options

GUIListViewEx - Insert, delete, move, drag and sort ListView items


#17 Bowmore

Bowmore

    Feinschmecker

  • Active Members
  • PipPipPipPipPipPip
  • 781 posts

Posted 19 November 2011 - 07:06 PM

This is a simple example of what I had in mind in my post above.
AutoIt         
#include <GUIConstantsEx.au3> #include <WindowsConstants.au3> Global $__gbRun = False GUIRegisterMsg($WM_COMMAND, "MY_WM_COMMAND") $GUI = GUICreate("Test GUI", 100, 100, -1, -1, BitOR($WS_THICKFRAME, $gui_ss_default_gui)) $Button = GUICtrlCreateButton("Start", 40, 20, 50, 20) GUISetState() While 1     $msg = GUIGetMsg()     Switch $msg         Case $GUI_EVENT_CLOSE             Exit         Case $Button             GUICtrlSetData($Button, "Stop")             $__gbRun = True             _LongFunc()            $__gbRun = False            GUICtrlSetData($Button, "Start")     EndSwitch WEnd Func _LongFunc()     While 1         If $__gbRun = False Then             If MsgBox(262452, "Warning", "Do you really want to stop the process?") = 6 Then                 ;clean up code goes here                 ExitLoop             Else                  $__gbRun = True             EndIf        EndIf        ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : _LongFunc = ' & "Running" & @CRLF &  '>Error code: ' & @error & @CRLF) ;### Debug Console       Sleep(1000)    WEnd EndFunc ;==>_LongFunc Func MY_WM_COMMAND($hWnd, $msg, $wParam, $lParam) Local $nNotifyCode = BitShift($wParam, 16) Local $nID = BitAND($wParam, 0x0000FFFF) Local $hCtrl = $lParam    Switch $nID         Case $Button              If GUICtrlRead($Button) = "Stop" And $__gbRun = True Then                 $__gbRun = False                 ConsoleWrite(">Button Start/Stop pressed" & @LF)                 Return 0              EndIf    EndSwitch    Return $GUI_RUNDEFMSG EndFunc ;==>MY_WM_COMMAND ;

Edited by Bowmore, 19 November 2011 - 07:14 PM.

"Programming today is a race between software engineers striving to build bigger and better idiot-proof programs, and the universe trying to build bigger and better idiots. So far, the universe is winning."- Rick Cook

#18 Chad2

Chad2

    Seeker

  • Active Members
  • 20 posts

Posted 21 February 2012 - 07:34 PM

Just to be clear on the blocking function. Does this only have to do with loops or blocks like msgbox inside the result of a registerMsg.

Example: Will this fire off the function _aboutPage() and close out the WM_SYSCOMMAND Function - IE Non Blocking?

Func _WM_SYSCOMMAND($hGUI, $iMsg, $wParam, $lParam) If $iMsg = $WM_SYSCOMMAND Then   If _WinAPI_LoWord($wParam) = 0x3000 Then ; About item selected from menu    _aboutPage()   EndIf EndIf EndFunc   ;==>_WM_SYSCOMMAND


#19 BrewManNH

BrewManNH

    באָבקעס מיט קודוצ׳ה

  • MVPs
  • 6,877 posts

Posted 21 February 2012 - 08:34 PM

It should jump to the _aboutPage function, it depends on what is in that function as to whether you lock up or not. Try it and see, just don't put an endless Do/While loop in the function and it will probably be ok. The MsgBox function hasn't ever caused a lock up for me using windows messages, this demo uses the MsgBox function in it without crashing it, but that's not to say it can't.

Just remember, a Windows message handler like this can't interrupt another windows message function until the first windows message function has ended. What I mean by this is, in this demo, if you put _Button() under the consolewrite for the $Button windows message, it will jump to the _Button function, but no other windows messages can be handled until that function ends, 10 seconds later.

How to ask questions the smart way!

Back up and restore Windows user files _Array.au3 - Modified array functions that include support for 2D arrays.ColorChooser - An add-on for SciTE that pops up a color dialog so you can select and paste a color code into a script.

Customizable Splashscreen GUI w/Progress Bar - Create a custom "splash screen" GUI with a progress bar and custom label.

_FileGetProperty - Retrieve the properties of a file SciTE Toolbar - A toolbar demo for use with the SciTE editorGUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.

GUIToolTip UDF Demo - Demo script to show how to use the GUIToolTip UDF to create and use customized tooltips.

Posted Image


#20 AZJIO

AZJIO

    Universalist

  • Active Members
  • PipPipPipPipPipPip
  • 1,001 posts

Posted 21 February 2012 - 08:57 PM

BrewManNH
WM_Ru.7z - 70 files. I hope it will be interesting to see.




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users