Jump to content

[SOLVED] _GUICtrlButton_Create


Recommended Posts

hello,

i know that _GUICtrlButton_Create has NO control-ID. but is there a way to

associate functions like GUICtrlSetOnEvent or a case-action if i am in a

while 1 $msg = GUIGetMsg() .... wend loop?

if both answers ar NO, what for is _GUICtrlButton_Create then?

maybe you ask why i want use that. in my script i have multiple gui's and if i want to add

buttons to the first gui for example, all other control-id's will change an thats a lot of work...

edit: solved it with Guiswitch()

Edited by andygo
Link to comment
Share on other sites

As far as you're concerned... Nothing. I have never used it myself.

As far as events go, you need to use WM_COMMAND...

GUICreate("test")

...

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

While 1
    ...
WEnd

Func WM_COMMAND($hWnd, $iMsg, $wParam, $lParam)
    Switch BitShift($wParam, 16)
        Case $BN_CLICKED
            ...
    EndSwitch
EndFunc
Link to comment
Share on other sites

  • Moderators

andygo,

This is a modified version of the example for _GUICtrlButton_Create taken from the Help file: :(

#include <GUIConstantsEx.au3>
#include <WindowsConstants.au3>
#Include <GuiButton.au3>
;#include <ButtonConstants.au3>

$hGUI = GUICreate("Test", 500, 500)

$hButton = _GUICtrlButton_Create($hGUI, "Button1", 10, 10, 90, 50)

GUISetState()

GUIRegisterMsg($WM_COMMAND, "WM_COMMAND")

$fBut_Command = False

While 1

    Switch GUIGetMsg()
        Case $GUI_EVENT_CLOSE
            Exit
    EndSwitch

    If $fBut_Command = True Then
        $fBut_Command = False
        MsgBox(0, "", "Button Pressed!")
    EndIf

WEnd

; React on a button click
Func WM_COMMAND($hWnd, $Msg, $wParam, $lParam)
    #forceref $hWnd, $Msg
    Switch $lParam ; Ctrl
        Case $hButton
            Switch BitShift($wParam, 16) ; Notify Code
                Case $BN_CLICKED
                    $fBut_Command = True
            EndSwitch
    EndSwitch
    Return $GUI_RUNDEFMSG
EndFunc   ;==>WM_COMMAND

if i want to add buttons to the first gui for example, all other control-id's will change

Why does this make it "a lot of work"? Surely you have your ControlIDs stored in variables? Could you please explain why you believe adding a button causes a problem, because at the moment I cannot se why it should. :)

M23

Edit:

solved it with Guiswitch()

Now I am really confused - what on earth has GUISwitch got to do with it? :idea: Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

An example to illustrate some principles:

#include <GuiConstantsEx.au3>
#include <GuiButton.au3>
#include <WinAPI.au3>

Opt("GuiOnEventMode", 1)

$hGUI = GUICreate("Test", 300, 300)
GUISetOnEvent($GUI_EVENT_CLOSE, "_Quit")

$idButton1 = GUICtrlCreateButton("One", 100, 100, 100, 30)
$hButton1 = GUICtrlGetHandle(-1)
GUICtrlSetOnEvent(-1, "_ButtonHit")
ConsoleWrite("$idButton1 = " & $idButton1 & "; $hButton1 = " & $hButton1 & @LF)

$hButton2 = _GUICtrlButton_Create($hGUI, "Two", 100, 150, 100, 30)
$idButton2 = _WinAPI_GetDlgCtrlID($hButton2)
GUICtrlSetOnEvent($idButton1, "_ButtonHit")
ConsoleWrite("$idButton2 = " & $idButton2 & "; $hButton2 = " & $hButton2 & @LF)

$idButton3 = GUICtrlCreateButton("Three", 100, 200, 100, 30)
$hButton3 = GUICtrlGetHandle(-1)
GUICtrlSetOnEvent(-1, "_ButtonHit")
ConsoleWrite("$idButton3 = " & $idButton3 & "; $hButton3 = " & $hButton3 & @LF)

GUISetState()

While 1
    Sleep(10)
WEnd

Func _Quit()
    Exit
EndFunc

Func _ButtonHit()
    ConsoleWrite("@GUI_CtrlHandle = " & @GUI_CtrlHandle & "; @GUI_CtrlId = " & @GUI_CtrlId & "; Text = " & ControlGetText($hGUI, "", @GUI_CtrlId) & @LF)
EndFunc

Note the control IDs of the buttons.

Note the native buttons have consecutive control IDs, and the UDF button is in a different range.

Note buttons One and Three respond, but Two does not.

The native AutoIt GuiOnEventMode handler is intended only for the native controls.

If you create it with the UDF, then event handling is on you, as by registering/handling WM_COMMAND messages in the _GuiCtrlButton_Create() example in the help file.

:idea:

Valuater's AutoIt 1-2-3, Class... Is now in Session!For those who want somebody to write the script for them: RentACoder"Any technology distinguishable from magic is insufficiently advanced." -- Geek's corollary to Clarke's law
Link to comment
Share on other sites

Why does this make it "a lot of work"? Surely you have your ControlIDs stored in variables? Could you please explain why you believe adding a button causes a problem, because at the moment I cannot se why it should. :)

M23

Edit: Now I am really confused - what on earth has GUISwitch got to do with it? :idea:

a lot of work: i dont have variables for all control-id's and some functions are build like

if guictrlread(34) = "something" then...

then i have to change many parts of the script (size = 2.700 lines) if i insert a new ctrlcreat in the middle

guiswitch: has nothing to do with _GUICtrlButton_Create but if i use it like

$gui1 = guicreate("example", ...)
guictrlcreatebutton ("b1", ...); control-id = 1

$gui2 = guicreate("ex2", ...)
guictrlcreatebutton ("b2", ...) ; control-id = 2

guiswitch($gui1)
guictrlcreatebutton ("b3", ...); control-id = 3 in first gui.

but with this method i can add controls without changing any existing id's.

Edited by andygo
Link to comment
Share on other sites

  • Moderators

andygo,

i dont have variables for all control-id's and some functions are build like "if guictrlread(34) = "something" then..."

Please tell me that you are just joking...... :)

However, to change that is really easy - you know of "Replace" in the <Edit> menu I presume? Just add a variable to store the ControlID when you create the control and then use "Replace" to change all the (34) to the variable name. You will have to do this for each control of course - but the time spent will be worthwhile, believe me, and you will have much cleaner code as a result. :(

The problem will have gone away forever and you can use the built-in functions which make life so much easier. :idea:

And please try and use variables in your next project - it is what they are there for! :)

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

  • Moderators

andygo,

"also a old rabbit can still learn"

In English we talk about "teaching old dogs new tricks" - so I understand perfectly! :)

Please do save your original script BEFORE you start any replacments - you never know what might happen! And if you have any problems, please PM me and we will sort it out together. :idea:

As to the GUISwitch solution, you realise that any controls you create in the script are in the same ControlID list, regardless of which GUI they are in? So your idea might work, but I would still recommend using variables to store the returned ControlIDs as it makes the problem go away completely.

M23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

andygo,

In English we talk about "teaching old dogs new tricks" - so I understand perfectly! :idea:

M23

And well you should too.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

  • Moderators

George,

I had a feeling you might pop up when I wrote that. :idea:

A very good evening (or whatever it is in BC) to you!

M23

Edit: Speeling!

Edited by Melba23

Public_Domain.png.2d871819fcb9957cf44f4514551a2935.png Any of my own code posted anywhere on the forum is available for use by others without any restriction of any kind

Open spoiler to see my UDFs:

Spoiler

ArrayMultiColSort ---- Sort arrays on multiple columns
ChooseFileFolder ---- Single and multiple selections from specified path treeview listing
Date_Time_Convert -- Easily convert date/time formats, including the language used
ExtMsgBox --------- A highly customisable replacement for MsgBox
GUIExtender -------- Extend and retract multiple sections within a GUI
GUIFrame ---------- Subdivide GUIs into many adjustable frames
GUIListViewEx ------- Insert, delete, move, drag, sort, edit and colour ListView items
GUITreeViewEx ------ Check/clear parent and child checkboxes in a TreeView
Marquee ----------- Scrolling tickertape GUIs
NoFocusLines ------- Remove the dotted focus lines from buttons, sliders, radios and checkboxes
Notify ------------- Small notifications on the edge of the display
Scrollbars ----------Automatically sized scrollbars with a single command
StringSize ---------- Automatically size controls to fit text
Toast -------------- Small GUIs which pop out of the notification area

 

Link to comment
Share on other sites

I see your typing is improving too. You had to know I couldn't let that one pass after you italicized it and after the comments of a few days ago. I knew when I saw the italics what you were refering to.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

hello,

after correcting all lines from 0 to 1.700 i am realy tired. to the end of script another 1.000 lines are waiting for me :idea: i will take some sleep now and continue tomorrow. meanwhile a question. with control-id's directly i can do this:

For $i = 5 to 19 Step +1
        GUICtrlSetState ($i, $GUI_DISABLE)
    next

with variable-names instead of control-id's i have to write a separate line for each, right?

thx, andy

Link to comment
Share on other sites

If the variable is an array you can do a loop.

For $i = 5 to 19 Step +1
        GUICtrlSetState ($aControlHwnd[$i], $GUI_DISABLE)
    next
GDIPlusDispose - A modified version of GDIPlus that auto disposes of its own objects before shutdown of the Dll using the same function Syntax as the original.EzMySql UDF - Use MySql Databases with autoit with syntax similar to SQLite UDF.
Link to comment
Share on other sites

It can also be done even if it's not array as long as you either use Variable names or reference points and the controls being set are in concecutive order

$Button_1 = GuiCtrlCreateButton (10, 100, 60, 30)
$Button_2 = GuiCtrlCreateButton (120, 100, 60, 30)
$Button_3 = GuiCtrlCreateButton (230, 100, 60, 30)
For $i = $Button_1 To $Button_3
    GUIctrlSetState($i, $GUI_DISABLE)
Next

or with reference points

$hStart = GUICtrlCreateDummy()
GuiCtrlCreateButton (10, 100, 60, 30)
GuiCtrlCreateButton (120, 100, 60, 30)
GuiCtrlCreateButton (230, 100, 60, 30)
$hEnd = GUICtrlCreateDummy()

For $i = $hStart To $hEnd
    GUICtrlSetState($i, $GUI_DISABLE)
Next

Note that the Step 1 or stepp +1 is not required That is the default anyway.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

Link to comment
Share on other sites

It can also be done even if it's not array as long as you either use Variable names or reference points and the controls being set are in concecutive order

$Button_1 = GuiCtrlCreateButton (10, 100, 60, 30)
$Button_2 = GuiCtrlCreateButton (120, 100, 60, 30)
$Button_3 = GuiCtrlCreateButton (230, 100, 60, 30)
For $i = $Button_1 To $Button_3
    GUIctrlSetState($i, $GUI_DISABLE)
Next

For $i = $hStart To $hEnd

GUICtrlSetState($i, $GUI_DISABLE)

Next

Note that the Step 1 or stepp +1 is not required That is the default anyway.

Thanks for those two tips. srcipt is now completely rewritten. all guictrlcreate's are now stored in $variables and code is cleaned up. i will test now every possible case.

if everything is fine, i could now add as much controls as i want, on any line in the script :idea:

Thanks Melba23 too for your tips.

Link to comment
Share on other sites

Thanks for those two tips. srcipt is now completely rewritten. all guictrlcreate's are now stored in $variables and code is cleaned up. i will test now every possible case.

if everything is fine, i could now add as much controls as i want, on any line in the script :idea:

Thanks Melba23 too for your tips.

Glad it's working. Just remember in the future to always asign a variable to a control. I usually do it even if I don't plan on referencing the contrl later, as in some labels. You never know when I'll change my mind and it makes things simpler all around.

George

Question about decompiling code? Read the decompiling FAQ and don't bother posting the question in the forums.

Be sure to read and follow the forum rules. -AKA the AutoIt Reading and Comprehension Skills test.***

The PCRE (Regular Expression) ToolKit for AutoIT - (Updated Oct 20, 2011 ver:3.0.1.13) - Please update your current version before filing any bug reports. The installer now includes both 32 and 64 bit versions. No change in version number.

Visit my Blog .. currently not active but it will soon be resplendent with news and views. Also please remove any links you may have to my website. it is soon to be closed and replaced with something else.

"Old age and treachery will always overcome youth and skill!"

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