AutoHotkey Community

It is currently May 27th, 2012, 8:12 am

All times are UTC [ DST ]




Post new topic Reply to topic  [ 4 posts ] 
Author Message
 Post subject: GUI -> Dynamic Buttons
PostPosted: January 10th, 2012, 11:49 pm 
Hi, I am a bit new to the GUI programming of AHK, so bare with me please.

Application Plan: Make a small gui script that show X amount of buttons depending how many SWGClients (a game) im running.
Functionality of the buttons is to hide/show the game window when pressed.
I got the Adding of Buttons dynamic made, but the functionality of them i am having a bit a trouble with howto make dynamic.

Notice how i have to make all the ButtonSWGClient1 / ButtonSWGClient2 / ButtonSWGClient3.

How would i go by making that more dynamic / scalable?

Code:
;
; Language:       English
; Platform:       Win9x/NT
; Author:         Andri(Titboy) <amg@quakemet.org>
;
; Script Function:
;  Will Parse a logfile generated from Star Wars Galaxies(TM). Will Generate a script file for SWG.
;
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
DetectHiddenWindows, On
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Height = 10
Width = 165


; Form
WinGet, ClientArray, List, SwgClient




loop %ClientArray%
{

   



    element := ClientArray%A_Index%
   
   ; MsgBox % "Element number " . A_Index . " is " . ClientArray%A_Index%
    Gui, Add, Button, x15 y%Height% w135 h20, SWGClient %A_Index%

Height := Height + 25

}

Height := Height + 25


Gui, Show, h%Height% w%Width%, StarWarsGalaxiesController


return



ButtonSWGClient1:
{
if not DllCall("IsWindowVisible", "UInt", ClientArray1) 
winshow, ahk_id %ClientArray1%
else
winhide, ahk_id %ClientArray1%

WinActivate StarWarsGalaxiesController
return
}

ButtonSWGClient2:
{
if not DllCall("IsWindowVisible", "UInt", ClientArray2) 
winshow, ahk_id %ClientArray2%
else
winhide, ahk_id %ClientArray2%

WinActivate StarWarsGalaxiesController
return
}

ButtonSWGClient3:
{
if not DllCall("IsWindowVisible", "UInt", ClientArray3) 
winshow, ahk_id %ClientArray3%
else
winhide, ahk_id %ClientArray3%

WinActivate StarWarsGalaxiesController
return
}

GuiClose:
ExitApp








Report this post
Top
  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2012, 11:52 am 
Offline

Joined: May 12th, 2009, 2:37 pm
Posts: 640
Location: Gloucester UK
I would make the buttons associated variables 1, 2, 3, 4 etc. as then you can use the A_GuiControl variable to reference the specific button
For example

Code:
#NoEnv  ; Recommended for performance and compatibility with future AutoHotkey releases.
DetectHiddenWindows, On
SendMode Input  ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir%  ; Ensures a consistent starting directory.

Height = 10
Width = 165

; Form
WinGet, ClientArray, List, SwgClient

loop %ClientArray%
{
    element := ClientArray%A_Index%
    Gui, Add, Button, v%A_Index% gButtonAction x15 y%Height% w135 h20 , SWGClient %A_Index%
    Height := Height + 25
}
Height := Height + 25
Gui, Show, h%Height% w%Width%, StarWarsGalaxiesController
return

ButtonAction:
ClientArray := ClientArray%A_GuiControl%

if not DllCall("IsWindowVisible", "UInt", ClientArray) 
winshow, ahk_id %ClientArray%
else
winhide, ahk_id %ClientArray%
WinActivate StarWarsGalaxiesController
return


The code is untested. I hope it works straight off but it may need a bit of tinkering. Either way, it should give you the general idea.
Hope it helps.

_________________
The sooner you fall behind, the more time you have to catch up.


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2012, 3:20 pm 
Offline

Joined: July 10th, 2008, 8:49 am
Posts: 1865
Location: Brussels, Belgium
Just passing by, the "element :=" line is useless. :wink:


Report this post
Top
 Profile  
Reply with quote  
 Post subject:
PostPosted: January 11th, 2012, 4:27 pm 
Offline
User avatar

Joined: March 19th, 2008, 12:43 am
Posts: 5482
Location: the tunnel(?=light)
The problem is less about the dynamic button creation than the dynamic label creation. You need to work with all buttons from one label and use GuiControlGet to determine which button you pressed (untested):

Code:
WinGet, ClientArray, List, SwgClient
Loop %   ClientArray
   Gui, Add, Button, w135 h20 gButtonSWGClient, SWGClient %A_Index%   ; control names will be added in order, 'Button1', 'Button2', etc.
Gui, Show, AutoSize Center, StarWarsGalaxiesController
return

ButtonSWGClient:
GuiControlGet, b, Focus   ; gets the current control in the GUI that has focus, which should be the last button you clicked
; MsgBox %   b
b :=   RegExReplace(b,"Button")   ; removes everything but the button number
if   !DllCall("IsWindowVisible", "UInt", (win :=   ClientArray%b%))
   WinShow, ahk_id %win%
else   WinHide, ahk_id %win%
WinActivate, StarWarsGalaxiesController
return

GuiClose:
ExitApp

_________________
Image
Try Quick Search for Autohotkey or see the tutorial for newbies.


Report this post
Top
 Profile  
Reply with quote  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 4 posts ] 

All times are UTC [ DST ]


Who is online

Users browsing this forum: BrandonHotkey, migz99 and 69 guests


You can post new topics in this forum
You can reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Powered by phpBB® Forum Software © phpBB Group