2014-11-20 86 views
1

我有這個小片劇本的,但是當我在使用PrintWindow它只有返回一個黑色的捕獲:有沒有辦法使用PrintWindow和控件? (使用的AutoIt)

PrintWindow()正在罰款的窗口句柄,但它與控制手柄不是。

(或者是有辦法來捕獲只在中間的窗口或東西的底部,而不需要獲取完整的窗口和切割嗎?)

AutoIt腳本:

Local $hWnd = ControlGetHandle("[CLASS:Notepad]","","Edit1") 
Local $pos = ControlGetPos($hWnd,"","") 
;MsgBox($MB_OK, "OK", $pos[0]) 
Local $Width = $pos[2] 
Local $Height = $pos[3] 

Local $hDC = _WinAPI_GetDC($hWnd) 
Local $memDC = _WinAPI_CreateCompatibleDC($hDC) 
Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height) 
_WinAPI_SelectObject ($memDC, $memBmp) 
;DllCall("User32.dll","int","PrintWindow","hwnd",$hWnd,"hwnd",$memDC,"int",0) 
;_WinAPI_BitBlt($hDC, 0, 0, $Width, $Height, $memDC, 0,0, $SRCCOPY) 
_WinAPI_BitBlt($memDC, 0, 0, $Width, $Height, $hDC, 0,0, $SRCCOPY) ;this is working now! 

_GDIPlus_Startup() 
Local $hBMP=_GDIPlus_BitmapCreateFromHBITMAP($memBmp) 
Local $hHBITMAP=_GDIPlus_BitmapCreateHBITMAPFromBitmap($hBMP) 

_WinAPI_DeleteObject($hDC) 
_WinAPI_ReleaseDC($hWnd, $hDC) 
_WinAPI_DeleteDC($memDC) 
_WinAPI_DeleteObject ($memBmp) 
_WinAPI_DeleteDC($hDC) 

$sPath = @ScriptDir & '\capture.bmp' 
_WinAPI_SaveHBITMAPToFile($sPath, $hHBITMAP) 

回答

1

我有這個小片劇本的,但是當我在使用PrintWindow它只有返回一個黑色的捕獲:

首先,我不知道你使用的語言,但你的代碼的問題是足以讓我嘗試提供解決方案。

馬上蝙蝠,在我看來,你的第二個參數爲PrintWindow是錯誤的(它是HWND,但它應該是HDC)。

其次,您的代碼中存在GDI泄漏,但我已更正它 - >在代碼中查看我的評論。長話短說,每次你在設備上下文中,你都會在選擇之前「推出」「站在那裏」的原始對象。該原始對象必須保存並「放回」。如果沒有,那麼隨着時間的推移,你的記憶將被耗盡,你的應用程序將凍結。只是谷歌的「GDI泄漏」,你會發現我所描述的內容的詳細解釋。

第三,當然你會因爲你的初始HDC是空的而被黑色捕獲 - >你需要將你的memDC的內容轉移到hDC。要做到這一點,你需要使用BitBlt函數。正如我所說,我不知道你工作的語言,但我試圖在下面的插圖中給你僞代碼,這樣你就可以知道該怎麼做。

Local $hWnd = ControlGetHandle("[CLASS:Notepad]","","Edit1") 
Local $pos = ControlGetPos($hWnd,"","") 
;MsgBox($MB_OK, "OK", $pos[0]) 
Local $Width = $pos[2] 
Local $Height = $pos[3] 

Local $hDC = _WinAPI_GetDC($hWnd) 
Local $memDC = _WinAPI_CreateCompatibleDC($hDC) 
Local $memBmp = _WinAPI_CreateCompatibleBitmap($hDC, $Width, $Height) 

Local $bmpOriginal = _WinAPI_SelectObject ($memDC, $memBmp) ;store original DC bitmap 

DllCall("User32.dll","int","PrintWindow","hwnd",$hWnd, 
    "hdc", ; I think this is an error, this parameter is of type HDC 
    $memDC,"int",0) 

DllCall("User32.dll","int","BitBlt", "hdc", memDC, 
    ... , ; fill in the rest of parameters 
    "hdc" , hDC, 
    ...) ; fill in the rest of parameters. Your last parameter should be SRCCOPY! 

; when done with the DC, first select back the original bitmap 
_WinAPI_SelectObject($memDC, $bmpOriginal) 
; now we can delete memory bitmap since it is no longer needed 
_WinAPI_DeleteObject($memBmp) 
; delete memory DC since we performed proper cleanup 
_WinAPI_DeleteDC($memDC) 
; release window's DC 
_WinAPI_ReleaseDC($hwnd, $hDC) 

我希望這會有所幫助,如果您還有其他問題請留言,我會盡力幫助。

(或者是有辦法來捕獲只在中間的窗口或東西的底部?)

是的,但首先我需要知道,如果上面的方法對你的作品。如果你仍然需要這第二部分,並會盡力幫助你,請留下評論。

祝你好運!

+0

謝謝你,我修復了我的代碼,但是我仍然有一個黑色的位圖,沒有任何可見的東西,我的函數PrintWindow在窗口句柄下工作正常,當我添加控制句柄時它不工作。 – shuji 2014-11-20 23:55:58

+0

hdc參數需要聲明爲hwnd我不知道爲什麼和在BitBlt調用之後,我的記事本編輯控件正在填充黑色。 – shuji 2014-11-21 00:16:25

+0

嘗試使用以下示例將'memBmp'呈現爲文件:[this](http://msdn.microsoft.com/en-us/library/windows/desktop/dd145119(v = vs.85).aspx )和[this](http://msdn.microsoft.com/en-us/library/windows/desktop/dd183402(v = vs.85).aspx)。簡單的「Result.bmp」就足夠了,只要確保擴展名是'.bmp'!這會告訴我們'PrintWindow'是否運行良好,因爲我懷疑編輯控件可能是問題所在...... – AlwaysLearningNewStuff 2014-11-21 01:03:36

1

最簡單 - 拍攝整個窗口的快照,然後剪切圖像的所需部分(例如控制)。通過PrintWindow控制黑色圖像的原因之一是控件的CS_PARENTDC。

+0

我並不在乎它是否更困難,因爲高分辨率顯示器需要很長時間才能截取屏幕截圖。如果您可以爲我想聽到的問題想一個解決方案,謝謝。 – shuji 2014-11-21 00:01:34

+0

也許下面的演示對你很有用(按鈕「PrintWindow_M」實現我的建議):http://files.rsdn.ru/42164/printlayered.zip(http://files.rsdn.ru/42164/printlayered .JPG)。 – kero 2014-11-21 01:33:04

相關問題