lon91ong 发表于 2011-1-2 16:19:05

[已解决]如何取得IE客户区的句柄进行后台取色呢

本帖最后由 lon91ong 于 2011-1-4 13:38 编辑

lanfengc大侠给出过的后台取色工具中包含了下面的函数,用来对窗口抓图
我现在想要修改成对IE窗口中的客户区抓图,不是整个窗口抓图,请问大侠要如何修改呢?
Func _WinCapture($hWnd, $iWidth = -1, $iHeight = -1)
      ;定义程序必须的变量
      Local $iH, $iW, $hDDC, $hCDC, $hBMP
      ;如果调用该函数时候没指定宽和高,则调用AIP获取窗口的宽和高作为抓图区域的宽和高
      If $iWidth = -1 Then $iWidth = _WinAPI_GetWindowWidth($hWnd)
      If $iHeight = -1 Then $iHeight = _WinAPI_GetWindowHeight($hWnd)
      ;创建一个DC句柄
    $hDDC = _WinAPI_GetDC($hWnd)
      ;创建一个与DC句柄兼容的内存句柄
    $hCDC = _WinAPI_CreateCompatibleDC($hDDC)
      ;从DC句柄创建一个宽高为调用值的位图句柄
    $hBMP = _WinAPI_CreateCompatibleBitmap($hDDC, $iWidth, $iHeight)
      ;将位图句柄复制到内存区域中去
    _WinAPI_SelectObject($hCDC, $hBMP)
      ;用 printwindow 取得程序窗口
    DllCall("User32.dll", "int", "PrintWindow", "hwnd", $hWnd, "hwnd", $hCDC, "int", 0)
      ;释放DC句柄
    _WinAPI_ReleaseDC($hWnd, $hDDC)
      ;释放内存句柄
    _WinAPI_DeleteDC($hCDC)
;~   _ScreenCapture_SaveImage(@DesktopDir&"\window.jpg", $hBMP)
    ;返回创建好的位图句柄
    Return $hBMP
EndFunc

ceoguang 发表于 2011-1-2 17:07:32


#include <winapi.au3>
#include <GdiPlus.au3>

$Hwnd = WinGetHandle("");取IE句柄,请确认IE窗口存在
_WinAPI_PrintScreenWithHwnd($Hwnd,200,200)

Func _WinAPI_PrintScreenWithHwnd($Hwnd, $iWidth = 0, $iHeight = 0, $sFileName = 0)
       
        ;参数:
        ;$Hwnd,目标窗口句柄.$sFileName(可选),要保存的文件路径,默在脚本目录
       
        ;返回值:
        ;成功,返回文件名
        ;失败,返回值为0,说明目标窗口被最小化,返回值为-1,说明给定的句柄不是一个有效的windows窗口
       
        If Not $sFileName Then $sFileName = @ScriptDir & "\截图.png"
        If IsIconic($Hwnd) Then Return SetError(-1, 0, 0)
        If Not (_WinAPI_IsWindow($Hwnd)) Then Return SetError(-2, 1, -1)
        _GDIPlus_Startup()
        $hDC = _WinAPI_GetDC($Hwnd)
        If Not $iWidth Then $iWidth = _WinAPI_GetWindowWidth($hWnd)
        If Not $iHeight Then $iHeight = _WinAPI_GetWindowHeight($hWnd)
        $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, $iWidth, $iHeight)
        $memDC = _WinAPI_CreateCompatibleDC($hDC)
        _WinAPI_SelectObject($memDC, $hBMP)
        _WinAPI__PrintWindow($Hwnd, $memDC, 0)
        $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
        _GDIPlus_ImageSaveToFile($hImage, $sFileName)
        _WinAPI_ReleaseDC($Hwnd, $hDC)
        _WinAPI_DeleteDC($memDC)
        _GDIPlus_Shutdown()
        Return SetError(0, 2, $sFileName)
EndFunc   ;==>_WinAPI_PrintScreenWithHwnd

Func _WinAPI__PrintWindow($Hwnd, $hDC, $nFlags = 0)
        Local $aResult = DllCall("user32.dll", "bool", "PrintWindow", "hwnd", $Hwnd, "handle", $hDC, "int", $nFlags)
        If @error Then Return SetError(@error, @extended, 0)
        Return $aResult
EndFunc   ;==>_WinAPI__PrintWindow

Func IsIconic($Hwnd)
        Local $nResult = DllCall("user32.dll", "int", "IsIconic", "hwnd", $Hwnd)
        Return $nResult
EndFunc   ;==>IsIconic

lon91ong 发表于 2011-1-2 17:17:41

太感激版主了!再一次帮了我大忙!

lon91ong 发表于 2011-1-2 17:27:57

刚才没试,现在试了一下,发现还是不对啊,我要的不是这个效果的截图
我想要的是不包括窗口框架的截图,只是网页区域的截图


大侠帮忙!

ceoguang 发表于 2011-1-2 20:04:12

刚才没试,现在试了一下,发现还是不对啊,我要的不是这个效果的截图
我想要的是不包括窗口框架的截图,只是网 ...
lon91ong 发表于 2011-1-2 17:27 http://www.autoitx.com/images/common/back.gif
拜读了一下lanfengc大侠的那个贴子,改了一下返回值.
下这的这个代码应该是你要的
运行结果如图:

代码:

#include <winapi.au3>
#include <GdiPlus.au3>

$Hwnd = WinGetHandle("Google - Chromium", "");Chrom句柄
_WinAPI_PrintScreenWithHwnd($Hwnd, 551, 288, 188, 33)

Func _WinAPI_PrintScreenWithHwnd($Hwnd, $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight, $iFormat = $GDIP_PXF32RGB)
       
        ;参数:
        ;$Hwnd   目标窗口句柄.$sFileName(可选),要保存的文件路径,默在脚本目录
        ;$iLeft    X座标的距离.
        ;$iTop   Y座标的距离.
        ;$iWidth,图片的宽度
        ;$iHeight图片的高度
        ;$iFormat图片的色深
        ;具体定义参考_GDIPlus_BitmapCloneArea()
        ;--------------------------
        ;返回值:
        ;成功,返回位图句柄
        ;失败,返回值为0,说明目标窗口被最小化,返回值为-1,说明给定的句柄不是一个有效的windows窗口.返回值为-2,说明目标窗口不可绘

        If IsIconic($Hwnd) Then Return SetError(-1, 0, 0)
        If Not (_WinAPI_IsWindow($Hwnd)) Then Return SetError(-2, 1, -1)
        _GDIPlus_Startup()
        $hDC = _WinAPI_GetDC($Hwnd)
        $hBMP = _WinAPI_CreateCompatibleBitmap($hDC, _WinAPI_GetWindowWidth($Hwnd), _WinAPI_GetWindowHeight($Hwnd)) ;创建位图,大小为目标窗口的高度及宽度
        $memDC = _WinAPI_CreateCompatibleDC($hDC)
        _WinAPI_SelectObject($memDC, $hBMP)
        _WinAPI__PrintWindow($Hwnd, $memDC, 0)
        $hImage = _GDIPlus_BitmapCreateFromHBITMAP($hBMP)
        If @error Then Return SetError(-3, 2, -2)
        $hClone = _GDIPlus_BitmapCloneArea($hImage, $iLeft, $iTop, $iWidth, $iHeight, $iFormat)
        _GDIPlus_ImageSaveToFile($hClone, @ScriptDir & "\截图.png") ;保存图片
        ;_GDIPlus_ImageDispose($hClone)
        _GDIPlus_ImageDispose($hImage)
        _WinAPI_ReleaseDC($Hwnd, $hDC)
        _WinAPI_DeleteDC($memDC)
        _GDIPlus_Shutdown()
        Return SetError(0, 2, $hClone);返回位图句柄
EndFunc   ;==>_WinAPI_PrintScreenWithHwnd

Func _WinAPI__PrintWindow($Hwnd, $hDC, $nFlags = 0)
        Local $aResult = DllCall("user32.dll", "bool", "PrintWindow", "hwnd", $Hwnd, "handle", $hDC, "int", $nFlags)
        If @error Then Return SetError(@error, @extended, 0)
        Return $aResult
EndFunc   ;==>_WinAPI__PrintWindow

Func IsIconic($Hwnd)
        Local $nResult = DllCall("user32.dll", "int", "IsIconic", "hwnd", $Hwnd)
        Return $nResult
EndFunc   ;==>IsIconic

ceoguang 发表于 2011-1-2 20:05:31

是截图.png.分辨率为1280*1024

ghl781258 发表于 2011-1-2 20:31:45

能不能后台截取指定区域的图呢?例如区域(200,300,500,800)

ceoguang 发表于 2011-1-2 20:34:36

5#的代码就是.只要窗口不被最小化都能操作

lon91ong 发表于 2011-1-2 22:17:56

回复 8# ceoguang


但是lanfengc大侠得那个后台取色最小化也可以的呀!

我想的是怎么能把lanfengc大侠的_WinCapture函数修改一下,可以再进一步接收目标窗口内某个控件的句柄,只对该控件截图
IE窗口的话就是控件

柔和de黑星 发表于 2011-1-8 17:02:53

高人应该做一个 后台取色的UDF 造福后人啊
哈哈哈哈

不过还是喜欢对 元素控制

lixatom 发表于 2011-11-17 11:05:53

学习了,准备用在外挂上

心照不宣 发表于 2012-2-5 21:18:18

不错,标注,非常好。

xjdjpbp 发表于 2013-11-10 17:24:38

回复 9# lon91ong

先用ControlGetPos得知該控件位置大小,在代入 _WinAPI_PrintScreenWithHwnd($Hwnd, $iLeft = 0, $iTop = 0, $iWidth = @DesktopWidth, $iHeight = @DesktopHeight, $iFormat = $GDIP_PXF32RGB)

就可以對該控件截圖了

veve 发表于 2017-5-21 14:53:48

绝对好的代码

erdaxia 发表于 2017-5-23 00:46:11

不敢细看——修行不够,很多看不懂,但却正是我想要做的事
页: [1]
查看完整版本: [已解决]如何取得IE客户区的句柄进行后台取色呢