Option Explicit
Public Type GUID
Data1 As Long
Data2 As Integer
Data3 As Integer
Data4(7) As Byte
End Type
Public Type PicBmp
Size As Long
Type As Long
hBmp As Long
hPal As Long
Reserved As Long
End Type
Public Declare Function GetDesktopWindow Lib _
"user32" () As Long
Public Declare Function CreateCompatibleDC Lib _
"gdi32" _
(ByVal hdc As Long) As Long
Public Declare Function CreateCompatibleBitmap Lib _
"gdi32" _
(ByVal hdc As Long, ByVal nWidth As Long, _
ByVal nHeight As Long) As Long
Public Declare Function SelectObject Lib "gdi32" _
(ByVal hdc As Long, ByVal hObject As Long) As Long
Public Declare 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
Public Declare Function DeleteDC Lib "gdi32" _
(ByVal hdc As Long) As Long
Public Declare Function GetWindowDC Lib "user32" _
(ByVal hwnd As Long) As Long
Public Declare Function ReleaseDC Lib "user32" _
(ByVal hwnd As Long, ByVal hdc As Long) As Long
Public Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
Public Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Public Declare Function GetClientRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Public Declare Function OleCreatePictureIndirect Lib _
"olepro32.dll" _
(PicDesc As PicBmp, RefIID As GUID, _
ByVal fPictureOwnsHandle As Long, _
IPic As IPicture) As Long
Public Function GetOLEScreenSnapshot() As Picture
Dim hWndSrc As Long
Dim hDCSrc As Long
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim WidthSrc As Long
Dim HeightSrc As Long
Dim r As Long
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
'取得Screen的長寬
WidthSrc = Screen.Width \ Screen.TwipsPerPixelX
HeightSrc = Screen.Height \ Screen.TwipsPerPixelY
'取得Screen的hWnd與其hDc
hWndSrc = GetDesktopWindow()
hDCSrc = GetWindowDC(hWndSrc)
'建一Memory DC
hDCMemory = CreateCompatibleDC(hDCSrc)
'Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
'把Screen的Image畫在memoryDC所指的Bitmap上
Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
hDCSrc, 0, 0, vbSrcCopy)
'Remove the new copy of the the on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)
'Release the device context resources back to the system
Call DeleteDC(hDCMemory)
Call ReleaseDC(hWndSrc, hDCSrc)
'Fill in OLE 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 = 0& 'Handle to palette (may be null)
End With
'Create OLE Picture object
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
'Return the new Picture object
Set GetOLEScreenSnapshot = IPic
End Function
Public Function GetOLEWindowSnapshot(frmSelect As Form) As Picture
Dim hWndSrc As Long
Dim hDCSrc As Long
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim WidthSrc As Long
Dim HeightSrc As Long
Dim r As Long
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
'Get a handle to the Active window and get the proper device context
hWndSrc = frmSelect.hwnd
hDCSrc = GetWindowDC(hWndSrc)
'Create a memory device context for the copy process
hDCMemory = CreateCompatibleDC(hDCSrc)
WidthSrc = frmSelect.ScaleX(frmSelect.Width, frmSelect.ScaleMode, vbPixels)
HeightSrc = frmSelect.ScaleY(frmSelect.Height, frmSelect.ScaleMode, vbPixels)
'Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
'Copy the Active Window image into the memory DC
Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
hDCSrc, 0, 0, vbSrcCopy)
'Remove the new copy of the the on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)
'Release the device context resources back to the system
Call DeleteDC(hDCMemory)
Call ReleaseDC(hWndSrc, hDCSrc)
'Fill in OLE 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 = 0& 'Handle to palette (may be null)
End With
'Create OLE Picture object
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
'Return the new Picture object
Set GetOLEWindowSnapshot = IPic
End Function
'這Function只取得ActiveForm的Client Area(不含Title, Menu, Border etc.)
Public Function GetOLEClientSnapshot(frmSelect As Form) As Picture
Dim hWndSrc As Long
Dim hDCSrc As Long
Dim hDCMemory As Long
Dim hBmp As Long
Dim hBmpPrev As Long
Dim WidthSrc As Long
Dim HeightSrc As Long
Dim r As Long
Dim pic As PicBmp
Dim IPic As IPicture
Dim IID_IDispatch As GUID
Dim rc As RECT
hWndSrc = frmSelect.hwnd
hDCSrc = GetDC(hWndSrc)
GetClientRect hWndSrc, rc
hDCMemory = CreateCompatibleDC(hDCSrc)
WidthSrc = rc.Right - rc.Left
HeightSrc = rc.Bottom - rc.Top
'Create a bitmap and place it in the memory DC
hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
hBmpPrev = SelectObject(hDCMemory, hBmp)
'Copy the on-screen image into the memory DC
Call BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, _
hDCSrc, 0, 0, vbSrcCopy)
'Remove the new copy of the the on-screen image
hBmp = SelectObject(hDCMemory, hBmpPrev)
'Release the device context resources back to the system
Call DeleteDC(hDCMemory)
Call ReleaseDC(hWndSrc, hDCSrc)
'Fill in OLE 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 = 0& 'Handle to palette (may be null)
End With
'Create OLE Picture object
Call OleCreatePictureIndirect(pic, IID_IDispatch, 1, IPic)
'Return the new Picture object
Set GetOLEClientSnapshot = IPic
End Function
|