找回密码
 注册
搜索
[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
查看: 141|回复: 1

[其他] 带倒计时的操作确认弹窗工具 - yesnotimer

[复制链接]
发表于 昨天 04:21 | 显示全部楼层 |阅读模式


写批处理脚本时需要这种工具,试了cido、cax都不能倒计时自动确认,故用ai写了一个ahk(v1)代码,可以转为exe,供批处理调用。

调用方法:
转为exe,方便批处理中使用


test.bat
  1. @echo off
  2. cd /d "%~dp0"
  3. yesnotimer "标题\n正文1\n正文2" 7
  4. if "%errorlevel%" equ "7" (
  5.         echo 你选择了 否
  6. ) else (
  7.         echo 你选择了 是
  8. )
  9. pause
复制代码



弹窗样式:
Snipaste_2026-07-31_04-06-04.png



yesnotimer.ahk

  1. #NoEnv
  2. #NoTrayIcon
  3. #SingleInstance, Force
  4. SetBatchLines, -1

  5. ;--------------------------------------------------
  6. ; 参数:
  7. ;   参数1 = 提示内容,支持用 \n 表示换行
  8. ;   参数2 = 倒计时秒数
  9. ;
  10. ; 返回值:
  11. ;   6 = 是
  12. ;   7 = 否、空格、Esc、关闭窗口或超时
  13. ;
  14. ; 快捷键:
  15. ;   Enter        = 是
  16. ;   小键盘 Enter = 是
  17. ;   Space        = 否
  18. ;   Esc          = 否
  19. ;--------------------------------------------------

  20. Prompt = %1%
  21. Timeout = %2%

  22. ; 未传入提示内容时的默认文字
  23. if (Prompt = "")
  24.     Prompt := "是否继续执行?"

  25. ; 将参数中的 \n 或 \N 转换成真正的换行
  26. Prompt := StrReplace(Prompt, "\n", "`n")
  27. Prompt := StrReplace(Prompt, "\N", "`n")

  28. ; 超时参数无效时,默认10秒
  29. if Timeout is not integer
  30.     Timeout := 10

  31. if (Timeout <= 0)
  32.     Timeout := 10

  33. Remaining := Timeout

  34. ;--------------------------------------------------
  35. ; 创建窗口
  36. ;--------------------------------------------------

  37. Gui, +AlwaysOnTop +HwndDialogHwnd
  38. Gui, Margin, 20, 18
  39. Gui, Font, s10, Microsoft YaHei UI

  40. Gui, Add, Text, w360 Center, %Prompt%

  41. Gui, Add, Text, w360 Center y+15 vCountdownText
  42.     , %Remaining% 秒后自动选择“否”。

  43. Gui, Add, Button, xm+75 y+18 w100 h30 Default vYesButton gChooseYes
  44.     , 是(&Y)

  45. Gui, Add, Button, x+20 w100 h30 vNoButton gChooseNo
  46.     , 否(&N)

  47. Gui, Show, AutoSize, 操作确认

  48. ; 默认将焦点放在“是”按钮
  49. GuiControl, Focus, YesButton

  50. SetTimer, UpdateCountdown, 1000
  51. return


  52. ;--------------------------------------------------
  53. ; 每秒更新倒计时
  54. ;--------------------------------------------------

  55. UpdateCountdown:
  56.     Remaining--

  57.     if (Remaining <= 0)
  58.     {
  59.         SetTimer, UpdateCountdown, Off
  60.         ExitApp, 7
  61.     }

  62.     GuiControl,, CountdownText
  63.         , %Remaining% 秒后自动选择“否”。
  64. return


  65. ;--------------------------------------------------
  66. ; 选择“是”
  67. ;--------------------------------------------------

  68. ChooseYes:
  69.     SetTimer, UpdateCountdown, Off
  70.     ExitApp, 6
  71. return


  72. ;--------------------------------------------------
  73. ; 选择“否”
  74. ;--------------------------------------------------

  75. ChooseNo:
  76.     SetTimer, UpdateCountdown, Off
  77.     ExitApp, 7
  78. return


  79. ;--------------------------------------------------
  80. ; 关闭窗口或按 Esc,均按“否”处理
  81. ;--------------------------------------------------

  82. GuiClose:
  83. GuiEscape:
  84.     SetTimer, UpdateCountdown, Off
  85.     ExitApp, 7
  86. return


  87. ;--------------------------------------------------
  88. ; 仅在本程序窗口激活时启用快捷键
  89. ;--------------------------------------------------

  90. #If WinActive("ahk_id " . DialogHwnd)

  91. Enter::
  92. NumpadEnter::
  93.     Gosub, ChooseYes
  94. return

  95. Space::
  96.     Gosub, ChooseNo
  97. return

  98. #If
复制代码



yesnotimer.exe文件

yesnotimer.rar (495.14 KB)
发表于 昨天 20:42 | 显示全部楼层
本帖最后由 aloha20200628 于 2026-7-31 20:53 编辑

给一个 bat+mshta 版本如下,熟悉网页编码的可以很容易进一步优化其中各元素细节...

test.bat
  1. <!-- :
  2. @echo off &for /f %%v in ('mshta "%~f0" ') do echo,%%v
  3. pause&exit/b
  4. -->
  5. <script>resizeTo(550,250);</script>
  6. <title>《示范:倒计时确认》</title><hta:application sysMenu='no' />
  7. <style>body{font-family:'Microsoft YaHei';color:white;} button{margin:0,30px;width:100px;}</style>
  8. <script>
  9.     ta=10; tr=ta; //设置倒计时长(秒数)
  10.     onload=function() { ti=setInterval(renew, 1000); };
  11.     function renew() {
  12.         tr--; t.innerText='剩余时间:'+tr+' 秒';
  13.         if (tr<=0) { clearInterval(ti); gotoBAT(-1); close(); } }
  14.     function gotoBAT(r) {
  15.         clearInterval(ti);
  16.         new ActiveXObject('scripting.fileSystemObject').getStandardStream(1).write(r);close(); }
  17. </script>
  18. <body bgcolor=black><center><h4>请点击以下按钮确认是或否</h4><p id=t>剩余时间:</p>
  19. <button style='background-color:green;' onclick=gotoBAT(0)>【是】</button>
  20. <button style='background-color:red;' onclick=gotoBAT(1)>【否】</button></center></body>
复制代码


以上代码的几点备注:
》第6行中可自定义弹窗标题
》第9行中可自定义倒计时长
》第18行可自定义窗口内提示词
》第19-20行可自定义两个按钮名称
》弹窗倒计时返回值回传给批处:是=0,否=1,超时=-1

评分

参与人数 1技术 +1 收起 理由
77七 + 1 感谢分享

查看全部评分

您需要登录后才可以回帖 登录 | 注册

本版积分规则

Archiver|手机版|小黑屋|批处理之家 ( 渝ICP备10000708号 )

GMT+8, 2026-8-1 05:13

Powered by Discuz! X3.5

© 2001-2026 Discuz! Team.

快速回复 返回顶部 返回列表