Visual Basic Tip: 02/19/96

Screen Capture.

The screen capture code listed below takes parameters X,Y,Width,and Height.
Your running VB program can easily copy any portion of the screen to the Clipboard.

  DefInt A-Z

  Declare Sub ReleaseDC Lib "User" (ByVal hWnd, ByVal hDC)
  Declare Sub OpenClipBoard Lib "User" (ByVal hWnd)
  Declare Sub EmptyClipBoard Lib "User" ()
  Declare Sub SetClipBoardData Lib "User" (ByVal CBFormat, ByVal hBitMap)
  Declare Sub CloseClipBoard Lib "User" ()
  Declare Sub SelectObject Lib "GDI" (ByVal hDC, ByVal hObj)
  Declare Sub DeleteDC Lib "GDI" (ByVal hDC)
  Declare Sub BitBlt Lib "GDI" (ByVal DestDC, ByVal X, ByVal Y, ByVal BWidth, ByVal BHeight, ByVal SourceDC, ByVal X, ByVal Y, ByVal Constant&)
  Declare Function CreateDC Lib "GDI" (ByVal Driver$, ByVal Dev&, ByVal O&, ByVal Init&)
  Declare Function CreateCompatibleDC Lib "GDI" (ByVal hDC)
  Declare Function CreateCompatibleBitmap Lib "GDI" (ByVal hDC, ByVal BWidth, ByVal BHeight)

Sub ScrnCap (Lt, Top, Rt, Bot)
    rWidth = Rt - Lt
    rHeight = Bot - Top
    SourceDC = CreateDC("DISPLAY", 0, 0, 0)
    DestDC = CreateCompatibleDC(SourceDC)
    BHandle = CreateCompatibleBitmap(SourceDC, rWidth, rHeight)
    SelectObject DestDC, BHandle
    BitBlt DestDC, 0, 0, rWidth, rHeight, SourceDC, Lt, Top, &HCC0020
    Wnd = Screen.ActiveForm.hWnd
    OpenClipBoard Wnd
    SetClipBoardData 2, BHandle
    DeleteDC DestDC
    ReleaseDC DHandle, SourceDC
End Sub

Sub Command1_Click ()
    Form1.Visible = False
    ScrnCap 0, 0, 640, 480
    Form1.Visible = True
    picture1 = clipboard.GetData()
End Sub

Return to Phil's Web Page

Created by: Philip