本帖最后由 defanive 于 2011-8-28 23:04 编辑
一些简单的例子
获得CMD的句柄 | @echo off | | CAPI Call kernel32 GetConsoleWindow | | echo %errorlevel% | | pauseCOPY |
闪烁CMD窗口 | @echo off | | CAPI Call kernel32 GetConsoleWindow | | set hCMD=%errorlevel% | | CAPI Call user32 FlashWindow #%hCMD% #1 | | pauseCOPY |
计算字符串长度 | @echo off | | set str=Defanive | | CAPI Call kernel32 lstrlenW $%str% | | echo %errorlevel% | | pauseCOPY |
计算字符串长度2(这样可以保留特殊字符) | @echo off | | set str=Defanive | | CAPI Call kernel32 lstrlenW *str | | echo %errorlevel% | | pauseCOPY |
获得鼠标的坐标 | @echo off | | set pos=xxxxyyyy | | CAPI Call user32 GetCursorPos ~pos | | CAPI Get ~pos | | pauseCOPY |
综合例子,获取3秒后鼠标所指的窗口标题(或内容) | @echo off | | setlocal enabledelayedexpansion | | set Pos=00000000 | | echo 3秒后获得鼠标所指的窗口标题 | | CAPI Call kernel32 Sleep #3000 | | CAPI Call user32 GetCursorPos ~Pos | | for /f "tokens=1,2" %%a in ('CAPI Get "~Pos" "#2"') do set /a "X=%%a","Y=%%b" | | CAPI Call user32 WindowFromPoint #%X% #%Y% | | set hWnd=%errorlevel% | | echo 窗口句柄:%hWnd% | | CAPI Call user32 SendMessageW #%hWnd% #14 #0 #0 | | set Length=%errorlevel% | | echo 标题长度:%Length% | | if "%Length%"=="0" echo 该窗口没有标题&pause&goto :eof | | for /l %%a in (1,1,%Length%) do set "Title=!Title! " | | set /a Length+=1 | | CAPI Call user32 SendMessageW #%hWnd% #13 #%Length% *Title | | echo 窗口标题:!Title! | | pauseCOPY |
通过API可以让批处理变得更强大
API编程相关可以在网上查找资料学习 |