OLE方式取得Screen畫面
來源:vb Net  cww修改

本站曾使用模擬按Print screen的方式取得Active Form/ Screen,但有時並不足以解決所有問題
,例如:取得MDIForm的Active Form,與只取得Form的某個部份。使用OLE的方式,取得的是
Picture物件,這就可以大大方方的與PictureBox等物件的Picture物件來合用,如果該圖要存檔,
也是用SavePicture 傳入該Picture物件便可。

使用方如下
Dim pic As IPictureDisp
Clipboard.Clear
'Set pic = GetOLEWindowSnapshot(Screen.ActiveForm)
Set pic = GetOLEClientSnapshot(Screen.ActiveForm)
Set Picture1.Picture = Pic   'Show 在PictureBox上
Clipboard.SetData pic, 2
MsgBox "可以到小畫家上貼上看一看"


以下在.Bas
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