|
本帖最后由 shofing 于 2018-9-13 13:58 编辑
网上找到了个VBA后台窗口截图代码,测试没有成功,请高手请帮完善下,感激
引用地址 http://wolfccb.com/?p=296
以下是代码
- Function ScreenDump(ByVal hWnd As Long, Optional ByVal croptop As Integer = 0, Optional ByVal cropbottom As Integer = 0, Optional ByVal cropleft As Integer = 0, Optional ByVal cropright As Integer = 0) As String
- 'Dump the screen of specified form and save to clipboard.
- 'hWnd is the handle of target form, croptop/cropbottom/cropleft/cropright are pixels to crop, 0 by default means no crop.
- 'Return string message
-
- Dim UserFormHwnd As Long, DeskHwnd As Long
- Dim hdc As Long
- Dim hdcMem, hdcMemc As Long
- Dim hBitmap, hBitmapc As Long
- Dim rect As RECT_Type
- Dim retval As Long
- Dim fwidth As Long, fheight As Long
- Dim isCrop As Boolean
- If croptop < 0 Or cropbottom < 0 Or cropleft < 0 Or cropright < 0 Then
- ScreenDump = "0-Wrong parameter"
- Exit Function
- End If
-
- If croptop > 0 Or cropbottom > 0 Or cropleft > 0 Or cropright > 0 Then
- isCrop = True
- End If
-
- ' Get window handle
- DeskHwnd = GetDesktopWindow()
- UserFormHwnd = hWnd
-
- ' Get screen coordinates
- Call GetWindowRect(UserFormHwnd, rect)
- fwidth = rect.right - rect.left
- fheight = rect.bottom - rect.top
- ' Get the device context of Desktop and allocate memory
- hdc = GetDC(DeskHwnd)
- hdcMem = CreateCompatibleDC(hdc)
- hBitmap = CreateCompatibleBitmap(hdc, fwidth, fheight)
- If hBitmap <> 0 Then
-
- retval = SelectObject(hdcMem, hBitmap)
- 'Redraw window before capture
- RedrawWindow UserFormHwnd, ByVal 0, ByVal 0, RDW_INVALIDATE Or RDW_ALLCHILDREN Or RDW_UPDATENOW
-
- ' Copy bitmap to memory location
-
- retval = PrintWindow(UserFormHwnd, hdcMem, 0)
- 'Crop image
- If isCrop = True Then
-
- fwidth = fwidth - cropleft - cropright
- fheight = fheight - croptop - cropbottom
-
- 'Allocate memory for cropped image
- hdcMemc = CreateCompatibleDC(hdc)
- hBitmapc = CreateCompatibleBitmap(hdc, fwidth, fheight)
- retval = SelectObject(hdcMemc, hBitmapc)
-
- 'Crop
- retval = BitBlt(hdcMemc, 0, 0, fwidth, fheight, hdcMem, cropleft, croptop, SRCCOPY)
- 'Set up the Clipboard and copy bitmap
- retval = OpenClipboard(DeskHwnd)
- retval = EmptyClipboard()
- retval = SetClipboardData(CF_BITMAP, hBitmapc)
- retval = CloseClipboard()
-
- 'Clean up
- retval = DeleteDC(hdcMemc)
- retval = DeleteObject(hBitmapc)
-
- Else
-
- 'Set up the Clipboard and copy bitmap
- retval = OpenClipboard(DeskHwnd)
- retval = EmptyClipboard()
- retval = SetClipboardData(CF_BITMAP, hBitmap)
- retval = CloseClipboard()
-
- End If
- ScreenDump = "1-Success"
-
- Else
-
- ScreenDump = "2-No bitmap"
-
- End If
- ' Clean up
- retval = DeleteDC(hdcMem)
- retval = ReleaseDC(DeskHwnd, hdc)
- retval = DeleteObject(hBitmap)
- End Function
复制代码
|
|