Print Page | Close Window

(RESOLVED) Copy to clipboard

Printed From: Codejock Forums
Category: Codejock Products
Forum Name: Chart Control
Forum Description: Topics Related to Codejock Chart Control
URL: http://forum.codejock.com/forum_posts.asp?TID=18181
Printed Date: 29 April 2024 at 4:18am
Software Version: Web Wiz Forums 12.04 - http://www.webwizforums.com


Topic: (RESOLVED) Copy to clipboard
Posted By: markr
Subject: (RESOLVED) Copy to clipboard
Date 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?



Replies:
Posted By: Oleg
Date Posted: 07 April 2011 at 12:05pm
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


Posted By: markr
Date Posted: 07 April 2011 at 1:36pm
Thanks for the tip, Oleg. Working fine now.  :-)


Posted By: znakeeye
Date Posted: 17 April 2011 at 4:27pm
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/


Posted By: markr
Date Posted: 18 April 2011 at 9:56am
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();
}


Posted By: znakeeye
Date Posted: 20 April 2011 at 1:53am
Thanks!

-------------
PokerMemento - http://www.pokermemento.com/


Posted By: znakeeye
Date Posted: 22 May 2011 at 4:28pm
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/


Posted By: markr
Date Posted: 23 May 2011 at 3:50pm
> 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.


Posted By: znakeeye
Date Posted: 13 June 2011 at 1:48am
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/



Print Page | Close Window

Forum Software by Web Wiz Forums® version 12.04 - http://www.webwizforums.com
Copyright ©2001-2021 Web Wiz Ltd. - https://www.webwiz.net