Autoit中用PrintWindow替代ScreenCapture函数实现截图

时间:2022-06-01 16:21:38

想截取躲在后面的窗体或控件,找到了PrintWindow函数,幸运的是Autoit3也对此进行了封装以方便使用。

于是乎,将帮助文件里的_WinAPI_PrintWindow()实例改写了一下,以替代ScreenCapture系列函数:

#include <WinAPIGdi.au3>
#include <Clipboard.au3> HotKeySet("q", "exam") While 1
Sleep(100)
WEnd Func exam()
Run(@SystemDir & '\calc.exe')
Local $hWnd = WinGetHandle("[CLASS:CalcFrame]")
If Not $hWnd Then
Exit
EndIf
PrintModule($hWnd)
EndFunc ;==>exam Func PrintModule($hWnd)
Local $iSize=ControlGetPos("","",$hWnd)
; Create bitmap
Local $hDC = _WinAPI_GetDC($hWnd)
Local $hDestDC = _WinAPI_CreateCompatibleDC($hDC)
Local $hBitmap = _WinAPI_CreateCompatibleBitmap($hDC, $iSize[2], $iSize[3])
Local $hDestSv = _WinAPI_SelectObject($hDestDC, $hBitmap) _WinAPI_PrintWindow($hWnd, $hDestDC) _WinAPI_ReleaseDC($hWnd, $hDC)
_WinAPI_DeleteDC($hDestDC) _ClipBoard_Open(0)
_ClipBoard_Empty()
_ClipBoard_SetDataEx($hBitmap, $CF_BITMAP)
_ClipBoard_Close() _WinAPI_DeleteObject($hBitmap)
EndFunc ;==>PrintModule

示例中的$hWnd不限于窗口句柄,有点Autoit编程经验的应该都了解。

如果想进一步截取某个窗体或控件的某一部分区域,加个GDIPlus函数将获取到的Bitmap克隆一下就可以了。

Au3这种脚本语言学习起来主要还是靠耐心钻研帮助文件,毕竟东西就那么多。

如有其它疑问或建议,欢迎在这方面志同道合的朋友加我微信(Z,E,,A,,,L,,S,K)交流 ^^