Codejock Forums Homepage
Forum Home Forum Home > Codejock Products > Visual C++ MFC > Chart Control
  New Posts New Posts RSS Feed - (RESOLVED) Copy to clipboard
  FAQ FAQ  Forum Search   Events   Register Register  Login Login

(RESOLVED) Copy to clipboard

 Post Reply Post Reply
Author
Message
markr View Drop Down
Senior Member
Senior Member


Joined: 01 August 2004
Status: Offline
Points: 443
Post Options Post Options   Thanks (0) Thanks(0)   Quote markr Quote  Post ReplyReply Direct Link To This Post Topic: (RESOLVED) Copy to clipboard
    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?
Back to Top
Oleg View Drop Down
Admin Group
Admin Group


Joined: 21 May 2003
Location: United States
Status: Offline
Points: 11234
Post Options Post Options   Thanks (0) Thanks(0)   Quote Oleg Quote  Post ReplyReply Direct Link To This Post 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
Back to Top
markr View Drop Down
Senior Member
Senior Member


Joined: 01 August 2004
Status: Offline
Points: 443
Post Options Post Options   Thanks (0) Thanks(0)   Quote markr Quote  Post ReplyReply Direct Link To This Post Posted: 07 April 2011 at 1:36pm
Thanks for the tip, Oleg. Working fine now.  :-)
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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/
Back to Top
markr View Drop Down
Senior Member
Senior Member


Joined: 01 August 2004
Status: Offline
Points: 443
Post Options Post Options   Thanks (0) Thanks(0)   Quote markr Quote  Post ReplyReply Direct Link To This Post 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();
}
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post Posted: 20 April 2011 at 1:53am
Thanks!
PokerMemento - http://www.pokermemento.com/
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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/
Back to Top
markr View Drop Down
Senior Member
Senior Member


Joined: 01 August 2004
Status: Offline
Points: 443
Post Options Post Options   Thanks (0) Thanks(0)   Quote markr Quote  Post ReplyReply Direct Link To This Post 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.
Back to Top
znakeeye View Drop Down
Senior Member
Senior Member
Avatar

Joined: 26 July 2006
Status: Offline
Points: 1672
Post Options Post Options   Thanks (0) Thanks(0)   Quote znakeeye Quote  Post ReplyReply Direct Link To This Post 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/
Back to Top
 Post Reply Post Reply
  Share Topic   

Forum Jump Forum Permissions View Drop Down

Forum Software by Web Wiz Forums® version 12.04
Copyright ©2001-2021 Web Wiz Ltd.

This page was generated in 0.188 seconds.