AutoHotKey循环实战:用While和Loop实现一个“按住测量”的屏幕标尺工具(附完整脚本)
AutoHotKey循环实战用While和Loop实现“按住测量”屏幕标尺工具在UI设计、网页排版或视频编辑场景中经常需要快速测量屏幕上两个点之间的距离或某个区域的像素尺寸。专业设计软件通常内置标尺工具但切换软件往往打断工作流。今天我们将用AutoHotKeyAHK打造一个零延迟的屏幕标尺工具只需按住鼠标左键拖动即可实时显示尺寸释放后自动消失——全程无需切换窗口。这个工具的核心逻辑是持续监测鼠标左键状态在按压期间动态计算起始点与当前位置的坐标差。下面通过两种循环结构While和Loop分别实现并逐步添加ESC退出、剪贴板复制等实用功能。所有代码均提供逐行解析即使AHK初学者也能快速定制。1. 工具核心逻辑与While循环实现任何屏幕测量工具都需要解决三个技术问题如何捕获鼠标按下事件如何持续获取实时坐标如何优雅地终止测量1.1 基础框架搭建首先设置坐标系模式为屏幕绝对坐标相对于显示器左上角确保测量结果不受窗口位置影响#SingleInstance Force ; 防止重复运行脚本 CoordMode Mouse, Screen ; 使用屏幕绝对坐标1.2 While循环监测鼠标状态通过~LButton::热键捕获左键按下事件使用GetKeyState()函数持续检测按压状态~LButton:: { ; 记录起始坐标 MouseGetPos begin_x, begin_y ; 实时更新测量结果 while GetKeyState(LButton, P) { MouseGetPos x, y width : Abs(begin_x - x) height : Abs(begin_y - y) ; 显示坐标和尺寸单位像素 ToolTip begin_x , begin_y → x , y n尺寸: width × height Sleep 10 ; 降低CPU占用 } ToolTip ; 释放鼠标后清除提示 }关键点解析GetKeyState(LButton, P)中的P参数表示检测物理按键状态Abs()函数确保宽度/高度始终为正数ToolTip默认显示在鼠标右下方可通过ToolTip 文本, X, Y自定义位置1.3 实时效果演示当你在屏幕上按住左键拖动时会看到类似这样的实时反馈1024,768 → 1560,920 尺寸: 536 × 1522. Loop循环重构与功能增强While循环适合状态持续监测而Loop更适合需要主动中断的场景。下面用Loop实现相同功能并添加实用扩展。2.1 基础Loop实现~LButton:: { MouseGetPos begin_x, begin_y Loop { if not GetKeyState(LButton, P) ; 检测左键释放 break MouseGetPos x, y ToolTip begin_x , begin_y → x , y n Abs(begin_x-x) × Abs(begin_y-y) Sleep 10 } ToolTip }2.2 添加ESC退出功能在循环内增加键盘状态检测按下ESC立即终止测量~LButton:: { MouseGetPos begin_x, begin_y Loop { if not GetKeyState(LButton, P) || GetKeyState(Esc, P) break ; ...其余代码同上... } ToolTip }2.3 测量结果自动复制到剪贴板释放鼠标时将最终尺寸存入剪贴板便于粘贴到其他软件~LButton:: { ; ...初始代码同上... Loop { ; ...循环内代码同上... } final_width : Abs(begin_x - x) final_height : Abs(begin_y - y) A_Clipboard : final_width × final_height 像素 ToolTip 已复制: A_Clipboard, begin_x, begin_y - 30 Sleep 1000 ; 显示1秒后消失 ToolTip }3. 高级功能扩展3.1 单位切换像素/厘米/英寸通过系统DPI计算物理尺寸添加单位切换热键unit : px ; 默认像素单位 ^!U:: ; CtrlAltU切换单位 { global unit if (unit px) unit : cm else if (unit cm) unit : in else unit : px MsgBox 当前单位: unit } ; 修改ToolTip显示部分 dpi : A_ScreenDPI if (unit cm) { width_text : Round(width/dpi*2.54, 2) cm height_text : Round(height/dpi*2.54, 2) cm } else if (unit in) { width_text : Round(width/dpi, 2) in height_text : Round(height/dpi, 2) in } else { width_text : width px height_text : height px } ToolTip begin_x , begin_y → x , y n width_text × height_text3.2 测量历史记录使用数组保存最近5次测量结果通过热键查看measureHistory : [] ~LButton:: { ; ...原有代码... final_width : Abs(begin_x - x) final_height : Abs(begin_y - y) ; 添加到历史记录 measureHistory.Push(final_width × final_height) if (measureHistory.Length 5) measureHistory.RemoveAt(1) } ^!H:: ; 查看历史记录 { global measureHistory historyText : for i, result in measureHistory historyText . i : result n MsgBox historyText || 无历史记录 }4. 完整脚本与使用技巧4.1 优化后的完整脚本#SingleInstance Force CoordMode Mouse, Screen unit : px measureHistory : [] ~LButton:: { MouseGetPos begin_x, begin_y Loop { if not GetKeyState(LButton, P) || GetKeyState(Esc, P) break MouseGetPos x, y width : Abs(begin_x - x) height : Abs(begin_y - y) ; 单位转换 dpi : A_ScreenDPI if (unit cm) { w_text : Round(width/dpi*2.54, 2) cm h_text : Round(height/dpi*2.54, 2) cm } else if (unit in) { w_text : Round(width/dpi, 2) in h_text : Round(height/dpi, 2) in } else { w_text : width px h_text : height px } ToolTip begin_x , begin_y → x , y n w_text × h_text Sleep 10 } final_size : width × height 像素 A_Clipboard : final_size measureHistory.Push(final_size) if (measureHistory.Length 5) measureHistory.RemoveAt(1) ToolTip 已复制: final_size, begin_x, begin_y - 30 Sleep 1500 ToolTip } ^!U:: ; 单位切换 { global unit if (unit px) unit : cm else if (unit cm) unit : in else unit : px ToolTip 单位: unit, A_CaretX, A_CaretY - 50 Sleep 1000 ToolTip } ^!H:: ; 历史记录 { global measureHistory historyText : for i, result in measureHistory historyText . i : result n MsgBox historyText || 无历史记录 }4.2 使用技巧与注意事项性能优化调整Sleep值10-50ms平衡流畅度与CPU占用添加#MaxThreadsPerHotkey 2防止快速连续触发导致冲突视觉定制; 修改ToolTip样式 ToolTip 尺寸: width × height,,, Consolas, s12 bold多显示器适配; 获取主显示器DPI dpi : DllCall(GetDpiForWindow, Ptr, A_ScriptHwnd)错误处理~LButton:: { try { ; 原有代码 } catch as e { MsgBox 错误: e.Message } }实际测试发现在4K显示器上测量时建议将ToolTip字体调大。遇到DPI缩放问题时可通过A_ScreenDPI动态调整计算逻辑。
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2550942.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!