Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Programming with clipboard problem

Programming with clipboard problem

Scheduled Pinned Locked Moved C / C++ / MFC
comhelp
1 Posts 1 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nguyenvhn
    wrote on last edited by
    #1

    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

    1 Reply Last reply
    0
    Reply
    • Reply as topic
    Log in to reply
    • Oldest to Newest
    • Newest to Oldest
    • Most Votes


    • Login

    • Don't have an account? Register

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • World
    • Users
    • Groups