D:\Program Files\MyKeymap-2.0-beta33\data\custom_functions.ahk
; 自定义的函数写在这个文件里, 然后能在 MyKeymap 中调用
; 使用如下写法,来加载当前目录下的其他 AutoHotKey v2 脚本
; #Include ../data/test.ahk
; 切换最小化和还原所有窗口(排除 Rainmeter)
ToggleMinimizeAll() {
static isMinimized := false
if (isMinimized) {
RestoreAllWindows()
isMinimized := false
} else {
MinimizeAllWindows()
isMinimized := true
}
}
; 新增函数:最小化所有窗口(排除 Rainmeter)
MinimizeAllWindows() {
windows := WinGetList()
for id in windows {
try { ; 防止无效窗口导致崩溃
processName := WinGetProcessName(id)
if (processName != "Rainmeter.exe") {
WinMinimize(id)
}
}
}
}
; 新增函数:还原所有最小化窗口(排除 Rainmeter)
RestoreAllWindows() {
windows := WinGetList()
for id in windows {
try {
; 先检查窗口是否存在,避免无效句柄
if !WinExist("ahk_id " id)
continue
; 排除 Rainmeter 进程
processName := WinGetProcessName(id)
if (processName == "Rainmeter.exe")
continue
; 获取窗口状态(关键修正点)
minMaxState := WinGetMinMax(id)
; AHK 中最小化状态值为 -1,最大化是 1,正常是 0
if (minMaxState == -1) {
WinRestore(id)
; 可选:激活窗口确保还原(部分系统需要)
; WinActivate(id)
}
} catch Error {
; 捕获异常(例如权限问题)
continue
}
}
}
/**
* 调整窗口大小并定位到屏幕底部(任务栏上方)且水平居中
* @param width 窗口宽度
* @param height 窗口高度
* @returns {void}
*/
; 将当前窗口调整至显示器底部并水平居中显示
windowbottom(width, height) {
; 检查当前是否有活动窗口,没有则直接返回
if NotActiveWin() {
return
}
; 设置DPI感知模式为UNAWARE(-1),让系统自动处理缩放
; 当执行窗口移动操作时,即使指定固定像素尺寸,系统会自动适配显示器缩放比例
DllCall("SetThreadDpiAwarenessContext", "ptr", -1, "ptr")
; 确保操作对象是当前活动窗口
WinExist("A")
; 如果窗口处于最大化/最小化状态,先恢复窗口
if (WindowMaxOrMin())
WinRestore
; 获取窗口当前位置和尺寸(&表示输出变量)
WinGetPos(&x, &y, &w, &h)
; 根据窗口中心点坐标获取所在显示器序号
ms := GetMonitorAt(x + w / 2, y + h / 2)
; 获取该显示器的工作区域坐标(排除任务栏等区域)
MonitorGetWorkArea(ms, &l, &t, &r, &b)
w := r - l ; 计算工作区实际可用宽度
h := b - t ; 计算工作区实际可用高度
; 计算最终窗口尺寸(不超过显示器工作区大小)
winW := Min(width, w+20) ; 取期望宽度与最大可用宽度的较小值
winH := Min(height, h) ; 取期望高度与最大可用高度的较小值
; 计算窗口位置(水平居中 + 底部对齐)
winX := l + (w - winW) / 2 ; 水平居中:左边界 + (可用宽度-窗口宽度)/2
winY := b - winH ; 垂直底部:工作区下边界 - 窗口高度
; 移动窗口到计算位置(使用新的尺寸)
WinMove(winX, winY, winW, winH)
; 还原DPI感知模式为PER_MONITOR_AWARE(-3),确保鼠标移动等操作精准
DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
; 设置窗口置顶状态(根据之前的配置)
SetWindowTopMost()
}
/**
* 调整窗口大小并定位到屏幕顶部(任务栏下方)且水平居中
* @param width 窗口宽度
* @param height 窗口高度
* @returns {void}
*/
windowtop(width, height) {
if NotActiveWin() {
return
}
DllCall("SetThreadDpiAwarenessContext", "ptr", -1, "ptr")
WinExist("A")
if (WindowMaxOrMin())
WinRestore
WinGetPos(&x, &y, &w, &h)
ms := GetMonitorAt(x + w / 2, y + h / 2)
MonitorGetWorkArea(ms, &l, &t, &r, &b)
w := r - l ; 工作区宽度
h := b - t ; 工作区高度
; 计算窗口尺寸(不超过工作区)
winW := Min(width, w+20)
winH := Min(height, h)
; 水平居中,顶部对齐
winX := l + (w - winW) / 2
winY := t ; 关键修改:从工作区顶部开始定位
WinMove(winX, winY, winW, winH)
DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
SetWindowTopMost()
}
/**
* 调整窗口大小并定位到屏幕左侧(垂直居中)
* @param width 窗口宽度
* @param height 窗口高度
* @returns {void}
*/
windowleft(width, height) {
if NotActiveWin() {
return
}
DllCall("SetThreadDpiAwarenessContext", "ptr", -1, "ptr")
WinExist("A")
if (WindowMaxOrMin())
WinRestore
WinGetPos(&x, &y, &w, &h)
ms := GetMonitorAt(x + w / 2, y + h / 2)
MonitorGetWorkArea(ms, &l, &t, &r, &b)
w := r - l ; 工作区宽度
h := b - t ; 工作区高度
; 计算窗口尺寸(不超过工作区)
winW := Min(width, w)
winH := Min(height, h)
; 左侧对齐,垂直居中
winX := l-10 ; 关键修改:从工作区左侧开始
winY := t + (h - winH) / 2 ; 垂直居中计算
WinMove(winX, winY, winW, winH)
DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
SetWindowTopMost()
}
/**
* 调整窗口大小并定位到屏幕右侧(垂直居中)
* @param width 窗口宽度
* @param height 窗口高度
* @returns {void}
*/
windowright(width, height) {
if NotActiveWin() {
return
}
DllCall("SetThreadDpiAwarenessContext", "ptr", -1, "ptr")
WinExist("A")
if (WindowMaxOrMin())
WinRestore
WinGetPos(&x, &y, &w, &h)
ms := GetMonitorAt(x + w / 2, y + h / 2)
MonitorGetWorkArea(ms, &l, &t, &r, &b)
w := r - l ; 工作区宽度
h := b - t ; 工作区高度
; 计算窗口尺寸(不超过工作区)
winW := Min(width, w)
winH := Min(height, h)
; 右侧对齐,垂直居中
winX := r - winW +10 ; 关键修改:从工作区右侧向左计算
winY := t + (h - winH) / 2 ; 垂直居中计算
WinMove(winX, winY, winW, winH)
DllCall("SetThreadDpiAwarenessContext", "ptr", -3, "ptr")
SetWindowTopMost()
}
/**
* 窗口最大化
*/
MaximizeWindows() {
if NotActiveWin() {
return
}
if WindowMaxOrMin() {
WinRestore("A")
SetWindowTopMost()
} else {
WinMaximize("A")
UnsetWindowTopMost()
}
}
; 置顶窗口函数
SetWindowTopMost() {
WinSetAlwaysOnTop(true, "A")
if (WinGetExStyle("A") & 0x8) { ; 状态校验
Tip(Translation().always_on_top_on)
; 可扩展功能点:记录置顶日志/触发声音反馈
}
}
; 取消置顶函数
UnsetWindowTopMost() {
WinSetAlwaysOnTop(false, "A")
if !(WinGetExStyle("A") & 0x8) { ; 状态校验
Tip(Translation().always_on_top_off)
; 可扩展功能点:窗口位置复位/透明度重置
}
}
;#Requires AutoHotkey v2.0
; 设置图像文件的路径(确保使用相对路径或绝对路径)
imagePath := "D:\Program Files\MyKeymap-2.0-beta33\data\ee.bmp" ; 替换为你的图像文件路径
; 设置图像搜索的模式
CoordMode("Mouse", "Screen")
CoordMode("Pixel", "Screen")
; 定义检查间隔时间(毫秒)
checkInterval := 1000 ; 每1秒检查一次
; 定义全局变量用于保存图像位置
global Fx := 0
global Fy := 0
global imageFound := false ; 用于标记是否已经找到图像
global timerRunning := false ; 用于标记定时器是否在运行
CheckImage() {
global Fx, Fy, imagePath, imageFound
; 查找目标窗口
if WinExist("ahk_exe chrome.exe") {
; 在整个屏幕范围内搜索图像
found := ImageSearch(&Fx, &Fy, 0, 0, A_ScreenWidth, A_ScreenHeight, imagePath)
; 检查图像是否找到
if found {
; 如果图像之前没有被找到,则执行操作
if !imageFound {
; 点击图像左上角位置向上偏移28像素
Sleep 500
Click Fx, Fy-28
Send("^v")
Sleep 500 ; 等待0.5秒,以确保粘贴完成
; 移动鼠标到屏幕中间
MouseMove A_ScreenWidth // 2, A_ScreenHeight // 2
imageFound := true ; 更新标志,表示图像已找到并处理
ShowToolTip("图像已找到并处理")
}
} else {
imageFound := false ; 如果没有找到图像,重置标志
ShowToolTip("未找到图像")
}
} else {
ShowToolTip("Chrome未运行")
}
}
; 显示提示信息并在1秒后消失的函数
ShowToolTip(message, x := 960, y := 0) {
ToolTip(message, x, y)
SetTimer(() => ToolTip(""), 1000, 1)
}
; 定义热键以手动控制定时器
;`::ToggleTimer() ; 切换定时器状态
;+`::StopTimer()
; 切换定时器状态函数
ToggleTimer() {
global timerRunning
if (timerRunning) {
SetTimer(CheckImage, -1) ; 暂停检测
timerRunning := false
ShowToolTip("定时器已暂停")
} else {
if WinExist("ahk_exe chrome.exe") {
SetTimer(CheckImage, checkInterval) ; 启动检测
timerRunning := true
ShowToolTip("定时器已启动,每 " checkInterval " 毫秒检查一次图像")
} else {
ShowToolTip("Chrome未运行,无法启动定时器")
}
}
}
; 停止定时器函数
StopTimer() {
SetTimer(CheckImage, 0) ; 停止检测
timerRunning := false
ShowToolTip("定时器已停止")
}
评论区