使用 AutoHotkey 创建屏幕准星脚本
简介
为方便以下简称 “我们”,我们将学习如何使用AutoHotkey(AHK)编写一个屏幕准星脚本。这个脚本可以在屏幕上显示一个可自定义的准星,可在游戏或其他应用中更精准地定位。我们将从基础概念开始,逐步讲解脚本的每个部分,并最终实现一个功能完善的准星工具。通过本章内容,助于掌握AHK的基本语法、GUI创建、事件处理以及如何通过快捷键控制脚本行为。
目录
AutoHotkey简介
AutoHotkey(AHK)是一个开源的脚本语言,主要用于Windows平台的自动化任务。它可以通过简单的脚本实现键盘快捷键、鼠标操作、窗口管理等功能。AHK的语法简单易学,适合初学者快速上手。
安装 AutoHotkey :从 AutoHotkey 官网 https://www.autohotkey.com/ 下载并安装。
编写脚本:使用任何文本编辑器(如Notepad++或Visual Studio Code)来编写AHK脚本,保存为 .ahk 文件,双击即可运行。
脚本结构概述
一个典型的AHK脚本包含以下几个部分:
变量初始化:定义脚本中使用的变量。
GUI创建:创建图形用户界面(GUI)。
事件处理:通过快捷键或鼠标事件触发特定功能。
函数定义:将重复使用的代码封装成函数。
初始化与屏幕分辨率获取
在脚本的开头,我们初始化了一些变量,并获取了屏幕的分辨率。
***ahk
; 初始化变量
crosshairVisible := true
crosshairStyle := 1 ; 1: 默认十字线
crosshairColor := "Red" ; 初始颜色
crosshairColorIndex := 1 ; 循环切换颜色
crosshairOffsetX := 0 ; 准星水平偏移
crosshairOffsetY := 0 ; 准星垂直偏移
crosshairThickness := 2 ; 初始准星粗细
defaultCrosshairThickness := 2 ; 默认与恢复
; 获取屏幕分辨率
SysGet, ScreenWidth, 78
SysGet, ScreenHeight, 79
变量初始化:我们定义了准星的可见性、样式、颜色、偏移量等变量。
屏幕分辨率获取:使用 SysGet 命令获取屏幕的宽度和高度,以便准星能够居中显示。
GUI创建与准星绘制
我们使用AHK的GUI功能创建了一个透明的窗口,并在窗口上绘制准星。
***ahk
; 创建GUI用于绘制十字线
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Color, 000000 ; 设置背景颜色为黑色
WinSet, TransColor, 000000 150 ; 设置黑色为完全透明,并设置十字线透明度
; 绘制默认十字线
DrawCrosshair()
; 显示十字线
Gui, Show, x0 y0 w%ScreenWidth% h%ScreenHeight%, Crosshair
GUI创建:Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20 创建了一个始终置顶、无标题栏的窗口。
透明度设置:WinSet, TransColor, 000000 150 将黑色设置为透明,并设置透明度为150。
绘制准星:DrawCrosshair() 函数负责根据当前样式和颜色绘制准星。
快捷键与事件处理
我们为脚本绑定了一些快捷键,用于控制准星的行为。
***ahk
Numpad1::ToggleCrosshair() ; 数字键盘1:准星 显示/隐藏
Numpad2::ToggleCrosshairStyle() ; 数字键盘2:切换准星样式
Numpad3::ExitApp ; 数字键盘3:退出脚本
Numpad0::ResetCrosshair() ; 数字键盘0:恢复准星位置/粗细
NumpadDot::ToggleCrosshairColor() ; 数字键盘小数点键:切换准星颜色
; 方向键调整准星位置
Up::MoveCrosshair(0, -1) ; 上方向键:向上移动准星
Down::MoveCrosshair(0, 1) ; 下方向键:向下移动准星
Left::MoveCrosshair(-1, 0) ; 左方向键:向左移动准星
Right::MoveCrosshair(1, 0) ; 右方向键:向右移动准星
; Alt + 上/下方向键调整准星粗细
!Up::AdjustCrosshairThickness(1) ; Alt + 上方向键:增加准星粗细
!Down::AdjustCrosshairThickness(-1) ; Alt + 下方向键:减少准星粗细
快捷键绑定:通过 :: 符号将快捷键与函数绑定。例如,Numpad1::ToggleCrosshair() 表示按下数字键盘1时调用 ToggleCrosshair() 函数。
事件处理:每个函数负责处理特定的功能,如切换准星样式、调整位置等。
文字动画
在脚本启动时,我们显示了一个简单的文字动画效果。
***ahk
ShowAnimation()
; 显示动画的函数
ShowAnimation() {
global ScreenWidth, ScreenHeight
; 创建动画GUI
Gui, Animation: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Animation: Color, 000000
WinSet, TransColor, 000000 150
; 计算矩形画面的位置和大小
rectWidth := 400
rectHeight := 200
rectX := (ScreenWidth // 2) - (rectWidth // 2)
rectY := (ScreenHeight // 2) - (rectHeight // 2)
; 绘制矩形画面
Gui, Animation: Add, Progress, x%rectX% y%rectY% w%rectWidth% h%rectHeight% BackgroundWhite
; 显示“准星哈”三字动画效果
text := "准星哈"
textLength := StrLen(text)
loop, %textLength% {
char := SubStr(text, A_Index, 1)
Gui, Animation: Font, s140 cred, Arial ; 字体大/颜色
Gui, Animation: Add, Text, x%rectX% y%rectY% w%rectWidth% h%rectHeight% Center BackgroundTrans, %char%
Gui, Animation: Show, x0 y0 w%ScreenWidth% h%ScreenHeight%, Animation
Sleep, 500 ; 每字显示500毫秒
Gui, Animation: Destroy
Gui, Animation: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Animation: Color, 000000
WinSet, TransColor, 000000 150
Gui, Animation: Add, Progress, x%rectX% y%rectY% w%rectWidth% h%rectHeight% BackgroundWhite
}
; 隐藏动画
Sleep, 1000 ; 等待1秒
Gui, Animation: Destroy
}
动画实现:通过循环显示每个字符,并在每个字符显示后短暂停顿,形成动画效果。
GUI控制:使用 Gui, Animation: 创建动画窗口,并在动画结束后销毁窗口。
完整代码
; 初始化变量
crosshairVisible := true
crosshairStyle := 1 ; 1: 默认准星
crosshairColor := "Red" ; 初始颜色
crosshairColorIndex := 1 ; 循环切换颜色
crosshairOffsetX := 0 ; 准星水平偏移
crosshairOffsetY := 0 ; 准星垂直偏移
crosshairThickness := 2 ; 初始准星粗细
defaultCrosshairThickness := 2 ; 默认/恢复
; 获取屏幕分辨率
SysGet, ScreenWidth, 78
SysGet, ScreenHeight, 79
; 计算十字线的初始位置和大小
crosshairSize := 20
crosshairX := (ScreenWidth // 2) - (crosshairSize // 2)
crosshairY := (ScreenHeight // 2) - (crosshairSize // 2)
; 显示动画
ShowAnimation()
; 创建GUI用于绘制十字线
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Color, 000000 ; 背景颜色 黑色
WinSet, TransColor, 000000 150 ; 完全透明
; 绘制默认十字线
DrawCrosshair()
; 显示十字线
Gui, Show, x0 y0 w%ScreenWidth% h%ScreenHeight%, Crosshair
; 快捷键设置
Numpad1::ToggleCrosshair() ; 数字键盘1:准星 显示/隐藏
Numpad2::ToggleCrosshairStyle() ; 数字键盘2:切换准星样式
Numpad3::ExitApp ; 数字键盘3:退出脚本
Numpad0::ResetCrosshair() ; 数字键盘0:恢复位置和粗细
NumpadDot::ToggleCrosshairColor() ; 数字键盘小数点键:切换颜色
; 方向键调整准星位置
Up::MoveCrosshair(0, -1) ; 上方向键:向上移动准星
Down::MoveCrosshair(0, 1) ; 下方向键:向下移动准星
Left::MoveCrosshair(-1, 0) ; 左方向键:向左移动准星
Right::MoveCrosshair(1, 0) ; 右方向键:向右移动准星
; Alt + 上/下方向键调整准星粗细
!Up::AdjustCrosshairThickness(1) ; Alt + 上方向键:加粗
!Down::AdjustCrosshairThickness(-1) ; Alt + 下方向键:减细
; 切换十字线显示状态
ToggleCrosshair() {
global crosshairVisible
crosshairVisible := !crosshairVisible
if (crosshairVisible) {
Gui, Show
} else {
Gui, Hide
}
}
; 切换十字线样式
ToggleCrosshairStyle() {
global crosshairStyle
crosshairStyle := Mod(crosshairStyle, 4) + 1 ; 循环切换样式
DrawCrosshair()
}
; 切换准星颜色
ToggleCrosshairColor() {
global crosshairColorIndex, crosshairColor
colors := ["Red", "Green", "Blue", "White", "Yellow", "Purple"]
crosshairColorIndex := Mod(crosshairColorIndex, 6) + 1
crosshairColor := colors[crosshairColorIndex]
DrawCrosshair()
}
; 恢复准星位置和粗细
ResetCrosshair() {
global crosshairOffsetX, crosshairOffsetY, crosshairThickness, defaultCrosshairThickness
crosshairOffsetX := 0
crosshairOffsetY := 0
crosshairThickness := defaultCrosshairThickness
DrawCrosshair()
}
; 移动准星
MoveCrosshair(deltaX, deltaY) {
global crosshairOffsetX, crosshairOffsetY
crosshairOffsetX += deltaX
crosshairOffsetY += deltaY
DrawCrosshair()
}
; 调整准星粗细
AdjustCrosshairThickness(delta) {
global crosshairThickness
crosshairThickness := Max(1, crosshairThickness + delta) ; 粗细不小于1
DrawCrosshair()
}
; 绘制十字线
DrawCrosshair() {
global crosshairStyle, crosshairX, crosshairY, crosshairSize, crosshairThickness, ScreenWidth, ScreenHeight, crosshairColor, crosshairOffsetX, crosshairOffsetY
; 清除之前的绘制
Gui, Destroy
Gui, +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Color, 000000
WinSet, TransColor, 000000 150
; 根据颜色设置背景颜色
color := crosshairColor
if (color = "Red") {
color := "BackgroundRed"
} else if (color = "Green") {
color := "BackgroundGreen"
} else if (color = "Blue") {
color := "BackgroundBlue"
} else if (color = "White") {
color := "BackgroundWhite"
} else if (color = "Yellow") {
color := "BackgroundYellow"
} else if (color = "Purple") {
color := "BackgroundPurple"
}
; 计算准星的实际位置
actualX := crosshairX + crosshairOffsetX
actualY := crosshairY + crosshairOffsetY
if (crosshairStyle = 1) {
; 默认十字线
Gui, Add, Progress, x0 y%actualY% w%ScreenWidth% h%crosshairThickness% %color% ; 水平线
Gui, Add, Progress, x%actualX% y0 w%crosshairThickness% h%ScreenHeight% %color% ; 垂直线
} else if (crosshairStyle = 2) {
; 小型十字线(长度50)
lineLength := 50
halfLine := lineLength // 2
; 水平线(居中并支持偏移根据需要决定是否需要偏移)
hLineX := actualX - halfLine + crosshairOffsetX
Gui, Add, Progress, x%hLineX% y%actualY% w%lineLength% h%crosshairThickness% %color%
; 垂直线(居中并支持偏移根据需要决定是否需要偏移)
vLineY := actualY - halfLine + crosshairOffsetY
Gui, Add, Progress, x%actualX% y%vLineY% w%crosshairThickness% h%lineLength% %color%
} else if (crosshairStyle = 3) {
; 小点准星
Gui, Add, Progress, x%actualX% y%actualY% w%crosshairThickness% h%crosshairThickness% %color% ; 小点
} else if (crosshairStyle = 4) {
; 垂直线,高度为屏幕的一半,从屏幕中心向下延伸
lineHeight := ScreenHeight // 2
Gui, Add, Progress, x%actualX% y%actualY% w%crosshairThickness% h%lineHeight% %color% ; 垂直线
}
; 显示十字线
Gui, Show, x0 y0 w%ScreenWidth% h%ScreenHeight%, Crosshair
}
; 文字动画(可有可无)
ShowAnimation() {
global ScreenWidth, ScreenHeight
; 创建动画GUI
Gui, Animation: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Animation: Color, 000000
WinSet, TransColor, 000000 150
; 计算矩形画面的位置和大小
rectWidth := 400
rectHeight := 200
rectX := (ScreenWidth // 2) - (rectWidth // 2)
rectY := (ScreenHeight // 2) - (rectHeight // 2)
; 绘制矩形画面
Gui, Animation: Add, Progress, x%rectX% y%rectY% w%rectWidth% h%rectHeight% BackgroundWhite
; 显示“准星哈”三字的动画效果
text := "准星哈"
textLength := StrLen(text)
loop, %textLength% {
char := SubStr(text, A_Index, 1)
Gui, Animation: Font, s140 cred, Arial ; 字体大小颜色
Gui, Animation: Add, Text, x%rectX% y%rectY% w%rectWidth% h%rectHeight% Center BackgroundTrans, %char%
Gui, Animation: Show, x0 y0 w%ScreenWidth% h%ScreenHeight%, Animation
Sleep, 500 ; 每字显示500毫秒
Gui, Animation: Destroy
Gui, Animation: +LastFound +AlwaysOnTop +ToolWindow -Caption +E0x20
Gui, Animation: Color, 000000
WinSet, TransColor, 000000 150
Gui, Animation: Add, Progress, x%rectX% y%rectY% w%rectWidth% h%rectHeight% BackgroundWhite
}
; 隐藏动画
Sleep, 1000 ; 等待1秒
Gui, Animation: Destroy
}
; 脚本启动时自动显示动画
ShowAnimation()
; 保持脚本运行
Return
运行效果
脚本优化与调试
调试技巧:使用 MsgBox 输出变量值,帮助调试脚本。
性能优化:避免在循环中进行不必要的GUI操作,减少脚本的CPU占用。
总结与扩展
在本文章中,我们学习了如何使用AutoHotkey创建一个屏幕准星脚本。通过本文章,应该掌握了AHK的基本语法、GUI创建、事件处理等技能。可以进一步扩展脚本功能,例如:
添加更多准星样式。
支持保存和加载准星配置。
增加更多快捷键功能。
文章尾言
通过本文章,应该掌握了如何使用 AutoHotkey 创建一个功能丰富的屏幕准星脚本。希望能将这些知识应用到实际项目中,并继续探索 AutoHotkey 的更多可能性。如果你有任何问题或建议,欢迎在评论区留言哟。祝您编程愉快!