|
Option Explicit
Private Declare Function GetDesktopWindow Lib "user32.dll" () _
As Long
Private Declare Function GetDC Lib "user32.dll" ( _
ByVal hwnd As Long) As Long
Private Declare Function ReleaseDC Lib "user32.dll" ( _
ByVal hwnd As Long, _
ByVal hdc As Long) As Long
Private Declare Function GetSystemMetrics Lib "user32.dll" ( _
ByVal nIndex As Long) As Long
Private Declare Function BitBlt Lib "gdi32.dll" ( _
ByVal hDestDC 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 dwRop As Long) As Long
Private Declare Function StretchBlt Lib "gdi32.dll" ( _
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
Public Enum eCaptureMode
CropPicture = 0
SizePicBox = 1
SizePicture = 2
End Enum
Public Function GetDesktop(ByRef pbPictureBox As PictureBox, _
Optional ByVal CaptureMode As eCaptureMode = CropPicture) _
As Boolean
Dim hDesktop As Long
Dim hDesktopDC As Long
Dim ScreenWidth As Long
Dim ScreenHeight As Long
hDesktop = GetDesktopWindow()
hDesktopDC = GetDC(hDesktop)
ScreenWidth = GetSystemMetrics(0&)
ScreenHeight = GetSystemMetrics(1&)
With pbPictureBox
If CaptureMode = SizePicBox Then
.Width = ScreenWidth * Screen.TwipsPerPixelX
.Height = ScreenHeight * Screen.TwipsPerPixelY
End If
.AutoRedraw = True
.Cls
End With
If CaptureMode = CropPicture Or CaptureMode = SizePicBox Then
Call BitBlt(pbPictureBox.hdc, 0&, 0&, _
ScreenWidth, ScreenHeight, _
hDesktopDC, 0&, 0&, vbSrcCopy)
Else
Call StretchBlt(pbPictureBox.hdc, 0&, 0&, _
pbPictureBox.Width \ Screen.TwipsPerPixelX, _
pbPictureBox.Height \ Screen.TwipsPerPixelY, _
hDesktopDC, 0&, 0&, _
ScreenWidth, ScreenHeight, vbSrcCopy)
End If
With pbPictureBox
.AutoRedraw = False
.Refresh
End With
Call ReleaseDC(hDesktop, hDesktopDC)
End Function
|
|