Jump to content

[SOLVED]help with handling events and logic


Recommended Posts

I'm using ANYGUI code created my quaizywabbit to create a control in my gui but I can't figure out a way to handle that control when i right click on the context menu and click the menuitem i want it to close the program but it doesn't?

#include <guiconstants.au3>
#include <anygui.au3>

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()


While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $button
            Test()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch
WEnd

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUISetState()
EndFunc
Edited by Hypertrophy
Link to comment
Share on other sites

You can do it using event mode:

#include <guiconstants.au3>
#include <anygui.au3>
Opt("GUIOnEventMode", 1)

$gui = GUICreate("GUI", 377, 377)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
GUICtrlCreateButton("click me", 70, 277, 70)
GUICtrlSetOnEvent(-1, "Test")
GUISetState()

While 1
    Sleep(20)
WEnd

Func _Close()
    GUIDelete($gui)
    Exit
EndFunc

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUICtrlSetOnEvent(-1, "_me")
    GUISetState()
EndFunc
 
 Func _me()
     ConsoleWrite("Clicked me" & @CRLF)
EndFunc

..or using some buggy code:

#include <guiconstants.au3>
#include <anygui.au3>

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

$item1 = -1 ; 0 will not do because it'll match the msg  of the GUIGetMsg():


While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $button
            Test()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $item1
            ConsoleWrite("Clicked me" & @CRLF)
    EndSwitch
WEnd

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUISetState()
EndFunc
Link to comment
Share on other sites

  • 2 weeks later...

You can do it using event mode:

#include <guiconstants.au3>
#include <anygui.au3>
Opt("GUIOnEventMode", 1)

$gui = GUICreate("GUI", 377, 377)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Close")
GUICtrlCreateButton("click me", 70, 277, 70)
GUICtrlSetOnEvent(-1, "Test")
GUISetState()

While 1
    Sleep(20)
WEnd

Func _Close()
    GUIDelete($gui)
    Exit
EndFunc

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUICtrlSetOnEvent(-1, "_me")
    GUISetState()
EndFunc
 
 Func _me()
     ConsoleWrite("Clicked me" & @CRLF)
EndFunc

..or using some buggy code:

#include <guiconstants.au3>
#include <anygui.au3>

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

$item1 = -1 ; 0 will not do because it'll match the msg  of the GUIGetMsg():


While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
        Case $button
            Test()
        Case $GUI_EVENT_CLOSE
            Exit
        Case $item1
            ConsoleWrite("Clicked me" & @CRLF)
    EndSwitch
WEnd

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUISetState()
EndFunc

I'm sorry. I wasn't clear in my first place. I want to do something more than ConsoleWrite(). I want that menuitem being clicked on result in the deletion of that control. I tried putting in GUICtrlDelete($pic) but it didn't work, it deleted the entire GUI.

Edited by Hypertrophy
Link to comment
Share on other sites

bump

You created the controls in a function and so all the variables in there were Local variables since you didn't specify otherwise, so they can't be seen outside of the function.

Try

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include "G:\anygui\anygui.au3"

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

$item1 = -1 ; 0 will not do because it'll match the msg of the GUIGetMsg():
Global $target,$pic,$menu,$item1;<----- Global declaration so th evariables are available outside of the function

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $item1
    ConsoleWrite("Clicked me" & @CRLF)
            GUISwitch($target)
            guictrldelete($item1);<------ deletes menu item
    EndSwitch
WEnd

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUISetState()
EndFunc
Edited by martin
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

Once again, awesome work. Thank you so much. :) Quick question though, I changed line 25 from $item1 to $pic so it would delete the button instead. The only thing I can't understand is why it will only create the button once. After I delete it one time it won't get re-created by clicking the "click me" button.

A simple changed but here's the code nonetheless:

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <anygui.au3>

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

$item1 = -1 ; 0 will not do because it'll match the msg of the GUIGetMsg():
Global $target,$pic,$menu,$item1;<----- Global declaration so th evariables are available outside of the function

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $item1
    ConsoleWrite("Clicked me" & @CRLF)
            GUISwitch($target)
            guictrldelete($pic);<------ deletes button
    EndSwitch
WEnd

Func Test()
    $target = _GuiTarget ("GUI")
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
    $pic = $pic[0]

    $menu = GUICtrlCreateContextMenu($pic)
    $item1 = GUICtrlCreateMenuItem("me",$menu)
    GUISetState()
EndFunc
Link to comment
Share on other sites

Once again, awesome work. Thank you so much. :) Quick question though, I changed line 25 from $item1 to $pic so it would delete the button instead. The only thing I can't understand is why it will only create the button once. After I delete it one time it won't get re-created by clicking the "click me" button.

A simple changed but here's the code nonetheless:

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include <anygui.au3>

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

$item1 = -1 ; 0 will not do because it'll match the msg of the GUIGetMsg():
Global $target,$pic,$menu,$item1;<----- Global declaration so th evariables are available outside of the function

While 1
 $msg = GUIGetMsg(1)
 Switch $msg[0]
 Case $button
 Test()
 Case $GUI_EVENT_CLOSE
 Exit
 Case $item1
 ConsoleWrite("Clicked me" & @CRLF)
 GUISwitch($target)
 guictrldelete($pic);<------ deletes button
 EndSwitch
WEnd

Func Test()
 $target = _GuiTarget ("GUI")
 $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
 $pic = $pic[0]

 $menu = GUICtrlCreateContextMenu($pic)
 $item1 = GUICtrlCreateMenuItem("me",$menu)
 GUISetState()
EndFunc

The problem is that you need to study the anygui udf. ;)

When you create a button, a child window is created and the button is a control in that child window. So if you want to delete the button you have to delete the button and the child window. (Well ok, you could just delete the child window.) If you don't then a new child is created but it will be hidden by the old one.

By setting $pic = $pic[0] you have lost access to the child window whose handle was $pic[2] untill then. So the answer is don't so that. Leave the returned result from _TargetAddButton as an array then you can do what you want with it later.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include "G:\anygui\anygui.au3"

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

Global $pic = -1, $menu = -1, $item1 = -1, $pic2 = -1;<----- Global declaration so th evariables are available outside of the function

Global $target = _GuiTarget("GUI");<--I moved this out of the function. You only need to create the $target once for a particular window.

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $item1
    ConsoleWrite("Clicked me" & @CRLF)
    GUISwitch($target)
    ;GUICtrlDelete($pic[0]);<------ deletes button
    GUIDelete($pic[2])
    $pic = -1
    $menu = -1
    $item1 = -1
    EndSwitch
WEnd



Func Test()

    If $pic = -1 Then
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
     GUISetState(); to show the newly created child window
    EndIf
    If $menu = -1 Then $menu = GUICtrlCreateContextMenu($pic[0])
    If $item1 = -1 Then $item1 = GUICtrlCreateMenuItem("me", $menu)
 
EndFunc ;==>Test
Serial port communications UDF Includes functions for binary transmission and reception.printing UDF Useful for graphs, forms, labels, reports etc.Add User Call Tips to SciTE for functions in UDFs not included with AutoIt and for your own scripts.Functions with parameters in OnEvent mode and for Hot Keys One function replaces GuiSetOnEvent, GuiCtrlSetOnEvent and HotKeySet.UDF IsConnected2 for notification of status of connected state of many urls or IPs, without slowing the script.
Link to comment
Share on other sites

The problem is that you need to study the anygui udf. :)

When you create a button, a child window is created and the button is a control in that child window. So if you want to delete the button you have to delete the button and the child window. (Well ok, you could just delete the child window.) If you don't then a new child is created but it will be hidden by the old one.

By setting $pic = $pic[0] you have lost access to the child window whose handle was $pic[2] untill then. So the answer is don't so that. Leave the returned result from _TargetAddButton as an array then you can do what you want with it later.

; *** Start added by AutoIt3Wrapper ***
#include <GUIConstantsEx.au3>
; *** End added by AutoIt3Wrapper ***
#AutoIt3Wrapper_Add_Constants=n
#include <guiconstants.au3>
#include "G:\anygui\anygui.au3"

$gui = GUICreate("GUI", 377, 377)
$button = GUICtrlCreateButton("click me", 70, 277, 70)
GUISetState()

Global $pic = -1, $menu = -1, $item1 = -1, $pic2 = -1;<----- Global declaration so th evariables are available outside of the function

Global $target = _GuiTarget("GUI");<--I moved this out of the function. You only need to create the $target once for a particular window.

While 1
    $msg = GUIGetMsg(1)
    Switch $msg[0]
    Case $button
    Test()
    Case $GUI_EVENT_CLOSE
    Exit
    Case $item1
    ConsoleWrite("Clicked me" & @CRLF)
    GUISwitch($target)
    ;GUICtrlDelete($pic[0]);<------ deletes button
    GUIDelete($pic[2])
    $pic = -1
    $menu = -1
    $item1 = -1
    EndSwitch
WEnd



Func Test()

    If $pic = -1 Then
    $pic = _TargetAddButton("Test", 77, 77, 77, 77, -1, -1, $target)
     GUISetState(); to show the newly created child window
    EndIf
    If $menu = -1 Then $menu = GUICtrlCreateContextMenu($pic[0])
    If $item1 = -1 Then $item1 = GUICtrlCreateMenuItem("me", $menu)
 
EndFunc ;==>Test

Ahh, that's where the problem was. Thank you very much. I appreciate all your help.
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...