ASE Home Page Products Download Purchase Support About ASE
ChartDirector Support
Forum HomeForum Home   SearchSearch

Message ListMessage List     Post MessagePost Message

  copy a chart to clipboard using MFC.
Posted by Hippo on May-12-2009 10:27
Hi,

I'm using MFC.
I don't know a way to copy a chart to the clipboard.

  Re: copy a chart to clipboard using MFC.
Posted by System Administrator on May-12-2009 20:26
Hi Hippo,

You can just copy the contents to the clipboard normally, just like any other MFC application. No ChartDirector methods are needed. See the Microsoft documentation on OpenClipboard, EmptyClipboard, SetClipboardData and CloseClipboard.

For example:

m_ChartViewer.OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, m_ChartViewer.GetBitmap());
CloseClipboard();

Hope this can help.

Regards
Peter Kwan

  Re: copy a chart to clipboard using MFC.
Posted by Peter Kwan on May-12-2009 20:28
Hi Hippo,

One thing to note about my previous message. The code I included are for your testing only. In real code, you would need to check for error return values and handle them according (eg. some other application may be using and locking the system clipboard, etc).

Regards
Peter Kwan

  Re: copy a chart to clipboard using MFC.
Posted by Ian Hammond on Jan-02-2025 17:31
Hi  Peter,

I would like to copy the current graph to the clipboard. I looked at this bit of code, but it doesn't appear to work for me. I am using c++, mfc. I am wondering if I am missing some prep code before I can use this section of code that is in this email chain. Is this section of code all that is necessary?

Many thanks

  Re: copy a chart to clipboard using MFC.
Posted by Peter Kwan on Jan-03-2025 13:27
Hi Ian,

Nowadays the MFC CChartViewer supports true transparency, while the windows clipboard does not support transparency. So we cannot directly put the m_ChartViewer.GetBitmap() on the clipboard. Instead, we need to create a compatible copy. In MFC, it is like:


// The HBITMAP from ChartDirector
CImage img;
img.Attach(m_ChartViewer.GetBitmap());


// Create another compatible bitmap in memory
CDC *pDC = GetDC();
CBitmap bitmap;
bitmap.CreateCompatibleBitmap(pDC, img.GetWidth(), img.GetHeight());
CDC memDC;
memDC.CreateCompatibleDC(pDC);

// Copy from the ChartDirector bitmap to the compatible bitmap
HANDLE h = memDC.SelectObject(bitmap);
img.BitBlt(memDC.GetSafeHdc(), 0, 0, img.GetWidth(), img.GetHeight(), 0, 0, SRCCOPY);
memDC.SelectObject(h);

// Put the compatible bitmap to clipboard
OpenClipboard();
EmptyClipboard();
SetClipboardData(CF_BITMAP, bitmap);
CloseClipboard();

// Clean up
ReleaseDC(pDC);
img.Detach();


(Note: The header file the CImage is "atlimage.h")

Best Regards
Peter Kwan