autohotkey-v2-gui
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAutoHotkey v2 GUI Applications
AutoHotkey v2 GUI 应用开发
Advanced GUI development patterns and optimization techniques for AutoHotkey v2.
AutoHotkey v2的高级GUI开发模式与优化技巧。
When to Apply
适用场景
- Building complex GUI applications with multiple controls
- Implementing event-driven user interfaces
- Optimizing GUI performance and memory usage
- Working with advanced controls like ListView, ComboBox, Tab
- 构建包含多个控件的复杂GUI应用
- 实现事件驱动的用户界面
- 优化GUI性能与内存占用
- 使用ListView、ComboBox、Tab等高级控件
Critical Rules
核心规范
Event Handling: Use OnEvent method, not g-labels
autohotkey
; WRONG - v1 g-label syntax
MyGui.Add("Button", "gButtonClick", "OK")
; RIGHT - v2 OnEvent method
Btn := MyGui.Add("Button", "w80", "OK")
Btn.OnEvent("Click", ButtonClick)Data Submission: Use Submit() method with variable names
autohotkey
; WRONG - accessing controls directly
EditText := MyGui["MyEdit"].Text
; RIGHT - Submit returns object with named values
MyGui.Add("Edit", "vUserName")
Data := MyGui.Submit()
UserName := Data.UserNameControl Visibility: Use Visible property, not Show/Hide commands
autohotkey
; WRONG - v1 command syntax
GuiControl, Hide, MyButton
; RIGHT - v2 property syntax
MyButton.Visible := false事件处理:使用OnEvent方法,而非g标签
autohotkey
; WRONG - v1 g-label syntax
MyGui.Add("Button", "gButtonClick", "OK")
; RIGHT - v2 OnEvent method
Btn := MyGui.Add("Button", "w80", "OK")
Btn.OnEvent("Click", ButtonClick)数据提交:使用带变量名的Submit()方法
autohotkey
; WRONG - accessing controls directly
EditText := MyGui["MyEdit"].Text
; RIGHT - Submit returns object with named values
MyGui.Add("Edit", "vUserName")
Data := MyGui.Submit()
UserName := Data.UserName控件可见性:使用Visible属性,而非Show/Hide命令
autohotkey
; WRONG - v1 command syntax
GuiControl, Hide, MyButton
; RIGHT - v2 property syntax
MyButton.Visible := falseKey Patterns
核心模式
Basic GUI Structure
基础GUI结构
autohotkey
MyGui := Gui("+Resize", "Application Title")
MyGui.OnEvent("Close", AppClose)
; Add controls with variables for data retrieval
NameEdit := MyGui.Add("Edit", "vUserName w200")
SubmitBtn := MyGui.Add("Button", "Default w80", "OK")
SubmitBtn.OnEvent("Click", ProcessForm)
MyGui.Show("w300 h150")
ProcessForm(*) {
Data := MyGui.Submit()
MsgBox("Hello " . Data.UserName)
}
AppClose(*) {
ExitApp
}autohotkey
MyGui := Gui("+Resize", "Application Title")
MyGui.OnEvent("Close", AppClose)
; Add controls with variables for data retrieval
NameEdit := MyGui.Add("Edit", "vUserName w200")
SubmitBtn := MyGui.Add("Button", "Default w80", "OK")
SubmitBtn.OnEvent("Click", ProcessForm)
MyGui.Show("w300 h150")
ProcessForm(*) {
Data := MyGui.Submit()
MsgBox("Hello " . Data.UserName)
}
AppClose(*) {
ExitApp
}ListView with Event Handling
带事件处理的ListView
autohotkey
MyGui := Gui()
LV := MyGui.Add("ListView", "r20 w700", ["Name", "Size (KB)"])
LV.OnEvent("DoubleClick", LV_DoubleClick)
; Populate ListView
Loop Files, A_MyDocuments "\*.*"
LV.Add(, A_LoopFileName, A_LoopFileSizeKB)
LV.ModifyCol() ; Auto-size columns
LV.ModifyCol(2, "Integer") ; Enable numeric sorting
LV_DoubleClick(LV, RowNumber) {
RowText := LV.GetText(RowNumber)
MsgBox("Selected: " . RowText)
}autohotkey
MyGui := Gui()
LV := MyGui.Add("ListView", "r20 w700", ["Name", "Size (KB)"])
LV.OnEvent("DoubleClick", LV_DoubleClick)
; Populate ListView
Loop Files, A_MyDocuments "\*.*"
LV.Add(, A_LoopFileName, A_LoopFileSizeKB)
LV.ModifyCol() ; Auto-size columns
LV.ModifyCol(2, "Integer") ; Enable numeric sorting
LV_DoubleClick(LV, RowNumber) {
RowText := LV.GetText(RowNumber)
MsgBox("Selected: " . RowText)
}Tab Control with Organized Layout
布局规整的Tab控件
autohotkey
MyGui := Gui()
Tab := MyGui.Add("Tab3",, ["Settings", "Data", "Output"])
; First tab controls
MyGui.Add("CheckBox", "vAutoStart", "Start automatically")
MyGui.Add("ComboBox", "vTheme", ["Light", "Dark", "Auto"])
; Second tab controls
Tab.UseTab(2)
MyGui.Add("Edit", "vDataPath w200")
MyGui.Add("Button", "x+10 yp", "Browse...")
; Third tab controls
Tab.UseTab(3)
MyGui.Add("Edit", "vOutput r10 w300")
Tab.UseTab() ; End tab association
MyGui.Add("Button", "Default", "Apply")autohotkey
MyGui := Gui()
Tab := MyGui.Add("Tab3",, ["Settings", "Data", "Output"])
; First tab controls
MyGui.Add("CheckBox", "vAutoStart", "Start automatically")
MyGui.Add("ComboBox", "vTheme", ["Light", "Dark", "Auto"])
; Second tab controls
Tab.UseTab(2)
MyGui.Add("Edit", "vDataPath w200")
MyGui.Add("Button", "x+10 yp", "Browse...")
; Third tab controls
Tab.UseTab(3)
MyGui.Add("Edit", "vOutput r10 w300")
Tab.UseTab() ; End tab association
MyGui.Add("Button", "Default", "Apply")Modal Dialog Pattern
模态对话框模式
autohotkey
MainGui := Gui("+Resize", "Main Window")
MainGui.Opt("+OwnDialogs") ; Make dialogs modal
ShowSettingsBtn := MainGui.Add("Button", "w100", "Settings")
ShowSettingsBtn.OnEvent("Click", ShowSettings)
ShowSettings(*) {
; Modal dialogs prevent interaction with main window
Result := MsgBox("Save current settings?", "Confirm", "YesNoCancel")
if (Result = "Yes") {
; Save logic here
}
}autohotkey
MainGui := Gui("+Resize", "Main Window")
MainGui.Opt("+OwnDialogs") ; Make dialogs modal
ShowSettingsBtn := MainGui.Add("Button", "w100", "Settings")
ShowSettingsBtn.OnEvent("Click", ShowSettings)
ShowSettings(*) {
; Modal dialogs prevent interaction with main window
Result := MsgBox("Save current settings?", "Confirm", "YesNoCancel")
if (Result = "Yes") {
; Save logic here
}
}Performance Optimization
性能优化
Memory Management for Large Data
大数据内存管理
autohotkey
; Pre-allocate string capacity for concatenation
VarSetStrCapacity(&LargeString, 5120000) ; ~10 MB
Loop Files, "C:\*.*", "R"
LargeString .= A_LoopFileFullPath . "`n"
; Free memory when done
LargeString := ""autohotkey
; Pre-allocate string capacity for concatenation
VarSetStrCapacity(&LargeString, 5120000) ; ~10 MB
Loop Files, "C:\*.*", "R"
LargeString .= A_LoopFileFullPath . "`n"
; Free memory when done
LargeString := ""Object Capacity Tuning
对象容量调优
autohotkey
; Pre-allocate array/map capacity for performance
DataArray := []
DataArray.Capacity := 1000 ; Avoid frequent reallocation
DataMap := Map()
DataMap.Capacity := 500autohotkey
; Pre-allocate array/map capacity for performance
DataArray := []
DataArray.Capacity := 1000 ; Avoid frequent reallocation
DataMap := Map()
DataMap.Capacity := 500Advanced Control Patterns
高级控件模式
ComboBox with Validation
带验证的ComboBox
autohotkey
ColorBox := MyGui.Add("ComboBox", "vColor", ["Red", "Green", "Blue"])
ColorBox.OnEvent("Change", ValidateColor)
ValidateColor(Ctrl, *) {
if (Ctrl.Text = "Red")
MsgBox("Warning: Red selected!")
}autohotkey
ColorBox := MyGui.Add("ComboBox", "vColor", ["Red", "Green", "Blue"])
ColorBox.OnEvent("Change", ValidateColor)
ValidateColor(Ctrl, *) {
if (Ctrl.Text = "Red")
MsgBox("Warning: Red selected!")
}Dynamic Control Management
动态控件管理
autohotkey
; Get control position for dynamic layouts
MyEdit.GetPos(&x, &y, &w, &h)
NewBtn := MyGui.Add("Button", "x" . (x + w + 10) . " y" . y, "Next")
; Retrieve control values without Submit
CurrentText := ControlGetText("Edit1", MyGui.Hwnd)autohotkey
; Get control position for dynamic layouts
MyEdit.GetPos(&x, &y, &w, &h)
NewBtn := MyGui.Add("Button", "x" . (x + w + 10) . " y" . y, "Next")
; Retrieve control values without Submit
CurrentText := ControlGetText("Edit1", MyGui.Hwnd)Common Mistakes
常见错误
- Missing variable names: Controls need "v" prefix for Submit() to retrieve values
- Incorrect positioning: Use "xm ym" to reset to margins, "x+m y+m" for relative positioning
- Event handler parameters: Modern event handlers receive (Ctrl, Info) parameters, not just (*)
- Window activation timing: Use NoActivate option when showing background windows to avoid focus stealing
- 缺少变量名:控件需要添加“v”前缀,才能通过Submit()获取值
- 布局定位错误:使用“xm ym”重置到边距,使用“x+m y+m”进行相对定位
- 事件处理函数参数错误:现代事件处理函数接收(Ctrl, Info)参数,而非仅(*)
- 窗口激活时机错误:显示后台窗口时使用NoActivate选项,避免抢占焦点