Discussion:
Copying an HBITMAP to the clipboard
(too old to reply)
John
2004-02-18 19:11:07 UTC
Permalink
How do you copy a HBITMAP to the clipboard?
Rodrigo Corral González [MVP]
2004-02-18 20:44:16 UTC
Permalink
http://www.codeguru.com/bitmap/copy_to_clipboard.shtml
--
Un saludo
Rodrigo Corral González [MVP]

microsoft.public.es.vc FAQ
http://vcfaq.europe.webmatrixhosting.net
John
2004-02-18 21:16:06 UTC
Permalink
I'm not using MFC
Rodrigo Corral González [MVP]
2004-02-18 21:30:54 UTC
Permalink
Open the clipboard by calling the OpenClipboard function.
Empty the clipboard by calling the EmptyClipboard function.
Call the SetClipboardData function SetClipboardData(CF_BITMAP, YourHBITMAP).
Close the clipboard by calling the CloseClipboard function.
--
Un saludo
Rodrigo Corral González [MVP]

microsoft.public.es.vc FAQ
http://vcfaq.europe.webmatrixhosting.net
John
2004-02-18 22:01:08 UTC
Permalink
That's exactly what I am doing but when I go to paste into paint I get an error message displayed

"Error getting the clipboard data!

Whats strange is that after the error message is displayed and I switch back to my ATL control the bitmap
isn't drawn in my control anymore, it's as if it's been destroyed.
Alexander Nickolov
2004-02-18 22:48:43 UTC
Permalink
In all the examples of standard formats I've seen in MSDN,
the object is lost to you once you place it on the clipboard.
I know that's not what the docs say...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
"Error getting the clipboard data!"
Whats strange is that after the error message is displayed and I switch back to my ATL control the bitmap
isn't drawn in my control anymore, it's as if it's been destroyed.
John
2004-02-19 00:31:08 UTC
Permalink
Thanks Alexander that fixed the problem if I copy the bitmap and place the copy in the clipboard
everything functions correctly.
Simon Trew
2004-02-27 08:32:35 UTC
Permalink
The docs say:
<quot url=http://tinyurl.com/3fclf" fromSite="msdn.microsoft.com">
After SetClipboardData is called, the system owns the object identified by
the hMem parameter. The application can read the data, but must not free the
handle or leave it locked until the CloseClipboard function is called. (The
application can access the data after calling CloseClipboard). If the hMem
parameter identifies a memory object, the object must have been allocated
using the function with the GMEM_MOVEABLE flag.
</quot>

Leaving aside the bit about GMEM_MOVEABLE, the fact that the clipboard now
owns the handle is exactly how I have always interpreted that. I don't
really know what it means about being able to "access the data after calling
CloseClipboard"-- I presumed it meant to be able to GetClipboardData() for a
paste.

The logic for this, of course, is that the app can terminate but the data
has still to be in the clipboard, so the clipboard must own it.

As an aside (I presume Alexander you know this but just for reference), if
you use OleSetClipboard with an object implementing IDataObject then you can
continue to "own" the data of course, and only access the underlying data
when called through that interface. This is what Excel does (I assume). At
the "Copy" point, only the reference to the data (held in excel) is given to
the clipboard (as an IDataObject), but when the app is closing, the data has
to be pasted (or not) onto the clipboard so that the reference can be freed
and the app close down, which is why asks whether you want to keep a large
amount of data on the clipboard when it closes down. Of course Excel is in a
world of it own when it comes to clipboard behavior, anyway.

S.

"Alexander Nickolov" <***@mvps.org> wrote in message news:***@TK2MSFTNGP12.phx.gbl...
In all the examples of standard formats I've seen in MSDN,
the object is lost to you once you place it on the clipboard.
I know that's not what the docs say...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
Post by John
That's exactly what I am doing but when I go to paste into paint I get an
"Error getting the clipboard data!"
Whats strange is that after the error message is displayed and I switch
back to my ATL control the bitmap
Post by John
isn't drawn in my control anymore, it's as if it's been destroyed.
Alexander Nickolov
2004-02-27 18:32:37 UTC
Permalink
That's part of what I meant. I was more focusing on the issue where
somebody else frees the clipboard afterwards - then the data cannot
be used anymore (that is after CloseClipboard). I believe that
statement in the parentheses is utterly wrong.
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
email: ***@mvps.org
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
Post by Simon Trew
<quot url=http://tinyurl.com/3fclf" fromSite="msdn.microsoft.com">
After SetClipboardData is called, the system owns the object identified by
the hMem parameter. The application can read the data, but must not free the
handle or leave it locked until the CloseClipboard function is called. (The
application can access the data after calling CloseClipboard). If the hMem
parameter identifies a memory object, the object must have been allocated
using the function with the GMEM_MOVEABLE flag.
</quot>
Leaving aside the bit about GMEM_MOVEABLE, the fact that the clipboard now
owns the handle is exactly how I have always interpreted that. I don't
really know what it means about being able to "access the data after calling
CloseClipboard"-- I presumed it meant to be able to GetClipboardData() for a
paste.
The logic for this, of course, is that the app can terminate but the data
has still to be in the clipboard, so the clipboard must own it.
As an aside (I presume Alexander you know this but just for reference), if
you use OleSetClipboard with an object implementing IDataObject then you can
continue to "own" the data of course, and only access the underlying data
when called through that interface. This is what Excel does (I assume). At
the "Copy" point, only the reference to the data (held in excel) is given to
the clipboard (as an IDataObject), but when the app is closing, the data has
to be pasted (or not) onto the clipboard so that the reference can be freed
and the app close down, which is why asks whether you want to keep a large
amount of data on the clipboard when it closes down. Of course Excel is in a
world of it own when it comes to clipboard behavior, anyway.
S.
In all the examples of standard formats I've seen in MSDN,
the object is lost to you once you place it on the clipboard.
I know that's not what the docs say...
--
=====================================
Alexander Nickolov
Microsoft MVP [VC], MCSD
MVP VC FAQ: http://www.mvps.org/vcfaq
=====================================
Post by John
That's exactly what I am doing but when I go to paste into paint I get an
"Error getting the clipboard data!"
Whats strange is that after the error message is displayed and I switch
back to my ATL control the bitmap
Post by John
isn't drawn in my control anymore, it's as if it's been destroyed.
Loading...