autohotkey - 为什么脚本不能正确地从 %SetWidth% 和 %SetHeight% 读取变量数据?
问题描述
当我用数字替换这些时,脚本可以正常工作。但为了简化事情,我想在脚本顶部定义几个变量(SetWidth 和 SetHeight),并在整个过程中根据需要调用它们。除了,由于某种原因,当我尝试调用它们时,它似乎无法正常工作。
#NoTrayIcon
#NoEnv ; Recommended for performance and compatibility with future AutoHotkey releases.
; #Warn ; Enable warnings to assist with detecting common errors.
SendMode Input ; Recommended for new scripts due to its superior speed and reliability.
SetWorkingDir %A_ScriptDir% ; Ensures a consistent starting directory.
SetWidth = 1616
SetHeight = 939
Run, ".\full throttle remastered.bat"
Process, Wait, throttle.exe, 120
Process, Exist, throttle.exe
Throttle = %ErrorLevel%
if Throttle != 0
{
Sleep, 2000
CenterWindow("ahk_exe throttle.exe")
}
else
{
MsgBox "Full Throttle Remastered could not be started"
}
return
; The following function centers the specified window on the screen if not already centered:
CenterWindow(WinTitle)
{
WinGetPos,xx,yy, winx, winy, %WinTitle%
x1 := xx + winx/2
y1 := yy + winy/2
loop 2
{
y1 := yy + winy/2
loop 2
{
if ((A_ScreenWidth/2 = x1) && (A_ScreenHeight/2 = y1) && (winx = %SetWidth%) && (winy = %SetHeight%))
{
msgbox got em
return
}
else
y1 := y1 + 0.5
}
x1 := x1 + 0.5
}
WinMove, ahk_exe throttle.exe,, 0, 0, %SetWidth%, %SetHeight%
WinGetPos,,, winx, winy, %WinTitle%
Sleep, 100
WinMove, %WinTitle%,, (A_ScreenWidth/2)-(winx/2), (A_ScreenHeight/2)-(winy/2)
}
return
解决方案
要访问函数中的全局变量,您需要
global
在函数内添加
CenterWindow(WinTitle){
global
WinGetPos,xx,yy, winx, winy, %WinTitle%
x1 := xx + winx/2
y1 := yy + winy/2
loop 2
{
y1 := yy + winy/2
loop 2
{
if ((A_ScreenWidth/2 = x1) && (A_ScreenHeight/2 = y1) && (winx := SetWidth) && (winy = SetHeight))
{
msgbox got em
return
}
else
y1 := y1 + 0.5
}
x1 := x1 + 0.5
}
WinMove, %WinTitle%,, 0, 0, SetWidth, SetHeight
WinGetPos,,, winx, winy, %WinTitle%
Sleep, 100
WinMove, %WinTitle%,, (A_ScreenWidth/2)-(winx/2), (A_ScreenHeight/2)-(winy/2)
}
或通过在函数之外(在自动执行部分或热键/热字符串/或其他函数中)将变量声明为超全局变量,方法是global
在它们之前添加:
global SetWidth := "1616"
global SetHeight := "939"
有关更多详细信息,请阅读局部变量和全局变量
推荐阅读
- flutter - 如何在颤动中将所有地理点从firestore提取到谷歌地图中
- java - 我如何将 kotlin 代码转换为 java [Android]
- javascript - 修复:如何将已经居中的 Flex 的中间项目居中
- docker - 在 docker-compose (binami/kafka) 中使用 SASL_PLAINTEXT 连接到 Kakfka 代理
- html - HTML/CSS 如何制作这样的按钮?
- dataframe - PySpark 地图功能 - 发送 n 行而不是 1 行来构建列表
- react-native - 按钮 onpress 不适用于自己的组件
- visual-studio-code - VSCode 中的 Gitlens 会根据任何文件更改自动开始 rebase
- java - 尝试使用 Apache POI 在 excel 中导出和着色行时出现异常
- python - Flask 博客,只希望一个用户能够发帖