Storing a window into clipboard and save as bitmap

Back Up Next

Author: Eric Aling (08 sep 1999)
Accessed:
Download: Download winclip.zip (13 kb) (WinClip.zip, 13kb)

Some people asked me how to get the current window on the clipboard so that users could paste it in some doc or whatever. Well, you could do a Alt+PrintScreen and paste it. It would be nicer if we could automate this process from powerscript. Well, this is possibe. Check out the example pibble. What does it do? First, we get the whole desktop as image. From that image, we take the window using the coordinates and dimensions. With that we make a bitmap in memory and place it on the clipboard. Besides that, I converted the bitmap data in memory to a bitmap in a blob. Very handy if you want to save the bitmap to file. Here's some code:

uLong ll_DeskHwnd,ll_hdc,ll_hdcMem ,ll_hBitmap
uLong ll_fleft, ll_ftop, ll_fwidth , ll_fheight
uLong ll_return

// The GetDesktopWindow function returns the handle of the Windows desktop 
// window. The desktop window covers the entire screen. 
// The desktop window is the area on top of which all icons and
// other windows are painted. 

ll_DeskHwnd = GetDesktopWindow()

// Get in pixel value the x,y,width,height of the window 
// because of the powerbuilder Units (PBU)

ll_fwidth=UnitsToPixels(This.width, XUnitsToPixels!)
ll_fheight=UnitsToPixels(This.height, YUnitsToPixels!)
ll_fleft=UnitsToPixels(This.X, XUnitsToPixels!)
ll_ftop=UnitsToPixels(This.Y, YUnitsToPixels!)

// 
// Get the device context of Desktop and allocate memory
// 

ll_hdc = GetDC(ll_DeskHwnd)
ll_hdcMem = CreateCompatibleDC(ll_hdc)
ll_hBitmap = CreateCompatibleBitmap(ll_hdc, ll_fwidth, ll_fheight)

If ll_hBitmap <> 0 Then
    ll_return = SelectObject(ll_hdcMem, ll_hBitmap)

// 
// Copy the Desktop bitmap to memory location
// 

ll_return = BitBlt(ll_hdcMem, 0, 0, ll_fwidth, ll_fheight, ll_hdc, ll_fleft, ll_ftop, 13369376)

// 
// try to store the bitmap into a blob so we can save it
//    this does not always work BTW
// 

    long ll_pixels = 1
    
    str_bitmapinfo lstr_Info
    str_bitmapinfo lstr_InfoDummy
    str_bitmapheader lstr_Header
    
    lstr_Info.bmiheader.bisize=40
    lstr_InfoDummy.bmiheader.bisize=40
    
    // Get the bitmapinfo
    
    if GetDIBits(ll_hdcMem, ll_hBitmap, 0, ll_fheight, 0, lstr_Info, 0) > 0 then

        if lstr_Info.bmiheader.biBitCount = 16 or &
            lstr_Info.bmiheader.biBitCount = 32 then
            ll_pixels = lstr_info.bmiheader.biwidth *     lstr_info.bmiheader.biheight
        end if
        
        lstr_Info.bmiColor[ll_pixels] = 0
        
        abl_data = blob(space(lstr_Info.bmiheader.bisizeimage))

        // get the actual bits

        GetDIBits(ll_hdcMem, ll_hBitmap, 0, ll_fheight, abl_data, lstr_Info, 0) 

        // create a bitmap header

        lstr_Header.bfType[1] = 'B'
        lstr_Header.bfType[2] = 'M'
        lstr_Header.bfSize     = lstr_Info.bmiheader.bisizeimage
        lstr_Header.bfReserved1 = 0
        lstr_Header.bfReserved2 = 0
        lstr_Header.bfOffBits = 54 + (ll_pixels * 4)

        // copy the header structure to a blob

        blob lbl_header
        lbl_header = blob(space(14))
        CopyBitmapFileHeader(lbl_header, lstr_Header, 14 )

        // copy the info structure to a blob

        blob lbl_info
        lbl_Info = blob(space(40 + ll_pixels * 4) )
        CopyBitmapInfo(lbl_Info, lstr_Info, 40 + ll_pixels * 4 )
        
        // add all together and we have a window bitmap in a blob
        
        abl_Data = lbl_header + lbl_info + abl_Data
        
    end if

// '---------------------------------------------
// ' Set up the Clipboard and copy bitmap
// '---------------------------------------------

ll_return = OpenClipboard(ll_DeskHwnd)
ll_return = EmptyClipboard()
ll_return = SetClipboardData(2, ll_hBitmap)
ll_return = CloseClipboard()

End If

// '---------------------------------------------
// ' Clean up handles
// '---------------------------------------------
ll_return = DeleteDC(ll_hdcMem)
ll_return = ReleaseDC(ll_DeskHwnd, ll_hdc)


return 1

Anyway, check out the example pibble. It is possible that the save of the bitmap does not work and I don't know why. It has something to do with the number of colors you are currently using for your display.

Enjoy!

 

Back Up Next