void CClipView::OnEditCopy()
{
    // Get the number of selected items.
    int iCount = m_wndList.GetSelCount();
    ASSERT(iCount > 0);
    // Get the list of selection IDs.
    int* pItems = new int [iCount];
    m_wndList.GetSelItems(iCount, pItems);
    // Create a list.
    CMyObList ObList;
    // Add all the items to the list.
    int i;
    CMyObj* pObj;
    for (i=0; i<iCount; i++) {
        pObj = (CMyObj*) m_wndList.GetItemData(pItems[i]);
        ObList.Append(pObj);
    }
    // Done with the item list.
    delete pItems;
    // Create a memory-file-based archive.
    CSharedFile mf (GMEM_MOVEABLE|GMEM_DDESHARE|GMEM_ZEROINIT);
    CArchive ar(&mf, CArchive::store);  
    ObList.Serialize(ar);
    ar.Close(); // Flush and close.
    HGLOBAL hMem = mf.Detach();
    if (!hMem) return;
    // Nuke the list but not the objects.
    ObList.RemoveAll();
    // Send the Clipboard the data.
    OpenClipboard();
    EmptyClipboard();
    SetClipboardData(theApp.m_uiMyListClipFormat, hMem);
    CloseClipboard();

void CClipView::OnEditPaste()
{
    OpenClipboard();
    HGLOBAL hMem = ::GetClipboardData(theApp.m_uiMyListClipFormat);
    if (!hMem) {
        CloseClipboard();
        return;
    }
    // Create a mem file.
    CSharedFile mf;
    mf.SetHandle(hMem);
    // Create the archive and get the data.
    CArchive ar(&mf, CArchive::load);  
    CMyObList PasteList;
    PasteList.Serialize(ar);
    ar.Close();
    mf.Detach();
    CloseClipboard();    // Add all the objects to the doc.
    CClipDoc* pDoc = GetDocument();
    ASSERT(pDoc);
    CMyObList* pObList = pDoc->GetObList();
    ASSERT(pObList);
    ASSERT(pObList->IsKindOf(RUNTIME_CLASS(CMyObList)));
    POSITION pos = NULL;
    // Remove each of the CMyObj objects from the paste list
    // and append them to the list.
    while (! PasteList.IsEmpty()) {
        CMyObj* pObj =  PasteList.RemoveHead();
        ASSERT(pObj);
        ASSERT(pObj->IsKindOf(RUNTIME_CLASS(CMyObj)));
        pObList->Append(pObj);
    }
    pDoc->SetModifiedFlag();
    pDoc->UpdateAllViews(NULL);
}

解决方案 »

  1.   

    用API函数CreateFileMapping和OpenFileMapping
    写:
    HANDLE MemHandle;
    MemHandle = CreateFileMapping  ((HANDLE0xFFFFFFFF,NULL,PAGE_READWRITE,0,2048,"filename");
    LPVOID pFile = MapViewOfFile(MemHandle,File_Map_WRITE,0,0,0);
    UnMapViewOfFile(pFile);
    读:
    HANDLE MemHandle = OpenFileMapping(FILE_MAP_READ,FALSE,"filename");
    LPVOID pFile = MapViewOfFile(MemHandle,File_Map_READ,0,0,0);
    UnMapViewOfFile(pFile);
    CloseHandle(MemHandle);