During the last days I have learnd a bit about AHK. Mainly I was interessted in how two windows can interact with each other. So here is a small showcase. Other Newbies like me might find it interesting. Any suggestion/hint regarding improvement (stable/learn effect) is appreciated.
Code:
; Showcase for two windows with interaction
; tested OS: Windows XP Pro
; AHK version: 1.0.26.00
; Date: 2005-02-14
Windowtitle1 = First Window
Windowtitle2 = Second Window
; GUI of 1st window
Gui, 1:Add, Checkbox, x10 y10 vCkbEditSimu , &Edit both windows simultaneously
Gui, 1:Add, Button, y+10 vBtnOpen2ndWindow gBtnOpen2ndWindow , &Open 2nd window
Gui, 1:Add, Text, y+10 vTxtNumber2ndWindow , Number from 2nd Window:
Gui, 1:Add, Edit, r1 ReadOnly vEdtNumber2ndWindow x+5 yp-3 w40,
Gui, 1:Add, Edit, r5 xs y+10 w172 vEdtEdit1stWindow , Try to edit this field when 2nd window is open
Gui, 1:Add, Button, Default xs+135 , &Close
; GUI of 2nd window, which is owned by 1st window
Gui, 2:+owner1
Gui, 2:Add, Text , x10 y10 , Input Number:
Gui, 2:Add, Edit , r1 Number Limit2 x+5 yp-3 w40 vEdtNumber, 10
Gui, 2:Add, Button, Default xs+30 y+5 , &Apply
Gui, 2:Add, Button, v2ndCancel g2ndCancel x+0 , &Cancel
; Show 1st window
Gui, 1:Show, x100 y100 , %Windowtitle1%
Return
; +++++++++++++++++++++++ End of auto-execute +++++++++++++++++++++++++++
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Open 2nd window
BtnOpen2ndWindow:
; get value of CkbEditSimu
Gui, 1:Submit, NoHide
; open 2nd window
Gui, 2:Show ,x350 y100, %Windowtitle2%
; if simultaneous edit is active ...
if CkbEditSimu
{ ; ... disable the open-button to open another 2nd window
GuiControl, 1:Disable, BtnOpen2ndWindow
}
else
{ ; ... otherwise hide the Cancel-Button, so that user can only press Apply-Button
GuiControl, 2:Hide, 2ndCancel ; Auto-Label doesn't work, Button needs variabel
; set 1st window inactive
Gui, 1: +Disabled
}
return
; +++++++++++++++++++ Apply Number from 2nd window into field of 1st window
2ButtonApply:
; get variable in field, if simultaneous edit keep window otherwise hide it
if CkbEditSimu
Gui, 2:Submit, NoHide
else
{
Gui, 2:Submit
GoSub, 2ndCancel
}
; set number into field of 1st "1:" window (important, AHK needs to know which GUI)
GuiControl,1:,EdtNumber2ndWindow,%EdtNumber%
; increase the number in the Apply field for easier testing
NewInput := EdtNumber + 1
GuiControl,2:,EdtNumber, %NewInput%
return
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Closing 2nd window
2ndCancel:
2GuiClose: ; 2GuiClose = GuiClose of second window
;restore all options of controla and GUI to normal
Gui, 1: -Disabled
GuiControl, 1:Enable, BtnOpen2ndWindow
GuiControl, 2:Show, 2ndCancel
;set focus on first window
WinActivate, %Windowtitle1%
; "Gui, 2:Cancel" keeps window in memory, so it can be opened again.
; "Gui, 2:Destroy" would release the memory but the window couldn't be open again.
Gui, 2:Cancel
return
; +++++++++++++++++++++++++++++++++++++++++++++++++++++++ Exiting 1st window
ButtonClose:
GuiClose:
ExitApp
I have run the script at home and at work. The differnce is that at home the windows pop up at the correct position. At work they initially pop up at in the center of the screen. Only when the 2nd window pops up a second time, it is at the correct position. Does anyone have an idea why? Is it because I do not have admin rights and AHK might not be fully installed?
EDIT:
- Updated the script with new 1.0.26.00 function for Gui, +/-Disabled
- Improved indentations
- removed the winwait, instead put restore of controls and GUI in the closing subroutine.