Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

VC++ win32 API programming: how can I get the image out of the clipboard and display it in a windows?

Use SelectObject() on your memory DC to select the bitmap into it. This makes me puzzled. I read the msdn but still don't know how to fill the second parameter of SelectObject().

like image 639
user185369 Avatar asked Mar 01 '23 05:03

user185369


1 Answers

Call OpenClipbard() to open the clipboard and call GetClipboardData() with a type of CF_BITMAP to get the handle to the image data stored on the clipboard. If there's no image on the clipboard, the NULL handle will be returned.

Then, inside your window's WM_PAINT handler, use BeginPaint() to get a device context for drawing into your window, and use CreateCompatibleDC() to create a memory device context for the bitmap. Use SelectObject() on your memory DC to select the bitmap into it, and finally use BitBlt() to blit the bitmap from the memory DC onto the window's DC. Don't forget to clean up -- call DeleteDC() to delete the memory DC, and call EndPaint() to end drawing.

Lastly, call CloseClipboard() when you're done. Note that the clipboard owns the bitmap handle, and as soon as you call CloseClipboard(), the bitmap will be destroyed. So, if you want to hang onto the bitmap after you've closed the clipboard, you'll need to make a copy of it.

like image 94
Adam Rosenfield Avatar answered Mar 21 '23 00:03

Adam Rosenfield