[green]'@--------------------------------------------------------------@
'@
'@ §lamKeys §oftware 2005® (VBSlammer)
'@
'@ :
'@ @FILENAME : -clsScreenShot.cls
'@ @CREATED : -2/20/2005 10:35:49 PM
'@ @PURPOSE : -Output a screenshot to file (Full Screen or Active Form)
'@ :
'@ @USAGE : Dim oScreenShot As New clsScreenShot
'@ :
'@ : With oScreenShot
'@ : .FilePath = "C:\"
'@ : .FileName = "SampleBitmap1.bmp"
'@ :
'@ : If .CaptureActiveForm = True Then
'@ :
'@ : MsgBox .FullPath & " saved successfully!"
'@ :
'@ : End If
'@ : End With
'@ :
'@ :
'@ @REFERENCES : -Standard OLE Types (OLEPro32.dll)
'@ :
'@ @NOTES : -Full Screen bitmaps may be in excess of 2MB in size.
'@ : -If declared "WithEvents" the app can detect runtime
'@ : -errors and receive notification of output status.
'@ :
'@ @NOTICE : -Open Source for public use - no warranty implied.
'@ : -Include this header with distributed source.
'@ :
'@--------------------------------------------------------------@[/green]
Option Compare Database
Option Explicit
[green]'@----------------------- Constants ----------------------------@[/green]
Private Const SRCCOPY = &HCC0020
Private Const S_OK = &H0
[green]'@----------------------- Variables ----------------------------@[/green]
Private mstrFilePath As String
Private mstrFileName As String
Private mErrorStrings As ErrorStrings
[green]'@------------------------- Events -----------------------------@[/green]
Public Event ErrorOccurred(ByVal ErrNum As Long, ByVal strMsg As String)
Public Event PictureSaved(ByVal strFileName As String, ByVal lngSize As Long, _
ByVal shotType As ScreenShotTypes)
[green]'@--------------------- API Functions --------------------------@[/green]
Private Declare Function BitBlt Lib "gdi32" (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 CreateCompatibleBitmap Lib "gdi32" (ByVal hDC As Long, _
ByVal nWidth As Long, ByVal nHeight As Long) As Long
Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function DeleteDC Lib "gdi32" (ByVal hDC As Long) As Long
Private Declare Function DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
Private Declare Function GetDC Lib "user32" (ByVal hWnd As Long) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hWnd As Long, lpRect As RECT) As Long
Private Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, _
RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
Private Declare Function ReleaseDC Lib "user32" (ByVal hWnd As Long, ByVal hDC As Long) As Long
Private Declare Function SelectObject Lib "gdi32" (ByVal hDC As Long, ByVal hObject As Long) As Long
[green]'@------------------------- Enums ------------------------------@[/green]
Public Enum ScreenShotTypes
ActiveForm
FullScreen
End Enum
Public Enum ScreenShotErrors
InvalidDeviceContext
InvalidPath
NoBitBlt
NoCreatePicture
NoDeleteDC
NoDeleteObject
NoReleaseDC
NoSelectObject
End Enum
[green]'@------------------------- Types ------------------------------@[/green]
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Private Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Private Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Private Type ErrorStrings
InvalidDeviceContext As String
InvalidPath As String
NoBitBlt As String
NoCreatePicture As String
NoDeleteDC As String
NoDeleteObject As String
NoReleaseDC As String
NoSelectObject As String
End Type
[green]'@---------------- Constructor / Destructor --------------------@[/green]
Private Sub Class_Initialize()
[green]' set default path and filename[/green]
mstrFilePath = CurrentProject.Path
mstrFileName = "Screenshot.bmp"
[green]' init error strings[/green]
With mErrorStrings
.InvalidDeviceContext = "Could not find a valid device context for: {%1}"
.InvalidPath = "The chosen path is invalid: {%1}"
.NoBitBlt = "The BitBlt() API function was not successful."
.NoCreatePicture = "The OLECreatePictureIndirect() API function was not successful."
.NoDeleteDC = "Could not delete the device context belonging to: {%1}"
.NoDeleteObject = "Could not delete the object referenced by: {%1}"
.NoReleaseDC = "Could not release the device context belonging to: {%1}"
.NoSelectObject = "Could not select {%1} into memory."
End With
End Sub
Private Sub Class_Terminate()
[green]'destructor code here[/green]
End Sub
[green]'@----------------------- Properties ---------------------------@[/green]
Public Property Get FilePath() As String
FilePath = mstrFilePath
End Property
Public Property Let FilePath(ByVal strFilePath As String)
If Dir(strFilePath) = "" Then
RaiseEvent ErrorOccurred(InvalidPath, Replace(mErrorStrings.InvalidPath, "{%1}", strFilePath))
Else
mstrFilePath = strFilePath
End If
End Property
Public Property Get FileName() As String
FileName = mstrFileName
End Property
Public Property Let FileName(ByVal strFileName As String)
mstrFileName = strFileName
End Property
Public Property Get FullPath() As String
FullPath = PathFix(FilePath, FileName)
End Property
[green]'@-------------------- Public Methods --------------------------@[/green]
Public Function CaptureActiveForm() As Boolean
CaptureActiveForm = OutputToBitmap(ActiveForm)
End Function
Public Function CaptureFullScreen() As Boolean
CaptureFullScreen = OutputToBitmap(FullScreen)
End Function
[green]'@------------------- Private Methods --------------------------@[/green]
Private Function OutputToBitmap(ByVal shotType As ScreenShotTypes) As Boolean
On Error GoTo ErrHandler
Dim frm As Form
Dim hWndDesktop As Long
Dim hDC As Long
Dim hDCMem As Long
Dim hBitmap As Long
Dim strFile As String
Dim RetVal As Long
Dim lngWide As Long
Dim lngHigh As Long
Dim picRect As RECT
Dim Pic As PicBmp
Dim IPic As IPicture [green]'requires reference to "Standard OLE Types"[/green]
Dim RefIID As GUID
[green]' get the desktop handle and device context[/green]
hWndDesktop = GetDesktopWindow()
hDC = GetDC(hWndDesktop)
[green]' get rectangle of requested type[/green]
Select Case shotType
Case ScreenShotTypes.ActiveForm
[green]' reference active form[/green]
Set frm = Screen.ActiveForm
[green]' get the form's bounding rectangle[/green]
Call GetWindowRect(frm.hWnd, picRect)
Case ScreenShotTypes.FullScreen
[green]' get the entire desktop's bounding rectangle[/green]
Call GetWindowRect(hWndDesktop, picRect)
End Select
[green]' calculate height and width[/green]
With picRect
lngWide = .Right - .Left
lngHigh = .Bottom - .Top
End With
[green]' create a new device context for painting[/green]
hDCMem = CreateCompatibleDC(hDC)
[green]' create an empty bitmap the size of the rectangle[/green]
hBitmap = CreateCompatibleBitmap(hDC, lngWide, lngHigh)
[green]' select the bitmap into the new device context[/green]
RetVal = SelectObject(hDCMem, hBitmap)
If RetVal = 0 Then
RaiseEvent ErrorOccurred(NoSelectObject, _
Replace(mErrorStrings.NoSelectObject, "{%1}", "hBitmap"))
Goto CleanUpHere
End If
[green]' blast the bits from the desktop onto the new device context in memory
' starting at the top left corner of the bounding rectangle[/green]
RetVal = BitBlt(hDCMem, 0, 0, lngWide, lngHigh, hDC, picRect.Left, picRect.Top, SRCCOPY)
If RetVal = 0 Then
RaiseEvent ErrorOccurred(NoBitBlt, mErrorStrings.NoBitBlt)
Goto CleanUpHere
End If
[green]' populate structures needed for output to file[/green]
With RefIID
.Data1 = &H20400
.Data4(0) = &HC0
.Data4(7) = &H46
End With
With Pic
.Size = Len(Pic)
.Type = &H1
.hBmp = hBitmap [green]'this is the pointer to the new bitmap[/green]
.hPal = 0&
End With
[green]' create the picture and save to file as bitmap[/green]
RetVal = OleCreatePictureIndirect(Pic, RefIID, 1, IPic)
If RetVal <> S_OK Then
RaiseEvent ErrorOccurred(NoCreatePicture, mErrorStrings.NoCreatePicture)
GoTo CleanUpHere
End If
strFile = FullPath
If Len(strFile) = 0 Then
RaiseEvent ErrorOccurred(InvalidPath, _
Replace(mErrorStrings.InvalidPath, "{%1}", mstrFilePath & " + " & mstrFileName))
Else
stdole.SavePicture IPic, strFile
RaiseEvent PictureSaved(strFile, FileLen(strFile), shotType)
OutputToBitmap = True
End If
CleanUpHere:
[green]' delete the bitmap reference[/green]
RetVal = DeleteObject(hBitmap)
If RetVal = 0 Then
RaiseEvent ErrorOccurred(NoDeleteObject, _
Replace(mErrorStrings.NoDeleteObject, "{%1}", "hBitmap"))
End If
[green]' release desktop resources[/green]
RetVal = ReleaseDC(hWndDesktop, hDC)
If RetVal = 0 Then
RaiseEvent ErrorOccurred(NoReleaseDC, _
Replace(mErrorStrings.NoReleaseDC, "{%1}", "Desktop"))
End If
[green]' delete memory resources[/green]
RetVal = DeleteDC(hDCMem)
If RetVal = 0 Then
RaiseEvent ErrorOccurred(NoDeleteDC, _
Replace(mErrorStrings.NoDeleteDC, "{%1}", "Buffer"))
End If
OutputToBitmap = True
ExitHere:
On Error Resume Next
If Not frm Is Nothing Then Set frm = Nothing
Exit Function
ErrHandler:
Dim lngErr As Long, strMsg As String
lngErr = Err: strMsg = Err.Description
Err.Clear
RaiseEvent ErrorOccurred(lngErr, strMsg)
End Function
[green]'@--------------------------------------------------------------@[/green]
Private Function PathFix(ByVal strPath As String, ByVal strFileName As String) As String
On Error GoTo ErrHandler
If Len(strPath) > 0 And Len(strFileName) > 0 Then
If Dir(strPath) = "" Then
If Dir(strPath & "\") = "" Then
Exit Function
End If
End If
If Right(strPath, 1) <> "\" Then
If Left(strFileName, 1) <> "\" Then
PathFix = strPath & "\" & strFileName
Else
PathFix = strPath & strFileName
End If
Else
If Left(strFileName, 1) <> "\" Then
PathFix = strPath & strFileName
Else
PathFix = Mid(strPath, 1, Len(strPath) - 1) & strFileName
End If
End If
End If
ExitHere:
Exit Function
ErrHandler:
Dim lngErr As Long, strMsg As String
lngErr = Err: strMsg = Err.Description
Err.Clear
RaiseEvent ErrorOccurred(lngErr, strMsg)
End Function
[green]'@---------------------- End of Class --------------------------@[/green]