Attribute VB_Name
=
"
ModuleCapturePic
"
Option
Explicit
Option
Base
0
'Powered by barenx
Private
Type PALETTEENTRY
peRed
As
Byte
peGreen
As
Byte
peBlue
As
Byte
peFlags
As
Byte
End
Type
Private
Type LOGPALETTE
palVersion
As
Integer
palNumEntries
As
Integer
palPalEntry(
255
)
As
PALETTEENTRY
'
Enough for 256 colors.
End
Type
Private
Type GUID
Data1
As
Long
Data2
As
Integer
Data3
As
Integer
Data4(
7
)
As
Byte
End
Type
Private
Const
RASTERCAPS
As
Long
=
38
Private
Const
RC_PALETTE
As
Long
=
&
H100
Private
Const
SIZEPALETTE
As
Long
=
104

Private
Type RECT
Left
As
Long
Top
As
Long
right
As
Long
bottom
As
Long
End
Type

Private
Declare
Function CreateCompatibleDC()
Function CreateCompatibleDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function CreateCompatibleBitmap()Function CreateCompatibleBitmap Lib "gdi32" (ByVal hdc As Long, ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function GetDeviceCaps()Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
Private Declare Function GetSystemPaletteEntries()Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
Private Declare Function CreatePalette()Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
Private Declare Function SelectObject()Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
Private Declare Function BitBlt()Function BitBlt Lib "gdi32" (ByVal hDCDest As Long, ByVal XDest As Long, ByVal YDest As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hDCSrc As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal dwRop As Long) As Long
Private Declare Function DeleteDC()Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetForegroundWindow()Function GetForegroundWindow Lib "user32" () As Long
Private Declare Function SelectPalette()Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
Private Declare Function RealizePalette()Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function GetWindowDC()Function GetWindowDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetDC()Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Private Declare Function GetWindowRect()Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function ReleaseDC()Function ReleaseDC Lib "user32" (ByVal hwnd As Long, ByVal hdc As Long) As Long
Private Declare Function GetDesktopWindow()Function GetDesktopWindow Lib "user32" () As Long
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type

Private Declare Function OleCreatePictureIndirect()Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
'-----------------------------------------------------------------
Private Type POINTAPI
x As Long
y As Long
End Type
Private Type BITMAP '14 bytes
bmType As Long
bmWidth As Long
bmHeight As Long
bmWidthBytes As Long
bmPlanes As Integer
bmBitsPixel As Integer
bmBits As Long
End Type
'-----------------------------------------------------------------
Private Declare Function CreateBitmap()Function CreateBitmap Lib "gdi32" (ByVal nWidth As Long, ByVal nHeight As Long, ByVal nPlanes As Long, ByVal nBitCount As Long, lpBits As Any) As Long
Private Declare Function CreateBitmapIndirect()Function CreateBitmapIndirect Lib "gdi32" (lpBitmap As BITMAP) As Long
Private Declare Function GetObject()Function GetObject Lib "gdi32" Alias "GetObjectA" (ByVal hObject As Long, ByVal nCount As Long, lpObject As Any) As Long
Private Declare Function DeleteObject()Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetMapMode()Function GetMapMode Lib "gdi32" (ByVal hdc As Long) As Long
Private Declare Function SetMapMode()Function SetMapMode Lib "gdi32" (ByVal hdc As Long, ByVal nMapMode As Long) As Long
Private Declare Function SetBkColor()Function SetBkColor Lib "gdi32" (ByVal hdc As Long, ByVal crColor As Long) As Long
Private Declare Function StretchBlt()Function StretchBlt Lib "gdi32" (ByVal hdc As Long, ByVal x As Long, ByVal y As Long, ByVal nWidth As Long, ByVal nHeight As Long, ByVal hSrcDC As Long, ByVal xSrc As Long, ByVal ySrc As Long, ByVal nSrcWidth As Long, ByVal nSrcHeight As Long, ByVal dwRop As Long) As Long
'-----------------------------------------------------------------
'==================================================================
' 函数: ShrinkBmp
'
' 功能: 伸缩位图
'
' 入口: dispHdc DC
' hBmp 位图句柄
' RatioX 横向比例
' RatioY 纵向比例
'
Public Function ShrinkBmp()Function ShrinkBmp(dispHdc As Long, hBmp As Long, RatioX As Single, RatioY As Single) As Long
'-----------------------------------------------------------------
Dim hBmpOut As Long ' output bitmap handle
Dim bm1 As BITMAP, bm2 As BITMAP ' temporary bitmap structs
Dim hdcMem1 As Long, hdcMem2 As Long ' temporary memory bitmap handles...
'-----------------------------------------------------------------
hdcMem1 = CreateCompatibleDC(dispHdc) ' create mem DC compatible to the display DC
hdcMem2 = CreateCompatibleDC(dispHdc) ' create mem DC compatible to the display DC
GetObject hBmp, LenB(bm1), bm1 ' select bitmap object
LSet bm2 = bm1 ' copy bitmap object
bm2.bmWidth = CLng(bm2.bmWidth * RatioX) ' scale output bitmap width
bm2.bmHeight = CLng(bm2.bmHeight * RatioY) ' scale output bitmap height
bm2.bmWidthBytes = ((((bm2.bmWidth * bm2.bmBitsPixel) + 15) 16) * 2) ' calculate bitmap width bytes
hBmpOut = CreateBitmapIndirect(bm2) ' create handle to output bitmap indirectly from new bm2
SelectObject hdcMem1, hBmp ' select original bitmap into mem dc
SelectObject hdcMem2, hBmpOut ' select new bitmap into mem dc
' stretch old bitmap into new bitmap
StretchBlt hdcMem2, 0, 0, bm2.bmWidth, bm2.bmHeight, _
hdcMem1, 0, 0, bm1.bmWidth, bm1.bmHeight, vbSrcCopy
DeleteDC hdcMem1 ' delete memory dc
DeleteDC hdcMem2 ' delete memory dc
ShrinkBmp = hBmpOut ' return handle to new bitmap
'-----------------------------------------------------------------
End Function
'==================================================================

'==================================================================
' 函数: DrawBitmap
'
' 功能: 将原位图其中的某一区域绘制到目标DC上
'
' 入口: DestDC '目标DC
' SrcBmp '原位图句柄
' BmpStartX '区域的起始点X
' BmpStartY '区域的起始点X
' BmpWidth '区域的宽
' BmpHeight '区域的高
'
Public Sub DrawBitmap()Sub DrawBitmap(ByVal DestDC&, _
ByVal SrcBmp&, _
Optional ByVal BmpStartX& = 0, _
Optional ByVal BmpStartY& = 0, _
Optional ByVal BmpWidth& = -1, _
Optional ByVal BmpHeight& = -1, _
Optional ByVal ToStartX& = 0, _
Optional ByVal ToStartY& = 0)
'-----------------------------------------------------------------
Dim udtBitMap As BITMAP
Dim SrcDCTemp&
Dim SrcOldBmp&
SrcDCTemp = CreateCompatibleDC(DestDC) 'Create a temporary HDC compatible to the Destination HDC
SrcOldBmp = SelectObject(SrcDCTemp, SrcBmp) 'Select the bitmap
GetObject SrcBmp, Len(udtBitMap), udtBitMap
'Get bmWidth and bmHeight
With udtBitMap
If BmpWidth = -1 Then BmpWidth = .bmWidth
If BmpHeight = -1 Then BmpHeight = .bmHeight
End With
'Paint SrcBmp to DestDC
BitBlt DestDC, ToStartX, ToStartY, BmpWidth, BmpHeight, SrcDCTemp, BmpStartX, BmpStartY, vbSrcCopy
'Release resource
SelectObject SrcDCTemp, SrcOldBmp
DeleteDC SrcDCTemp
'-----------------------------------------------------------------
End Sub
'==================================================================
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CreateBitmapPicture
' - Creates a bitmap type Picture object from a bitmap and
' palette.
'
' hBmp
' - Handle to a bitmap.
'
' hPal
' - Handle to a Palette.
' - Can be null if the bitmap doesn't use a palette.
'
' Returns
' - Returns a Picture object containing the bitmap.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'

Private Function CreateBitmapPicture()Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
Dim r As Long
Dim pic As PicBmp
' IPicture requires a reference to "Standard OLE Types."
Dim IPic As IPicture
Dim IID_IDispatch As GUID
' Fill in with IDispatch Interface ID.
With IID_IDispatch
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
' Fill Pic with necessary parts.
With pic
.Size = Len(pic) ' Length of structure.
.Type = vbPicTypeBitmap ' Type of Picture (bitmap).
.hBmp = hBmp ' Handle to bitmap.
.hPal = hPal ' Handle to palette (may be null).
End With
' Create Picture object.
r = OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
' Return the new Picture object.
Set CreateBitmapPicture = IPic
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureWindow
' - Captures any portion of a window.
'
' hWndSrc
' - Handle to the window to be captured.
'
' Client
' - If True CaptureWindow captures from the client area of the
' window.
' - If False CaptureWindow captures from the entire window.
'
' LeftSrc, TopSrc, WidthSrc, HeightSrc
' - Specify the portion of the window to capture.
' - Dimensions need to be specified in pixels.
'
' Returns
' - Returns a Picture object containing a bitmap of the specified
' portion of the window that was captured.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
''''''
'

Public Function CaptureWindow()Function CaptureWindow(ByVal hWndSrc As Long, ByVal Client As Boolean, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim r As Long
Dim hDCSrc As Long
Dim hPal As Long
Dim hPalPrev As Long
Dim RasterCapsScrn As Long
Dim HasPaletteScrn As Long
Dim PaletteSizeScrn As Long
Dim LogPal As LOGPALETTE
' Depending on the value of Client get the proper device context.
If Client Then
hDCSrc = GetDC(hWndSrc) ' Get device context for client area.
Else
hDCSrc = GetWindowDC(hWndSrc) ' Get device context for entire
' window.
End If
' Create a memory device context for the copy process.
hDCMemory = CreateCompatibleDC(hDCSrc)
' Create a bitmap and place it in the memory DC.
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
' Get screen properties.
RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
' capabilities.
HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
' support.
PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
' palette.
' If the screen has a palette make a copy and realize it.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
' Create a copy of the system palette.
LogPal.palVersion = &H300
LogPal.palNumEntries = 256
r = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
hPal = CreatePalette(LogPal)
' Select the new palette into the memory DC and realize it.
hPalPrev = SelectPalette(hDCMemory, hPal, 0)
r = RealizePalette(hDCMemory)
End If
' Copy the on-screen image into the memory DC.
r = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
' Remove the new copy of the on-screen image.
hBmp = SelectObject(hDCMemory, hBmpPrev)
' If the screen has a palette get back the palette that was
' selected in previously.
If HasPaletteScrn And (PaletteSizeScrn = 256) Then
hPal = SelectPalette(hDCMemory, hPalPrev, 0)
End If
' Release the device context resources back to the system.
r = DeleteDC(hDCMemory)
r = ReleaseDC(hWndSrc, hDCSrc)
' Call CreateBitmapPicture to create a picture object from the
' bitmap and palette handles. Then return the resulting picture
' object.
Set CaptureWindow = CreateBitmapPicture(hBmp, hPal)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureScreen
' - Captures the entire screen.
'
' Returns
' - Returns a Picture object containing a bitmap of the screen.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureScreen()Function CaptureScreen() As Picture
Dim hWndScreen As Long
' Get a handle to the desktop window.
hWndScreen = GetDesktopWindow()
' Call CaptureWindow to capture the entire desktop give the handle
' and return the resulting Picture object.
Set CaptureScreen = CaptureWindow(hWndScreen, False, 0, 0, Screen.Width Screen.TwipsPerPixelX, Screen.Height Screen.TwipsPerPixelY)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureForm
' - Captures an entire form including title bar and border.
'
' frmSrc
' - The Form object to capture.
'
' Returns
' - Returns a Picture object containing a bitmap of the entire
' form.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureForm()Function CaptureForm(frmSrc As Form) As Picture
' Call CaptureWindow to capture the entire form given its window
' handle and then return the resulting Picture object.
Set CaptureForm = CaptureWindow(frmSrc.hwnd, False, 0, 0, frmSrc.ScaleX(frmSrc.Width, vbTwips, vbPixels), frmSrc.ScaleY(frmSrc.Height, vbTwips, vbPixels))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureClient
' - Captures the client area of a form.
'
' frmSrc
' - The Form object to capture.
'
' Returns
' - Returns a Picture object containing a bitmap of the form's
' client area.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureClient()Function CaptureClient(frmSrc As Form) As Picture
' Call CaptureWindow to capture the client area of the form given
' its window handle and return the resulting Picture object.
Set CaptureClient = CaptureWindow(frmSrc.hwnd, True, 0, 0, frmSrc.ScaleX(frmSrc.ScaleWidth, frmSrc.ScaleMode, vbPixels), frmSrc.ScaleY(frmSrc.ScaleHeight, frmSrc.ScaleMode, vbPixels))
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' CaptureActiveWindow
' - Captures the currently active window on the screen.
'
' Returns
' - Returns a Picture object containing a bitmap of the active
' window.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Function CaptureActiveWindow()Function CaptureActiveWindow() As Picture
Dim hWndActive As Long
Dim r As Long
Dim RectActive As RECT
' Get a handle to the active/foreground window.
hWndActive = GetForegroundWindow()
' Get the dimensions of the window.
r = GetWindowRect(hWndActive, RectActive)
' Call CaptureWindow to capture the active window given its
' handle and return the Resulting Picture object.
Set CaptureActiveWindow = CaptureWindow(hWndActive, False, 0, 0, RectActive.right - RectActive.Left, RectActive.bottom - RectActive.Top)
End Function
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
' PrintPictureToFitPage
' - Prints a Picture object as big as possible.
'
' Prn
' - Destination Printer object.
'
' Pic
' - Source Picture object.
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
'
Public Sub PrintPictureToFitPage()Sub PrintPictureToFitPage(Prn As Printer, pic As Picture)
Const vbHiMetric As Integer = 8
Dim PicRatio As Double
Dim PrnWidth As Double
Dim PrnHeight As Double
Dim PrnRatio As Double
Dim PrnPicWidth As Double
Dim PrnPicHeight As Double
' Determine if picture should be printed in landscape or portrait
' and set the orientation.
If pic.Height >= pic.Width Then
Prn.Orientation = vbPRORPortrait ' Taller than wide.
Else
Prn.Orientation = vbPRORLandscape ' Wider than tall.
End If
' Calculate device independent Width-to-Height ratio for picture.
PicRatio = pic.Width / pic.Height
' Calculate the dimentions of the printable area in HiMetric.
PrnWidth = Prn.ScaleX(Prn.ScaleWidth, Prn.ScaleMode, vbHiMetric)
PrnHeight = Prn.ScaleY(Prn.ScaleHeight, Prn.ScaleMode, vbHiMetric)
' Calculate device independent Width to Height ratio for printer.
PrnRatio = PrnWidth / PrnHeight
' Scale the output to the printable area.
If PicRatio >= PrnRatio Then
' Scale picture to fit full width of printable area.
PrnPicWidth = Prn.ScaleX(PrnWidth, vbHiMetric, Prn.ScaleMode)
PrnPicHeight = Prn.ScaleY(PrnWidth / PicRatio, vbHiMetric, Prn.ScaleMode)
Else
' Scale picture to fit full height of printable area.
PrnPicHeight = Prn.ScaleY(PrnHeight, vbHiMetric, Prn.ScaleMode)
PrnPicWidth = Prn.ScaleX(PrnHeight * PicRatio, vbHiMetric, Prn.ScaleMode)
End If
' Print the picture using the PaintPicture method.
Prn.PaintPicture pic, 0, 0, PrnPicWidth, PrnPicHeight
End Sub
'-------------------------------------------------------------------
这段VB代码详细展示了如何实现屏幕抓取和截图功能。包括创建兼容的设备上下文,位图处理,以及捕获窗口和屏幕的不同部分。通过`CaptureWindow`、`CaptureScreen`、`CaptureForm`和`CaptureClient`等函数,可以实现窗口客户区、整个窗口、活动窗口和表单区域的截图。最后,`PrintPictureToFitPage`函数用于将图片按比例打印到页面上。
1538

被折叠的 条评论
为什么被折叠?



