![]() |
(RESOLVED) Copy to clipboard |
Post Reply ![]() |
Author | |
markr ![]() Senior Member ![]() Joined: 01 August 2004 Status: Offline Points: 443 |
![]() ![]() ![]() ![]() ![]() Posted: 07 April 2011 at 11:15am |
Hello,
I need to copy a chart image (rendered via CXTPChartControl) to the Windows clipboard. I tried deriving from CXTPChartControl and adding a method that copies the contents of the CBitmap cache object (m_bmpCache member) to the clipboard, but it does not work. Here's what I'm trying to do (stripped of error checking for brevity): VERIFY(OpenClipboard()); VERIFY(EmptyClipboard()); VERIFY(SetClipboardData(CF_BITMAP, m_bmpCache.GetSafeHandle())); CloseClipboard(); None of these function calls fail, but clipboard contents are not valid. Perhaps CBitmap cache object is not safe to use for this purpose? |
|
![]() |
|
Oleg ![]() Admin Group ![]() Joined: 21 May 2003 Location: United States Status: Offline Points: 11234 |
![]() ![]() ![]() ![]() ![]() |
You better create copy of bitmap because you should "detach" it to clipboard:
VERIFY(SetClipboardData(CF_BITMAP, bmpCopy.Detach()));
|
|
Oleg, Support Team
CODEJOCK SOFTWARE SOLUTIONS |
|
![]() |
|
markr ![]() Senior Member ![]() Joined: 01 August 2004 Status: Offline Points: 443 |
![]() ![]() ![]() ![]() ![]() |
Thanks for the tip, Oleg. Working fine now. :-)
|
|
![]() |
|
znakeeye ![]() Senior Member ![]() ![]() Joined: 26 July 2006 Status: Offline Points: 1672 |
![]() ![]() ![]() ![]() ![]() |
I keep getting a black image. What am I doing wrong?
This is my copy-function: BOOL CopyBitmap(CBitmap &bmpDest, CBitmap &bmpSource) { bmpDest.DeleteObject(); BITMAP bm; bmpSource.GetBitmap(&bm); CDC sourceDC; sourceDC.CreateCompatibleDC(NULL); bmpDest.CreateCompatibleBitmap(&sourceDC, bm.bmWidth, bm.bmHeight); CDC destDC; destDC.CreateCompatibleDC(NULL); CBitmap *pOldSourceBitmap = sourceDC.SelectObject(&bmpSource); CBitmap *pOldDestBitmap = destDC.SelectObject(&bmpDest); destDC.BitBlt(0, 0, bm.bmWidth, bm.bmHeight, &sourceDC, 0, 0, SRCCOPY); sourceDC.SelectObject(pOldSourceBitmap); destDC.SelectObject(pOldDestBitmap); return (bmpDest.m_hObject != NULL); } |
|
PokerMemento - http://www.pokermemento.com/
|
|
![]() |
|
markr ![]() Senior Member ![]() Joined: 01 August 2004 Status: Offline Points: 443 |
![]() ![]() ![]() ![]() ![]() |
Hi,
Here's the code I'm currently using for this purpose. void CDFDChartControl::CopyToClipboard() { BITMAP bmpInfo; m_bmpCache.GetBitmap(&bmpInfo); CBitmap bitmap; CClientDC dcClient(AfxGetMainWnd()); CDC dcDest; dcDest.CreateCompatibleDC(&dcClient); CDC dcSource; dcSource.CreateCompatibleDC(&dcClient); HBITMAP oldhBitmap = (HBITMAP)dcSource.SelectObject((HBITMAP)m_bmpCache); bitmap.CreateCompatibleBitmap(&dcClient, bmpInfo.bmWidth, bmpInfo.bmHeight); CBitmap* pOldBitmap = dcDest.SelectObject(&bitmap); ::BitBlt(dcDest, 0, 0, bmpInfo.bmWidth, bmpInfo.bmHeight, dcSource, 0, 0, SRCCOPY); dcSource.SelectObject(oldhBitmap); dcDest.SelectObject(pOldBitmap); VERIFY(::OpenClipboard(AfxGetMainWnd()->m_hWnd)); VERIFY(::EmptyClipboard()); VERIFY(::SetClipboardData(CF_BITMAP, bitmap.Detach())); ::CloseClipboard(); } |
|
![]() |
|
znakeeye ![]() Senior Member ![]() ![]() Joined: 26 July 2006 Status: Offline Points: 1672 |
![]() ![]() ![]() ![]() ![]() |
Thanks!
|
|
PokerMemento - http://www.pokermemento.com/
|
|
![]() |
|
znakeeye ![]() Senior Member ![]() ![]() Joined: 26 July 2006 Status: Offline Points: 1672 |
![]() ![]() ![]() ![]() ![]() |
When pasting the image into Paint, it tells me:
--------------------------- Paint --------------------------- The information on the Clipboard can't be inserted into Paint. --------------------------- OK --------------------------- What?!
|
|
PokerMemento - http://www.pokermemento.com/
|
|
![]() |
|
markr ![]() Senior Member ![]() Joined: 01 August 2004 Status: Offline Points: 443 |
![]() ![]() ![]() ![]() ![]() |
> The information on the Clipboard can't be inserted into Paint
You're using the code I shared with you? Not sure what's going on there. I'm using that exact code in a production release without any problems. |
|
![]() |
|
znakeeye ![]() Senior Member ![]() ![]() Joined: 26 July 2006 Status: Offline Points: 1672 |
![]() ![]() ![]() ![]() ![]() |
It seems CClientDC is the key. This does not work:
hdc = ::GetDC(m_hWnd); // Tried NULL too... // Create compatible dc What's the difference between GetDC and CClientDC?
|
|
PokerMemento - http://www.pokermemento.com/
|
|
![]() |
Post Reply ![]() |
|
Tweet
|
Forum Jump | Forum Permissions ![]() You cannot post new topics in this forum You cannot reply to topics in this forum You cannot delete your posts in this forum You cannot edit your posts in this forum You cannot create polls in this forum You cannot vote in polls in this forum |