Programming with clipboard problem
-
Hello every body. I am developing an application with clipboard supporting. When users press Ctrl+C or Ctrl+X, the application copies three types of data into clipboard: application defined data, text only data, windows metafile data. I've used COleDataSource to implement that capacities. The below is my code:
//Application defined data implementation
CSharedFile sf(GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
CArchive ar (&sf, CArchive::store);
//Serialize application defined data to ar;
ar.Close();DWORD dwLen = (DWORD) sf.GetLength();
HGLOBAL hMem = sf.Detach();
COleDataSource* pSource=NULL;
if (hMem){
hMem = ::GlobalReAlloc(hMem, dwLen, GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
if (hMem){
pSource = new COleDataSource();
pSource->CacheGlobalData(CF_APPLICATIONDEFINED, hMem);
}
}//Text clipboard implementation
CSharedFile txtsf(GMEM_ZEROINIT|GMEM_DDESHARE|GMEM_MOVEABLE);
txtsf.Write((LPCTSTR)st, st.GetLength()+1);dwLen = (DWORD) txtsf.GetLength();
hMem = txtsf.Detach();
if (hMem){
hMem = ::GlobalReAlloc(hMem, dwLen, GMEM_MOVEABLE | GMEM_DDESHARE | GMEM_ZEROINIT);
if (hMem){
if (!pSource)
pSource = new COleDataSource();
pSource->CacheGlobalData(CF_TEXT, hMem);
}
}//Metafile clipboard implementation
CDC DC;
CDC* pDC = NULL;
CMetaFileDC MetaDC;
DC.CreateCompatibleDC(NULL);
CRect rect(0,0,width, height);
CRect rectMeta(0,0,0,0);
rectMeta.left = MulDiv(rect.left*100, DC.GetDeviceCaps(HORZSIZE), DC.GetDeviceCaps(HORZRES));
rectMeta.top = MulDiv(rect.top*100, DC.GetDeviceCaps(VERTSIZE), DC.GetDeviceCaps(VERTRES));
rectMeta.right = MulDiv(rect.right*100, DC.GetDeviceCaps(HORZSIZE), DC.GetDeviceCaps(HORZRES));
rectMeta.bottom = MulDiv(rect.bottom*100, DC.GetDeviceCaps(VERTSIZE), DC.GetDeviceCaps(VERTRES));
// Create an enhanced Metafile DC
MetaDC.CreateEnhanced(&DC, NULL, rectMeta, _T("Anything\0Image\0\0"));
MetaDC.SetAttribDC(DC.m_hDC);
MetaDC.SetMapMode(MM_TEXT);pDC = &MetaDC;
CBrush brush;
brush.CreateSysColorBrush(COLOR_WINDOW);
pDC->FillRect(rect, &brush);
brush.DeleteObject();Draw(pDC);
STGMEDIUM std;
std.tymed=TYMED_ENHMF;
std.hEnhMetaFile=MetaDC.CloseEnhanced();if (!pSource)
pSource = new COleDataSource();pSource->CacheData(CF_ENHMETAFILE, &std);
//Put on clipboard
pSource->SetClipboard();Everything worked well but when application was closed, an exception occured in an ole module. After application has been closed, I could not paste the image to Paint e