Results 1 to 10 of 10

Thread: Print Screen

  1. #1

    Thread Starter
    Lively Member TB's Avatar
    Join Date
    Feb 2001
    Location
    Austria
    Posts
    106

    Question

    How can I copy the screen to the clipboard (Print Screen Key) in my Visual Basic program?
    (I tried this with the Sendkeys Function, but that didn't work.)
    Thank you for any help.

    Thomas
    mojo

  2. #2
    Fanatic Member gwdash's Avatar
    Join Date
    Aug 2000
    Location
    Minnesota
    Posts
    666
    Try This:
    Code:
    Const RC_PALETTE As Long = &H100
    Const SIZEPALETTE As Long = 104
    Const RASTERCAPS As Long = 38
    Private Type PALETTEENTRY
        peRed As Byte
        peGreen As Byte
        peBlue As Byte
        peFlags As Byte
    End Type
    Private Type LOGPALETTE
        palVersion As Integer
        palNumEntries As Integer
        palPalEntry(255) As PALETTEENTRY ' Enough for 256 colors
    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 Declare Function OleCreatePictureIndirect Lib "olepro32.dll" (PicDesc As PicBmp, RefIID As GUID, ByVal fPictureOwnsHandle As Long, IPic As IPicture) As Long
    Private Declare Function CreateCompatibleDC Lib "gdi32" (ByVal hdc 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 SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function GetDeviceCaps Lib "gdi32" (ByVal hdc As Long, ByVal iCapabilitiy As Long) As Long
    Private Declare Function GetSystemPaletteEntries Lib "gdi32" (ByVal hdc As Long, ByVal wStartIndex As Long, ByVal wNumEntries As Long, lpPaletteEntries As PALETTEENTRY) As Long
    Private Declare Function CreatePalette Lib "gdi32" (lpLogPalette As LOGPALETTE) As Long
    Private Declare Function SelectPalette Lib "gdi32" (ByVal hdc As Long, ByVal hPalette As Long, ByVal bForceBackground As Long) As Long
    Private Declare Function RealizePalette Lib "gdi32" (ByVal hdc As Long) As Long
    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 DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Function CreateBitmapPicture(ByVal hBmp As Long, ByVal hPal As Long) As Picture
        Dim R As Long, Pic As PicBmp, IPic As IPicture, IID_IDispatch As GUID
    
        'Fill GUID info
        With IID_IDispatch
            .Data1 = &H20400
            .Data4(0) = &HC0
            .Data4(7) = &H46
        End With
    
        'Fill picture info
        With Pic
            .Size = Len(Pic) ' Length of structure
            .Type = vbPicTypeBitmap ' Type of Picture (bitmap)
            .hBmp = hBmp ' Handle to bitmap
            .hPal = hPal ' Handle to palette (may be null)
        End With
    
        'Create the picture
        R = OleCreatePictureIndirect(Pic, IID_IDispatch, 1, IPic)
    
        'Return the new picture
        Set CreateBitmapPicture = IPic
    End Function
    Function hDCToPicture(ByVal hDCSrc As Long, ByVal LeftSrc As Long, ByVal TopSrc As Long, ByVal WidthSrc As Long, ByVal HeightSrc As Long) As Picture
        Dim hDCMemory As Long, hBmp As Long, hBmpPrev As Long, R As Long
        Dim hPal As Long, hPalPrev As Long, RasterCapsScrn As Long, HasPaletteScrn As Long
        Dim PaletteSizeScrn As Long, LogPal As LOGPALETTE
    
        'Create a compatible device context
        hDCMemory = CreateCompatibleDC(hDCSrc)
        'Create a compatible bitmap
        hBmp = CreateCompatibleBitmap(hDCSrc, WidthSrc, HeightSrc)
        'Select the compatible bitmap into our compatible device context
        hBmpPrev = SelectObject(hDCMemory, hBmp)
    
        'Raster capabilities?
        RasterCapsScrn = GetDeviceCaps(hDCSrc, RASTERCAPS) ' Raster
        'Does our picture use a palette?
        HasPaletteScrn = RasterCapsScrn And RC_PALETTE ' Palette
        'What's the size of that palette?
        PaletteSizeScrn = GetDeviceCaps(hDCSrc, SIZEPALETTE) ' Size of
    
        If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            'Set the palette version
            LogPal.palVersion = &H300
            'Number of palette entries
            LogPal.palNumEntries = 256
            'Retrieve the system palette entries
            R = GetSystemPaletteEntries(hDCSrc, 0, 256, LogPal.palPalEntry(0))
            'Create the palette
            hPal = CreatePalette(LogPal)
            'Select the palette
            hPalPrev = SelectPalette(hDCMemory, hPal, 0)
            'Realize the palette
            R = RealizePalette(hDCMemory)
        End If
        
        'Copy the source image to our compatible device context
        R = BitBlt(hDCMemory, 0, 0, WidthSrc, HeightSrc, hDCSrc, LeftSrc, TopSrc, vbSrcCopy)
        
        'Restore the old bitmap
        hBmp = SelectObject(hDCMemory, hBmpPrev)
        
        If HasPaletteScrn And (PaletteSizeScrn = 256) Then
            'Select the palette
            hPal = SelectPalette(hDCMemory, hPalPrev, 0)
        End If
        
        'Delete our memory DC
        R = DeleteDC(hDCMemory)
    
        Set hDCToPicture = CreateBitmapPicture(hBmp, hPal)
    End Function
    Private Sub Form_Load()
        'KPD-Team 1999
        'URL: http://www.allapi.net/
        'E-Mail: [email protected]
        'Create a picture object from the screen
        Clipboard.SetData (hDCToPicture(GetDC(0), 0, 0, Screen.Width / Screen.TwipsPerPixelX, Screen.Height / Screen.TwipsPerPixelY), vbCFBitmap
    End Sub
    GWDASH
    [b]VB6, Perl, ASP, HTML, JavaScript, VBScript, SQL, C, C++, Linux , Java, PHP, MySQL, XML[b]

  3. #3
    Member
    Join Date
    Jul 1999
    Posts
    42
    ' Very nice code but the code is not working because of some coding errors. I have put a command button on the form and adjusted the code in the following way.
    Private Sub Command1_Click()

    'KPD-Team 1999
    'URL: http://www.allapi.net/
    'E-Mail: [email protected]
    'Create a picture object from the screen
    Dim hWndDeskTop, hDCDeskTop As Long
    Dim pic As Picture

    hWndDeskTop = GetDesktopWindow() ' Get the desktop window handle
    hDCDeskTop = GetDC(hWndDeskTop) ' Get desktop device context ( hDCDeskTop = GetDC(0) will also work)
    ' Get picture object
    Set pic = hDCToPicture(hDC, 0, 0, CLng(Screen.Width / Screen.TwipsPerPixelX), CLng(Screen.Height / Screen.TwipsPerPixelY))
    ' Copy to clipboard
    Clipboard.SetData pic, vbCFBitmap
    ' Test screen capture
    Me.Picture = pic
    Me.Width = Screen.Width
    Me.Height = Screen.Height
    End Sub

  4. #4
    Member
    Join Date
    Jul 1999
    Posts
    42
    ' Some simple bitmap copy example to play with
    ' Put the code in a form with a command button on it
    Option Explicit

    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 GetDesktopWindow Lib "user32" () As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long

    Private Const SRCCOPY = &HCC0020

    Private Sub Command1_Click()
    Dim hWndDeskTop As Long, hDCDeskTop As Long

    ' Hide this form so it will not be in the captured bitmap
    Me.Hide
    DoEvents
    ' Get desktop window and device context
    hWndDeskTop = GetDesktopWindow()
    hDCDeskTop = GetDC(hWndDeskTop)
    ' Copy desktop bitmap to this form's bitmap
    ' using the bit block transfer function BitBlt
    BitBlt Me.hdc, 0, 0, CLng(Screen.Width / Screen.TwipsPerPixelX), CLng(Screen.Height / Screen.TwipsPerPixelY), hDCDeskTop, 0, 0, SRCCOPY
    ' Me this form visible again
    Me.Show
    End Sub

  5. #5
    Member
    Join Date
    Jul 1999
    Posts
    42
    Try using StretchBlt instead of BitBlt:

    StretchBlt Me.hdc, 0, 0, ScaleX(ScaleWidth, vbTwips, vbPixels), ScaleY(ScaleHeight, vbTwips, vbPixels), _
    hDCDeskTop, 0, 0, CLng(Screen.Width / Screen.TwipsPerPixelX), CLng(Screen.Height / Screen.TwipsPerPixelY), SRCCOPY

  6. #6
    to Alfred:

    can you show me the code of capture screen
    by using the BitBlt and StretchBlt to Windows Clipboard

    Thanks

    tong,
    bug is nature.

  7. #7

    To Alfred Pls Help me

    i want to know how to use the BitBlt and StretchBlt func
    to capture screen to clipboard.
    bug is nature.

  8. #8
    Fanatic Member
    Join Date
    Sep 1999
    Location
    Bethel, North Carolina, USA
    Posts
    987
    If you have to do it the hard way then here it is .....

    Add a command button to a form
    Code:
    Option Explicit
    
    Private Declare Function EmptyClipboard Lib "user32" () As Long
    Private Declare Function OpenClipboard Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function CloseClipboard Lib "user32" () As Long
    Private Declare Function SetClipboardData Lib "user32" (ByVal wFormat As Long, ByVal hMem As Long) As Long
    Private Declare Function GetDC Lib "user32" (ByVal hwnd As Long) As Long
    Private Declare Function GetDesktopWindow Lib "user32" () As Long
    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 DeleteObject Lib "gdi32" (ByVal hObject As Long) As Long
    Private Declare Function SelectObject Lib "gdi32" (ByVal hdc As Long, ByVal hObject As Long) As Long
    Private Declare Function DeleteDC Lib "gdi32" (ByVal hdc As Long) As Long
    
    Private Const CF_BITMAP = 2
    
    Public Function CopyScreenToClipboard() As Long
     Dim lngScreenDC As Long
     Dim lngScreenH As Long, lngScreenW As Long
     Dim lngDC As Long
     Dim lngBMP As Long, lngOrigBMP As Long
     
     ' get a handle to the screen
     lngScreenDC = GetDC(GetDesktopWindow)
     ' get the dimensions of the screen in pixels
     lngScreenH = Screen.Height \ Screen.TwipsPerPixelY
     lngScreenW = Screen.Width \ Screen.TwipsPerPixelX
     
     ' create a dc to temporarily hold the screen bmp
     lngDC = CreateCompatibleDC(lngScreenDC)
     
     ' create the bitmap in memory to hold picture of the screen
     lngBMP = CreateCompatibleBitmap(lngScreenDC, lngScreenW, lngScreenH)
     
     ' put the BMP created into the DC created
     lngOrigBMP = SelectObject(lngDC, lngBMP)
     
     ' blt the current state of the screen to the DC
     BitBlt lngDC, 0, 0, lngScreenW, lngScreenH, lngScreenDC, 0, 0, vbSrcCopy
     
        'open the clipboard
        If OpenClipboard(Me.hwnd) Then
            
             ' clear the clipboard of current data
            If EmptyClipboard Then
            
                ' put the bitmap in clipboard
                SetClipboardData CF_BITMAP, lngBMP
     
                ' don't need to be thw owner of the clipboard anymore so let it go
                CloseClipboard
                
            Else
            
                MsgBox "Error saving data to clipboard!"
            
            End If
            
        Else
     
            MsgBox "Error saving data to clipboard!"
     
        End If
    
     
     ' free memory
     SelectObject lngDC, lngOrigBMP
     DeleteDC lngDC
        
    End Function
    
    Private Sub Command1_Click()
        CopyScreenToClipboard
    End Sub
    {Insert random techno-babble here}

    {Insert quote from some long gone mofo here}

  9. #9
    thank you so much youngbuck.

    it's nice code.

    tong,
    bug is nature.

  10. #10
    Member
    Join Date
    Jul 1999
    Posts
    42
    Yes its very nice.
    Last edited by Alfred; Feb 14th, 2001 at 03:15 AM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width