Quantcast
Channel: AutoHotkey Community
Viewing all articles
Browse latest Browse all 579

Scripts and Functions (v2) • Re: Script template for an application with a main GUI, settings GUI, TrayMenu, and MenuBar

$
0
0

Hi,
This is a script template for an application with a main GUI, settings GUI, TrayMenu, and MenuBar. I'm just an intermediate AutoHotkey user. Please let me know if there is something wrong, or if you would do something differently.

Edit:
- Added Resize option for main GUI.
- Added map object to store the GUI values.
- Added open Main GUI when tray icon is double-clicked.

CODE:

#Requires AutoHotkey v2.0
#SingleInstance
CoordMode('ToolTip')

^1:: gMain_Show() ; Ctrl+1
^2:: gSettings_Show() ; Ctrl+2

scriptName := 'Application Name'
scriptVersion := 'v1.0.0'
gMainTitle := 'Main GUI - ' scriptName ' ' scriptVersion
gSettingsTitle := 'Settings - ' scriptName

; Map object for storing GUI values.
mUser := Map()

A_TrayMenu.Delete
A_TrayMenu.Add('Main GUI', gMain_Show)
A_TrayMenu.Add('Settings GUI', gSettings_Show)
A_TrayMenu.Add('Open Script Folder', (*) => Run(A_ScriptDir))
A_TrayMenu.Add('Edit Script', (*) => Edit())
A_TrayMenu.Add('Reload', (*) => Reload())
A_TrayMenu.Add('Exit', (*) => ExitApp())

; Double-clicking on tray icon will open the main GUI. See AHK_NOTIFYICON function at the end.
OnMessage(0x404, AHK_NOTIFYICON)

gMain_Show()

gMain_Show(*) {

if WinExist(gMainTitle ' ahk_class AutoHotkeyGUI') || WinExist(gSettingsTitle ' ahk_class AutoHotkeyGUI')
Return

Global gMain := Gui('+Resize +MinSize500x250', gMainTitle)
gMain.OnEvent('Size', gMain_Size)
gMain.OnEvent('Close', gMain_Close)

gMain.width := 800
gMain.height := 600
gMain.btnWidth := 140
gMain.btnHeight := 40
gMain.btnsWidth := gMain.btnWidth*3 + gMain.MarginX*2
btnsPosX := gMain.width - gMain.btnsWidth - gMain.MarginX

editWidth := 300
gMain.Add('Text', '', 'Edit 1:')
gMain.edit_Edit1 := gMain.Add('Edit', 'w' editWidth ' -Wrap', mUser.Has('edit1') ? mUser['edit1'] : '')
gMain.Add('Text', '', 'Edit 2:')
gMain.edit_Edit2 := gMain.Add('Edit', 'w' editWidth ' -Wrap', mUser.Has('edit2') ? mUser['edit2'] : '')

gMain.btn_Settings := gMain.Add('Button', 'x' btnsPosX ' y' gMain.height - gMain.btnHeight - gMain.MarginY ' w' gMain.btnWidth ' h' gMain.btnHeight, 'Settings')
gMain.btn_Settings.OnEvent('Click', gSettings_Show)
gMain.btn_OK := gMain.Add('Button', 'x+' gMain.MarginX ' w' gMain.btnWidth ' h' gMain.btnHeight ' Default', 'OK')
gMain.btn_OK.OnEvent('Click', gMain_btn_OK_Click)
gMain.btn_Cancel := gMain.Add('Button', 'x+' gMain.MarginX ' w' gMain.btnWidth ' h' gMain.btnHeight, 'Cancel')
gMain.btn_Cancel.OnEvent('Click', gMain_Close)

gMain.mb := MenuBar()
gMain.mb_file := Menu()
gMain.mb_options := Menu()
gMain.mb.Add('File', gMain.mb_file)
gMain.mb.Add('Options', gMain.mb_options)
gMain.mb_options.Add('Settings...', gSettings_Show)
gMain.MenuBar := gMain.mb

gMain.Show('h' gMain.height ' w' gMain.width)
}

;==============================================

gMain_Close(*) {

try gSettings.Destroy()
gMain.Destroy()
}

;==============================================

gMain_Size(guiObj, minMax, width, height) {

if minMax = -1
Return

MoveControls(
{Control: gMain.btn_Settings, x: width - gMain.btnsWidth - gMain.MarginX, y: height - gMain.btnHeight - gMain.MarginY},
{Control: gMain.btn_OK, x: width - gMain.btnWidth*2 - gMain.MarginX*2, y: height - gMain.btnHeight - gMain.MarginY},
{Control: gMain.btn_Cancel, x: width - gMain.btnWidth - gMain.MarginX, y: height - gMain.btnHeight - gMain.MarginY},
)

DllCall('RedrawWindow', 'ptr', gMain.hwnd, 'ptr', 0, 'ptr', 0, 'uint', 0x0081)
}

;==============================================

gMain_btn_OK_Click(ctrlObj, *) {

CoordMode('Mouse', 'Screen')
MouseGetPos(&mX, &mY)
ToolTip('You clicked the ' ctrlObj.Text ' button of Main GUI', mX, mY), SetTimer(ToolTip, -3000)

mUser['edit1'] := gMain.edit_Edit1.Text
mUser['edit2'] := gMain.edit_Edit2.Text

gMain_Close()
}

;==============================================

gSettings_Show(*) {

if WinExist(gSettingsTitle ' ahk_class AutoHotkeyGUI')
Return

Global gSettings := Gui('-MinimizeBox', gSettingsTitle)
gSettings.OnEvent('Close', gSettings_Close)

try gMain.Opt('+Disabled')
try gSettings.Opt('+Owner' gMain.hwnd)

gSettingsWidth := 600
gSettingsHeight := 400
btnWidth := 140
btnHeight := 40
btnsWidth := btnWidth*2 + gSettings.MarginX
btnsPosX := gSettingsWidth - btnsWidth - gSettings.MarginX

gSettings.btn_OK := gSettings.Add('Button', 'x' btnsPosX ' y' gSettingsHeight - btnHeight - gSettings.MarginY ' w' btnWidth ' h' btnHeight ' Default', 'OK')
gSettings.btn_OK.OnEvent('Click', gSettings_btn_OK_Click)
gSettings.btn_Cancel := gSettings.Add('Button', 'x+' gSettings.MarginX ' w' btnWidth ' h' btnHeight, 'Cancel')
gSettings.btn_Cancel.OnEvent('Click', gSettings_Close)
gSettings.Show('h' gSettingsHeight ' w' gSettingsWidth)
}

;==============================================

gSettings_Close(*) {

try gMain.Opt('-Disabled')
gSettings.Destroy()
}

;==============================================

gSettings_btn_OK_Click(ctrlObj, *) {

CoordMode('Mouse', 'Screen')
MouseGetPos(&mX, &mY)
ToolTip('You clicked the ' ctrlObj.Text ' button of Settings GUI', mX, mY), SetTimer(ToolTip, -3000)
gSettings_Close()
}

;==============================================
; MoveControls by Descolada. (from UIATreeInspector.ahk) https://github.com/Descolada/UIA-v2
MoveControls(ctrls*) {

for ctrl in ctrls
ctrl.Control.Move(ctrl.HasOwnProp('x') ? ctrl.x : unset, ctrl.HasOwnProp('y') ? ctrl.y : unset, ctrl.HasOwnProp('w') ? ctrl.w : unset, ctrl.HasOwnProp('h') ? ctrl.h : unset)
}

;==============================================

AHK_NOTIFYICON(wParam, lParam, msg, hwnd) {

CoordMode('Mouse', 'Screen')
MouseGetPos(&mX, &mY)

if lParam = 0x201
if KeyWait('LButton', 'D T0.25') ; Double-click
gMain_Show()
else ; Click
ToolTip('You clicked the tray icon', mX, mY), SetTimer(ToolTip, -3000)
}



give this a try!

https://github.com/samfisherirl/Easy-Auto-GUI-for-AHK-v2

Statistics: Posted by sashaatx — Today, 09:27



Viewing all articles
Browse latest Browse all 579

Trending Articles