Clipboard cleansing via API 
Author Message
 Clipboard cleansing via API

From Excel/VBA I need to copy as an image/bitmap a chart and some text
information. using the VBA SavePicture does not work because if the
monitor resolution is too low, the rect that gets copied is not
complete (it takes only visible portion of selection). Using the copy
command will place several formats into the clipboard, however, there
is no way to select WHICH to paste programatically, so a native type
gets selected.

My solution is to remove all of the formats except for Bitmap. The
only way to do this is to delete copy the bitmap out of the clipboard,
clear the clipboard and reinserting it as bitmap format. This is where
I am experiencing difficulty.

I was first attempting to simply copy the memory to a new allocation
(the source allocated memory is released when the clipboard is
emptied) using this code:

    If OpenClipboard(0&) <> 0 Then

        hClipMem = GetClipboardData(CF_BITMAP)
        If hClipMem <> 0 Then
            lpClipMem = GlobalLock(hClipMem)

            hCopyMem = GlobalAlloc(&H42, Len(bmp))
            lpCopyMem = GlobalLock(hCopyMem)

            CopyMemory hCopyMem, ByVal hClipMem, Len(bmp)
            GlobalUnlock hClipMem

            r = EmptyClipboard()

            r = SetClipboardData(CF_BITMAP, hCopyMem)
            GlobalUnlock hCopyMem

        End If
        r = CloseClipboard()
    End If

Next I tried to copy the data into a local bitmap variable:

    If OpenClipboard(0&) Then
        r = GlobalLock(hBitmap)
        hBitmap = GetClipboardData(CF_BITMAP)
        r = GetObject(hBitmap, Len(bmp), bmp)
        r = GlobalUnlock(hBitmap)
        r = EmptyClipboard()
        r = SetClipboardData(CF_BITMAP, hBitmap)
        r = CloseClipboard()
    End If

however when placing the data back on clipboard the bitmap is invalid.
 Perhaps if I can obtain a handle to the new bitmap or do i need to
create a DC and copy the bitmap over? Any help would be greatly
appreciated!

Regards,
Charles



Wed, 04 May 2005 04:26:53 GMT  
 Clipboard cleansing via API

Quote:
> From Excel/VBA I need to copy as an image/bitmap a chart and some text
> information. using the VBA SavePicture does not work because if the
> monitor resolution is too low, the rect that gets copied is not
> complete (it takes only visible portion of selection). Using the copy
> command will place several formats into the clipboard, however, there
> is no way to select WHICH to paste programatically, so a native type
> gets selected.

> My solution is to remove all of the formats except for Bitmap. The
> only way to do this is to delete copy the bitmap out of the clipboard,
> clear the clipboard and reinserting it as bitmap format. This is where
> I am experiencing difficulty.

> I was first attempting to simply copy the memory to a new allocation
> (the source allocated memory is released when the clipboard is
> emptied) using this code:

<snip>

Quote:
> however when placing the data back on clipboard the bitmap is invalid.
>  Perhaps if I can obtain a handle to the new bitmap or do i need to
> create a DC and copy the bitmap over? Any help would be greatly
> appreciated!

As far as I can see all you're doing here is copying over the Bitmap header,
the Bitmap data however is not being copied.  Have a look at this article
for an alternative solution:
http://www.vbaccelerator.com/tips/vba0008.htm
Hope this helps,

    Mike

 -- EDais --

 - Microsoft Visual Basic MVP -
WWW: Http://EDais.earlsoft.co.uk/




Wed, 04 May 2005 04:41:10 GMT  
 Clipboard cleansing via API
The work has been done already.

Check out Stephen Bullen's extremely useful PastePicture routine.

http://www.bmsltd.co.uk/Excel/Default.htm

Robin.


Quote:
> From Excel/VBA I need to copy as an image/bitmap a chart and some text
> information. using the VBA SavePicture does not work because if the
> monitor resolution is too low, the rect that gets copied is not
> complete (it takes only visible portion of selection). Using the copy
> command will place several formats into the clipboard, however, there
> is no way to select WHICH to paste programatically, so a native type
> gets selected.

> My solution is to remove all of the formats except for Bitmap. The
> only way to do this is to delete copy the bitmap out of the clipboard,
> clear the clipboard and reinserting it as bitmap format. This is where
> I am experiencing difficulty.

> I was first attempting to simply copy the memory to a new allocation
> (the source allocated memory is released when the clipboard is
> emptied) using this code:

>     If OpenClipboard(0&) <> 0 Then

>         hClipMem = GetClipboardData(CF_BITMAP)
>         If hClipMem <> 0 Then
>             lpClipMem = GlobalLock(hClipMem)

>             hCopyMem = GlobalAlloc(&H42, Len(bmp))
>             lpCopyMem = GlobalLock(hCopyMem)

>             CopyMemory hCopyMem, ByVal hClipMem, Len(bmp)
>             GlobalUnlock hClipMem

>             r = EmptyClipboard()

>             r = SetClipboardData(CF_BITMAP, hCopyMem)
>             GlobalUnlock hCopyMem

>         End If
>         r = CloseClipboard()
>     End If

> Next I tried to copy the data into a local bitmap variable:

>     If OpenClipboard(0&) Then
>         r = GlobalLock(hBitmap)
>         hBitmap = GetClipboardData(CF_BITMAP)
>         r = GetObject(hBitmap, Len(bmp), bmp)
>         r = GlobalUnlock(hBitmap)
>         r = EmptyClipboard()
>         r = SetClipboardData(CF_BITMAP, hBitmap)
>         r = CloseClipboard()
>     End If

> however when placing the data back on clipboard the bitmap is invalid.
>  Perhaps if I can obtain a handle to the new bitmap or do i need to
> create a DC and copy the bitmap over? Any help would be greatly
> appreciated!

> Regards,
> Charles



Wed, 04 May 2005 08:45:00 GMT  
 
 [ 3 post ] 

 Relevant Pages 

1. Copy screen to clipboard via API

2. PROBLEM: Adding a printer via API, then creating a printer Share via API (source included)

3. how to cleanse your colon

4. colon cleanse

5. Using Clipboard via vba (Excel)?

6. Problems with images pasted via the clipboard

7. SSDBGrid und Excel, data exchange via clipboard ?

8. Passing keystrokes to DOS via Windows clipboard using VB

9. Recordset to Excel Via Clipboard?

10. Window's API Call: GetClipboardData to retrieve a Bitmap from Clipboard

11. clipboard API

12. Using Clipboard through API, why GlobalLock

 

 
Powered by phpBB® Forum Software