Jump to content

GUICtrlSetOnEvent


Recommended Posts

I noticed that when I create a GUI and set the event on button click with GUICtrlSetOnEvent sometimes the event doesn't work so when I click the button nothing happens. The GUI is the current GUI when I create the button and use the GUICtrlSetOnEvent function.  It mainly occures when I call MsgBox function or _ArrayDisplay prior to creating the GUI.  I don't know if I can provide example script but wanted to let you know.

Link to comment
Share on other sites

_ArrayDisplay is a blocking function so your GUI controls won't work, MsgBox could also be a  modal window which will block it, depending upon what parameters you use i calling it.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

2 hours ago, grzesiek said:

MsgBox function or _ArrayDisplay prior to creating the GUI. 

@BrewManNH  I was thinking the same as you, but he said prior so the GUI doesn't exist at the moment MsgBox or ArrayDisplay is showed.

Edited by Nine
Link to comment
Share on other sites

@Nine Good point, missed that part.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I managed to make the example. The message boxes appears and then you can click the OK button, which doesn't work, but if I don't register WM_ACTIVATE it works.

Opt("GUIOnEventMode", 1)

GUICreate("")
GUISetState()
GUIRegisterMsg(0x6, "WM_ACTIVATE")

Func WM_ACTIVATE($hWnd, $iMsg, $wParam, $lParam)
EndFunc

Global $GUI = 0, $OK = 0

AdlibRegister("Adlib", 50)

MsgBox(0,"","")
$GUI = 1
MsgBox(0,"","")
MsgBox(0,"","")
MsgBox(0,"","")


Func Adlib()

   If $GUI Then
   
      $GUI = 0

      GUICreate("", 200, 200)
      GUICtrlCreateButton("OK", 50, 50, 100, 50)
      GUICtrlSetOnEvent(-1, "OK")
      GUISetState()

      Do
      Until Sleep(50) And $OK

      MsgBox(0,"","OK")

   EndIf

EndFunc

Func OK()

   $OK = 1

EndFunc

 

Edited by grzesiek
Link to comment
Share on other sites

Why are you recreating the GUI every 50ms?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

I don't recreate every 50 ms, only if $GUI is set to 1 and then it returns to 0, so you can add $GUI = 0. The workaround is to change to GUIGetMsg() instead of GUICtrlSetOnEvent to detect click.

Edited by grzesiek
Link to comment
Share on other sites

@grzesiek I understand the problem.  If you remove the adlibregister () and call your adlib () function after setting $GUI = 1, it is working fine. Like this :

Global $GUI = 0, $OK = 0

;AdlibRegister("Adlib", 50)

MsgBox(0,"","")
$GUI = 1
Adlib ()
MsgBox(0,"","")

The problem you are creating is you give control the a MsgBox () and you steal its control to give it to the 2nd GUI with adlibregister ().  But the current control still remains to the MsgBox (), that is why the GUI is not responding.  If you call it explicitly like I am doing, you will never get the issue, not matter what you do before or after. Not a bug, just misunderstanding how Windows works.

Link to comment
Share on other sites

Why it works when I don't register WM_ACTIVATE? So I understand that somehow the Adlib function is called during MsgBox? The script is stopped when I call MsgBox so the Adlib function is not called at that time, but only when I close MsgBox.

Link to comment
Share on other sites

If you monitor the calls to WM_ACTIVATE function.  It is exactly what I was telling you.  The first GUI is never deactivated by using adlibregister () to invoke 2nd GUI.  Moreover, no more messages are sent to WM_ACTIVATE function after that.  It seems that windows stop sending this message when all the conditions you created are gathered together.  You will have to ask Microsoft cause it is not an issue with AutoIt.  You could replicate the same behavior with VB if you wish to.

Link to comment
Share on other sites

I noticed that if I also register WM_NULL message and send this message to first GUI inside Adlib then it works.

 

Opt("GUIOnEventMode", 1)

Global $hGUI = GUICreate("")
GUISetState()
GUIRegisterMsg(0x6, "WM_ACTIVATE")
GUIRegisterMsg(0x0, "WM_NULL")

Func WM_ACTIVATE($hWnd, $iMsg, $wParam, $lParam)
EndFunc

Func WM_NULL($hWnd, $iMsg, $wParam, $lParam)
EndFunc

Global $GUI = 0, $OK = 0

AdlibRegister("Adlib", 50)

MsgBox(0,"","")
$GUI = 1
MsgBox(0,"","")
MsgBox(0,"","")
MsgBox(0,"","")

Func Adlib()

   If $GUI Then

      $GUI = 0

      DllCall("User32.dll", "lresult", "SendMessage", "hwnd", $hGUI, "uint", 0, "wparam", 0, "lparam", 0)

      GUICreate("", 200, 200)
      GUICtrlCreateButton("OK", 50, 50, 100, 50)
      GUICtrlSetOnEvent(-1, "OK")
      GUISetState()

      Do
      Until Sleep(50) And $OK

      MsgBox(0,"","OK")

   EndIf

EndFunc

Func OK()

   $OK = 1

EndFunc

 

Link to comment
Share on other sites

I need to ask this, what is the point of all this? What EXACTLY are you trying to show, prove, disprove, or report with all of this spaghetti coding?

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

1 minute ago, grzesiek said:

That I can't normally use GUICtrlSetOnEvent inside Adlib

Wrong, you can.

1 minute ago, grzesiek said:

and that Adlib collidates with messages.

Wrong again.

All your code shows is that you're calling the GUIRegisterMsg function too soon in your script. If you move the GUIRegisterMsg function to below the $GUI = 1 line, it works 100% of the time. See below.

Opt("GUIOnEventMode", 1)
Global $hGUI = GUICreate("")
ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hGUI = ' & $hGUI & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
GUISetState()
Func WM_ACTIVATE($hWnd, $iMsg, $wParam, $lParam)
     ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $hWnd = ' & $hWnd & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
EndFunc   ;==>WM_ACTIVATE
Global $GUI = 0, $OK = 0
AdlibRegister("Adlib", 50)
MsgBox(0, "", "1")
$GUI = 1
Sleep(60) ; used to make sure that the adlib function is called before the GUIRegisterMsg function is.
GUIRegisterMsg(0x6, "WM_ACTIVATE")
MsgBox(0, "", "2")
MsgBox(0, "", "3")
MsgBox(0, "", "4")
Func Adlib()
     If $GUI Then
          $GUI = 0
          DllCall("User32.dll", "lresult", "SendMessage", "hwnd", $hGUI, "uint", 0, "wparam", 0, "lparam", 0)
          $GUI1 = GUICreate("", 200, 200)
          ConsoleWrite('@@ Debug(' & @ScriptLineNumber & ') : $GUI1 = ' & $GUI1 & @CRLF & '>Error code: ' & @error & @CRLF) ;### Debug Console
          GUICtrlCreateButton("OK", 50, 50, 100, 50)
          GUICtrlSetOnEvent(-1, "OK")
          GUISetState()
          Do
          Until Sleep(50) And $OK
          MsgBox(0, "", "OK")
     EndIf
EndFunc   ;==>Adlib
Func OK()
     $OK = 1
EndFunc   ;==>OK

I moved the message handler call to below where the $GUI variable is set to 1, waited 60ms, then called it. I added a few ConsoleWrites in there to show which GUI is sending the messages to the message handler in case things weren't clear. Adlib functions work with message handlers, and with OnEvent triggers, you just have to code things better.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

Link to comment
Share on other sites

But since GUI messages are also fired during Sleep, isn't it possible that Adlib will be called durring some message? The other solution is unregister WM_ACTIVATE before $GUI = 1, so Adlib will be called without this message. I think I have now enough information, thank you.

Link to comment
Share on other sites

2 minutes ago, grzesiek said:

But since GUI messages are also fired during Sleep, isn't it possible that Adlib will be called durring some message?

Completely irrelevant to your previous claims. 

3 minutes ago, grzesiek said:

The other solution is unregister WM_ACTIVATE before $GUI = 1, so Adlib will be called without this message.

Adlib isn't being called with our without this message, completely separate functions that don't interact with each other.

The real solution is to learn where you're making mistakes and stop making them. It isn't to come up with a convoluted workaround when it's not necessary because you coded something else wrong. You're putting in even more spaghetti code to compensate for zero error checking and zero flow control.

If I posted any code, assume that code was written using the latest release version unless stated otherwise. Also, if it doesn't work on XP I can't help with that because I don't have access to XP, and I'm not going to.
Give a programmer the correct code and he can do his work for a day. Teach a programmer to debug and he can do his work for a lifetime - by Chirag Gude
How to ask questions the smart way!

I hereby grant any person the right to use any code I post, that I am the original author of, on the autoitscript.com forums, unless I've specifically stated otherwise in the code or the thread post. If you do use my code all I ask, as a courtesy, is to make note of where you got it from.

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 editor  -  GUIRegisterMsg demo - Demo script to show how to use the Windows messages to interact with controls and your GUI.  -   Latin Square password generator

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